^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <pty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <sigio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <um_malloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Protected by sigio_lock(), also used by sigio_cleanup, which is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * exitcall.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int write_sigio_pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static unsigned long write_sigio_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * These arrays are initialized before the sigio thread is started, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * the descriptors closed after it is killed. So, it can't see them change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * On the UML side, they are changed under the sigio_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define SIGIO_FDS_INIT {-1, -1}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int write_sigio_fds[2] = SIGIO_FDS_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int sigio_private[2] = SIGIO_FDS_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct pollfds {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct pollfd *poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * synchronizes with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct pollfds current_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static struct pollfds next_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static struct pollfds all_sigio_fds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int write_sigio_thread(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct pollfds *fds, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct pollfd *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int i, n, respond_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) os_fix_helper_signals();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) fds = ¤t_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) n = poll(fds->poll, fds->used, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (errno == EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) printk(UM_KERN_ERR "write_sigio_thread : poll returned "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) "%d, errno = %d\n", n, errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for (i = 0; i < fds->used; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) p = &fds->poll[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (p->revents == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (p->fd == sigio_private[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) CATCH_EINTR(n = read(sigio_private[1], &c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) sizeof(c)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (n != sizeof(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) printk(UM_KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "write_sigio_thread : "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "read on socket failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) "err = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) tmp = current_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) current_poll = next_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) next_poll = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) respond_fd = sigio_private[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) respond_fd = write_sigio_fds[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fds->used--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) memmove(&fds->poll[i], &fds->poll[i + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) (fds->used - i) * sizeof(*fds->poll));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (n != sizeof(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) printk(UM_KERN_ERR "write_sigio_thread : "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) "write on socket failed, err = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int need_poll(struct pollfds *polls, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct pollfd *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (n <= polls->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (new == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) printk(UM_KERN_ERR "need_poll : failed to allocate new "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "pollfds\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kfree(polls->poll);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) polls->poll = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) polls->size = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Must be called with sigio_lock held, because it's needed by the marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * critical section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void update_thread(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) flags = set_signals_trace(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (n != sizeof(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (n != sizeof(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) set_signals_trace(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Critical section start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (write_sigio_pid != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) os_kill_process(write_sigio_pid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) free_stack(write_sigio_stack, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) write_sigio_pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) close(sigio_private[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) close(sigio_private[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) close(write_sigio_fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) close(write_sigio_fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Critical section end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) set_signals_trace(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int add_sigio_fd(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct pollfd *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int err = 0, i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) sigio_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) for (i = 0; i < all_sigio_fds.used; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (all_sigio_fds.poll[i].fd == fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (i == all_sigio_fds.used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) p = &all_sigio_fds.poll[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) for (i = 0; i < current_poll.used; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (current_poll.poll[i].fd == fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) n = current_poll.used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) err = need_poll(&next_poll, n + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) memcpy(next_poll.poll, current_poll.poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) current_poll.used * sizeof(struct pollfd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) next_poll.poll[n] = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) next_poll.used = n + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) update_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) sigio_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ignore_sigio_fd(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct pollfd *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int err = 0, i, n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * This is called from exitcalls elsewhere in UML - if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * sigio_cleanup has already run, then update_thread will hang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * or fail because the thread is no longer running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (write_sigio_pid == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) sigio_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) for (i = 0; i < current_poll.used; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (current_poll.poll[i].fd == fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (i == current_poll.used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) err = need_poll(&next_poll, current_poll.used - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) for (i = 0; i < current_poll.used; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) p = ¤t_poll.poll[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (p->fd != fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) next_poll.poll[n++] = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) next_poll.used = current_poll.used - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) update_thread();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) sigio_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static struct pollfd *setup_initial_poll(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct pollfd *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (p == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) "poll\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *p = ((struct pollfd) { .fd = fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) .events = POLLIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) .revents = 0 });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static void write_sigio_workaround(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) struct pollfd *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int l_write_sigio_fds[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int l_sigio_private[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int l_write_sigio_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* We call this *tons* of times - and most ones we must just fail. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) sigio_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) l_write_sigio_pid = write_sigio_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) sigio_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (l_write_sigio_pid != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) err = os_pipe(l_write_sigio_fds, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) "err = %d\n", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err = os_pipe(l_sigio_private, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) "err = %d\n", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto out_close1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) p = setup_initial_poll(l_sigio_private[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) goto out_close2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) sigio_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * Did we race? Don't try to optimize this, please, it's not so likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * to happen, and no more than once at the boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (write_sigio_pid != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) current_poll = ((struct pollfds) { .poll = p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .used = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) .size = 1 });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (write_sigio_irq(l_write_sigio_fds[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) goto out_clear_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) CLONE_FILES | CLONE_VM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) &write_sigio_stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (write_sigio_pid < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) goto out_clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) sigio_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) out_clear:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) write_sigio_pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) write_sigio_fds[0] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) write_sigio_fds[1] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) sigio_private[0] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sigio_private[1] = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) out_clear_poll:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) current_poll = ((struct pollfds) { .poll = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .size = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .used = 0 });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) sigio_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) out_close2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) close(l_sigio_private[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) close(l_sigio_private[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) out_close1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) close(l_write_sigio_fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) close(l_write_sigio_fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) void sigio_broken(int fd, int read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) write_sigio_workaround();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) sigio_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) "for descriptor %d\n", fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) all_sigio_fds.poll[all_sigio_fds.used++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ((struct pollfd) { .fd = fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) .events = read ? POLLIN : POLLOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) .revents = 0 });
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) sigio_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* Changed during early boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int pty_output_sigio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int pty_close_sigio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void maybe_sigio_broken(int fd, int read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (!isatty(fd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if ((read || pty_output_sigio) && (!read || pty_close_sigio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) sigio_broken(fd, read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static void sigio_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (write_sigio_pid == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) os_kill_process(write_sigio_pid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) free_stack(write_sigio_stack, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) write_sigio_pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) __uml_exitcall(sigio_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* Used as a flag during SIGIO testing early in boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) static int got_sigio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static void __init handler(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) got_sigio = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct openpty_arg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int slave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static void openpty_cb(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct openpty_arg *info = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) info->err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) info->err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int async_pty(int master, int slave)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) flags = fcntl(master, F_GETFL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (flags < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) (fcntl(master, F_SETOWN, os_getpid()) < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static void __init check_one_sigio(void (*proc)(int, int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) struct sigaction old, new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct openpty_arg pty = { .master = -1, .slave = -1 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int master, slave, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) initial_thread_cb(openpty_cb, &pty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (pty.err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) -pty.err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) master = pty.master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) slave = pty.slave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if ((master == -1) || (slave == -1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) "pty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* Not now, but complain so we now where we failed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) err = raw(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) err = async_pty(master, slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) "err = %d\n", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (sigaction(SIGIO, NULL, &old) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) new = old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) new.sa_handler = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (sigaction(SIGIO, &new, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) got_sigio = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) (*proc)(master, slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) close(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) close(slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (sigaction(SIGIO, &old, NULL) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static void tty_output(int master, int slave)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) char buf[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) memset(buf, 0, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) while (write(master, buf, sizeof(buf)) > 0) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (errno != EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) while (((n = read(slave, buf, sizeof(buf))) > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) !({ barrier(); got_sigio; }))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (got_sigio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) printk(UM_KERN_CONT "Yes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) pty_output_sigio = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) } else if (n == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) printk(UM_KERN_CONT "No, enabling workaround\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static void tty_close(int master, int slave)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) "close...");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) close(slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (got_sigio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) printk(UM_KERN_CONT "Yes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) pty_close_sigio = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) printk(UM_KERN_CONT "No, enabling workaround\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static void __init check_sigio(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if ((access("/dev/ptmx", R_OK) < 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) (access("/dev/ptyp0", R_OK) < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) printk(UM_KERN_WARNING "No pseudo-terminals available - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) "skipping pty SIGIO check\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) check_one_sigio(tty_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) check_one_sigio(tty_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* Here because it only does the SIGIO testing for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) void __init os_check_bugs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) check_sigio();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }