2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
14 #include <sys/socket.h>
18 #include "kern_util.h"
19 #include "user_util.h"
23 /* Changed during early boot */
24 int pty_output_sigio = 0;
25 int pty_close_sigio = 0;
27 /* Used as a flag during SIGIO testing early in boot */
28 static volatile int got_sigio = 0;
30 void __init handler(int sig)
41 static void openpty_cb(void *arg)
43 struct openpty_arg *info = arg;
46 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
50 void __init check_one_sigio(void (*proc)(int, int))
52 struct sigaction old, new;
53 struct openpty_arg pty = { .master = -1, .slave = -1 };
54 int master, slave, err;
56 initial_thread_cb(openpty_cb, &pty);
58 printk("openpty failed, errno = %d\n", -pty.err);
65 if((master == -1) || (slave == -1)){
66 printk("openpty failed to allocate a pty\n");
70 /* Not now, but complain so we now where we failed. */
73 panic("check_sigio : __raw failed, errno = %d\n", -err);
75 err = os_sigio_async(master, slave);
77 panic("tty_fds : sigio_async failed, err = %d\n", -err);
79 if(sigaction(SIGIO, NULL, &old) < 0)
80 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
82 new.sa_handler = handler;
83 if(sigaction(SIGIO, &new, NULL) < 0)
84 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
87 (*proc)(master, slave);
89 os_close_file(master);
92 if(sigaction(SIGIO, &old, NULL) < 0)
93 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
96 static void tty_output(int master, int slave)
101 printk("Checking that host ptys support output SIGIO...");
103 memset(buf, 0, sizeof(buf));
105 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
107 panic("check_sigio : write failed, errno = %d\n", errno);
108 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
112 pty_output_sigio = 1;
113 } else if (n == -EAGAIN) {
114 printk("No, enabling workaround\n");
116 panic("check_sigio : read failed, err = %d\n", n);
120 static void tty_close(int master, int slave)
122 printk("Checking that host ptys support SIGIO on close...");
124 os_close_file(slave);
129 else printk("No, enabling workaround\n");
132 void __init check_sigio(void)
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 "
140 check_one_sigio(tty_output);
141 check_one_sigio(tty_close);
144 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
147 static int write_sigio_pid = -1;
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.
153 static int write_sigio_fds[2] = { -1, -1 };
154 static int sigio_private[2] = { -1, -1 };
162 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
163 * synchronizes with it.
165 struct pollfds current_poll = {
171 struct pollfds next_poll = {
177 static int write_sigio_thread(void *unused)
179 struct pollfds *fds, tmp;
181 int i, n, respond_fd;
184 signal(SIGWINCH, SIG_IGN);
187 n = poll(fds->poll, fds->used, -1);
189 if(errno == EINTR) continue;
190 printk("write_sigio_thread : poll returned %d, "
191 "errno = %d\n", n, errno);
193 for(i = 0; i < fds->used; 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));
199 printk("write_sigio_thread : "
200 "read failed, err = %d\n", -n);
202 current_poll = next_poll;
204 respond_fd = sigio_private[1];
207 respond_fd = write_sigio_fds[1];
209 memmove(&fds->poll[i], &fds->poll[i + 1],
210 (fds->used - i) * sizeof(*fds->poll));
213 n = os_write_file(respond_fd, &c, sizeof(c));
215 printk("write_sigio_thread : write failed, "
223 static int need_poll(int n)
225 if(n <= next_poll.size){
229 kfree(next_poll.poll);
230 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");
242 /* Must be called with sigio_lock held, because it's needed by the marked
243 * critical section. */
244 static void update_thread(void)
250 flags = set_signals(0);
251 n = os_write_file(sigio_private[0], &c, sizeof(c));
253 printk("update_thread : write failed, err = %d\n", -n);
257 n = os_read_file(sigio_private[0], &c, sizeof(c));
259 printk("update_thread : read failed, err = %d\n", -n);
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 */
278 int add_sigio_fd(int fd, int read)
280 int err = 0, i, n, events;
283 for(i = 0; i < current_poll.used; i++){
284 if(current_poll.poll[i].fd == fd)
288 n = current_poll.used + 1;
293 for(i = 0; i < current_poll.used; i++)
294 next_poll.poll[i] = current_poll.poll[i];
296 if(read) events = POLLIN;
297 else events = POLLOUT;
299 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
308 int ignore_sigio_fd(int fd)
311 int err = 0, i, n = 0;
314 for(i = 0; i < current_poll.used; i++){
315 if(current_poll.poll[i].fd == fd) break;
317 if(i == current_poll.used)
320 err = need_poll(current_poll.used - 1);
324 for(i = 0; i < current_poll.used; i++){
325 p = ¤t_poll.poll[i];
326 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
329 printk("ignore_sigio_fd : fd %d not found\n", fd);
340 static int setup_initial_poll(int fd)
344 p = um_kmalloc_atomic(sizeof(struct pollfd));
346 printk("setup_initial_poll : failed to allocate poll\n");
349 *p = ((struct pollfd) { .fd = fd,
352 current_poll = ((struct pollfds) { .poll = p,
358 void write_sigio_workaround(void)
364 if(write_sigio_pid != -1)
367 err = os_pipe(write_sigio_fds, 1, 1);
369 printk("write_sigio_workaround - os_pipe 1 failed, "
373 err = os_pipe(sigio_private, 1, 1);
375 printk("write_sigio_workaround - os_pipe 2 failed, "
379 if(setup_initial_poll(sigio_private[1]))
382 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
383 CLONE_FILES | CLONE_VM, &stack, 0);
385 if(write_sigio_pid < 0) goto out_close2;
387 if(write_sigio_irq(write_sigio_fds[0]))
395 os_kill_process(write_sigio_pid, 1);
396 write_sigio_pid = -1;
398 os_close_file(sigio_private[0]);
399 os_close_file(sigio_private[1]);
401 os_close_file(write_sigio_fds[0]);
402 os_close_file(write_sigio_fds[1]);
406 int read_sigio_fd(int fd)
411 n = os_read_file(fd, &c, sizeof(c));
414 printk("read_sigio_fd - read failed, err = %d\n", -n);
418 printk("read_sigio_fd - short read, bytes = %d\n", n);
425 static void sigio_cleanup(void)
427 if (write_sigio_pid != -1) {
428 os_kill_process(write_sigio_pid, 1);
429 write_sigio_pid = -1;
433 __uml_exitcall(sigio_cleanup);