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