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 | * |
| 54 | * The point of this is to handle SIGWINCH on consoles which have host ttys and |
| 55 | * relay them inside UML to whatever might be running on the console and cares |
| 56 | * about the window size (since SIGWINCH notifies about terminal size changes). |
| 57 | * |
| 58 | * So, we have a separate thread for each host tty attached to a UML device |
| 59 | * (side-issue - I'm annoyed that one thread can't have multiple controlling |
| 60 | * ttys for purposed of handling SIGWINCH, but I imagine there are other reasons |
| 61 | * that doesn't make any sense). |
| 62 | * |
| 63 | * SIGWINCH can't be received synchronously, so you have to set up to receive it |
| 64 | * as a signal. That being the case, if you are going to wait for it, it is |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 65 | * convenient to sit in sigsuspend() and wait for the signal to bounce you out of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * it (see below for how we make sure to exit only on SIGWINCH). |
| 67 | */ |
| 68 | |
| 69 | static void winch_handler(int sig) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | struct winch_data { |
| 74 | int pty_fd; |
| 75 | int pipe_fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static int winch_thread(void *arg) |
| 79 | { |
| 80 | struct winch_data *data = arg; |
| 81 | sigset_t sigs; |
| 82 | int pty_fd, pipe_fd; |
| 83 | int count, err; |
| 84 | char c = 1; |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | pty_fd = data->pty_fd; |
| 87 | pipe_fd = data->pipe_fd; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame^] | 88 | count = os_write_file(pipe_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if(count != sizeof(c)) |
| 90 | printk("winch_thread : failed to write synchronization " |
| 91 | "byte, err = %d\n", -count); |
| 92 | |
| 93 | /* 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] | 94 | * 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] | 95 | * SIGWINCH. */ |
| 96 | |
| 97 | signal(SIGWINCH, winch_handler); |
| 98 | sigfillset(&sigs); |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 99 | /* Block all signals possible. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){ |
| 101 | printk("winch_thread : sigprocmask failed, errno = %d\n", |
| 102 | errno); |
| 103 | exit(1); |
| 104 | } |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 105 | /* In sigsuspend(), block anything else than SIGWINCH. */ |
| 106 | sigdelset(&sigs, SIGWINCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | if(setsid() < 0){ |
| 109 | printk("winch_thread : setsid failed, errno = %d\n", errno); |
| 110 | exit(1); |
| 111 | } |
| 112 | |
| 113 | err = os_new_tty_pgrp(pty_fd, os_getpid()); |
| 114 | if(err < 0){ |
| 115 | printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err); |
| 116 | exit(1); |
| 117 | } |
| 118 | |
| 119 | /* These are synchronization calls between various UML threads on the |
| 120 | * host - since they are not different kernel threads, we cannot use |
| 121 | * kernel semaphores. We don't use SysV semaphores because they are |
Jan Engelhardt | 03a67a4 | 2006-11-30 05:32:19 +0100 | [diff] [blame] | 122 | * persistent. */ |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame^] | 123 | count = os_read_file(pipe_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if(count != sizeof(c)) |
| 125 | printk("winch_thread : failed to read synchronization byte, " |
| 126 | "err = %d\n", -count); |
| 127 | |
| 128 | while(1){ |
| 129 | /* This will be interrupted by SIGWINCH only, since other signals |
| 130 | * are blocked.*/ |
Bodo Stroesser | ed1b58d | 2005-09-03 15:57:24 -0700 | [diff] [blame] | 131 | sigsuspend(&sigs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame^] | 133 | count = os_write_file(pipe_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if(count != sizeof(c)) |
| 135 | printk("winch_thread : write failed, err = %d\n", |
| 136 | -count); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out) |
| 141 | { |
| 142 | struct winch_data data; |
| 143 | unsigned long stack; |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 144 | int fds[2], n, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | char c; |
| 146 | |
| 147 | err = os_pipe(fds, 1, 1); |
| 148 | if(err < 0){ |
| 149 | printk("winch_tramp : os_pipe failed, err = %d\n", -err); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 150 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | data = ((struct winch_data) { .pty_fd = fd, |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 154 | .pipe_fd = fds[1] } ); |
| 155 | /* CLONE_FILES so this thread doesn't hold open files which are open |
| 156 | * now, but later closed. This is a problem with /dev/net/tun. |
| 157 | */ |
| 158 | err = run_helper_thread(winch_thread, &data, CLONE_FILES, &stack, 0); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 159 | if(err < 0){ |
Andrew Morton | b16895b | 2007-05-06 14:51:03 -0700 | [diff] [blame] | 160 | printk("fork of winch_thread failed - errno = %d\n", -err); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 161 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | *fd_out = fds[0]; |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame^] | 165 | n = os_read_file(fds[0], &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if(n != sizeof(c)){ |
| 167 | printk("winch_tramp : failed to read synchronization byte\n"); |
| 168 | printk("read failed, err = %d\n", -n); |
| 169 | printk("fd %d will not support SIGWINCH\n", fd); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 170 | err = -EINVAL; |
Jeff Dike | 1d2ddcf | 2006-02-07 12:58:41 -0800 | [diff] [blame] | 171 | goto out_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 173 | return err ; |
| 174 | |
| 175 | out_close: |
| 176 | os_close_file(fds[1]); |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 177 | os_close_file(fds[0]); |
| 178 | out: |
| 179 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void register_winch(int fd, struct tty_struct *tty) |
| 183 | { |
Jeff Dike | 1f96ddb | 2005-06-08 15:48:27 -0700 | [diff] [blame] | 184 | int pid, thread, thread_fd = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int count; |
| 186 | char c = 1; |
| 187 | |
| 188 | if(!isatty(fd)) |
| 189 | return; |
| 190 | |
| 191 | pid = tcgetpgrp(fd); |
| 192 | if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, |
| 193 | tty) && (pid == -1)){ |
| 194 | thread = winch_tramp(fd, tty, &thread_fd); |
Jeff Dike | da00d9a | 2005-06-08 15:48:01 -0700 | [diff] [blame] | 195 | if(thread > 0){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | register_winch_irq(thread_fd, fd, thread, tty); |
| 197 | |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame^] | 198 | count = os_write_file(thread_fd, &c, sizeof(c)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if(count != sizeof(c)) |
| 200 | printk("register_winch : failed to write " |
| 201 | "synchronization byte, err = %d\n", |
| 202 | -count); |
| 203 | } |
| 204 | } |
| 205 | } |