^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "chan_user.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <um_malloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct fd_chan {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct termios tt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) char str[sizeof("1234567890\0")];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void *fd_init(char *str, int device, const struct chan_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct fd_chan *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (*str != ':') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) printk(UM_KERN_ERR "fd_init : channel type 'fd' must specify a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) "file descriptor\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) str++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) n = strtoul(str, &end, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if ((*end != '\0') || (end == str)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) printk(UM_KERN_ERR "fd_init : couldn't parse file descriptor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) "'%s'\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *data = ((struct fd_chan) { .fd = n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .raw = opts->raw });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int fd_open(int input, int output, int primary, void *d, char **dev_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct fd_chan *data = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (data->raw && isatty(data->fd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) CATCH_EINTR(err = tcgetattr(data->fd, &data->tt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) err = raw(data->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) sprintf(data->str, "%d", data->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *dev_out = data->str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return data->fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void fd_close(int fd, void *d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct fd_chan *data = d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (!data->raw || !isatty(fd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &data->tt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) printk(UM_KERN_ERR "Failed to restore terminal state - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) "errno = %d\n", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) data->raw = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) const struct chan_ops fd_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .type = "fd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .init = fd_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .open = fd_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .close = fd_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .read = generic_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .write = generic_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .console_write = generic_console_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .window_size = generic_window_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .free = generic_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .winch = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };