^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) 2000 - 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 <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "chan_user.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <um_malloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) void generic_close(int fd, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) close(fd);
^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) int generic_read(int fd, char *c_out, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) n = read(fd, c_out, sizeof(*c_out));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (n > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) else if (n == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) else if (errno == EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* XXX Trivial wrapper around write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int generic_write(int fd, const char *buf, int n, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) err = write(fd, buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) else if (errno == EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) else if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int generic_window_size(int fd, void *unused, unsigned short *rows_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned short *cols_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct winsize size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (ioctl(fd, TIOCGWINSZ, &size) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *rows_out = size.ws_row;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *cols_out = size.ws_col;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return ret;
^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) void generic_free(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int generic_console_write(int fd, const char *buf, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) sigset_t old, no_sigio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct termios save, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (isatty(fd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) sigemptyset(&no_sigio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) sigaddset(&no_sigio, SIGIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) CATCH_EINTR(err = tcgetattr(fd, &save));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) new = save;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * The terminal becomes a bit less raw, to handle \n also as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * "Carriage Return", not only as "New Line". Otherwise, the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * line won't start at the first column.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) new.c_oflag |= OPOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = generic_write(fd, buf, n, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Restore raw mode, in any case; we *must* ignore any error apart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * EINTR, except for debug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (isatty(fd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sigprocmask(SIG_SETMASK, &old, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * UML SIGWINCH handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * The point of this is to handle SIGWINCH on consoles which have host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * ttys and relay them inside UML to whatever might be running on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * console and cares about the window size (since SIGWINCH notifies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * about terminal size changes).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * So, we have a separate thread for each host tty attached to a UML
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * device (side-issue - I'm annoyed that one thread can't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * multiple controlling ttys for the purpose of handling SIGWINCH, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * I imagine there are other reasons that doesn't make any sense).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * SIGWINCH can't be received synchronously, so you have to set up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * receive it as a signal. That being the case, if you are going to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * wait for it, it is convenient to sit in sigsuspend() and wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * the signal to bounce you out of it (see below for how we make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * to exit only on SIGWINCH).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static void winch_handler(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct winch_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int pty_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int pipe_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int winch_thread(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct winch_data *data = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) sigset_t sigs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int pty_fd, pipe_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) char c = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pty_fd = data->pty_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) pipe_fd = data->pipe_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) count = write(pipe_fd, &c, sizeof(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (count != sizeof(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) printk(UM_KERN_ERR "winch_thread : failed to write "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) "synchronization byte, err = %d\n", -count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * We are not using SIG_IGN on purpose, so don't fix it as I thought to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * do! If using SIG_IGN, the sigsuspend() call below would not stop on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * SIGWINCH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) signal(SIGWINCH, winch_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) sigfillset(&sigs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Block all signals possible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* In sigsuspend(), block anything else than SIGWINCH. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) sigdelset(&sigs, SIGWINCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (setsid() < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) printk(UM_KERN_ERR "winch_thread : TIOCSCTTY failed on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) "fd %d err = %d\n", pty_fd, errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) printk(UM_KERN_ERR "winch_thread : tcsetpgrp failed on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) "fd %d err = %d\n", pty_fd, errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * These are synchronization calls between various UML threads on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * host - since they are not different kernel threads, we cannot use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * kernel semaphores. We don't use SysV semaphores because they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * persistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) count = read(pipe_fd, &c, sizeof(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (count != sizeof(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) printk(UM_KERN_ERR "winch_thread : failed to read "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) "synchronization byte, err = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) while(1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * This will be interrupted by SIGWINCH only, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * other signals are blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) sigsuspend(&sigs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) count = write(pipe_fd, &c, sizeof(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (count != sizeof(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) printk(UM_KERN_ERR "winch_thread : write failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) "err = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned long *stack_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct winch_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int fds[2], n, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) err = os_pipe(fds, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) data = ((struct winch_data) { .pty_fd = fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) .pipe_fd = fds[1] } );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * CLONE_FILES so this thread doesn't hold open files which are open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * now, but later closed in a different thread. This is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * problem with /dev/net/tun, which if held open by this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * thread, prevents the TUN/TAP device from being reused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *fd_out = fds[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) n = read(fds[0], &c, sizeof(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (n != sizeof(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) printk(UM_KERN_ERR "winch_tramp : failed to read "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) "synchronization byte\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) printk(UM_KERN_ERR "read failed, err = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) err = os_set_fd_block(*fd_out, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) "non-blocking.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) close(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) close(fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void register_winch(int fd, struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) unsigned long stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int pid, thread, count, thread_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) char c = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!isatty(fd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) pid = tcgetpgrp(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (is_skas_winch(pid, fd, port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) register_winch_irq(-1, fd, -1, port, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (pid == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) thread = winch_tramp(fd, port, &thread_fd, &stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (thread < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) register_winch_irq(thread_fd, fd, thread, port, stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) count = write(thread_fd, &c, sizeof(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (count != sizeof(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) printk(UM_KERN_ERR "register_winch : failed to write "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) "synchronization byte, err = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }