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> 11f206aabbSJeff Dike #include <errno.h> 12f206aabbSJeff Dike #include <string.h> 13f206aabbSJeff Dike #include <sched.h> 14f206aabbSJeff Dike #include <sys/socket.h> 15f206aabbSJeff Dike #include <sys/poll.h> 16f206aabbSJeff Dike #include "init.h" 17f206aabbSJeff Dike #include "user.h" 18f206aabbSJeff Dike #include "kern_util.h" 19f206aabbSJeff Dike #include "user_util.h" 20f206aabbSJeff Dike #include "sigio.h" 21f206aabbSJeff Dike #include "os.h" 22c13e5690SPaolo 'Blaisorblade' Giarrusso #include "um_malloc.h" 23f206aabbSJeff Dike 24f206aabbSJeff Dike /* Protected by sigio_lock(), also used by sigio_cleanup, which is an 25f206aabbSJeff Dike * exitcall. 26f206aabbSJeff Dike */ 27f206aabbSJeff Dike static int write_sigio_pid = -1; 28f206aabbSJeff Dike 29f206aabbSJeff Dike /* These arrays are initialized before the sigio thread is started, and 30f206aabbSJeff Dike * the descriptors closed after it is killed. So, it can't see them change. 31f206aabbSJeff Dike * On the UML side, they are changed under the sigio_lock. 32f206aabbSJeff Dike */ 335f4e8fd0SJeff Dike #define SIGIO_FDS_INIT {-1, -1} 345f4e8fd0SJeff Dike 355f4e8fd0SJeff Dike static int write_sigio_fds[2] = SIGIO_FDS_INIT; 365f4e8fd0SJeff Dike static int sigio_private[2] = SIGIO_FDS_INIT; 37f206aabbSJeff Dike 38f206aabbSJeff Dike struct pollfds { 39f206aabbSJeff Dike struct pollfd *poll; 40f206aabbSJeff Dike int size; 41f206aabbSJeff Dike int used; 42f206aabbSJeff Dike }; 43f206aabbSJeff Dike 44f206aabbSJeff Dike /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread 45f206aabbSJeff Dike * synchronizes with it. 46f206aabbSJeff Dike */ 4719bdf040SJeff Dike static struct pollfds current_poll; 4819bdf040SJeff Dike static struct pollfds next_poll; 4919bdf040SJeff Dike static struct pollfds all_sigio_fds; 50f206aabbSJeff Dike 51f206aabbSJeff Dike static int write_sigio_thread(void *unused) 52f206aabbSJeff Dike { 53f206aabbSJeff Dike struct pollfds *fds, tmp; 54f206aabbSJeff Dike struct pollfd *p; 55f206aabbSJeff Dike int i, n, respond_fd; 56f206aabbSJeff Dike char c; 57f206aabbSJeff Dike 58f206aabbSJeff Dike signal(SIGWINCH, SIG_IGN); 59f206aabbSJeff Dike fds = ¤t_poll; 60f206aabbSJeff Dike while(1){ 61f206aabbSJeff Dike n = poll(fds->poll, fds->used, -1); 62f206aabbSJeff Dike if(n < 0){ 63f206aabbSJeff Dike if(errno == EINTR) continue; 64f206aabbSJeff Dike printk("write_sigio_thread : poll returned %d, " 65f206aabbSJeff Dike "errno = %d\n", n, errno); 66f206aabbSJeff Dike } 67f206aabbSJeff Dike for(i = 0; i < fds->used; i++){ 68f206aabbSJeff Dike p = &fds->poll[i]; 69f206aabbSJeff Dike if(p->revents == 0) continue; 70f206aabbSJeff Dike if(p->fd == sigio_private[1]){ 71f206aabbSJeff Dike n = os_read_file(sigio_private[1], &c, sizeof(c)); 72f206aabbSJeff Dike if(n != sizeof(c)) 73f206aabbSJeff Dike printk("write_sigio_thread : " 7419bdf040SJeff Dike "read on socket failed, " 7519bdf040SJeff Dike "err = %d\n", -n); 76f206aabbSJeff Dike tmp = current_poll; 77f206aabbSJeff Dike current_poll = next_poll; 78f206aabbSJeff Dike next_poll = tmp; 79f206aabbSJeff Dike respond_fd = sigio_private[1]; 80f206aabbSJeff Dike } 81f206aabbSJeff Dike else { 82f206aabbSJeff Dike respond_fd = write_sigio_fds[1]; 83f206aabbSJeff Dike fds->used--; 84f206aabbSJeff Dike memmove(&fds->poll[i], &fds->poll[i + 1], 85f206aabbSJeff Dike (fds->used - i) * sizeof(*fds->poll)); 86f206aabbSJeff Dike } 87f206aabbSJeff Dike 88f206aabbSJeff Dike n = os_write_file(respond_fd, &c, sizeof(c)); 89f206aabbSJeff Dike if(n != sizeof(c)) 9019bdf040SJeff Dike printk("write_sigio_thread : write on socket " 9119bdf040SJeff Dike "failed, err = %d\n", -n); 92f206aabbSJeff Dike } 93f206aabbSJeff Dike } 94f206aabbSJeff Dike 95f206aabbSJeff Dike return 0; 96f206aabbSJeff Dike } 97f206aabbSJeff Dike 9819bdf040SJeff Dike static int need_poll(struct pollfds *polls, int n) 99f206aabbSJeff Dike { 10019bdf040SJeff Dike if(n <= polls->size){ 10119bdf040SJeff Dike polls->used = n; 10219bdf040SJeff Dike return 0; 103f206aabbSJeff Dike } 10419bdf040SJeff Dike kfree(polls->poll); 10519bdf040SJeff Dike polls->poll = um_kmalloc_atomic(n * sizeof(struct pollfd)); 10619bdf040SJeff Dike if(polls->poll == NULL){ 107f206aabbSJeff Dike printk("need_poll : failed to allocate new pollfds\n"); 10819bdf040SJeff Dike polls->size = 0; 10919bdf040SJeff Dike polls->used = 0; 11019bdf040SJeff Dike return -ENOMEM; 111f206aabbSJeff Dike } 11219bdf040SJeff Dike polls->size = n; 11319bdf040SJeff Dike polls->used = n; 11419bdf040SJeff Dike return 0; 115f206aabbSJeff Dike } 116f206aabbSJeff Dike 117f206aabbSJeff Dike /* Must be called with sigio_lock held, because it's needed by the marked 11819bdf040SJeff Dike * critical section. 11919bdf040SJeff Dike */ 120f206aabbSJeff Dike static void update_thread(void) 121f206aabbSJeff Dike { 122f206aabbSJeff Dike unsigned long flags; 123f206aabbSJeff Dike int n; 124f206aabbSJeff Dike char c; 125f206aabbSJeff Dike 126f206aabbSJeff Dike flags = set_signals(0); 127f206aabbSJeff Dike n = os_write_file(sigio_private[0], &c, sizeof(c)); 128f206aabbSJeff Dike if(n != sizeof(c)){ 129f206aabbSJeff Dike printk("update_thread : write failed, err = %d\n", -n); 130f206aabbSJeff Dike goto fail; 131f206aabbSJeff Dike } 132f206aabbSJeff Dike 133f206aabbSJeff Dike n = os_read_file(sigio_private[0], &c, sizeof(c)); 134f206aabbSJeff Dike if(n != sizeof(c)){ 135f206aabbSJeff Dike printk("update_thread : read failed, err = %d\n", -n); 136f206aabbSJeff Dike goto fail; 137f206aabbSJeff Dike } 138f206aabbSJeff Dike 139f206aabbSJeff Dike set_signals(flags); 140f206aabbSJeff Dike return; 141f206aabbSJeff Dike fail: 142f206aabbSJeff Dike /* Critical section start */ 143f206aabbSJeff Dike if(write_sigio_pid != -1) 144f206aabbSJeff Dike os_kill_process(write_sigio_pid, 1); 145f206aabbSJeff Dike write_sigio_pid = -1; 146f206aabbSJeff Dike close(sigio_private[0]); 147f206aabbSJeff Dike close(sigio_private[1]); 148f206aabbSJeff Dike close(write_sigio_fds[0]); 149f206aabbSJeff Dike close(write_sigio_fds[1]); 150f206aabbSJeff Dike /* Critical section end */ 151f206aabbSJeff Dike set_signals(flags); 152f206aabbSJeff Dike } 153f206aabbSJeff Dike 15419bdf040SJeff Dike int add_sigio_fd(int fd) 155f206aabbSJeff Dike { 15619bdf040SJeff Dike struct pollfd *p; 15719bdf040SJeff Dike int err = 0, i, n; 158f206aabbSJeff Dike 159f206aabbSJeff Dike sigio_lock(); 16019bdf040SJeff Dike for(i = 0; i < all_sigio_fds.used; i++){ 16119bdf040SJeff Dike if(all_sigio_fds.poll[i].fd == fd) 16219bdf040SJeff Dike break; 16319bdf040SJeff Dike } 16419bdf040SJeff Dike if(i == all_sigio_fds.used) 16519bdf040SJeff Dike goto out; 16619bdf040SJeff Dike 16719bdf040SJeff Dike p = &all_sigio_fds.poll[i]; 16819bdf040SJeff Dike 169f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++){ 170f206aabbSJeff Dike if(current_poll.poll[i].fd == fd) 171f206aabbSJeff Dike goto out; 172f206aabbSJeff Dike } 173f206aabbSJeff Dike 174f206aabbSJeff Dike n = current_poll.used + 1; 17519bdf040SJeff Dike err = need_poll(&next_poll, n); 176f206aabbSJeff Dike if(err) 177f206aabbSJeff Dike goto out; 178f206aabbSJeff Dike 179f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++) 180f206aabbSJeff Dike next_poll.poll[i] = current_poll.poll[i]; 181f206aabbSJeff Dike 18219bdf040SJeff Dike next_poll.poll[n - 1] = *p; 183f206aabbSJeff Dike update_thread(); 184f206aabbSJeff Dike out: 185f206aabbSJeff Dike sigio_unlock(); 18619bdf040SJeff Dike return err; 187f206aabbSJeff Dike } 188f206aabbSJeff Dike 189f206aabbSJeff Dike int ignore_sigio_fd(int fd) 190f206aabbSJeff Dike { 191f206aabbSJeff Dike struct pollfd *p; 192f206aabbSJeff Dike int err = 0, i, n = 0; 193f206aabbSJeff Dike 19461232f2fSJeff Dike /* This is called from exitcalls elsewhere in UML - if 19561232f2fSJeff Dike * sigio_cleanup has already run, then update_thread will hang 19661232f2fSJeff Dike * or fail because the thread is no longer running. 19761232f2fSJeff Dike */ 19861232f2fSJeff Dike if(write_sigio_pid == -1) 19961232f2fSJeff Dike return -EIO; 20061232f2fSJeff Dike 201f206aabbSJeff Dike sigio_lock(); 202f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++){ 203f206aabbSJeff Dike if(current_poll.poll[i].fd == fd) break; 204f206aabbSJeff Dike } 205f206aabbSJeff Dike if(i == current_poll.used) 206f206aabbSJeff Dike goto out; 207f206aabbSJeff Dike 20819bdf040SJeff Dike err = need_poll(&next_poll, current_poll.used - 1); 209f206aabbSJeff Dike if(err) 210f206aabbSJeff Dike goto out; 211f206aabbSJeff Dike 212f206aabbSJeff Dike for(i = 0; i < current_poll.used; i++){ 213f206aabbSJeff Dike p = ¤t_poll.poll[i]; 21419bdf040SJeff Dike if(p->fd != fd) 21519bdf040SJeff Dike next_poll.poll[n++] = *p; 216f206aabbSJeff Dike } 217f206aabbSJeff Dike 218f206aabbSJeff Dike update_thread(); 219f206aabbSJeff Dike out: 220f206aabbSJeff Dike sigio_unlock(); 22161232f2fSJeff Dike return err; 222f206aabbSJeff Dike } 223f206aabbSJeff Dike 224f206aabbSJeff Dike static struct pollfd *setup_initial_poll(int fd) 225f206aabbSJeff Dike { 226f206aabbSJeff Dike struct pollfd *p; 227f206aabbSJeff Dike 228f206aabbSJeff Dike p = um_kmalloc(sizeof(struct pollfd)); 229f206aabbSJeff Dike if (p == NULL) { 230f206aabbSJeff Dike printk("setup_initial_poll : failed to allocate poll\n"); 231f206aabbSJeff Dike return NULL; 232f206aabbSJeff Dike } 233f206aabbSJeff Dike *p = ((struct pollfd) { .fd = fd, 234f206aabbSJeff Dike .events = POLLIN, 235f206aabbSJeff Dike .revents = 0 }); 236f206aabbSJeff Dike return p; 237f206aabbSJeff Dike } 238f206aabbSJeff Dike 2398e64d96aSJeff Dike static void write_sigio_workaround(void) 240f206aabbSJeff Dike { 241f206aabbSJeff Dike unsigned long stack; 242f206aabbSJeff Dike struct pollfd *p; 243f206aabbSJeff Dike int err; 244f206aabbSJeff Dike int l_write_sigio_fds[2]; 245f206aabbSJeff Dike int l_sigio_private[2]; 246f206aabbSJeff Dike int l_write_sigio_pid; 247f206aabbSJeff Dike 248f206aabbSJeff Dike /* We call this *tons* of times - and most ones we must just fail. */ 249f206aabbSJeff Dike sigio_lock(); 250f206aabbSJeff Dike l_write_sigio_pid = write_sigio_pid; 251f206aabbSJeff Dike sigio_unlock(); 252f206aabbSJeff Dike 253f206aabbSJeff Dike if (l_write_sigio_pid != -1) 254f206aabbSJeff Dike return; 255f206aabbSJeff Dike 256f206aabbSJeff Dike err = os_pipe(l_write_sigio_fds, 1, 1); 257f206aabbSJeff Dike if(err < 0){ 258f206aabbSJeff Dike printk("write_sigio_workaround - os_pipe 1 failed, " 259f206aabbSJeff Dike "err = %d\n", -err); 260f206aabbSJeff Dike return; 261f206aabbSJeff Dike } 262f206aabbSJeff Dike err = os_pipe(l_sigio_private, 1, 1); 263f206aabbSJeff Dike if(err < 0){ 264f206aabbSJeff Dike printk("write_sigio_workaround - os_pipe 2 failed, " 265f206aabbSJeff Dike "err = %d\n", -err); 266f206aabbSJeff Dike goto out_close1; 267f206aabbSJeff Dike } 268f206aabbSJeff Dike 269f206aabbSJeff Dike p = setup_initial_poll(l_sigio_private[1]); 270f206aabbSJeff Dike if(!p) 271f206aabbSJeff Dike goto out_close2; 272f206aabbSJeff Dike 273f206aabbSJeff Dike sigio_lock(); 274f206aabbSJeff Dike 275f206aabbSJeff Dike /* Did we race? Don't try to optimize this, please, it's not so likely 276f206aabbSJeff Dike * to happen, and no more than once at the boot. */ 277f206aabbSJeff Dike if(write_sigio_pid != -1) 2785f4e8fd0SJeff Dike goto out_free; 2795f4e8fd0SJeff Dike 2805f4e8fd0SJeff Dike current_poll = ((struct pollfds) { .poll = p, 2815f4e8fd0SJeff Dike .used = 1, 2825f4e8fd0SJeff Dike .size = 1 }); 2835f4e8fd0SJeff Dike 2845f4e8fd0SJeff Dike if (write_sigio_irq(l_write_sigio_fds[0])) 2855f4e8fd0SJeff Dike goto out_clear_poll; 2865f4e8fd0SJeff Dike 2875f4e8fd0SJeff Dike memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds)); 2885f4e8fd0SJeff Dike memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private)); 289f206aabbSJeff Dike 290f206aabbSJeff Dike write_sigio_pid = run_helper_thread(write_sigio_thread, NULL, 291f206aabbSJeff Dike CLONE_FILES | CLONE_VM, &stack, 0); 292f206aabbSJeff Dike 293f206aabbSJeff Dike if (write_sigio_pid < 0) 294f206aabbSJeff Dike goto out_clear; 295f206aabbSJeff Dike 296f206aabbSJeff Dike sigio_unlock(); 297f206aabbSJeff Dike return; 298f206aabbSJeff Dike 299f206aabbSJeff Dike out_clear: 300f206aabbSJeff Dike write_sigio_pid = -1; 3015f4e8fd0SJeff Dike write_sigio_fds[0] = -1; 3025f4e8fd0SJeff Dike write_sigio_fds[1] = -1; 3035f4e8fd0SJeff Dike sigio_private[0] = -1; 3045f4e8fd0SJeff Dike sigio_private[1] = -1; 3055f4e8fd0SJeff Dike out_clear_poll: 3065f4e8fd0SJeff Dike current_poll = ((struct pollfds) { .poll = NULL, 3075f4e8fd0SJeff Dike .size = 0, 3085f4e8fd0SJeff Dike .used = 0 }); 309f206aabbSJeff Dike out_free: 3105f4e8fd0SJeff Dike sigio_unlock(); 311e6fb54abSPaolo 'Blaisorblade' Giarrusso kfree(p); 312f206aabbSJeff Dike out_close2: 313f206aabbSJeff Dike close(l_sigio_private[0]); 314f206aabbSJeff Dike close(l_sigio_private[1]); 315f206aabbSJeff Dike out_close1: 316f206aabbSJeff Dike close(l_write_sigio_fds[0]); 317f206aabbSJeff Dike close(l_write_sigio_fds[1]); 318f206aabbSJeff Dike } 319f206aabbSJeff Dike 3208e64d96aSJeff Dike void maybe_sigio_broken(int fd, int read) 3218e64d96aSJeff Dike { 32219bdf040SJeff Dike int err; 32319bdf040SJeff Dike 3248e64d96aSJeff Dike if(!isatty(fd)) 3258e64d96aSJeff Dike return; 3268e64d96aSJeff Dike 3278e64d96aSJeff Dike if((read || pty_output_sigio) && (!read || pty_close_sigio)) 3288e64d96aSJeff Dike return; 3298e64d96aSJeff Dike 3308e64d96aSJeff Dike write_sigio_workaround(); 33119bdf040SJeff Dike 33219bdf040SJeff Dike sigio_lock(); 33319bdf040SJeff Dike err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1); 33419bdf040SJeff Dike if(err){ 33519bdf040SJeff Dike printk("maybe_sigio_broken - failed to add pollfd\n"); 33619bdf040SJeff Dike goto out; 33719bdf040SJeff Dike } 33819bdf040SJeff Dike all_sigio_fds.poll[all_sigio_fds.used++] = 33919bdf040SJeff Dike ((struct pollfd) { .fd = fd, 34019bdf040SJeff Dike .events = read ? POLLIN : POLLOUT, 34119bdf040SJeff Dike .revents = 0 }); 34219bdf040SJeff Dike out: 34319bdf040SJeff Dike sigio_unlock(); 3448e64d96aSJeff Dike } 3458e64d96aSJeff Dike 34629ac1c21SJeff Dike static void sigio_cleanup(void) 347f206aabbSJeff Dike { 348f206aabbSJeff Dike if(write_sigio_pid != -1){ 349f206aabbSJeff Dike os_kill_process(write_sigio_pid, 1); 350f206aabbSJeff Dike write_sigio_pid = -1; 351f206aabbSJeff Dike } 352f206aabbSJeff Dike } 35329ac1c21SJeff Dike 35429ac1c21SJeff Dike __uml_exitcall(sigio_cleanup); 355