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;
29 static unsigned long write_sigio_stack
;
31 /* These arrays are initialized before the sigio thread is started, and
32 * the descriptors closed after it is killed. So, it can't see them change.
33 * On the UML side, they are changed under the sigio_lock.
35 #define SIGIO_FDS_INIT {-1, -1}
37 static int write_sigio_fds
[2] = SIGIO_FDS_INIT
;
38 static int sigio_private
[2] = SIGIO_FDS_INIT
;
46 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
47 * synchronizes with it.
49 static struct pollfds current_poll
;
50 static struct pollfds next_poll
;
51 static struct pollfds all_sigio_fds
;
53 static int write_sigio_thread(void *unused
)
55 struct pollfds
*fds
, tmp
;
60 signal(SIGWINCH
, SIG_IGN
);
63 n
= poll(fds
->poll
, fds
->used
, -1);
65 if(errno
== EINTR
) continue;
66 printk("write_sigio_thread : poll returned %d, "
67 "errno = %d\n", n
, errno
);
69 for(i
= 0; i
< fds
->used
; i
++){
71 if(p
->revents
== 0) continue;
72 if(p
->fd
== sigio_private
[1]){
73 CATCH_EINTR(n
= read(sigio_private
[1], &c
,
76 printk("write_sigio_thread : "
77 "read on socket failed, "
80 current_poll
= next_poll
;
82 respond_fd
= sigio_private
[1];
85 respond_fd
= write_sigio_fds
[1];
87 memmove(&fds
->poll
[i
], &fds
->poll
[i
+ 1],
88 (fds
->used
- i
) * sizeof(*fds
->poll
));
91 CATCH_EINTR(n
= write(respond_fd
, &c
, sizeof(c
)));
93 printk("write_sigio_thread : write on socket "
94 "failed, err = %d\n", errno
);
101 static int need_poll(struct pollfds
*polls
, int n
)
108 new = kmalloc(n
* sizeof(struct pollfd
), UM_GFP_ATOMIC
);
110 printk("need_poll : failed to allocate new pollfds\n");
114 memcpy(new, polls
->poll
, polls
->used
* sizeof(struct pollfd
));
122 /* Must be called with sigio_lock held, because it's needed by the marked
125 static void update_thread(void)
131 flags
= set_signals(0);
132 n
= write(sigio_private
[0], &c
, sizeof(c
));
134 printk("update_thread : write failed, err = %d\n", errno
);
138 CATCH_EINTR(n
= read(sigio_private
[0], &c
, sizeof(c
)));
140 printk("update_thread : read failed, err = %d\n", errno
);
147 /* Critical section start */
148 if (write_sigio_pid
!= -1) {
149 os_kill_process(write_sigio_pid
, 1);
150 free_stack(write_sigio_stack
, 0);
152 write_sigio_pid
= -1;
153 close(sigio_private
[0]);
154 close(sigio_private
[1]);
155 close(write_sigio_fds
[0]);
156 close(write_sigio_fds
[1]);
157 /* Critical section end */
161 int add_sigio_fd(int fd
)
167 for(i
= 0; i
< all_sigio_fds
.used
; i
++){
168 if(all_sigio_fds
.poll
[i
].fd
== fd
)
171 if(i
== all_sigio_fds
.used
)
174 p
= &all_sigio_fds
.poll
[i
];
176 for(i
= 0; i
< current_poll
.used
; i
++){
177 if(current_poll
.poll
[i
].fd
== fd
)
181 n
= current_poll
.used
;
182 err
= need_poll(&next_poll
, n
+ 1);
186 memcpy(next_poll
.poll
, current_poll
.poll
,
187 current_poll
.used
* sizeof(struct pollfd
));
188 next_poll
.poll
[n
] = *p
;
189 next_poll
.used
= n
+ 1;
196 int ignore_sigio_fd(int fd
)
199 int err
= 0, i
, n
= 0;
201 /* This is called from exitcalls elsewhere in UML - if
202 * sigio_cleanup has already run, then update_thread will hang
203 * or fail because the thread is no longer running.
205 if(write_sigio_pid
== -1)
209 for(i
= 0; i
< current_poll
.used
; i
++){
210 if(current_poll
.poll
[i
].fd
== fd
) break;
212 if(i
== current_poll
.used
)
215 err
= need_poll(&next_poll
, current_poll
.used
- 1);
219 for(i
= 0; i
< current_poll
.used
; i
++){
220 p
= ¤t_poll
.poll
[i
];
222 next_poll
.poll
[n
++] = *p
;
224 next_poll
.used
= current_poll
.used
- 1;
232 static struct pollfd
*setup_initial_poll(int fd
)
236 p
= kmalloc(sizeof(struct pollfd
), UM_GFP_KERNEL
);
238 printk("setup_initial_poll : failed to allocate poll\n");
241 *p
= ((struct pollfd
) { .fd
= fd
,
247 static void write_sigio_workaround(void)
251 int l_write_sigio_fds
[2];
252 int l_sigio_private
[2];
253 int l_write_sigio_pid
;
255 /* We call this *tons* of times - and most ones we must just fail. */
257 l_write_sigio_pid
= write_sigio_pid
;
260 if (l_write_sigio_pid
!= -1)
263 err
= os_pipe(l_write_sigio_fds
, 1, 1);
265 printk("write_sigio_workaround - os_pipe 1 failed, "
269 err
= os_pipe(l_sigio_private
, 1, 1);
271 printk("write_sigio_workaround - os_pipe 2 failed, "
276 p
= setup_initial_poll(l_sigio_private
[1]);
282 /* Did we race? Don't try to optimize this, please, it's not so likely
283 * to happen, and no more than once at the boot. */
284 if(write_sigio_pid
!= -1)
287 current_poll
= ((struct pollfds
) { .poll
= p
,
291 if (write_sigio_irq(l_write_sigio_fds
[0]))
294 memcpy(write_sigio_fds
, l_write_sigio_fds
, sizeof(l_write_sigio_fds
));
295 memcpy(sigio_private
, l_sigio_private
, sizeof(l_sigio_private
));
297 write_sigio_pid
= run_helper_thread(write_sigio_thread
, NULL
,
298 CLONE_FILES
| CLONE_VM
,
301 if (write_sigio_pid
< 0)
308 write_sigio_pid
= -1;
309 write_sigio_fds
[0] = -1;
310 write_sigio_fds
[1] = -1;
311 sigio_private
[0] = -1;
312 sigio_private
[1] = -1;
314 current_poll
= ((struct pollfds
) { .poll
= NULL
,
321 close(l_sigio_private
[0]);
322 close(l_sigio_private
[1]);
324 close(l_write_sigio_fds
[0]);
325 close(l_write_sigio_fds
[1]);
328 /* Changed during early boot */
329 static int pty_output_sigio
= 0;
330 static int pty_close_sigio
= 0;
332 void maybe_sigio_broken(int fd
, int read
)
339 if((read
|| pty_output_sigio
) && (!read
|| pty_close_sigio
))
342 write_sigio_workaround();
345 err
= need_poll(&all_sigio_fds
, all_sigio_fds
.used
+ 1);
347 printk("maybe_sigio_broken - failed to add pollfd for "
348 "descriptor %d\n", fd
);
352 all_sigio_fds
.poll
[all_sigio_fds
.used
++] =
353 ((struct pollfd
) { .fd
= fd
,
354 .events
= read
? POLLIN
: POLLOUT
,
360 static void sigio_cleanup(void)
362 if (write_sigio_pid
== -1)
365 os_kill_process(write_sigio_pid
, 1);
366 free_stack(write_sigio_stack
, 0);
367 write_sigio_pid
= -1;
370 __uml_exitcall(sigio_cleanup
);
372 /* Used as a flag during SIGIO testing early in boot */
373 static volatile int got_sigio
= 0;
375 static void __init
handler(int sig
)
386 static void openpty_cb(void *arg
)
388 struct openpty_arg
*info
= arg
;
391 if(openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
395 static int async_pty(int master
, int slave
)
399 flags
= fcntl(master
, F_GETFL
);
403 if((fcntl(master
, F_SETFL
, flags
| O_NONBLOCK
| O_ASYNC
) < 0) ||
404 (fcntl(master
, F_SETOWN
, os_getpid()) < 0))
407 if((fcntl(slave
, F_SETFL
, flags
| O_NONBLOCK
) < 0))
413 static void __init
check_one_sigio(void (*proc
)(int, int))
415 struct sigaction old
, new;
416 struct openpty_arg pty
= { .master
= -1, .slave
= -1 };
417 int master
, slave
, err
;
419 initial_thread_cb(openpty_cb
, &pty
);
421 printk("openpty failed, errno = %d\n", -pty
.err
);
428 if((master
== -1) || (slave
== -1)){
429 printk("openpty failed to allocate a pty\n");
433 /* Not now, but complain so we now where we failed. */
436 panic("check_sigio : __raw failed, errno = %d\n", -err
);
438 err
= async_pty(master
, slave
);
440 panic("tty_fds : sigio_async failed, err = %d\n", -err
);
442 if(sigaction(SIGIO
, NULL
, &old
) < 0)
443 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno
);
445 new.sa_handler
= handler
;
446 if(sigaction(SIGIO
, &new, NULL
) < 0)
447 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno
);
450 (*proc
)(master
, slave
);
455 if(sigaction(SIGIO
, &old
, NULL
) < 0)
456 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno
);
459 static void tty_output(int master
, int slave
)
464 printk("Checking that host ptys support output SIGIO...");
466 memset(buf
, 0, sizeof(buf
));
468 while(write(master
, buf
, sizeof(buf
)) > 0) ;
470 panic("tty_output : write failed, errno = %d\n", errno
);
471 while(((n
= read(slave
, buf
, sizeof(buf
))) > 0) && !got_sigio
) ;
475 pty_output_sigio
= 1;
477 else if(n
== -EAGAIN
)
478 printk("No, enabling workaround\n");
479 else panic("tty_output : read failed, err = %d\n", n
);
482 static void tty_close(int master
, int slave
)
484 printk("Checking that host ptys support SIGIO on close...");
491 else printk("No, enabling workaround\n");
494 void __init
check_sigio(void)
496 if((os_access("/dev/ptmx", OS_ACC_R_OK
) < 0) &&
497 (os_access("/dev/ptyp0", OS_ACC_R_OK
) < 0)){
498 printk("No pseudo-terminals available - skipping pty SIGIO "
502 check_one_sigio(tty_output
);
503 check_one_sigio(tty_close
);
506 /* Here because it only does the SIGIO testing for now */
507 void __init
os_check_bugs(void)