blob: 8ccf6a36f1c62b9faba9c5f3e0ec83c8adf8c47c [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>
Jeff Dikec65badb2007-05-06 14:51:06 -070011#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <errno.h>
13#include <string.h>
14#include <sched.h>
15#include <sys/socket.h>
16#include <sys/poll.h>
17#include "init.h"
18#include "user.h"
19#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "sigio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070022#include "um_malloc.h"
Jeff Dikec65badb2007-05-06 14:51:06 -070023#include "init.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jeff Dikef206aab2006-03-27 01:14:33 -080025/* Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * exitcall.
27 */
28static int write_sigio_pid = -1;
29
30/* These arrays are initialized before the sigio thread is started, and
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 Dike5f4e8fd2006-03-27 01:14:40 -080034#define SIGIO_FDS_INIT {-1, -1}
35
36static int write_sigio_fds[2] = SIGIO_FDS_INIT;
37static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct pollfds {
40 struct pollfd *poll;
41 int size;
42 int used;
43};
44
45/* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
46 * synchronizes with it.
47 */
Jeff Dike19bdf042006-09-25 23:33:04 -070048static struct pollfds current_poll;
49static struct pollfds next_poll;
50static struct pollfds all_sigio_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static int write_sigio_thread(void *unused)
53{
54 struct pollfds *fds, tmp;
55 struct pollfd *p;
56 int i, n, respond_fd;
57 char c;
58
Jeff Dikecd2ee4a2005-05-05 16:15:32 -070059 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 fds = &current_poll;
61 while(1){
62 n = poll(fds->poll, fds->used, -1);
63 if(n < 0){
64 if(errno == EINTR) continue;
65 printk("write_sigio_thread : poll returned %d, "
66 "errno = %d\n", n, errno);
67 }
68 for(i = 0; i < fds->used; i++){
69 p = &fds->poll[i];
70 if(p->revents == 0) continue;
71 if(p->fd == sigio_private[1]){
72 n = os_read_file(sigio_private[1], &c, sizeof(c));
73 if(n != sizeof(c))
74 printk("write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070075 "read on socket failed, "
76 "err = %d\n", -n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 tmp = current_poll;
78 current_poll = next_poll;
79 next_poll = tmp;
80 respond_fd = sigio_private[1];
81 }
82 else {
83 respond_fd = write_sigio_fds[1];
84 fds->used--;
85 memmove(&fds->poll[i], &fds->poll[i + 1],
86 (fds->used - i) * sizeof(*fds->poll));
87 }
88
89 n = os_write_file(respond_fd, &c, sizeof(c));
90 if(n != sizeof(c))
Jeff Dike19bdf042006-09-25 23:33:04 -070091 printk("write_sigio_thread : write on socket "
92 "failed, err = %d\n", -n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -080095
96 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Jeff Dike19bdf042006-09-25 23:33:04 -070099static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Jeff Dike838e56a2007-02-16 01:27:21 -0800101 struct pollfd *new;
102
103 if(n <= polls->size)
Jeff Dike19bdf042006-09-25 23:33:04 -0700104 return 0;
Jeff Dike838e56a2007-02-16 01:27:21 -0800105
106 new = um_kmalloc_atomic(n * sizeof(struct pollfd));
107 if(new == NULL){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 printk("need_poll : failed to allocate new pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700109 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800111
112 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
113 kfree(polls->poll);
114
115 polls->poll = new;
Jeff Dike19bdf042006-09-25 23:33:04 -0700116 polls->size = n;
Jeff Dike19bdf042006-09-25 23:33:04 -0700117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/* Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700121 * critical section.
122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static void update_thread(void)
124{
125 unsigned long flags;
126 int n;
127 char c;
128
129 flags = set_signals(0);
130 n = os_write_file(sigio_private[0], &c, sizeof(c));
131 if(n != sizeof(c)){
132 printk("update_thread : write failed, err = %d\n", -n);
133 goto fail;
134 }
135
136 n = os_read_file(sigio_private[0], &c, sizeof(c));
137 if(n != sizeof(c)){
138 printk("update_thread : read failed, err = %d\n", -n);
139 goto fail;
140 }
141
142 set_signals(flags);
143 return;
144 fail:
145 /* Critical section start */
Jeff Dikef206aab2006-03-27 01:14:33 -0800146 if(write_sigio_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 os_kill_process(write_sigio_pid, 1);
148 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800149 close(sigio_private[0]);
150 close(sigio_private[1]);
151 close(write_sigio_fds[0]);
152 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Critical section end */
154 set_signals(flags);
155}
156
Jeff Dike19bdf042006-09-25 23:33:04 -0700157int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Jeff Dike19bdf042006-09-25 23:33:04 -0700159 struct pollfd *p;
160 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 sigio_lock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700163 for(i = 0; i < all_sigio_fds.used; i++){
164 if(all_sigio_fds.poll[i].fd == fd)
165 break;
166 }
167 if(i == all_sigio_fds.used)
168 goto out;
169
170 p = &all_sigio_fds.poll[i];
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 for(i = 0; i < current_poll.used; i++){
Jeff Dikef206aab2006-03-27 01:14:33 -0800173 if(current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto out;
175 }
176
Jeff Dike838e56a2007-02-16 01:27:21 -0800177 n = current_poll.used;
178 err = need_poll(&next_poll, n + 1);
Jeff Dikef206aab2006-03-27 01:14:33 -0800179 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out;
181
Jeff Dike838e56a2007-02-16 01:27:21 -0800182 memcpy(next_poll.poll, current_poll.poll,
183 current_poll.used * sizeof(struct pollfd));
184 next_poll.poll[n] = *p;
185 next_poll.used = n + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 update_thread();
187 out:
188 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192int ignore_sigio_fd(int fd)
193{
194 struct pollfd *p;
195 int err = 0, i, n = 0;
196
Jeff Dike61232f22006-07-10 04:45:11 -0700197 /* This is called from exitcalls elsewhere in UML - if
198 * sigio_cleanup has already run, then update_thread will hang
199 * or fail because the thread is no longer running.
200 */
201 if(write_sigio_pid == -1)
202 return -EIO;
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 sigio_lock();
205 for(i = 0; i < current_poll.used; i++){
206 if(current_poll.poll[i].fd == fd) break;
207 }
208 if(i == current_poll.used)
209 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800210
Jeff Dike19bdf042006-09-25 23:33:04 -0700211 err = need_poll(&next_poll, current_poll.used - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if(err)
213 goto out;
214
215 for(i = 0; i < current_poll.used; i++){
216 p = &current_poll.poll[i];
Jeff Dike19bdf042006-09-25 23:33:04 -0700217 if(p->fd != fd)
218 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800220 next_poll.used = current_poll.used - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 update_thread();
223 out:
224 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Jeff Dikef206aab2006-03-27 01:14:33 -0800228static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct pollfd *p;
231
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800232 p = um_kmalloc(sizeof(struct pollfd));
233 if (p == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 printk("setup_initial_poll : failed to allocate poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800235 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700237 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .events = POLLIN,
239 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800240 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700243static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 unsigned long stack;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800246 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800248 int l_write_sigio_fds[2];
249 int l_sigio_private[2];
250 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800252 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800254 l_write_sigio_pid = write_sigio_pid;
255 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800257 if (l_write_sigio_pid != -1)
258 return;
259
260 err = os_pipe(l_write_sigio_fds, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if(err < 0){
262 printk("write_sigio_workaround - os_pipe 1 failed, "
263 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800264 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800266 err = os_pipe(l_sigio_private, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if(err < 0){
Jeff Dikef206aab2006-03-27 01:14:33 -0800268 printk("write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 "err = %d\n", -err);
270 goto out_close1;
271 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800272
273 p = setup_initial_poll(l_sigio_private[1]);
274 if(!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 goto out_close2;
276
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800277 sigio_lock();
278
279 /* Did we race? Don't try to optimize this, please, it's not so likely
280 * to happen, and no more than once at the boot. */
281 if(write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800282 goto out_free;
283
284 current_poll = ((struct pollfds) { .poll = p,
285 .used = 1,
286 .size = 1 });
287
288 if (write_sigio_irq(l_write_sigio_fds[0]))
289 goto out_clear_poll;
290
291 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
292 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800293
294 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 CLONE_FILES | CLONE_VM, &stack, 0);
296
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800297 if (write_sigio_pid < 0)
298 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 sigio_unlock();
301 return;
302
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800303out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800305 write_sigio_fds[0] = -1;
306 write_sigio_fds[1] = -1;
307 sigio_private[0] = -1;
308 sigio_private[1] = -1;
309out_clear_poll:
310 current_poll = ((struct pollfds) { .poll = NULL,
311 .size = 0,
312 .used = 0 });
313out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800314 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700315 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800316out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800317 close(l_sigio_private[0]);
318 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800319out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800320 close(l_write_sigio_fds[0]);
321 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Jeff Dikec65badb2007-05-06 14:51:06 -0700324/* Changed during early boot */
325static int pty_output_sigio = 0;
326static int pty_close_sigio = 0;
327
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700328void maybe_sigio_broken(int fd, int read)
329{
Jeff Dike19bdf042006-09-25 23:33:04 -0700330 int err;
331
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700332 if(!isatty(fd))
333 return;
334
335 if((read || pty_output_sigio) && (!read || pty_close_sigio))
336 return;
337
338 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700339
340 sigio_lock();
341 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
Jeff Dike04a51e62007-02-28 20:13:11 -0800342 if(err){
343 printk("maybe_sigio_broken - failed to add pollfd for "
344 "descriptor %d\n", fd);
Jeff Dike19bdf042006-09-25 23:33:04 -0700345 goto out;
Jeff Dike04a51e62007-02-28 20:13:11 -0800346 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800347
Jeff Dike19bdf042006-09-25 23:33:04 -0700348 all_sigio_fds.poll[all_sigio_fds.used++] =
349 ((struct pollfd) { .fd = fd,
350 .events = read ? POLLIN : POLLOUT,
351 .revents = 0 });
352out:
353 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700354}
355
Jeff Dike29ac1c22006-07-10 04:45:12 -0700356static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Jeff Dikef206aab2006-03-27 01:14:33 -0800358 if(write_sigio_pid != -1){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 os_kill_process(write_sigio_pid, 1);
360 write_sigio_pid = -1;
361 }
362}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700363
364__uml_exitcall(sigio_cleanup);
Jeff Dikec65badb2007-05-06 14:51:06 -0700365
366/* Used as a flag during SIGIO testing early in boot */
367static volatile int got_sigio = 0;
368
369static void __init handler(int sig)
370{
371 got_sigio = 1;
372}
373
374struct openpty_arg {
375 int master;
376 int slave;
377 int err;
378};
379
380static void openpty_cb(void *arg)
381{
382 struct openpty_arg *info = arg;
383
384 info->err = 0;
385 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
386 info->err = -errno;
387}
388
389static int async_pty(int master, int slave)
390{
391 int flags;
392
393 flags = fcntl(master, F_GETFL);
394 if(flags < 0)
395 return -errno;
396
397 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
398 (fcntl(master, F_SETOWN, os_getpid()) < 0))
399 return -errno;
400
401 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
402 return -errno;
403
404 return(0);
405}
406
407static void __init check_one_sigio(void (*proc)(int, int))
408{
409 struct sigaction old, new;
410 struct openpty_arg pty = { .master = -1, .slave = -1 };
411 int master, slave, err;
412
413 initial_thread_cb(openpty_cb, &pty);
414 if(pty.err){
415 printk("openpty failed, errno = %d\n", -pty.err);
416 return;
417 }
418
419 master = pty.master;
420 slave = pty.slave;
421
422 if((master == -1) || (slave == -1)){
423 printk("openpty failed to allocate a pty\n");
424 return;
425 }
426
427 /* Not now, but complain so we now where we failed. */
428 err = raw(master);
429 if (err < 0)
430 panic("check_sigio : __raw failed, errno = %d\n", -err);
431
432 err = async_pty(master, slave);
433 if(err < 0)
434 panic("tty_fds : sigio_async failed, err = %d\n", -err);
435
436 if(sigaction(SIGIO, NULL, &old) < 0)
437 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
438 new = old;
439 new.sa_handler = handler;
440 if(sigaction(SIGIO, &new, NULL) < 0)
441 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
442
443 got_sigio = 0;
444 (*proc)(master, slave);
445
446 close(master);
447 close(slave);
448
449 if(sigaction(SIGIO, &old, NULL) < 0)
450 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
451}
452
453static void tty_output(int master, int slave)
454{
455 int n;
456 char buf[512];
457
458 printk("Checking that host ptys support output SIGIO...");
459
460 memset(buf, 0, sizeof(buf));
461
462 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
463 if(errno != EAGAIN)
Jeff Dikeef0470c2007-05-06 14:51:33 -0700464 panic("tty_output : write failed, errno = %d\n", errno);
Jeff Dikec65badb2007-05-06 14:51:06 -0700465 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
466
467 if(got_sigio){
468 printk("Yes\n");
469 pty_output_sigio = 1;
470 }
Jeff Dikeef0470c2007-05-06 14:51:33 -0700471 else if(n == -EAGAIN)
472 printk("No, enabling workaround\n");
473 else panic("tty_output : read failed, err = %d\n", n);
Jeff Dikec65badb2007-05-06 14:51:06 -0700474}
475
476static void tty_close(int master, int slave)
477{
478 printk("Checking that host ptys support SIGIO on close...");
479
480 close(slave);
481 if(got_sigio){
482 printk("Yes\n");
483 pty_close_sigio = 1;
484 }
485 else printk("No, enabling workaround\n");
486}
487
488void __init check_sigio(void)
489{
490 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
491 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
492 printk("No pseudo-terminals available - skipping pty SIGIO "
493 "check\n");
494 return;
495 }
496 check_one_sigio(tty_output);
497 check_one_sigio(tty_close);
498}
499
500/* Here because it only does the SIGIO testing for now */
501void __init os_check_bugs(void)
502{
503 check_sigio();
504}