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