Jeff Dike | 8e36706 | 2006-03-27 01:14:32 -0800 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <unistd.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <termios.h> |
| 9 | #include <pty.h> |
| 10 | #include <signal.h> |
| 11 | #include <errno.h> |
| 12 | #include <string.h> |
| 13 | #include <sched.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/poll.h> |
| 16 | #include "init.h" |
| 17 | #include "user.h" |
| 18 | #include "kern_util.h" |
| 19 | #include "user_util.h" |
| 20 | #include "sigio.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "os.h" |
| 22 | |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 23 | /* Protected by sigio_lock(), also used by sigio_cleanup, which is an |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * exitcall. |
| 25 | */ |
| 26 | static int write_sigio_pid = -1; |
| 27 | |
| 28 | /* These arrays are initialized before the sigio thread is started, and |
| 29 | * the descriptors closed after it is killed. So, it can't see them change. |
| 30 | * On the UML side, they are changed under the sigio_lock. |
| 31 | */ |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 32 | #define SIGIO_FDS_INIT {-1, -1} |
| 33 | |
| 34 | static int write_sigio_fds[2] = SIGIO_FDS_INIT; |
| 35 | static int sigio_private[2] = SIGIO_FDS_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | struct pollfds { |
| 38 | struct pollfd *poll; |
| 39 | int size; |
| 40 | int used; |
| 41 | }; |
| 42 | |
| 43 | /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread |
| 44 | * synchronizes with it. |
| 45 | */ |
| 46 | struct pollfds current_poll = { |
| 47 | .poll = NULL, |
| 48 | .size = 0, |
| 49 | .used = 0 |
| 50 | }; |
| 51 | |
| 52 | struct pollfds next_poll = { |
| 53 | .poll = NULL, |
| 54 | .size = 0, |
| 55 | .used = 0 |
| 56 | }; |
| 57 | |
| 58 | static int write_sigio_thread(void *unused) |
| 59 | { |
| 60 | struct pollfds *fds, tmp; |
| 61 | struct pollfd *p; |
| 62 | int i, n, respond_fd; |
| 63 | char c; |
| 64 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 65 | signal(SIGWINCH, SIG_IGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | fds = ¤t_poll; |
| 67 | while(1){ |
| 68 | n = poll(fds->poll, fds->used, -1); |
| 69 | if(n < 0){ |
| 70 | if(errno == EINTR) continue; |
| 71 | printk("write_sigio_thread : poll returned %d, " |
| 72 | "errno = %d\n", n, errno); |
| 73 | } |
| 74 | for(i = 0; i < fds->used; i++){ |
| 75 | p = &fds->poll[i]; |
| 76 | if(p->revents == 0) continue; |
| 77 | if(p->fd == sigio_private[1]){ |
| 78 | n = os_read_file(sigio_private[1], &c, sizeof(c)); |
| 79 | if(n != sizeof(c)) |
| 80 | printk("write_sigio_thread : " |
| 81 | "read failed, err = %d\n", -n); |
| 82 | tmp = current_poll; |
| 83 | current_poll = next_poll; |
| 84 | next_poll = tmp; |
| 85 | respond_fd = sigio_private[1]; |
| 86 | } |
| 87 | else { |
| 88 | respond_fd = write_sigio_fds[1]; |
| 89 | fds->used--; |
| 90 | memmove(&fds->poll[i], &fds->poll[i + 1], |
| 91 | (fds->used - i) * sizeof(*fds->poll)); |
| 92 | } |
| 93 | |
| 94 | n = os_write_file(respond_fd, &c, sizeof(c)); |
| 95 | if(n != sizeof(c)) |
| 96 | printk("write_sigio_thread : write failed, " |
| 97 | "err = %d\n", -n); |
| 98 | } |
| 99 | } |
Jeff Dike | 1b57e9c | 2006-01-06 00:18:49 -0800 | [diff] [blame] | 100 | |
| 101 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int need_poll(int n) |
| 105 | { |
| 106 | if(n <= next_poll.size){ |
| 107 | next_poll.used = n; |
| 108 | return(0); |
| 109 | } |
Jesper Juhl | b2325fe | 2005-11-07 01:01:35 -0800 | [diff] [blame] | 110 | kfree(next_poll.poll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd)); |
| 112 | if(next_poll.poll == NULL){ |
| 113 | printk("need_poll : failed to allocate new pollfds\n"); |
| 114 | next_poll.size = 0; |
| 115 | next_poll.used = 0; |
| 116 | return(-1); |
| 117 | } |
| 118 | next_poll.size = n; |
| 119 | next_poll.used = n; |
| 120 | return(0); |
| 121 | } |
| 122 | |
| 123 | /* Must be called with sigio_lock held, because it's needed by the marked |
| 124 | * critical section. */ |
| 125 | static void update_thread(void) |
| 126 | { |
| 127 | unsigned long flags; |
| 128 | int n; |
| 129 | char c; |
| 130 | |
| 131 | flags = set_signals(0); |
| 132 | n = os_write_file(sigio_private[0], &c, sizeof(c)); |
| 133 | if(n != sizeof(c)){ |
| 134 | printk("update_thread : write failed, err = %d\n", -n); |
| 135 | goto fail; |
| 136 | } |
| 137 | |
| 138 | n = os_read_file(sigio_private[0], &c, sizeof(c)); |
| 139 | if(n != sizeof(c)){ |
| 140 | printk("update_thread : read failed, err = %d\n", -n); |
| 141 | goto fail; |
| 142 | } |
| 143 | |
| 144 | set_signals(flags); |
| 145 | return; |
| 146 | fail: |
| 147 | /* Critical section start */ |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 148 | if(write_sigio_pid != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | os_kill_process(write_sigio_pid, 1); |
| 150 | write_sigio_pid = -1; |
Jeff Dike | 8e36706 | 2006-03-27 01:14:32 -0800 | [diff] [blame] | 151 | close(sigio_private[0]); |
| 152 | close(sigio_private[1]); |
| 153 | close(write_sigio_fds[0]); |
| 154 | close(write_sigio_fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* Critical section end */ |
| 156 | set_signals(flags); |
| 157 | } |
| 158 | |
| 159 | int add_sigio_fd(int fd, int read) |
| 160 | { |
| 161 | int err = 0, i, n, events; |
| 162 | |
| 163 | sigio_lock(); |
| 164 | for(i = 0; i < current_poll.used; i++){ |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 165 | if(current_poll.poll[i].fd == fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | n = current_poll.used + 1; |
| 170 | err = need_poll(n); |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 171 | if(err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | goto out; |
| 173 | |
| 174 | for(i = 0; i < current_poll.used; i++) |
| 175 | next_poll.poll[i] = current_poll.poll[i]; |
| 176 | |
| 177 | if(read) events = POLLIN; |
| 178 | else events = POLLOUT; |
| 179 | |
| 180 | next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd, |
| 181 | .events = events, |
| 182 | .revents = 0 }); |
| 183 | update_thread(); |
| 184 | out: |
| 185 | sigio_unlock(); |
| 186 | return(err); |
| 187 | } |
| 188 | |
| 189 | int ignore_sigio_fd(int fd) |
| 190 | { |
| 191 | struct pollfd *p; |
| 192 | int err = 0, i, n = 0; |
| 193 | |
| 194 | sigio_lock(); |
| 195 | for(i = 0; i < current_poll.used; i++){ |
| 196 | if(current_poll.poll[i].fd == fd) break; |
| 197 | } |
| 198 | if(i == current_poll.used) |
| 199 | goto out; |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | err = need_poll(current_poll.used - 1); |
| 202 | if(err) |
| 203 | goto out; |
| 204 | |
| 205 | for(i = 0; i < current_poll.used; i++){ |
| 206 | p = ¤t_poll.poll[i]; |
| 207 | if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i]; |
| 208 | } |
| 209 | if(n == i){ |
| 210 | printk("ignore_sigio_fd : fd %d not found\n", fd); |
| 211 | err = -1; |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | update_thread(); |
| 216 | out: |
| 217 | sigio_unlock(); |
| 218 | return(err); |
| 219 | } |
| 220 | |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 221 | static struct pollfd *setup_initial_poll(int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | struct pollfd *p; |
| 224 | |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 225 | p = um_kmalloc(sizeof(struct pollfd)); |
| 226 | if (p == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | printk("setup_initial_poll : failed to allocate poll\n"); |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 228 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | *p = ((struct pollfd) { .fd = fd, |
| 231 | .events = POLLIN, |
| 232 | .revents = 0 }); |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 233 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jeff Dike | 8e64d96a | 2006-07-10 04:45:11 -0700 | [diff] [blame^] | 236 | static void write_sigio_workaround(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | unsigned long stack; |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 239 | struct pollfd *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | int err; |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 241 | int l_write_sigio_fds[2]; |
| 242 | int l_sigio_private[2]; |
| 243 | int l_write_sigio_pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 245 | /* We call this *tons* of times - and most ones we must just fail. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | sigio_lock(); |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 247 | l_write_sigio_pid = write_sigio_pid; |
| 248 | sigio_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 250 | if (l_write_sigio_pid != -1) |
| 251 | return; |
| 252 | |
| 253 | err = os_pipe(l_write_sigio_fds, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | if(err < 0){ |
| 255 | printk("write_sigio_workaround - os_pipe 1 failed, " |
| 256 | "err = %d\n", -err); |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 257 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 259 | err = os_pipe(l_sigio_private, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if(err < 0){ |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 261 | printk("write_sigio_workaround - os_pipe 2 failed, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | "err = %d\n", -err); |
| 263 | goto out_close1; |
| 264 | } |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 265 | |
| 266 | p = setup_initial_poll(l_sigio_private[1]); |
| 267 | if(!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | goto out_close2; |
| 269 | |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 270 | sigio_lock(); |
| 271 | |
| 272 | /* Did we race? Don't try to optimize this, please, it's not so likely |
| 273 | * to happen, and no more than once at the boot. */ |
| 274 | if(write_sigio_pid != -1) |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 275 | goto out_free; |
| 276 | |
| 277 | current_poll = ((struct pollfds) { .poll = p, |
| 278 | .used = 1, |
| 279 | .size = 1 }); |
| 280 | |
| 281 | if (write_sigio_irq(l_write_sigio_fds[0])) |
| 282 | goto out_clear_poll; |
| 283 | |
| 284 | memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds)); |
| 285 | memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private)); |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 286 | |
| 287 | write_sigio_pid = run_helper_thread(write_sigio_thread, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | CLONE_FILES | CLONE_VM, &stack, 0); |
| 289 | |
Paolo 'Blaisorblade' Giarrusso | b6a2b13 | 2006-01-18 17:42:57 -0800 | [diff] [blame] | 290 | if (write_sigio_pid < 0) |
| 291 | goto out_clear; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | sigio_unlock(); |
| 294 | return; |
| 295 | |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 296 | out_clear: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | write_sigio_pid = -1; |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 298 | write_sigio_fds[0] = -1; |
| 299 | write_sigio_fds[1] = -1; |
| 300 | sigio_private[0] = -1; |
| 301 | sigio_private[1] = -1; |
| 302 | out_clear_poll: |
| 303 | current_poll = ((struct pollfds) { .poll = NULL, |
| 304 | .size = 0, |
| 305 | .used = 0 }); |
| 306 | out_free: |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 307 | sigio_unlock(); |
Paolo 'Blaisorblade' Giarrusso | e6fb54a | 2006-04-10 22:53:36 -0700 | [diff] [blame] | 308 | kfree(p); |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 309 | out_close2: |
Jeff Dike | 8e36706 | 2006-03-27 01:14:32 -0800 | [diff] [blame] | 310 | close(l_sigio_private[0]); |
| 311 | close(l_sigio_private[1]); |
Jeff Dike | 5f4e8fd | 2006-03-27 01:14:40 -0800 | [diff] [blame] | 312 | out_close1: |
Jeff Dike | 8e36706 | 2006-03-27 01:14:32 -0800 | [diff] [blame] | 313 | close(l_write_sigio_fds[0]); |
| 314 | close(l_write_sigio_fds[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Jeff Dike | 8e64d96a | 2006-07-10 04:45:11 -0700 | [diff] [blame^] | 317 | void maybe_sigio_broken(int fd, int read) |
| 318 | { |
| 319 | if(!isatty(fd)) |
| 320 | return; |
| 321 | |
| 322 | if((read || pty_output_sigio) && (!read || pty_close_sigio)) |
| 323 | return; |
| 324 | |
| 325 | write_sigio_workaround(); |
| 326 | add_sigio_fd(fd, read); |
| 327 | } |
| 328 | |
Jeff Dike | 8e36706 | 2006-03-27 01:14:32 -0800 | [diff] [blame] | 329 | void sigio_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
Jeff Dike | f206aab | 2006-03-27 01:14:33 -0800 | [diff] [blame] | 331 | if(write_sigio_pid != -1){ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | os_kill_process(write_sigio_pid, 1); |
| 333 | write_sigio_pid = -1; |
| 334 | } |
| 335 | } |