Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <unistd.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <errno.h> |
| 9 | #include <termios.h> |
| 10 | #include <string.h> |
| 11 | #include <signal.h> |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 12 | #include <sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <sys/stat.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <sys/socket.h> |
| 16 | #include "kern_util.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "chan_user.h" |
| 18 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "os.h" |
| 20 | #include "choose-mode.h" |
| 21 | #include "mode.h" |
| 22 | |
Paolo 'Blaisorblade' Giarrusso | 55c033c | 2005-11-13 16:07:11 -0800 | [diff] [blame] | 23 | int generic_console_write(int fd, const char *buf, int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | struct termios save, new; |
| 26 | int err; |
| 27 | |
| 28 | if(isatty(fd)){ |
| 29 | CATCH_EINTR(err = tcgetattr(fd, &save)); |
| 30 | if (err) |
| 31 | goto error; |
| 32 | new = save; |
| 33 | /* The terminal becomes a bit less raw, to handle \n also as |
| 34 | * "Carriage Return", not only as "New Line". Otherwise, the new |
| 35 | * line won't start at the first column.*/ |
| 36 | new.c_oflag |= OPOST; |
| 37 | CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new)); |
| 38 | if (err) |
| 39 | goto error; |
| 40 | } |
| 41 | err = generic_write(fd, buf, n, NULL); |
| 42 | /* Restore raw mode, in any case; we *must* ignore any error apart |
| 43 | * EINTR, except for debug.*/ |
| 44 | if(isatty(fd)) |
| 45 | CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save)); |
| 46 | return(err); |
| 47 | error: |
| 48 | return(-errno); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * UML SIGWINCH handling |
| 53 | * |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 54 | * The point of this is to handle SIGWINCH on consoles which have host |
| 55 | * ttys and relay them inside UML to whatever might be running on the |
| 56 | * console and cares about the window size (since SIGWINCH notifies |
| 57 | * about terminal size changes). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 59 | * So, we have a separate thread for each host tty attached to a UML |
| 60 | * device (side-issue - I'm annoyed that one thread can't have |
| 61 | * multiple controlling ttys for the purpose of handling SIGWINCH, but |
| 62 | * I imagine there are other reasons that doesn't make any sense). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 64 | * SIGWINCH can't be received synchronously, so you have to set up to |
| 65 | * receive it as a signal. That being the case, if you are going to |
| 66 | * wait for it, it is convenient to sit in sigsuspend() and wait for |
| 67 | * the signal to bounce you out of it (see below for how we make sure |
| 68 | * to exit only on SIGWINCH). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | */ |
| 70 | |
| 71 | static void winch_handler(int sig) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | struct winch_data { |
| 76 | int pty_fd; |
| 77 | int pipe_fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static int winch_thread(void *arg) |
| 81 | { |
| 82 | struct winch_data *data = arg; |
| 83 | sigset_t sigs; |
| 84 | int pty_fd, pipe_fd; |
| 85 | int count, err; |
| 86 | char c = 1; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | pty_fd = data->pty_fd; |
| 89 | pipe_fd = data->pipe_fd; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 90 | count = os_write_file(pipe_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | if(count != sizeof(c)) |
| 92 | printk("winch_thread : failed to write synchronization " |
| 93 | "byte, err = %d\n", -count); |
| 94 | |
| 95 | /* We are not using SIG_IGN on purpose, so don't fix it as I thought to |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 96 | * do! If using SIG_IGN, the sigsuspend() call below would not stop on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * SIGWINCH. */ |
| 98 | |
| 99 | signal(SIGWINCH, winch_handler); |
| 100 | sigfillset(&sigs); |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 101 | /* Block all signals possible. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){ |
| 103 | printk("winch_thread : sigprocmask failed, errno = %d\n", |
| 104 | errno); |
| 105 | exit(1); |
| 106 | } |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 107 | /* In sigsuspend(), block anything else than SIGWINCH. */ |
| 108 | sigdelset(&sigs, SIGWINCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | if(setsid() < 0){ |
| 111 | printk("winch_thread : setsid failed, errno = %d\n", errno); |
| 112 | exit(1); |
| 113 | } |
| 114 | |
| 115 | err = os_new_tty_pgrp(pty_fd, os_getpid()); |
| 116 | if(err < 0){ |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 117 | printk("winch_thread : new_tty_pgrp failed on fd %d, " |
| 118 | "err = %d\n", pty_fd, -err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | exit(1); |
| 120 | } |
| 121 | |
| 122 | /* These are synchronization calls between various UML threads on the |
| 123 | * host - since they are not different kernel threads, we cannot use |
| 124 | * kernel semaphores. We don't use SysV semaphores because they are |
Jan Engelhardt | 03a67a4 | 2006-11-30 05:32:19 +0100 | [diff] [blame] | 125 | * persistent. */ |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 126 | count = os_read_file(pipe_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if(count != sizeof(c)) |
| 128 | printk("winch_thread : failed to read synchronization byte, " |
| 129 | "err = %d\n", -count); |
| 130 | |
| 131 | while(1){ |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 132 | /* This will be interrupted by SIGWINCH only, since |
| 133 | * other signals are blocked. |
| 134 | */ |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 135 | sigsuspend(&sigs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 137 | count = os_write_file(pipe_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | if(count != sizeof(c)) |
| 139 | printk("winch_thread : write failed, err = %d\n", |
| 140 | -count); |
| 141 | } |
| 142 | } |
| 143 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 144 | static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out, |
| 145 | unsigned long *stack_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | struct winch_data data; |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 148 | int fds[2], n, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | char c; |
| 150 | |
| 151 | err = os_pipe(fds, 1, 1); |
| 152 | if(err < 0){ |
| 153 | printk("winch_tramp : os_pipe failed, err = %d\n", -err); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 154 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | data = ((struct winch_data) { .pty_fd = fd, |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 158 | .pipe_fd = fds[1] } ); |
| 159 | /* CLONE_FILES so this thread doesn't hold open files which are open |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 160 | * now, but later closed in a different thread. This is a |
| 161 | * problem with /dev/net/tun, which if held open by this |
| 162 | * thread, prevents the TUN/TAP device from being reused. |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 163 | */ |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 164 | err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 165 | if(err < 0){ |
Andrew Morton | b16895b | 2007-05-06 14:51:03 -0700 | [diff] [blame] | 166 | printk("fork of winch_thread failed - errno = %d\n", -err); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 167 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | *fd_out = fds[0]; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 171 | n = os_read_file(fds[0], &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if(n != sizeof(c)){ |
| 173 | printk("winch_tramp : failed to read synchronization byte\n"); |
| 174 | printk("read failed, err = %d\n", -n); |
| 175 | printk("fd %d will not support SIGWINCH\n", fd); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 176 | err = -EINVAL; |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 177 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
Eduard-Gabriel Munteanu | 89df6bf | 2007-07-15 23:38:51 -0700 | [diff] [blame] | 179 | |
| 180 | if (os_set_fd_block(*fd_out, 0)) { |
| 181 | printk("winch_tramp: failed to set thread_fd non-blocking.\n"); |
| 182 | goto out_close; |
| 183 | } |
| 184 | |
| 185 | return err; |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 186 | |
| 187 | out_close: |
| 188 | os_close_file(fds[1]); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 189 | os_close_file(fds[0]); |
| 190 | out: |
| 191 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void register_winch(int fd, struct tty_struct *tty) |
| 195 | { |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 196 | unsigned long stack; |
| 197 | int pid, thread, count, thread_fd = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | char c = 1; |
| 199 | |
| 200 | if(!isatty(fd)) |
| 201 | return; |
| 202 | |
| 203 | pid = tcgetpgrp(fd); |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 204 | if (!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, tty) && |
| 205 | (pid == -1)) { |
| 206 | thread = winch_tramp(fd, tty, &thread_fd, &stack); |
| 207 | if (thread < 0) |
| 208 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Jeff Dike | 42a359e | 2007-07-15 23:38:55 -0700 | [diff] [blame] | 210 | register_winch_irq(thread_fd, fd, thread, tty, stack); |
| 211 | |
| 212 | count = os_write_file(thread_fd, &c, sizeof(c)); |
| 213 | if(count != sizeof(c)) |
| 214 | printk("register_winch : failed to write " |
| 215 | "synchronization byte, err = %d\n", -count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | } |