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 CATCH_EINTR(n
= read(sigio_private
[1], &c
,
75 printk("write_sigio_thread : "
76 "read on socket failed, "
79 current_poll
= next_poll
;
81 respond_fd
= sigio_private
[1];
84 respond_fd
= write_sigio_fds
[1];
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
)));
92 printk("write_sigio_thread : write on socket "
93 "failed, err = %d\n", errno
);
100 static int need_poll(struct pollfds
*polls
, int n
)
107 new = um_kmalloc_atomic(n
* sizeof(struct pollfd
));
109 printk("need_poll : failed to allocate new pollfds\n");
113 memcpy(new, polls
->poll
, polls
->used
* sizeof(struct pollfd
));
121 /* Must be called with sigio_lock held, because it's needed by the marked
124 static void update_thread(void)
130 flags
= set_signals(0);
131 n
= write(sigio_private
[0], &c
, sizeof(c
));
133 printk("update_thread : write failed, err = %d\n", errno
);
137 CATCH_EINTR(n
= read(sigio_private
[0], &c
, sizeof(c
)));
139 printk("update_thread : read failed, err = %d\n", errno
);
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 */
158 int add_sigio_fd(int fd
)
164 for(i
= 0; i
< all_sigio_fds
.used
; i
++){
165 if(all_sigio_fds
.poll
[i
].fd
== fd
)
168 if(i
== all_sigio_fds
.used
)
171 p
= &all_sigio_fds
.poll
[i
];
173 for(i
= 0; i
< current_poll
.used
; i
++){
174 if(current_poll
.poll
[i
].fd
== fd
)
178 n
= current_poll
.used
;
179 err
= need_poll(&next_poll
, n
+ 1);
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;
193 int ignore_sigio_fd(int fd
)
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)
206 for(i
= 0; i
< current_poll
.used
; i
++){
207 if(current_poll
.poll
[i
].fd
== fd
) break;
209 if(i
== current_poll
.used
)
212 err
= need_poll(&next_poll
, current_poll
.used
- 1);
216 for(i
= 0; i
< current_poll
.used
; i
++){
217 p
= ¤t_poll
.poll
[i
];
219 next_poll
.poll
[n
++] = *p
;
221 next_poll
.used
= current_poll
.used
- 1;
229 static struct pollfd
*setup_initial_poll(int fd
)
233 p
= um_kmalloc(sizeof(struct pollfd
));
235 printk("setup_initial_poll : failed to allocate poll\n");
238 *p
= ((struct pollfd
) { .fd
= fd
,
244 static void write_sigio_workaround(void)
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. */
255 l_write_sigio_pid
= write_sigio_pid
;
258 if (l_write_sigio_pid
!= -1)
261 err
= os_pipe(l_write_sigio_fds
, 1, 1);
263 printk("write_sigio_workaround - os_pipe 1 failed, "
267 err
= os_pipe(l_sigio_private
, 1, 1);
269 printk("write_sigio_workaround - os_pipe 2 failed, "
274 p
= setup_initial_poll(l_sigio_private
[1]);
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)
285 current_poll
= ((struct pollfds
) { .poll
= p
,
289 if (write_sigio_irq(l_write_sigio_fds
[0]))
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)
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;
311 current_poll
= ((struct pollfds
) { .poll
= NULL
,
318 close(l_sigio_private
[0]);
319 close(l_sigio_private
[1]);
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
)
336 if((read
|| pty_output_sigio
) && (!read
|| pty_close_sigio
))
339 write_sigio_workaround();
342 err
= need_poll(&all_sigio_fds
, all_sigio_fds
.used
+ 1);
344 printk("maybe_sigio_broken - failed to add pollfd for "
345 "descriptor %d\n", fd
);
349 all_sigio_fds
.poll
[all_sigio_fds
.used
++] =
350 ((struct pollfd
) { .fd
= fd
,
351 .events
= read
? POLLIN
: POLLOUT
,
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
)
381 static void openpty_cb(void *arg
)
383 struct openpty_arg
*info
= arg
;
386 if(openpty(&info
->master
, &info
->slave
, NULL
, NULL
, NULL
))
390 static int async_pty(int master
, int slave
)
394 flags
= fcntl(master
, F_GETFL
);
398 if((fcntl(master
, F_SETFL
, flags
| O_NONBLOCK
| O_ASYNC
) < 0) ||
399 (fcntl(master
, F_SETOWN
, os_getpid()) < 0))
402 if((fcntl(slave
, F_SETFL
, flags
| O_NONBLOCK
) < 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
);
416 printk("openpty failed, errno = %d\n", -pty
.err
);
423 if((master
== -1) || (slave
== -1)){
424 printk("openpty failed to allocate a pty\n");
428 /* Not now, but complain so we now where we failed. */
431 panic("check_sigio : __raw failed, errno = %d\n", -err
);
433 err
= async_pty(master
, slave
);
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
);
440 new.sa_handler
= handler
;
441 if(sigaction(SIGIO
, &new, NULL
) < 0)
442 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno
);
445 (*proc
)(master
, 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
)
459 printk("Checking that host ptys support output SIGIO...");
461 memset(buf
, 0, sizeof(buf
));
463 while(write(master
, buf
, sizeof(buf
)) > 0) ;
465 panic("tty_output : write failed, errno = %d\n", errno
);
466 while(((n
= read(slave
, buf
, sizeof(buf
))) > 0) && !got_sigio
) ;
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...");
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 "
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)
This page took 0.048913 seconds and 6 git commands to generate.