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