1f206aabbSJeff Dike /* 2f206aabbSJeff Dike * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) 3f206aabbSJeff Dike * Licensed under the GPL 4f206aabbSJeff Dike */ 5f206aabbSJeff Dike 6f206aabbSJeff Dike #include <unistd.h> 7f206aabbSJeff Dike #include <stdlib.h> 8f206aabbSJeff Dike #include <termios.h> 9f206aabbSJeff Dike #include <pty.h> 10f206aabbSJeff Dike #include <signal.h> 11c65badbdSJeff Dike #include <fcntl.h> 12f206aabbSJeff Dike #include <errno.h> 13f206aabbSJeff Dike #include <string.h> 14f206aabbSJeff Dike #include <sched.h> 15f206aabbSJeff Dike #include <sys/socket.h> 16f206aabbSJeff Dike #include <sys/poll.h> 17f206aabbSJeff Dike #include "init.h" 18f206aabbSJeff Dike #include "user.h" 19f206aabbSJeff Dike #include "kern_util.h" 20f206aabbSJeff Dike #include "user_util.h" 21f206aabbSJeff Dike #include "sigio.h" 22f206aabbSJeff Dike #include "os.h" 23c13e5690SPaolo 'Blaisorblade' Giarrusso #include "um_malloc.h" 24c65badbdSJeff Dike #include "init.h" 25f206aabbSJeff Dike 26f206aabbSJeff Dike /* Protected by sigio_lock(), also used by sigio_cleanup, which is an 27f206aabbSJeff Dike * exitcall. 28f206aabbSJeff Dike */ 29f206aabbSJeff Dike static int write_sigio_pid = -1; 30f206aabbSJeff Dike 31f206aabbSJeff Dike /* These arrays are initialized before the sigio thread is started, and 32f206aabbSJeff Dike * the descriptors closed after it is killed. So, it can't see them change. 33f206aabbSJeff Dike * On the UML side, they are changed under the sigio_lock. 34f206aabbSJeff Dike */ 355f4e8fd0SJeff Dike #define SIGIO_FDS_INIT {-1, -1} 365f4e8fd0SJeff Dike 375f4e8fd0SJeff Dike static int write_sigio_fds[2] = SIGIO_FDS_INIT; 385f4e8fd0SJeff Dike static int sigio_private[2] = SIGIO_FDS_INIT; 39f206aabbSJeff Dike 40f206aabbSJeff Dike struct pollfds { 41f206aabbSJeff Dike struct pollfd *poll; 42f206aabbSJeff Dike int size; 43f206aabbSJeff Dike int used; 44f206aabbSJeff Dike }; 45f206aabbSJeff Dike 46f206aabbSJeff Dike /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread 47f206aabbSJeff Dike * synchronizes with it. 48f206aabbSJeff Dike */ 4919bdf040SJeff Dike static struct pollfds current_poll; 5019bdf040SJeff Dike static struct pollfds next_poll; 5119bdf040SJeff Dike static struct pollfds all_sigio_fds; 52f206aabbSJeff Dike 53f206aabbSJeff Dike static int write_sigio_thread(void *unused) 54f206aabbSJeff Dike { 55f206aabbSJeff Dike struct pollfds *fds, tmp; 56f206aabbSJeff Dike struct pollfd *p; 57f206aabbSJeff Dike int i, n, respond_fd; 58f206aabbSJeff Dike char c; 59f206aabbSJeff Dike 60f206aabbSJeff Dike signal(SIGWINCH, SIG_IGN); 61f206aabbSJeff Dike fds = ¤t_poll; 62f206aabbSJeff Dike while(1){ 63f206aabbSJeff Dike n = poll(fds->poll, fds->used, -1); 64f206aabbSJeff Dike if(n < 0){ 65f206aabbSJeff Dike if(errno == EINTR) continue; 66f206aabbSJeff Dike printk("write_sigio_thread : poll returned %d, " 67f206aabbSJeff Dike "errno = %d\n", n, errno); 68f206aabbSJeff Dike } 69f206aabbSJeff Dike for(i = 0; i < fds->used; i++){ 70f206aabbSJeff Dike p = &fds->poll[i]; 71f206aabbSJeff Dike if(p->revents == 0) continue; 72f206aabbSJeff Dike if(p->fd == sigio_private[1]){ 73f206aabbSJeff Dike n = os_read_file(sigio_private[1], &c, sizeof(c)); 74f206aabbSJeff Dike if(n != sizeof(c)) 75f206aabbSJeff Dike printk("write_sigio_thread : " 7619bdf040SJeff Dike "read on socket failed, " 7719bdf040SJeff Dike "err = %d\n", -n); 78f206aabbSJeff Dike tmp = current_poll; 79f206aabbSJeff Dike current_poll = next_poll; 80f206aabbSJeff Dike next_poll = tmp; 81f206aabbSJeff Dike respond_fd = sigio_private[1]; 82f206aabbSJeff Dike } 83f206aabbSJeff Dike else { 84f206aabbSJeff Dike respond_fd = write_sigio_fds[1]; 85f206aabbSJeff Dike fds->used--; 86f206aabbSJeff Dike memmove(&fds->poll[i], &fds->poll[i + 1], 87f206aabbSJeff Dike (fds->used - i) * sizeof(*fds->poll)); 88f206aabbSJeff Dike } 89f206aabbSJeff Dike 90f206aabbSJeff Dike n = os_write_file(respond_fd, &c, sizeof(c)); 91f206aabbSJeff Dike if(n != sizeof(c)) 9219bdf040SJeff Dike printk("write_sigio_thread : write on socket " 9319bdf040SJeff Dike "failed, err = %d\n", -n); 94f206aabbSJeff Dike } 95f206aabbSJeff Dike } 96f206aabbSJeff Dike 97f206aabbSJeff Dike return 0; 98f206aabbSJeff Dike } 99f206aabbSJeff Dike 10019bdf040SJeff Dike static int need_poll(struct pollfds *polls, int n) 101f206aabbSJeff Dike { 102838e56a1SJeff Dike struct pollfd *new; 103838e56a1SJeff Dike 104838e56a1SJeff Dike if(n <= polls->size) 10519bdf040SJeff Dike return 0; 106838e56a1SJeff Dike 107838e56a1SJeff Dike new = um_kmalloc_atomic(n * sizeof(struct pollfd)); 108838e56a1SJeff Dike if(new == NULL){ 109f206aabbSJeff Dike printk("need_poll : failed to allocate new pollfds\n"); 11019bdf040SJeff Dike return -ENOMEM; 111f206aabbSJeff Dike } 112838e56a1SJeff Dike 113838e56a1SJeff Dike memcpy(new, polls->poll, polls->used * sizeof(struct pollfd)); 114838e56a1SJeff Dike kfree(polls->poll); 115838e56a1SJeff Dike 116838e56a1SJeff Dike polls->poll = new; 11719bdf040SJeff Dike polls->size = n; 11819bdf040SJeff Dike return 0; 119f206aabbSJeff Dike } 120f206aabbSJeff Dike 121f206aabbSJeff Dike /* Must be called with sigio_lock held, because it's needed by the marked 12219bdf040SJeff Dike * critical section. 12319bdf040SJeff Dike */ 124f206aabbSJeff Dike static void update_thread(void) 125f206aabbSJeff Dike { 126f206aabbSJeff Dike unsigned long flags; 127f206aabbSJeff Dike int n; 128f206aabbSJeff Dike char c; 129f206aabbSJeff Dike 130f206aabbSJeff Dike flags = set_signals(0); 131f206aabbSJeff Dike n = os_write_file(sigio_private[0], &c, sizeof(c)); 132f206aabbSJeff Dike if(n != sizeof(c)){ 133f206aabbSJeff Dike printk("update_thread : write failed, err = %d\n", -n); 134f206aabbSJeff Dike goto fail; 135f206aabbSJeff Dike } 136f206aabbSJeff Dike 137f206aabbSJeff Dike n = os_read_file(sigio_private[0], &c, sizeof(c)); 138f206aabbSJeff Dike if(n != sizeof(c)){ 139f206aabbSJeff Dike printk("update_thread : read failed, err = %d\n", -n); 140f206aabbSJeff Dike goto fail; 141f206aabbSJeff Dike } 142f206aabbSJeff Dike 143f206aabbSJeff Dike set_signals(flags); 144f206aabbSJeff Dike return; 145f206aabbSJeff Dike fail: 146f206aabbSJeff Dike /* Critical section start */ 147f206aabbSJeff Dike if(write_sigio_pid != -1) 148f206aabbSJeff Dike os_kill_process(write_sigio_pid, 1); 149f206aabbSJeff Dike write_sigio_pid = -1; 150f206aabbSJeff Dike close(sigio_private[0]); 151f206aabbSJeff Dike close(sigio_private[1]); 152f206aabbSJeff Dike close(write_sigio_fds[0]); 153f206aabbSJeff Dike close(write_sigio_fds[1]); 154f206aabbSJeff Dike /* Critical section end */ 155f206aabbSJeff Dike set_signals(flags); 156f206aabbSJeff Dike } 157f206aabbSJeff Dike 15819bdf040SJeff Dike int add_sigio_fd(int fd) 159f206aabbSJeff Dike { 16019bdf040SJeff Dike struct pollfd *p; 16119bdf040SJeff Dike int err = 0, i, n; 162f206aabbSJeff Dike 163f206aabbSJeff Dike sigio_lock(); 16419bdf040SJeff Dike for(i = 0; i < all_sigio_fds.used; i++){ 16519bdf040SJeff Dike if(all_sigio_fds.poll[i].fd == fd) 16619bdf040SJeff Dike break; 16719bdf040SJeff Dike } 16819bdf040SJeff Dike if(i == all_sigio_fds.used) 16919bdf040SJeff Dike goto out; 17019bdf040SJeff Dike 17119bdf040SJeff Dike p = &all_sigio_fds.poll[i]; 17219bdf040SJeff Dike 173f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++){ 174f206aabbSJeff Dike if(current_poll.poll[i].fd == fd) 175f206aabbSJeff Dike goto out; 176f206aabbSJeff Dike } 177f206aabbSJeff Dike 178838e56a1SJeff Dike n = current_poll.used; 179838e56a1SJeff Dike err = need_poll(&next_poll, n + 1); 180f206aabbSJeff Dike if(err) 181f206aabbSJeff Dike goto out; 182f206aabbSJeff Dike 183838e56a1SJeff Dike memcpy(next_poll.poll, current_poll.poll, 184838e56a1SJeff Dike current_poll.used * sizeof(struct pollfd)); 185838e56a1SJeff Dike next_poll.poll[n] = *p; 186838e56a1SJeff Dike next_poll.used = n + 1; 187f206aabbSJeff Dike update_thread(); 188f206aabbSJeff Dike out: 189f206aabbSJeff Dike sigio_unlock(); 19019bdf040SJeff Dike return err; 191f206aabbSJeff Dike } 192f206aabbSJeff Dike 193f206aabbSJeff Dike int ignore_sigio_fd(int fd) 194f206aabbSJeff Dike { 195f206aabbSJeff Dike struct pollfd *p; 196f206aabbSJeff Dike int err = 0, i, n = 0; 197f206aabbSJeff Dike 19861232f2fSJeff Dike /* This is called from exitcalls elsewhere in UML - if 19961232f2fSJeff Dike * sigio_cleanup has already run, then update_thread will hang 20061232f2fSJeff Dike * or fail because the thread is no longer running. 20161232f2fSJeff Dike */ 20261232f2fSJeff Dike if(write_sigio_pid == -1) 20361232f2fSJeff Dike return -EIO; 20461232f2fSJeff Dike 205f206aabbSJeff Dike sigio_lock(); 206f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++){ 207f206aabbSJeff Dike if(current_poll.poll[i].fd == fd) break; 208f206aabbSJeff Dike } 209f206aabbSJeff Dike if(i == current_poll.used) 210f206aabbSJeff Dike goto out; 211f206aabbSJeff Dike 21219bdf040SJeff Dike err = need_poll(&next_poll, current_poll.used - 1); 213f206aabbSJeff Dike if(err) 214f206aabbSJeff Dike goto out; 215f206aabbSJeff Dike 216f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++){ 217f206aabbSJeff Dike p = ¤t_poll.poll[i]; 21819bdf040SJeff Dike if(p->fd != fd) 21919bdf040SJeff Dike next_poll.poll[n++] = *p; 220f206aabbSJeff Dike } 221838e56a1SJeff Dike next_poll.used = current_poll.used - 1; 222f206aabbSJeff Dike 223f206aabbSJeff Dike update_thread(); 224f206aabbSJeff Dike out: 225f206aabbSJeff Dike sigio_unlock(); 22661232f2fSJeff Dike return err; 227f206aabbSJeff Dike } 228f206aabbSJeff Dike 229f206aabbSJeff Dike static struct pollfd *setup_initial_poll(int fd) 230f206aabbSJeff Dike { 231f206aabbSJeff Dike struct pollfd *p; 232f206aabbSJeff Dike 233f206aabbSJeff Dike p = um_kmalloc(sizeof(struct pollfd)); 234f206aabbSJeff Dike if (p == NULL) { 235f206aabbSJeff Dike printk("setup_initial_poll : failed to allocate poll\n"); 236f206aabbSJeff Dike return NULL; 237f206aabbSJeff Dike } 238f206aabbSJeff Dike *p = ((struct pollfd) { .fd = fd, 239f206aabbSJeff Dike .events = POLLIN, 240f206aabbSJeff Dike .revents = 0 }); 241f206aabbSJeff Dike return p; 242f206aabbSJeff Dike } 243f206aabbSJeff Dike 2448e64d96aSJeff Dike static void write_sigio_workaround(void) 245f206aabbSJeff Dike { 246f206aabbSJeff Dike unsigned long stack; 247f206aabbSJeff Dike struct pollfd *p; 248f206aabbSJeff Dike int err; 249f206aabbSJeff Dike int l_write_sigio_fds[2]; 250f206aabbSJeff Dike int l_sigio_private[2]; 251f206aabbSJeff Dike int l_write_sigio_pid; 252f206aabbSJeff Dike 253f206aabbSJeff Dike /* We call this *tons* of times - and most ones we must just fail. */ 254f206aabbSJeff Dike sigio_lock(); 255f206aabbSJeff Dike l_write_sigio_pid = write_sigio_pid; 256f206aabbSJeff Dike sigio_unlock(); 257f206aabbSJeff Dike 258f206aabbSJeff Dike if (l_write_sigio_pid != -1) 259f206aabbSJeff Dike return; 260f206aabbSJeff Dike 261f206aabbSJeff Dike err = os_pipe(l_write_sigio_fds, 1, 1); 262f206aabbSJeff Dike if(err < 0){ 263f206aabbSJeff Dike printk("write_sigio_workaround - os_pipe 1 failed, " 264f206aabbSJeff Dike "err = %d\n", -err); 265f206aabbSJeff Dike return; 266f206aabbSJeff Dike } 267f206aabbSJeff Dike err = os_pipe(l_sigio_private, 1, 1); 268f206aabbSJeff Dike if(err < 0){ 269f206aabbSJeff Dike printk("write_sigio_workaround - os_pipe 2 failed, " 270f206aabbSJeff Dike "err = %d\n", -err); 271f206aabbSJeff Dike goto out_close1; 272f206aabbSJeff Dike } 273f206aabbSJeff Dike 274f206aabbSJeff Dike p = setup_initial_poll(l_sigio_private[1]); 275f206aabbSJeff Dike if(!p) 276f206aabbSJeff Dike goto out_close2; 277f206aabbSJeff Dike 278f206aabbSJeff Dike sigio_lock(); 279f206aabbSJeff Dike 280f206aabbSJeff Dike /* Did we race? Don't try to optimize this, please, it's not so likely 281f206aabbSJeff Dike * to happen, and no more than once at the boot. */ 282f206aabbSJeff Dike if(write_sigio_pid != -1) 2835f4e8fd0SJeff Dike goto out_free; 2845f4e8fd0SJeff Dike 2855f4e8fd0SJeff Dike current_poll = ((struct pollfds) { .poll = p, 2865f4e8fd0SJeff Dike .used = 1, 2875f4e8fd0SJeff Dike .size = 1 }); 2885f4e8fd0SJeff Dike 2895f4e8fd0SJeff Dike if (write_sigio_irq(l_write_sigio_fds[0])) 2905f4e8fd0SJeff Dike goto out_clear_poll; 2915f4e8fd0SJeff Dike 2925f4e8fd0SJeff Dike memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds)); 2935f4e8fd0SJeff Dike memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private)); 294f206aabbSJeff Dike 295f206aabbSJeff Dike write_sigio_pid = run_helper_thread(write_sigio_thread, NULL, 296f206aabbSJeff Dike CLONE_FILES | CLONE_VM, &stack, 0); 297f206aabbSJeff Dike 298f206aabbSJeff Dike if (write_sigio_pid < 0) 299f206aabbSJeff Dike goto out_clear; 300f206aabbSJeff Dike 301f206aabbSJeff Dike sigio_unlock(); 302f206aabbSJeff Dike return; 303f206aabbSJeff Dike 304f206aabbSJeff Dike out_clear: 305f206aabbSJeff Dike write_sigio_pid = -1; 3065f4e8fd0SJeff Dike write_sigio_fds[0] = -1; 3075f4e8fd0SJeff Dike write_sigio_fds[1] = -1; 3085f4e8fd0SJeff Dike sigio_private[0] = -1; 3095f4e8fd0SJeff Dike sigio_private[1] = -1; 3105f4e8fd0SJeff Dike out_clear_poll: 3115f4e8fd0SJeff Dike current_poll = ((struct pollfds) { .poll = NULL, 3125f4e8fd0SJeff Dike .size = 0, 3135f4e8fd0SJeff Dike .used = 0 }); 314f206aabbSJeff Dike out_free: 3155f4e8fd0SJeff Dike sigio_unlock(); 316e6fb54abSPaolo 'Blaisorblade' Giarrusso kfree(p); 317f206aabbSJeff Dike out_close2: 318f206aabbSJeff Dike close(l_sigio_private[0]); 319f206aabbSJeff Dike close(l_sigio_private[1]); 320f206aabbSJeff Dike out_close1: 321f206aabbSJeff Dike close(l_write_sigio_fds[0]); 322f206aabbSJeff Dike close(l_write_sigio_fds[1]); 323f206aabbSJeff Dike } 324f206aabbSJeff Dike 325c65badbdSJeff Dike /* Changed during early boot */ 326c65badbdSJeff Dike static int pty_output_sigio = 0; 327c65badbdSJeff Dike static int pty_close_sigio = 0; 328c65badbdSJeff Dike 3298e64d96aSJeff Dike void maybe_sigio_broken(int fd, int read) 3308e64d96aSJeff Dike { 33119bdf040SJeff Dike int err; 33219bdf040SJeff Dike 3338e64d96aSJeff Dike if(!isatty(fd)) 3348e64d96aSJeff Dike return; 3358e64d96aSJeff Dike 3368e64d96aSJeff Dike if((read || pty_output_sigio) && (!read || pty_close_sigio)) 3378e64d96aSJeff Dike return; 3388e64d96aSJeff Dike 3398e64d96aSJeff Dike write_sigio_workaround(); 34019bdf040SJeff Dike 34119bdf040SJeff Dike sigio_lock(); 34219bdf040SJeff Dike err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1); 34304a51e66SJeff Dike if(err){ 34404a51e66SJeff Dike printk("maybe_sigio_broken - failed to add pollfd for " 34504a51e66SJeff Dike "descriptor %d\n", fd); 34619bdf040SJeff Dike goto out; 34704a51e66SJeff Dike } 348838e56a1SJeff Dike 34919bdf040SJeff Dike all_sigio_fds.poll[all_sigio_fds.used++] = 35019bdf040SJeff Dike ((struct pollfd) { .fd = fd, 35119bdf040SJeff Dike .events = read ? POLLIN : POLLOUT, 35219bdf040SJeff Dike .revents = 0 }); 35319bdf040SJeff Dike out: 35419bdf040SJeff Dike sigio_unlock(); 3558e64d96aSJeff Dike } 3568e64d96aSJeff Dike 35729ac1c21SJeff Dike static void sigio_cleanup(void) 358f206aabbSJeff Dike { 359f206aabbSJeff Dike if(write_sigio_pid != -1){ 360f206aabbSJeff Dike os_kill_process(write_sigio_pid, 1); 361f206aabbSJeff Dike write_sigio_pid = -1; 362f206aabbSJeff Dike } 363f206aabbSJeff Dike } 36429ac1c21SJeff Dike 36529ac1c21SJeff Dike __uml_exitcall(sigio_cleanup); 366c65badbdSJeff Dike 367c65badbdSJeff Dike /* Used as a flag during SIGIO testing early in boot */ 368c65badbdSJeff Dike static volatile int got_sigio = 0; 369c65badbdSJeff Dike 370c65badbdSJeff Dike static void __init handler(int sig) 371c65badbdSJeff Dike { 372c65badbdSJeff Dike got_sigio = 1; 373c65badbdSJeff Dike } 374c65badbdSJeff Dike 375c65badbdSJeff Dike struct openpty_arg { 376c65badbdSJeff Dike int master; 377c65badbdSJeff Dike int slave; 378c65badbdSJeff Dike int err; 379c65badbdSJeff Dike }; 380c65badbdSJeff Dike 381c65badbdSJeff Dike static void openpty_cb(void *arg) 382c65badbdSJeff Dike { 383c65badbdSJeff Dike struct openpty_arg *info = arg; 384c65badbdSJeff Dike 385c65badbdSJeff Dike info->err = 0; 386c65badbdSJeff Dike if(openpty(&info->master, &info->slave, NULL, NULL, NULL)) 387c65badbdSJeff Dike info->err = -errno; 388c65badbdSJeff Dike } 389c65badbdSJeff Dike 390c65badbdSJeff Dike static int async_pty(int master, int slave) 391c65badbdSJeff Dike { 392c65badbdSJeff Dike int flags; 393c65badbdSJeff Dike 394c65badbdSJeff Dike flags = fcntl(master, F_GETFL); 395c65badbdSJeff Dike if(flags < 0) 396c65badbdSJeff Dike return -errno; 397c65badbdSJeff Dike 398c65badbdSJeff Dike if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) || 399c65badbdSJeff Dike (fcntl(master, F_SETOWN, os_getpid()) < 0)) 400c65badbdSJeff Dike return -errno; 401c65badbdSJeff Dike 402c65badbdSJeff Dike if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0)) 403c65badbdSJeff Dike return -errno; 404c65badbdSJeff Dike 405c65badbdSJeff Dike return(0); 406c65badbdSJeff Dike } 407c65badbdSJeff Dike 408c65badbdSJeff Dike static void __init check_one_sigio(void (*proc)(int, int)) 409c65badbdSJeff Dike { 410c65badbdSJeff Dike struct sigaction old, new; 411c65badbdSJeff Dike struct openpty_arg pty = { .master = -1, .slave = -1 }; 412c65badbdSJeff Dike int master, slave, err; 413c65badbdSJeff Dike 414c65badbdSJeff Dike initial_thread_cb(openpty_cb, &pty); 415c65badbdSJeff Dike if(pty.err){ 416c65badbdSJeff Dike printk("openpty failed, errno = %d\n", -pty.err); 417c65badbdSJeff Dike return; 418c65badbdSJeff Dike } 419c65badbdSJeff Dike 420c65badbdSJeff Dike master = pty.master; 421c65badbdSJeff Dike slave = pty.slave; 422c65badbdSJeff Dike 423c65badbdSJeff Dike if((master == -1) || (slave == -1)){ 424c65badbdSJeff Dike printk("openpty failed to allocate a pty\n"); 425c65badbdSJeff Dike return; 426c65badbdSJeff Dike } 427c65badbdSJeff Dike 428c65badbdSJeff Dike /* Not now, but complain so we now where we failed. */ 429c65badbdSJeff Dike err = raw(master); 430c65badbdSJeff Dike if (err < 0) 431c65badbdSJeff Dike panic("check_sigio : __raw failed, errno = %d\n", -err); 432c65badbdSJeff Dike 433c65badbdSJeff Dike err = async_pty(master, slave); 434c65badbdSJeff Dike if(err < 0) 435c65badbdSJeff Dike panic("tty_fds : sigio_async failed, err = %d\n", -err); 436c65badbdSJeff Dike 437c65badbdSJeff Dike if(sigaction(SIGIO, NULL, &old) < 0) 438c65badbdSJeff Dike panic("check_sigio : sigaction 1 failed, errno = %d\n", errno); 439c65badbdSJeff Dike new = old; 440c65badbdSJeff Dike new.sa_handler = handler; 441c65badbdSJeff Dike if(sigaction(SIGIO, &new, NULL) < 0) 442c65badbdSJeff Dike panic("check_sigio : sigaction 2 failed, errno = %d\n", errno); 443c65badbdSJeff Dike 444c65badbdSJeff Dike got_sigio = 0; 445c65badbdSJeff Dike (*proc)(master, slave); 446c65badbdSJeff Dike 447c65badbdSJeff Dike close(master); 448c65badbdSJeff Dike close(slave); 449c65badbdSJeff Dike 450c65badbdSJeff Dike if(sigaction(SIGIO, &old, NULL) < 0) 451c65badbdSJeff Dike panic("check_sigio : sigaction 3 failed, errno = %d\n", errno); 452c65badbdSJeff Dike } 453c65badbdSJeff Dike 454c65badbdSJeff Dike static void tty_output(int master, int slave) 455c65badbdSJeff Dike { 456c65badbdSJeff Dike int n; 457c65badbdSJeff Dike char buf[512]; 458c65badbdSJeff Dike 459c65badbdSJeff Dike printk("Checking that host ptys support output SIGIO..."); 460c65badbdSJeff Dike 461c65badbdSJeff Dike memset(buf, 0, sizeof(buf)); 462c65badbdSJeff Dike 463c65badbdSJeff Dike while(os_write_file(master, buf, sizeof(buf)) > 0) ; 464c65badbdSJeff Dike if(errno != EAGAIN) 465c65badbdSJeff Dike panic("check_sigio : write failed, errno = %d\n", errno); 466c65badbdSJeff Dike while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ; 467c65badbdSJeff Dike 468c65badbdSJeff Dike if(got_sigio){ 469c65badbdSJeff Dike printk("Yes\n"); 470c65badbdSJeff Dike pty_output_sigio = 1; 471c65badbdSJeff Dike } 472c65badbdSJeff Dike else if(n == -EAGAIN) printk("No, enabling workaround\n"); 473c65badbdSJeff Dike else panic("check_sigio : read failed, err = %d\n", n); 474c65badbdSJeff Dike } 475c65badbdSJeff Dike 476c65badbdSJeff Dike static void tty_close(int master, int slave) 477c65badbdSJeff Dike { 478c65badbdSJeff Dike printk("Checking that host ptys support SIGIO on close..."); 479c65badbdSJeff Dike 480c65badbdSJeff Dike close(slave); 481c65badbdSJeff Dike if(got_sigio){ 482c65badbdSJeff Dike printk("Yes\n"); 483c65badbdSJeff Dike pty_close_sigio = 1; 484c65badbdSJeff Dike } 485c65badbdSJeff Dike else printk("No, enabling workaround\n"); 486c65badbdSJeff Dike } 487c65badbdSJeff Dike 488c65badbdSJeff Dike void __init check_sigio(void) 489c65badbdSJeff Dike { 490c65badbdSJeff Dike if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) && 491c65badbdSJeff Dike (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){ 492c65badbdSJeff Dike printk("No pseudo-terminals available - skipping pty SIGIO " 493c65badbdSJeff Dike "check\n"); 494c65badbdSJeff Dike return; 495c65badbdSJeff Dike } 496c65badbdSJeff Dike check_one_sigio(tty_output); 497c65badbdSJeff Dike check_one_sigio(tty_close); 498c65badbdSJeff Dike } 499c65badbdSJeff Dike 500c65badbdSJeff Dike /* Here because it only does the SIGIO testing for now */ 501c65badbdSJeff Dike void __init os_check_bugs(void) 502c65badbdSJeff Dike { 503c65badbdSJeff Dike check_sigio(); 504c65badbdSJeff Dike } 505