blob: f7b18e157d3509d9b4302b1dc8dfd39c02756630 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 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
23/* Changed during early boot */
24int pty_output_sigio = 0;
25int pty_close_sigio = 0;
26
27/* Used as a flag during SIGIO testing early in boot */
28static volatile int got_sigio = 0;
29
30void __init handler(int sig)
31{
32 got_sigio = 1;
33}
34
35struct openpty_arg {
36 int master;
37 int slave;
38 int err;
39};
40
41static void openpty_cb(void *arg)
42{
43 struct openpty_arg *info = arg;
44
45 info->err = 0;
46 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
47 info->err = -errno;
48}
49
50void __init check_one_sigio(void (*proc)(int, int))
51{
52 struct sigaction old, new;
53 struct openpty_arg pty = { .master = -1, .slave = -1 };
54 int master, slave, err;
55
56 initial_thread_cb(openpty_cb, &pty);
57 if(pty.err){
58 printk("openpty failed, errno = %d\n", -pty.err);
59 return;
60 }
61
62 master = pty.master;
63 slave = pty.slave;
64
65 if((master == -1) || (slave == -1)){
66 printk("openpty failed to allocate a pty\n");
67 return;
68 }
69
70 /* Not now, but complain so we now where we failed. */
71 err = raw(master);
72 if (err < 0)
73 panic("check_sigio : __raw failed, errno = %d\n", -err);
74
75 err = os_sigio_async(master, slave);
76 if(err < 0)
77 panic("tty_fds : sigio_async failed, err = %d\n", -err);
78
79 if(sigaction(SIGIO, NULL, &old) < 0)
80 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
81 new = old;
82 new.sa_handler = handler;
83 if(sigaction(SIGIO, &new, NULL) < 0)
84 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
85
86 got_sigio = 0;
87 (*proc)(master, slave);
88
89 os_close_file(master);
90 os_close_file(slave);
91
92 if(sigaction(SIGIO, &old, NULL) < 0)
93 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
94}
95
96static void tty_output(int master, int slave)
97{
98 int n;
99 char buf[512];
100
101 printk("Checking that host ptys support output SIGIO...");
102
103 memset(buf, 0, sizeof(buf));
104
105 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
106 if(errno != EAGAIN)
107 panic("check_sigio : write failed, errno = %d\n", errno);
108 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
109
110 if (got_sigio) {
111 printk("Yes\n");
112 pty_output_sigio = 1;
113 } else if (n == -EAGAIN) {
114 printk("No, enabling workaround\n");
115 } else {
116 panic("check_sigio : read failed, err = %d\n", n);
117 }
118}
119
120static void tty_close(int master, int slave)
121{
122 printk("Checking that host ptys support SIGIO on close...");
123
124 os_close_file(slave);
125 if(got_sigio){
126 printk("Yes\n");
127 pty_close_sigio = 1;
128 }
129 else printk("No, enabling workaround\n");
130}
131
132void __init check_sigio(void)
133{
134 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
135 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
136 printk("No pseudo-terminals available - skipping pty SIGIO "
137 "check\n");
138 return;
139 }
140 check_one_sigio(tty_output);
141 check_one_sigio(tty_close);
142}
143
144/* Protected by sigio_lock(), also used by sigio_cleanup, which is an
145 * exitcall.
146 */
147static int write_sigio_pid = -1;
148
149/* These arrays are initialized before the sigio thread is started, and
150 * the descriptors closed after it is killed. So, it can't see them change.
151 * On the UML side, they are changed under the sigio_lock.
152 */
153static int write_sigio_fds[2] = { -1, -1 };
154static int sigio_private[2] = { -1, -1 };
155
156struct pollfds {
157 struct pollfd *poll;
158 int size;
159 int used;
160};
161
162/* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
163 * synchronizes with it.
164 */
165struct pollfds current_poll = {
166 .poll = NULL,
167 .size = 0,
168 .used = 0
169};
170
171struct pollfds next_poll = {
172 .poll = NULL,
173 .size = 0,
174 .used = 0
175};
176
177static int write_sigio_thread(void *unused)
178{
179 struct pollfds *fds, tmp;
180 struct pollfd *p;
181 int i, n, respond_fd;
182 char c;
183
Jeff Dikecd2ee4a2005-05-05 16:15:32 -0700184 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 fds = &current_poll;
186 while(1){
187 n = poll(fds->poll, fds->used, -1);
188 if(n < 0){
189 if(errno == EINTR) continue;
190 printk("write_sigio_thread : poll returned %d, "
191 "errno = %d\n", n, errno);
192 }
193 for(i = 0; i < fds->used; i++){
194 p = &fds->poll[i];
195 if(p->revents == 0) continue;
196 if(p->fd == sigio_private[1]){
197 n = os_read_file(sigio_private[1], &c, sizeof(c));
198 if(n != sizeof(c))
199 printk("write_sigio_thread : "
200 "read failed, err = %d\n", -n);
201 tmp = current_poll;
202 current_poll = next_poll;
203 next_poll = tmp;
204 respond_fd = sigio_private[1];
205 }
206 else {
207 respond_fd = write_sigio_fds[1];
208 fds->used--;
209 memmove(&fds->poll[i], &fds->poll[i + 1],
210 (fds->used - i) * sizeof(*fds->poll));
211 }
212
213 n = os_write_file(respond_fd, &c, sizeof(c));
214 if(n != sizeof(c))
215 printk("write_sigio_thread : write failed, "
216 "err = %d\n", -n);
217 }
218 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -0800219
220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static int need_poll(int n)
224{
225 if(n <= next_poll.size){
226 next_poll.used = n;
227 return(0);
228 }
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800229 kfree(next_poll.poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
231 if(next_poll.poll == NULL){
232 printk("need_poll : failed to allocate new pollfds\n");
233 next_poll.size = 0;
234 next_poll.used = 0;
235 return(-1);
236 }
237 next_poll.size = n;
238 next_poll.used = n;
239 return(0);
240}
241
242/* Must be called with sigio_lock held, because it's needed by the marked
243 * critical section. */
244static void update_thread(void)
245{
246 unsigned long flags;
247 int n;
248 char c;
249
250 flags = set_signals(0);
251 n = os_write_file(sigio_private[0], &c, sizeof(c));
252 if(n != sizeof(c)){
253 printk("update_thread : write failed, err = %d\n", -n);
254 goto fail;
255 }
256
257 n = os_read_file(sigio_private[0], &c, sizeof(c));
258 if(n != sizeof(c)){
259 printk("update_thread : read failed, err = %d\n", -n);
260 goto fail;
261 }
262
263 set_signals(flags);
264 return;
265 fail:
266 /* Critical section start */
267 if(write_sigio_pid != -1)
268 os_kill_process(write_sigio_pid, 1);
269 write_sigio_pid = -1;
270 os_close_file(sigio_private[0]);
271 os_close_file(sigio_private[1]);
272 os_close_file(write_sigio_fds[0]);
273 os_close_file(write_sigio_fds[1]);
274 /* Critical section end */
275 set_signals(flags);
276}
277
278int add_sigio_fd(int fd, int read)
279{
280 int err = 0, i, n, events;
281
282 sigio_lock();
283 for(i = 0; i < current_poll.used; i++){
284 if(current_poll.poll[i].fd == fd)
285 goto out;
286 }
287
288 n = current_poll.used + 1;
289 err = need_poll(n);
290 if(err)
291 goto out;
292
293 for(i = 0; i < current_poll.used; i++)
294 next_poll.poll[i] = current_poll.poll[i];
295
296 if(read) events = POLLIN;
297 else events = POLLOUT;
298
299 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
300 .events = events,
301 .revents = 0 });
302 update_thread();
303 out:
304 sigio_unlock();
305 return(err);
306}
307
308int ignore_sigio_fd(int fd)
309{
310 struct pollfd *p;
311 int err = 0, i, n = 0;
312
313 sigio_lock();
314 for(i = 0; i < current_poll.used; i++){
315 if(current_poll.poll[i].fd == fd) break;
316 }
317 if(i == current_poll.used)
318 goto out;
319
320 err = need_poll(current_poll.used - 1);
321 if(err)
322 goto out;
323
324 for(i = 0; i < current_poll.used; i++){
325 p = &current_poll.poll[i];
326 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
327 }
328 if(n == i){
329 printk("ignore_sigio_fd : fd %d not found\n", fd);
330 err = -1;
331 goto out;
332 }
333
334 update_thread();
335 out:
336 sigio_unlock();
337 return(err);
338}
339
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800340static struct pollfd* setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct pollfd *p;
343
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800344 p = um_kmalloc(sizeof(struct pollfd));
345 if (p == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 printk("setup_initial_poll : failed to allocate poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800347 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 *p = ((struct pollfd) { .fd = fd,
350 .events = POLLIN,
351 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800352 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355void write_sigio_workaround(void)
356{
357 unsigned long stack;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800358 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800360 int l_write_sigio_fds[2];
361 int l_sigio_private[2];
362 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800364 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800366 l_write_sigio_pid = write_sigio_pid;
367 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800369 if (l_write_sigio_pid != -1)
370 return;
371
372 err = os_pipe(l_write_sigio_fds, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if(err < 0){
374 printk("write_sigio_workaround - os_pipe 1 failed, "
375 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800376 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800378 err = os_pipe(l_sigio_private, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if(err < 0){
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800380 printk("write_sigio_workaround - os_pipe 1 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 "err = %d\n", -err);
382 goto out_close1;
383 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800384
385 p = setup_initial_poll(l_sigio_private[1]);
386 if(!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 goto out_close2;
388
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800389 sigio_lock();
390
391 /* Did we race? Don't try to optimize this, please, it's not so likely
392 * to happen, and no more than once at the boot. */
393 if(write_sigio_pid != -1)
394 goto out_unlock;
395
396 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 CLONE_FILES | CLONE_VM, &stack, 0);
398
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800399 if (write_sigio_pid < 0)
400 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800402 if (write_sigio_irq(l_write_sigio_fds[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto out_kill;
404
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800405 /* Success, finally. */
406 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
407 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
408
409 current_poll = ((struct pollfds) { .poll = p,
410 .used = 1,
411 .size = 1 });
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sigio_unlock();
414 return;
415
416 out_kill:
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800417 l_write_sigio_pid = write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 write_sigio_pid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 sigio_unlock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800420 /* Going to call waitpid, avoid holding the lock. */
421 os_kill_process(l_write_sigio_pid, 1);
422 goto out_free;
423
424 out_clear:
425 write_sigio_pid = -1;
426 out_unlock:
427 sigio_unlock();
428 out_free:
429 kfree(p);
430 out_close2:
431 os_close_file(l_sigio_private[0]);
432 os_close_file(l_sigio_private[1]);
433 out_close1:
434 os_close_file(l_write_sigio_fds[0]);
435 os_close_file(l_write_sigio_fds[1]);
436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439int read_sigio_fd(int fd)
440{
441 int n;
442 char c;
443
444 n = os_read_file(fd, &c, sizeof(c));
445 if(n != sizeof(c)){
446 if(n < 0) {
447 printk("read_sigio_fd - read failed, err = %d\n", -n);
448 return(n);
449 }
450 else {
451 printk("read_sigio_fd - short read, bytes = %d\n", n);
452 return(-EIO);
453 }
454 }
455 return(n);
456}
457
458static void sigio_cleanup(void)
459{
460 if (write_sigio_pid != -1) {
461 os_kill_process(write_sigio_pid, 1);
462 write_sigio_pid = -1;
463 }
464}
465
466__uml_exitcall(sigio_cleanup);