blob: f6457765b17db86c7b03c3879e11e8f1da16934b [file] [log] [blame]
Jeff Dike8e367062006-03-27 01:14:32 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <unistd.h>
7#include <stdlib.h>
8#include <termios.h>
9#include <pty.h>
10#include <signal.h>
11#include <errno.h>
12#include <string.h>
13#include <sched.h>
14#include <sys/socket.h>
15#include <sys/poll.h>
16#include "init.h"
17#include "user.h"
18#include "kern_util.h"
19#include "user_util.h"
20#include "sigio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "os.h"
22
Jeff Dikef206aab2006-03-27 01:14:33 -080023/* Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * exitcall.
25 */
26static int write_sigio_pid = -1;
27
28/* These arrays are initialized before the sigio thread is started, and
29 * the descriptors closed after it is killed. So, it can't see them change.
30 * On the UML side, they are changed under the sigio_lock.
31 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080032#define SIGIO_FDS_INIT {-1, -1}
33
34static int write_sigio_fds[2] = SIGIO_FDS_INIT;
35static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct pollfds {
38 struct pollfd *poll;
39 int size;
40 int used;
41};
42
43/* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
44 * synchronizes with it.
45 */
Jeff Dike19bdf042006-09-25 23:33:04 -070046static struct pollfds current_poll;
47static struct pollfds next_poll;
48static struct pollfds all_sigio_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static int write_sigio_thread(void *unused)
51{
52 struct pollfds *fds, tmp;
53 struct pollfd *p;
54 int i, n, respond_fd;
55 char c;
56
Jeff Dikecd2ee4a2005-05-05 16:15:32 -070057 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 fds = &current_poll;
59 while(1){
60 n = poll(fds->poll, fds->used, -1);
61 if(n < 0){
62 if(errno == EINTR) continue;
63 printk("write_sigio_thread : poll returned %d, "
64 "errno = %d\n", n, errno);
65 }
66 for(i = 0; i < fds->used; i++){
67 p = &fds->poll[i];
68 if(p->revents == 0) continue;
69 if(p->fd == sigio_private[1]){
70 n = os_read_file(sigio_private[1], &c, sizeof(c));
71 if(n != sizeof(c))
72 printk("write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070073 "read on socket failed, "
74 "err = %d\n", -n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 tmp = current_poll;
76 current_poll = next_poll;
77 next_poll = tmp;
78 respond_fd = sigio_private[1];
79 }
80 else {
81 respond_fd = write_sigio_fds[1];
82 fds->used--;
83 memmove(&fds->poll[i], &fds->poll[i + 1],
84 (fds->used - i) * sizeof(*fds->poll));
85 }
86
87 n = os_write_file(respond_fd, &c, sizeof(c));
88 if(n != sizeof(c))
Jeff Dike19bdf042006-09-25 23:33:04 -070089 printk("write_sigio_thread : write on socket "
90 "failed, err = %d\n", -n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -080093
94 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Jeff Dike19bdf042006-09-25 23:33:04 -070097static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Jeff Dike19bdf042006-09-25 23:33:04 -070099 if(n <= polls->size){
100 polls->used = n;
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700103 kfree(polls->poll);
104 polls->poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
105 if(polls->poll == NULL){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 printk("need_poll : failed to allocate new pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700107 polls->size = 0;
108 polls->used = 0;
109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700111 polls->size = n;
112 polls->used = n;
113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116/* Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700117 * critical section.
118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static void update_thread(void)
120{
121 unsigned long flags;
122 int n;
123 char c;
124
125 flags = set_signals(0);
126 n = os_write_file(sigio_private[0], &c, sizeof(c));
127 if(n != sizeof(c)){
128 printk("update_thread : write failed, err = %d\n", -n);
129 goto fail;
130 }
131
132 n = os_read_file(sigio_private[0], &c, sizeof(c));
133 if(n != sizeof(c)){
134 printk("update_thread : read failed, err = %d\n", -n);
135 goto fail;
136 }
137
138 set_signals(flags);
139 return;
140 fail:
141 /* Critical section start */
Jeff Dikef206aab2006-03-27 01:14:33 -0800142 if(write_sigio_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 os_kill_process(write_sigio_pid, 1);
144 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800145 close(sigio_private[0]);
146 close(sigio_private[1]);
147 close(write_sigio_fds[0]);
148 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* Critical section end */
150 set_signals(flags);
151}
152
Jeff Dike19bdf042006-09-25 23:33:04 -0700153int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Jeff Dike19bdf042006-09-25 23:33:04 -0700155 struct pollfd *p;
156 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 sigio_lock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700159 for(i = 0; i < all_sigio_fds.used; i++){
160 if(all_sigio_fds.poll[i].fd == fd)
161 break;
162 }
163 if(i == all_sigio_fds.used)
164 goto out;
165
166 p = &all_sigio_fds.poll[i];
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 for(i = 0; i < current_poll.used; i++){
Jeff Dikef206aab2006-03-27 01:14:33 -0800169 if(current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out;
171 }
172
173 n = current_poll.used + 1;
Jeff Dike19bdf042006-09-25 23:33:04 -0700174 err = need_poll(&next_poll, n);
Jeff Dikef206aab2006-03-27 01:14:33 -0800175 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto out;
177
178 for(i = 0; i < current_poll.used; i++)
179 next_poll.poll[i] = current_poll.poll[i];
180
Jeff Dike19bdf042006-09-25 23:33:04 -0700181 next_poll.poll[n - 1] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 update_thread();
183 out:
184 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700185 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188int ignore_sigio_fd(int fd)
189{
190 struct pollfd *p;
191 int err = 0, i, n = 0;
192
Jeff Dike61232f22006-07-10 04:45:11 -0700193 /* This is called from exitcalls elsewhere in UML - if
194 * sigio_cleanup has already run, then update_thread will hang
195 * or fail because the thread is no longer running.
196 */
197 if(write_sigio_pid == -1)
198 return -EIO;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 sigio_lock();
201 for(i = 0; i < current_poll.used; i++){
202 if(current_poll.poll[i].fd == fd) break;
203 }
204 if(i == current_poll.used)
205 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800206
Jeff Dike19bdf042006-09-25 23:33:04 -0700207 err = need_poll(&next_poll, current_poll.used - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if(err)
209 goto out;
210
211 for(i = 0; i < current_poll.used; i++){
212 p = &current_poll.poll[i];
Jeff Dike19bdf042006-09-25 23:33:04 -0700213 if(p->fd != fd)
214 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 update_thread();
218 out:
219 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Jeff Dikef206aab2006-03-27 01:14:33 -0800223static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct pollfd *p;
226
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800227 p = um_kmalloc(sizeof(struct pollfd));
228 if (p == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 printk("setup_initial_poll : failed to allocate poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800230 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700232 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .events = POLLIN,
234 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800235 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700238static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 unsigned long stack;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800241 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800243 int l_write_sigio_fds[2];
244 int l_sigio_private[2];
245 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800247 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800249 l_write_sigio_pid = write_sigio_pid;
250 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800252 if (l_write_sigio_pid != -1)
253 return;
254
255 err = os_pipe(l_write_sigio_fds, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if(err < 0){
257 printk("write_sigio_workaround - os_pipe 1 failed, "
258 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800259 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800261 err = os_pipe(l_sigio_private, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if(err < 0){
Jeff Dikef206aab2006-03-27 01:14:33 -0800263 printk("write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 "err = %d\n", -err);
265 goto out_close1;
266 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800267
268 p = setup_initial_poll(l_sigio_private[1]);
269 if(!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto out_close2;
271
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800272 sigio_lock();
273
274 /* Did we race? Don't try to optimize this, please, it's not so likely
275 * to happen, and no more than once at the boot. */
276 if(write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800277 goto out_free;
278
279 current_poll = ((struct pollfds) { .poll = p,
280 .used = 1,
281 .size = 1 });
282
283 if (write_sigio_irq(l_write_sigio_fds[0]))
284 goto out_clear_poll;
285
286 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
287 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800288
289 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 CLONE_FILES | CLONE_VM, &stack, 0);
291
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800292 if (write_sigio_pid < 0)
293 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sigio_unlock();
296 return;
297
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800298out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800300 write_sigio_fds[0] = -1;
301 write_sigio_fds[1] = -1;
302 sigio_private[0] = -1;
303 sigio_private[1] = -1;
304out_clear_poll:
305 current_poll = ((struct pollfds) { .poll = NULL,
306 .size = 0,
307 .used = 0 });
308out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800309 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700310 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800311out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800312 close(l_sigio_private[0]);
313 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800314out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800315 close(l_write_sigio_fds[0]);
316 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700319void maybe_sigio_broken(int fd, int read)
320{
Jeff Dike19bdf042006-09-25 23:33:04 -0700321 int err;
322
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700323 if(!isatty(fd))
324 return;
325
326 if((read || pty_output_sigio) && (!read || pty_close_sigio))
327 return;
328
329 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700330
331 sigio_lock();
332 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
333 if(err){
334 printk("maybe_sigio_broken - failed to add pollfd\n");
335 goto out;
336 }
337 all_sigio_fds.poll[all_sigio_fds.used++] =
338 ((struct pollfd) { .fd = fd,
339 .events = read ? POLLIN : POLLOUT,
340 .revents = 0 });
341out:
342 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700343}
344
Jeff Dike29ac1c22006-07-10 04:45:12 -0700345static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Jeff Dikef206aab2006-03-27 01:14:33 -0800347 if(write_sigio_pid != -1){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 os_kill_process(write_sigio_pid, 1);
349 write_sigio_pid = -1;
350 }
351}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700352
353__uml_exitcall(sigio_cleanup);