allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / um / os-Linux / sigio.c
blob8d4e0c6b8c92e085e697557f68e59b87af7afe23
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <termios.h>
9 #include <pty.h>
10 #include <signal.h>
11 #include <fcntl.h>
12 #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"
20 #include "sigio.h"
21 #include "os.h"
22 #include "um_malloc.h"
23 #include "init.h"
25 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
26 * exitcall.
28 static int write_sigio_pid = -1;
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.
34 #define SIGIO_FDS_INIT {-1, -1}
36 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
37 static int sigio_private[2] = SIGIO_FDS_INIT;
39 struct pollfds {
40 struct pollfd *poll;
41 int size;
42 int used;
45 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
46 * synchronizes with it.
48 static struct pollfds current_poll;
49 static struct pollfds next_poll;
50 static struct pollfds all_sigio_fds;
52 static int write_sigio_thread(void *unused)
54 struct pollfds *fds, tmp;
55 struct pollfd *p;
56 int i, n, respond_fd;
57 char c;
59 signal(SIGWINCH, SIG_IGN);
60 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);
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 CATCH_EINTR(n = read(sigio_private[1], &c,
73 sizeof(c)));
74 if(n != sizeof(c))
75 printk("write_sigio_thread : "
76 "read on socket failed, "
77 "err = %d\n", errno);
78 tmp = current_poll;
79 current_poll = next_poll;
80 next_poll = tmp;
81 respond_fd = sigio_private[1];
83 else {
84 respond_fd = write_sigio_fds[1];
85 fds->used--;
86 memmove(&fds->poll[i], &fds->poll[i + 1],
87 (fds->used - i) * sizeof(*fds->poll));
90 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
91 if(n != sizeof(c))
92 printk("write_sigio_thread : write on socket "
93 "failed, err = %d\n", errno);
97 return 0;
100 static int need_poll(struct pollfds *polls, int n)
102 struct pollfd *new;
104 if(n <= polls->size)
105 return 0;
107 new = um_kmalloc_atomic(n * sizeof(struct pollfd));
108 if(new == NULL){
109 printk("need_poll : failed to allocate new pollfds\n");
110 return -ENOMEM;
113 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
114 kfree(polls->poll);
116 polls->poll = new;
117 polls->size = n;
118 return 0;
121 /* Must be called with sigio_lock held, because it's needed by the marked
122 * critical section.
124 static void update_thread(void)
126 unsigned long flags;
127 int n;
128 char c;
130 flags = set_signals(0);
131 n = write(sigio_private[0], &c, sizeof(c));
132 if(n != sizeof(c)){
133 printk("update_thread : write failed, err = %d\n", errno);
134 goto fail;
137 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
138 if(n != sizeof(c)){
139 printk("update_thread : read failed, err = %d\n", errno);
140 goto fail;
143 set_signals(flags);
144 return;
145 fail:
146 /* Critical section start */
147 if(write_sigio_pid != -1)
148 os_kill_process(write_sigio_pid, 1);
149 write_sigio_pid = -1;
150 close(sigio_private[0]);
151 close(sigio_private[1]);
152 close(write_sigio_fds[0]);
153 close(write_sigio_fds[1]);
154 /* Critical section end */
155 set_signals(flags);
158 int add_sigio_fd(int fd)
160 struct pollfd *p;
161 int err = 0, i, n;
163 sigio_lock();
164 for(i = 0; i < all_sigio_fds.used; i++){
165 if(all_sigio_fds.poll[i].fd == fd)
166 break;
168 if(i == all_sigio_fds.used)
169 goto out;
171 p = &all_sigio_fds.poll[i];
173 for(i = 0; i < current_poll.used; i++){
174 if(current_poll.poll[i].fd == fd)
175 goto out;
178 n = current_poll.used;
179 err = need_poll(&next_poll, n + 1);
180 if(err)
181 goto out;
183 memcpy(next_poll.poll, current_poll.poll,
184 current_poll.used * sizeof(struct pollfd));
185 next_poll.poll[n] = *p;
186 next_poll.used = n + 1;
187 update_thread();
188 out:
189 sigio_unlock();
190 return err;
193 int ignore_sigio_fd(int fd)
195 struct pollfd *p;
196 int err = 0, i, n = 0;
198 /* This is called from exitcalls elsewhere in UML - if
199 * sigio_cleanup has already run, then update_thread will hang
200 * or fail because the thread is no longer running.
202 if(write_sigio_pid == -1)
203 return -EIO;
205 sigio_lock();
206 for(i = 0; i < current_poll.used; i++){
207 if(current_poll.poll[i].fd == fd) break;
209 if(i == current_poll.used)
210 goto out;
212 err = need_poll(&next_poll, current_poll.used - 1);
213 if(err)
214 goto out;
216 for(i = 0; i < current_poll.used; i++){
217 p = &current_poll.poll[i];
218 if(p->fd != fd)
219 next_poll.poll[n++] = *p;
221 next_poll.used = current_poll.used - 1;
223 update_thread();
224 out:
225 sigio_unlock();
226 return err;
229 static struct pollfd *setup_initial_poll(int fd)
231 struct pollfd *p;
233 p = um_kmalloc(sizeof(struct pollfd));
234 if (p == NULL) {
235 printk("setup_initial_poll : failed to allocate poll\n");
236 return NULL;
238 *p = ((struct pollfd) { .fd = fd,
239 .events = POLLIN,
240 .revents = 0 });
241 return p;
244 static void write_sigio_workaround(void)
246 unsigned long stack;
247 struct pollfd *p;
248 int err;
249 int l_write_sigio_fds[2];
250 int l_sigio_private[2];
251 int l_write_sigio_pid;
253 /* We call this *tons* of times - and most ones we must just fail. */
254 sigio_lock();
255 l_write_sigio_pid = write_sigio_pid;
256 sigio_unlock();
258 if (l_write_sigio_pid != -1)
259 return;
261 err = os_pipe(l_write_sigio_fds, 1, 1);
262 if(err < 0){
263 printk("write_sigio_workaround - os_pipe 1 failed, "
264 "err = %d\n", -err);
265 return;
267 err = os_pipe(l_sigio_private, 1, 1);
268 if(err < 0){
269 printk("write_sigio_workaround - os_pipe 2 failed, "
270 "err = %d\n", -err);
271 goto out_close1;
274 p = setup_initial_poll(l_sigio_private[1]);
275 if(!p)
276 goto out_close2;
278 sigio_lock();
280 /* Did we race? Don't try to optimize this, please, it's not so likely
281 * to happen, and no more than once at the boot. */
282 if(write_sigio_pid != -1)
283 goto out_free;
285 current_poll = ((struct pollfds) { .poll = p,
286 .used = 1,
287 .size = 1 });
289 if (write_sigio_irq(l_write_sigio_fds[0]))
290 goto out_clear_poll;
292 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
293 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
295 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
296 CLONE_FILES | CLONE_VM, &stack, 0);
298 if (write_sigio_pid < 0)
299 goto out_clear;
301 sigio_unlock();
302 return;
304 out_clear:
305 write_sigio_pid = -1;
306 write_sigio_fds[0] = -1;
307 write_sigio_fds[1] = -1;
308 sigio_private[0] = -1;
309 sigio_private[1] = -1;
310 out_clear_poll:
311 current_poll = ((struct pollfds) { .poll = NULL,
312 .size = 0,
313 .used = 0 });
314 out_free:
315 sigio_unlock();
316 kfree(p);
317 out_close2:
318 close(l_sigio_private[0]);
319 close(l_sigio_private[1]);
320 out_close1:
321 close(l_write_sigio_fds[0]);
322 close(l_write_sigio_fds[1]);
325 /* Changed during early boot */
326 static int pty_output_sigio = 0;
327 static int pty_close_sigio = 0;
329 void maybe_sigio_broken(int fd, int read)
331 int err;
333 if(!isatty(fd))
334 return;
336 if((read || pty_output_sigio) && (!read || pty_close_sigio))
337 return;
339 write_sigio_workaround();
341 sigio_lock();
342 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
343 if(err){
344 printk("maybe_sigio_broken - failed to add pollfd for "
345 "descriptor %d\n", fd);
346 goto out;
349 all_sigio_fds.poll[all_sigio_fds.used++] =
350 ((struct pollfd) { .fd = fd,
351 .events = read ? POLLIN : POLLOUT,
352 .revents = 0 });
353 out:
354 sigio_unlock();
357 static void sigio_cleanup(void)
359 if(write_sigio_pid != -1){
360 os_kill_process(write_sigio_pid, 1);
361 write_sigio_pid = -1;
365 __uml_exitcall(sigio_cleanup);
367 /* Used as a flag during SIGIO testing early in boot */
368 static volatile int got_sigio = 0;
370 static void __init handler(int sig)
372 got_sigio = 1;
375 struct openpty_arg {
376 int master;
377 int slave;
378 int err;
381 static void openpty_cb(void *arg)
383 struct openpty_arg *info = arg;
385 info->err = 0;
386 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
387 info->err = -errno;
390 static int async_pty(int master, int slave)
392 int flags;
394 flags = fcntl(master, F_GETFL);
395 if(flags < 0)
396 return -errno;
398 if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
399 (fcntl(master, F_SETOWN, os_getpid()) < 0))
400 return -errno;
402 if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
403 return -errno;
405 return(0);
408 static void __init check_one_sigio(void (*proc)(int, int))
410 struct sigaction old, new;
411 struct openpty_arg pty = { .master = -1, .slave = -1 };
412 int master, slave, err;
414 initial_thread_cb(openpty_cb, &pty);
415 if(pty.err){
416 printk("openpty failed, errno = %d\n", -pty.err);
417 return;
420 master = pty.master;
421 slave = pty.slave;
423 if((master == -1) || (slave == -1)){
424 printk("openpty failed to allocate a pty\n");
425 return;
428 /* Not now, but complain so we now where we failed. */
429 err = raw(master);
430 if (err < 0)
431 panic("check_sigio : __raw failed, errno = %d\n", -err);
433 err = async_pty(master, slave);
434 if(err < 0)
435 panic("tty_fds : sigio_async failed, err = %d\n", -err);
437 if(sigaction(SIGIO, NULL, &old) < 0)
438 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
439 new = old;
440 new.sa_handler = handler;
441 if(sigaction(SIGIO, &new, NULL) < 0)
442 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
444 got_sigio = 0;
445 (*proc)(master, slave);
447 close(master);
448 close(slave);
450 if(sigaction(SIGIO, &old, NULL) < 0)
451 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
454 static void tty_output(int master, int slave)
456 int n;
457 char buf[512];
459 printk("Checking that host ptys support output SIGIO...");
461 memset(buf, 0, sizeof(buf));
463 while(write(master, buf, sizeof(buf)) > 0) ;
464 if(errno != EAGAIN)
465 panic("tty_output : write failed, errno = %d\n", errno);
466 while(((n = read(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
468 if(got_sigio){
469 printk("Yes\n");
470 pty_output_sigio = 1;
472 else if(n == -EAGAIN)
473 printk("No, enabling workaround\n");
474 else panic("tty_output : read failed, err = %d\n", n);
477 static void tty_close(int master, int slave)
479 printk("Checking that host ptys support SIGIO on close...");
481 close(slave);
482 if(got_sigio){
483 printk("Yes\n");
484 pty_close_sigio = 1;
486 else printk("No, enabling workaround\n");
489 void __init check_sigio(void)
491 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
492 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
493 printk("No pseudo-terminals available - skipping pty SIGIO "
494 "check\n");
495 return;
497 check_one_sigio(tty_output);
498 check_one_sigio(tty_close);
501 /* Here because it only does the SIGIO testing for now */
502 void __init os_check_bugs(void)
504 check_sigio();