2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
15 #include <sys/socket.h>
19 #include "kern_util.h"
22 #include "um_malloc.h"
25 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
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
;
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
;
59 signal(SIGWINCH
, SIG_IGN
);
62 n
= poll(fds
->poll
, fds
->used
, -1);
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
++){
70 if(p
->revents
== 0) continue;
71 if(p
->fd
== sigio_private
[1]){
72 n
= os_read_file(sigio_private
[1], &c
, sizeof(c
));
74 printk("write_sigio_thread : "
75 "read on socket failed, "
78 current_poll
= next_poll
;
80 respond_fd
= sigio_private
[1];
83 respond_fd
= write_sigio_fds
[1];
85 memmove(&fds
->poll
[i
], &fds
->poll
[i
+ 1],
86 (fds
->used
- i
) * sizeof(*fds
->poll
));
89 n
= os_write_file(respond_fd
, &c
, sizeof(c
));
91 printk("write_sigio_thread : write on socket "
92 "failed, err = %d\n", -n
);
99 static int need_poll(struct pollfds
*polls
, int n
)
106 new = um_kmalloc_atomic(n
* sizeof(struct pollfd
));
108 printk("need_poll : failed to allocate new pollfds\n");
112 memcpy(new, polls
->poll
, polls
->used
* sizeof(struct pollfd
));
120 /* Must be called with sigio_lock held, because it's needed by the marked
123 static void update_thread(void)
129 flags
= set_signals(0);
130 n
= os_write_file(sigio_private
[0], &c
, sizeof(c
));
132 printk("update_thread : write failed, err = %d\n", -n
);
136 n
= os_read_file(sigio_private
[0], &c
, sizeof(c
));
138 printk("update_thread : read failed, err = %d\n", -n
);
145 /* Critical section start */
146 if(write_sigio_pid
!= -1)
147 os_kill_process(write_sigio_pid
, 1);
148 write_sigio_pid
= -1;
149 close(sigio_private
[0]);
150 close(sigio_private
[1]);
151 close(write_sigio_fds
[0]);
152 close(write_sigio_fds
[1]);
153 /* Critical section end */
157 int add_sigio_fd(int fd
)
163 for(i
= 0; i
< all_sigio_fds
.used
; i
++){
164 if(all_sigio_fds
.poll
[i
].fd
== fd
)
167 if(i
== all_sigio_fds
.used
)
170 p
= &all_sigio_fds
.poll
[i
];
172 for(i
= 0; i
< current_poll
.used
; i
++){
173 if(current_poll
.poll
[i
].fd
== fd
)
177 n
= current_poll
.used
;
178 err
= need_poll(&next_poll
, n
+ 1);
182 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;
192 int ignore_sigio_fd(int fd
)
195 int err
= 0, i
, n
= 0;
197 /* 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.
201 if(write_sigio_pid
== -1)
205 for(i
= 0; i
< current_poll
.used
; i
++){
206 if(current_poll
.poll
[i
].fd
== fd
) break;
208 if(i
== current_poll
.used
)
211 err
= need_poll(&next_poll
, current_poll
.used
- 1);
215 for(i
= 0; i
< current_poll
.used
; i
++){
216 p
= ¤t_poll
.poll
[i
];
218 next_poll
.poll
[n
++] = *p
;
220 next_poll
.used
= current_poll
.used
- 1;
228 static struct pollfd
*setup_initial_poll(int fd
)
232 p
= um_kmalloc(sizeof(struct pollfd
));
234 printk("setup_initial_poll : failed to allocate poll\n");
237 *p
= ((struct pollfd
) { .fd
= fd
,
243 static void write_sigio_workaround(void)
248 int l_write_sigio_fds
[2];
249 int l_sigio_private
[2];
250 int l_write_sigio_pid
;
252 /* We call this *tons* of times - and most ones we must just fail. */
254 l_write_sigio_pid
= write_sigio_pid
;
257 if (l_write_sigio_pid
!= -1)
260 err
= os_pipe(l_write_sigio_fds
, 1, 1);
262 printk("write_sigio_workaround - os_pipe 1 failed, "
266 err
= os_pipe(l_sigio_private
, 1, 1);
268 printk("write_sigio_workaround - os_pipe 2 failed, "
273 p
= setup_initial_poll(l_sigio_private
[1]);
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)
284 current_poll
= ((struct pollfds
) { .poll
= p
,
288 if (write_sigio_irq(l_write_sigio_fds
[0]))
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
));
294 write_sigio_pid
= run_helper_thread(write_sigio_thread
, NULL
,
295 CLONE_FILES
| CLONE_VM
, &stack
, 0);
297 if (write_sigio_pid
< 0)
304 write_sigio_pid
= -1;
305 write_sigio_fds
[0] = -1;
306 write_sigio_fds
[1] = -1;
307 sigio_private
[0] = -1;
308 sigio_private
[1] = -1;
310 current_poll
= ((struct pollfds
) { .poll
= NULL
,
317 close(l_sigio_private
[0]);
318 close(l_sigio_private
[1]);
320 close(l_write_sigio_fds
[0]);
321 close(l_write_sigio_fds
[1]);
324 /* Changed during early boot */
325 static int pty_output_sigio
= 0;
326 static int pty_close_sigio
= 0;
328 void maybe_sigio_broken(int fd
, int read
)
335 if((read
|| pty_output_sigio
) && (!read
|| pty_close_sigio
))
338 write_sigio_workaround();
341 err
= need_poll(&all_sigio_fds
, all_sigio_fds
.used
+ 1);
343 printk("maybe_sigio_broken - failed to add pollfd for "
344 "descriptor %d\n", fd
);
348 all_sigio_fds
.poll
[all_sigio_fds
.used
++] =
349 ((struct pollfd
) { .fd
= fd
,
350 .events
= read
? POLLIN
: POLLOUT
,
356 static void sigio_cleanup(void)
358 if(write_sigio_pid
!= -1){
359 os_kill_process(write_sigio_pid
, 1);
360 write_sigio_pid
= -1;
364 __uml_exitcall(sigio_cleanup
);
366 /* Used as a flag during SIGIO testing early in boot */
367 static volatile int got_sigio
= 0;
369 static void __init
handler(int sig
)
380 static void openpty_cb(void *arg
)
382 struct openpty_arg
*info
= arg
;
385 if(openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
389 static int async_pty(int master
, int slave
)
393 flags
= fcntl(master
, F_GETFL
);
397 if((fcntl(master
, F_SETFL
, flags
| O_NONBLOCK
| O_ASYNC
) < 0) ||
398 (fcntl(master
, F_SETOWN
, os_getpid()) < 0))
401 if((fcntl(slave
, F_SETFL
, flags
| O_NONBLOCK
) < 0))
407 static void __init
check_one_sigio(void (*proc
)(int, int))
409 struct sigaction old
, new;
410 struct openpty_arg pty
= { .master
= -1, .slave
= -1 };
411 int master
, slave
, err
;
413 initial_thread_cb(openpty_cb
, &pty
);
415 printk("openpty failed, errno = %d\n", -pty
.err
);
422 if((master
== -1) || (slave
== -1)){
423 printk("openpty failed to allocate a pty\n");
427 /* Not now, but complain so we now where we failed. */
430 panic("check_sigio : __raw failed, errno = %d\n", -err
);
432 err
= async_pty(master
, slave
);
434 panic("tty_fds : sigio_async failed, err = %d\n", -err
);
436 if(sigaction(SIGIO
, NULL
, &old
) < 0)
437 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno
);
439 new.sa_handler
= handler
;
440 if(sigaction(SIGIO
, &new, NULL
) < 0)
441 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno
);
444 (*proc
)(master
, slave
);
449 if(sigaction(SIGIO
, &old
, NULL
) < 0)
450 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno
);
453 static void tty_output(int master
, int slave
)
458 printk("Checking that host ptys support output SIGIO...");
460 memset(buf
, 0, sizeof(buf
));
462 while(os_write_file(master
, buf
, sizeof(buf
)) > 0) ;
464 panic("check_sigio : write failed, errno = %d\n", errno
);
465 while(((n
= os_read_file(slave
, buf
, sizeof(buf
))) > 0) && !got_sigio
) ;
469 pty_output_sigio
= 1;
471 else if(n
== -EAGAIN
) printk("No, enabling workaround\n");
472 else panic("check_sigio : read failed, err = %d\n", n
);
475 static void tty_close(int master
, int slave
)
477 printk("Checking that host ptys support SIGIO on close...");
484 else printk("No, enabling workaround\n");
487 void __init
check_sigio(void)
489 if((os_access("/dev/ptmx", OS_ACC_R_OK
) < 0) &&
490 (os_access("/dev/ptyp0", OS_ACC_R_OK
) < 0)){
491 printk("No pseudo-terminals available - skipping pty SIGIO "
495 check_one_sigio(tty_output
);
496 check_one_sigio(tty_close
);
499 /* Here because it only does the SIGIO testing for now */
500 void __init
os_check_bugs(void)
This page took 0.043213 seconds and 6 git commands to generate.