Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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" |
| 21 | #include "helper.h" |
| 22 | #include "os.h" |
| 23 | |
| 24 | /* Changed during early boot */ |
| 25 | int pty_output_sigio = 0; |
| 26 | int pty_close_sigio = 0; |
| 27 | |
| 28 | /* Used as a flag during SIGIO testing early in boot */ |
| 29 | static volatile int got_sigio = 0; |
| 30 | |
| 31 | void __init handler(int sig) |
| 32 | { |
| 33 | got_sigio = 1; |
| 34 | } |
| 35 | |
| 36 | struct openpty_arg { |
| 37 | int master; |
| 38 | int slave; |
| 39 | int err; |
| 40 | }; |
| 41 | |
| 42 | static void openpty_cb(void *arg) |
| 43 | { |
| 44 | struct openpty_arg *info = arg; |
| 45 | |
| 46 | info->err = 0; |
| 47 | if(openpty(&info->master, &info->slave, NULL, NULL, NULL)) |
| 48 | info->err = -errno; |
| 49 | } |
| 50 | |
| 51 | void __init check_one_sigio(void (*proc)(int, int)) |
| 52 | { |
| 53 | struct sigaction old, new; |
| 54 | struct openpty_arg pty = { .master = -1, .slave = -1 }; |
| 55 | int master, slave, err; |
| 56 | |
| 57 | initial_thread_cb(openpty_cb, &pty); |
| 58 | if(pty.err){ |
| 59 | printk("openpty failed, errno = %d\n", -pty.err); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | master = pty.master; |
| 64 | slave = pty.slave; |
| 65 | |
| 66 | if((master == -1) || (slave == -1)){ |
| 67 | printk("openpty failed to allocate a pty\n"); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | /* Not now, but complain so we now where we failed. */ |
| 72 | err = raw(master); |
| 73 | if (err < 0) |
| 74 | panic("check_sigio : __raw failed, errno = %d\n", -err); |
| 75 | |
| 76 | err = os_sigio_async(master, slave); |
| 77 | if(err < 0) |
| 78 | panic("tty_fds : sigio_async failed, err = %d\n", -err); |
| 79 | |
| 80 | if(sigaction(SIGIO, NULL, &old) < 0) |
| 81 | panic("check_sigio : sigaction 1 failed, errno = %d\n", errno); |
| 82 | new = old; |
| 83 | new.sa_handler = handler; |
| 84 | if(sigaction(SIGIO, &new, NULL) < 0) |
| 85 | panic("check_sigio : sigaction 2 failed, errno = %d\n", errno); |
| 86 | |
| 87 | got_sigio = 0; |
| 88 | (*proc)(master, slave); |
| 89 | |
| 90 | os_close_file(master); |
| 91 | os_close_file(slave); |
| 92 | |
| 93 | if(sigaction(SIGIO, &old, NULL) < 0) |
| 94 | panic("check_sigio : sigaction 3 failed, errno = %d\n", errno); |
| 95 | } |
| 96 | |
| 97 | static void tty_output(int master, int slave) |
| 98 | { |
| 99 | int n; |
| 100 | char buf[512]; |
| 101 | |
| 102 | printk("Checking that host ptys support output SIGIO..."); |
| 103 | |
| 104 | memset(buf, 0, sizeof(buf)); |
| 105 | |
| 106 | while(os_write_file(master, buf, sizeof(buf)) > 0) ; |
| 107 | if(errno != EAGAIN) |
| 108 | panic("check_sigio : write failed, errno = %d\n", errno); |
| 109 | while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ; |
| 110 | |
| 111 | if (got_sigio) { |
| 112 | printk("Yes\n"); |
| 113 | pty_output_sigio = 1; |
| 114 | } else if (n == -EAGAIN) { |
| 115 | printk("No, enabling workaround\n"); |
| 116 | } else { |
| 117 | panic("check_sigio : read failed, err = %d\n", n); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static void tty_close(int master, int slave) |
| 122 | { |
| 123 | printk("Checking that host ptys support SIGIO on close..."); |
| 124 | |
| 125 | os_close_file(slave); |
| 126 | if(got_sigio){ |
| 127 | printk("Yes\n"); |
| 128 | pty_close_sigio = 1; |
| 129 | } |
| 130 | else printk("No, enabling workaround\n"); |
| 131 | } |
| 132 | |
| 133 | void __init check_sigio(void) |
| 134 | { |
| 135 | if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) && |
| 136 | (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){ |
| 137 | printk("No pseudo-terminals available - skipping pty SIGIO " |
| 138 | "check\n"); |
| 139 | return; |
| 140 | } |
| 141 | check_one_sigio(tty_output); |
| 142 | check_one_sigio(tty_close); |
| 143 | } |
| 144 | |
| 145 | /* Protected by sigio_lock(), also used by sigio_cleanup, which is an |
| 146 | * exitcall. |
| 147 | */ |
| 148 | static int write_sigio_pid = -1; |
| 149 | |
| 150 | /* These arrays are initialized before the sigio thread is started, and |
| 151 | * the descriptors closed after it is killed. So, it can't see them change. |
| 152 | * On the UML side, they are changed under the sigio_lock. |
| 153 | */ |
| 154 | static int write_sigio_fds[2] = { -1, -1 }; |
| 155 | static int sigio_private[2] = { -1, -1 }; |
| 156 | |
| 157 | struct pollfds { |
| 158 | struct pollfd *poll; |
| 159 | int size; |
| 160 | int used; |
| 161 | }; |
| 162 | |
| 163 | /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread |
| 164 | * synchronizes with it. |
| 165 | */ |
| 166 | struct pollfds current_poll = { |
| 167 | .poll = NULL, |
| 168 | .size = 0, |
| 169 | .used = 0 |
| 170 | }; |
| 171 | |
| 172 | struct pollfds next_poll = { |
| 173 | .poll = NULL, |
| 174 | .size = 0, |
| 175 | .used = 0 |
| 176 | }; |
| 177 | |
| 178 | static int write_sigio_thread(void *unused) |
| 179 | { |
| 180 | struct pollfds *fds, tmp; |
| 181 | struct pollfd *p; |
| 182 | int i, n, respond_fd; |
| 183 | char c; |
| 184 | |
Jeff Dike | cd2ee4a | 2005-05-05 16:15:32 -0700 | [diff] [blame] | 185 | signal(SIGWINCH, SIG_IGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | fds = ¤t_poll; |
| 187 | while(1){ |
| 188 | n = poll(fds->poll, fds->used, -1); |
| 189 | if(n < 0){ |
| 190 | if(errno == EINTR) continue; |
| 191 | printk("write_sigio_thread : poll returned %d, " |
| 192 | "errno = %d\n", n, errno); |
| 193 | } |
| 194 | for(i = 0; i < fds->used; i++){ |
| 195 | p = &fds->poll[i]; |
| 196 | if(p->revents == 0) continue; |
| 197 | if(p->fd == sigio_private[1]){ |
| 198 | n = os_read_file(sigio_private[1], &c, sizeof(c)); |
| 199 | if(n != sizeof(c)) |
| 200 | printk("write_sigio_thread : " |
| 201 | "read failed, err = %d\n", -n); |
| 202 | tmp = current_poll; |
| 203 | current_poll = next_poll; |
| 204 | next_poll = tmp; |
| 205 | respond_fd = sigio_private[1]; |
| 206 | } |
| 207 | else { |
| 208 | respond_fd = write_sigio_fds[1]; |
| 209 | fds->used--; |
| 210 | memmove(&fds->poll[i], &fds->poll[i + 1], |
| 211 | (fds->used - i) * sizeof(*fds->poll)); |
| 212 | } |
| 213 | |
| 214 | n = os_write_file(respond_fd, &c, sizeof(c)); |
| 215 | if(n != sizeof(c)) |
| 216 | printk("write_sigio_thread : write failed, " |
| 217 | "err = %d\n", -n); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static int need_poll(int n) |
| 223 | { |
| 224 | if(n <= next_poll.size){ |
| 225 | next_poll.used = n; |
| 226 | return(0); |
| 227 | } |
| 228 | if(next_poll.poll != NULL) kfree(next_poll.poll); |
| 229 | next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd)); |
| 230 | if(next_poll.poll == NULL){ |
| 231 | printk("need_poll : failed to allocate new pollfds\n"); |
| 232 | next_poll.size = 0; |
| 233 | next_poll.used = 0; |
| 234 | return(-1); |
| 235 | } |
| 236 | next_poll.size = n; |
| 237 | next_poll.used = n; |
| 238 | return(0); |
| 239 | } |
| 240 | |
| 241 | /* Must be called with sigio_lock held, because it's needed by the marked |
| 242 | * critical section. */ |
| 243 | static void update_thread(void) |
| 244 | { |
| 245 | unsigned long flags; |
| 246 | int n; |
| 247 | char c; |
| 248 | |
| 249 | flags = set_signals(0); |
| 250 | n = os_write_file(sigio_private[0], &c, sizeof(c)); |
| 251 | if(n != sizeof(c)){ |
| 252 | printk("update_thread : write failed, err = %d\n", -n); |
| 253 | goto fail; |
| 254 | } |
| 255 | |
| 256 | n = os_read_file(sigio_private[0], &c, sizeof(c)); |
| 257 | if(n != sizeof(c)){ |
| 258 | printk("update_thread : read failed, err = %d\n", -n); |
| 259 | goto fail; |
| 260 | } |
| 261 | |
| 262 | set_signals(flags); |
| 263 | return; |
| 264 | fail: |
| 265 | /* Critical section start */ |
| 266 | if(write_sigio_pid != -1) |
| 267 | os_kill_process(write_sigio_pid, 1); |
| 268 | write_sigio_pid = -1; |
| 269 | os_close_file(sigio_private[0]); |
| 270 | os_close_file(sigio_private[1]); |
| 271 | os_close_file(write_sigio_fds[0]); |
| 272 | os_close_file(write_sigio_fds[1]); |
| 273 | /* Critical section end */ |
| 274 | set_signals(flags); |
| 275 | } |
| 276 | |
| 277 | int add_sigio_fd(int fd, int read) |
| 278 | { |
| 279 | int err = 0, i, n, events; |
| 280 | |
| 281 | sigio_lock(); |
| 282 | for(i = 0; i < current_poll.used; i++){ |
| 283 | if(current_poll.poll[i].fd == fd) |
| 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | n = current_poll.used + 1; |
| 288 | err = need_poll(n); |
| 289 | if(err) |
| 290 | goto out; |
| 291 | |
| 292 | for(i = 0; i < current_poll.used; i++) |
| 293 | next_poll.poll[i] = current_poll.poll[i]; |
| 294 | |
| 295 | if(read) events = POLLIN; |
| 296 | else events = POLLOUT; |
| 297 | |
| 298 | next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd, |
| 299 | .events = events, |
| 300 | .revents = 0 }); |
| 301 | update_thread(); |
| 302 | out: |
| 303 | sigio_unlock(); |
| 304 | return(err); |
| 305 | } |
| 306 | |
| 307 | int ignore_sigio_fd(int fd) |
| 308 | { |
| 309 | struct pollfd *p; |
| 310 | int err = 0, i, n = 0; |
| 311 | |
| 312 | sigio_lock(); |
| 313 | for(i = 0; i < current_poll.used; i++){ |
| 314 | if(current_poll.poll[i].fd == fd) break; |
| 315 | } |
| 316 | if(i == current_poll.used) |
| 317 | goto out; |
| 318 | |
| 319 | err = need_poll(current_poll.used - 1); |
| 320 | if(err) |
| 321 | goto out; |
| 322 | |
| 323 | for(i = 0; i < current_poll.used; i++){ |
| 324 | p = ¤t_poll.poll[i]; |
| 325 | if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i]; |
| 326 | } |
| 327 | if(n == i){ |
| 328 | printk("ignore_sigio_fd : fd %d not found\n", fd); |
| 329 | err = -1; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | update_thread(); |
| 334 | out: |
| 335 | sigio_unlock(); |
| 336 | return(err); |
| 337 | } |
| 338 | |
| 339 | static int setup_initial_poll(int fd) |
| 340 | { |
| 341 | struct pollfd *p; |
| 342 | |
| 343 | p = um_kmalloc(sizeof(struct pollfd)); |
| 344 | if(p == NULL){ |
| 345 | printk("setup_initial_poll : failed to allocate poll\n"); |
| 346 | return(-1); |
| 347 | } |
| 348 | *p = ((struct pollfd) { .fd = fd, |
| 349 | .events = POLLIN, |
| 350 | .revents = 0 }); |
| 351 | current_poll = ((struct pollfds) { .poll = p, |
| 352 | .used = 1, |
| 353 | .size = 1 }); |
| 354 | return(0); |
| 355 | } |
| 356 | |
| 357 | void write_sigio_workaround(void) |
| 358 | { |
| 359 | unsigned long stack; |
| 360 | int err; |
| 361 | |
| 362 | sigio_lock(); |
| 363 | if(write_sigio_pid != -1) |
| 364 | goto out; |
| 365 | |
| 366 | err = os_pipe(write_sigio_fds, 1, 1); |
| 367 | if(err < 0){ |
| 368 | printk("write_sigio_workaround - os_pipe 1 failed, " |
| 369 | "err = %d\n", -err); |
| 370 | goto out; |
| 371 | } |
| 372 | err = os_pipe(sigio_private, 1, 1); |
| 373 | if(err < 0){ |
| 374 | printk("write_sigio_workaround - os_pipe 2 failed, " |
| 375 | "err = %d\n", -err); |
| 376 | goto out_close1; |
| 377 | } |
| 378 | if(setup_initial_poll(sigio_private[1])) |
| 379 | goto out_close2; |
| 380 | |
| 381 | write_sigio_pid = run_helper_thread(write_sigio_thread, NULL, |
| 382 | CLONE_FILES | CLONE_VM, &stack, 0); |
| 383 | |
| 384 | if(write_sigio_pid < 0) goto out_close2; |
| 385 | |
| 386 | if(write_sigio_irq(write_sigio_fds[0])) |
| 387 | goto out_kill; |
| 388 | |
| 389 | out: |
| 390 | sigio_unlock(); |
| 391 | return; |
| 392 | |
| 393 | out_kill: |
| 394 | os_kill_process(write_sigio_pid, 1); |
| 395 | write_sigio_pid = -1; |
| 396 | out_close2: |
| 397 | os_close_file(sigio_private[0]); |
| 398 | os_close_file(sigio_private[1]); |
| 399 | out_close1: |
| 400 | os_close_file(write_sigio_fds[0]); |
| 401 | os_close_file(write_sigio_fds[1]); |
| 402 | sigio_unlock(); |
| 403 | } |
| 404 | |
| 405 | int read_sigio_fd(int fd) |
| 406 | { |
| 407 | int n; |
| 408 | char c; |
| 409 | |
| 410 | n = os_read_file(fd, &c, sizeof(c)); |
| 411 | if(n != sizeof(c)){ |
| 412 | if(n < 0) { |
| 413 | printk("read_sigio_fd - read failed, err = %d\n", -n); |
| 414 | return(n); |
| 415 | } |
| 416 | else { |
| 417 | printk("read_sigio_fd - short read, bytes = %d\n", n); |
| 418 | return(-EIO); |
| 419 | } |
| 420 | } |
| 421 | return(n); |
| 422 | } |
| 423 | |
| 424 | static void sigio_cleanup(void) |
| 425 | { |
| 426 | if (write_sigio_pid != -1) { |
| 427 | os_kill_process(write_sigio_pid, 1); |
| 428 | write_sigio_pid = -1; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | __uml_exitcall(sigio_cleanup); |