2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
14 #include <sys/socket.h>
18 #include "kern_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;
54 struct openpty_arg pty = { .master = -1, .slave = -1 };
55 int master, slave, err;
57 initial_thread_cb(openpty_cb, &pty);
59 printk("openpty failed, errno = %d\n", -pty.err);
66 if((master == -1) || (slave == -1)){
67 printk("openpty failed to allocate a pty\n");
71 /* XXX These can fail with EINTR */
72 if(tcgetattr(master, &tt) < 0)
73 panic("check_sigio : tcgetattr failed, errno = %d\n", errno);
75 if(tcsetattr(master, TCSADRAIN, &tt) < 0)
76 panic("check_sigio : tcsetattr failed, errno = %d\n", errno);
78 err = os_sigio_async(master, slave);
80 panic("tty_fds : sigio_async failed, err = %d\n", -err);
82 if(sigaction(SIGIO, NULL, &old) < 0)
83 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
85 new.sa_handler = handler;
86 if(sigaction(SIGIO, &new, NULL) < 0)
87 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
90 (*proc)(master, slave);
92 os_close_file(master);
95 if(sigaction(SIGIO, &old, NULL) < 0)
96 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
99 static void tty_output(int master, int slave)
104 printk("Checking that host ptys support output SIGIO...");
106 memset(buf, 0, sizeof(buf));
108 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
110 panic("check_sigio : write failed, errno = %d\n", errno);
111 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
115 pty_output_sigio = 1;
117 else if(n == -EAGAIN) printk("No, enabling workaround\n");
118 else panic("check_sigio : read failed, err = %d\n", n);
121 static void tty_close(int master, int slave)
123 printk("Checking that host ptys support SIGIO on close...");
125 os_close_file(slave);
130 else printk("No, enabling workaround\n");
133 void __init check_sigio(void)
135 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
136 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
137 printk("No pseudo-terminals available - skipping pty SIGIO "
141 check_one_sigio(tty_output);
142 check_one_sigio(tty_close);
145 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
148 static int write_sigio_pid = -1;
150 /* These arrays are initialized before the sigio thread is started, and
151 * the descriptors closed after it is killed. So, it can't see them change.
152 * On the UML side, they are changed under the sigio_lock.
154 static int write_sigio_fds[2] = { -1, -1 };
155 static int sigio_private[2] = { -1, -1 };
163 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
164 * synchronizes with it.
166 struct pollfds current_poll = {
172 struct pollfds next_poll = {
178 static int write_sigio_thread(void *unused)
180 struct pollfds *fds, tmp;
182 int i, n, respond_fd;
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, "
221 static int need_poll(int n)
223 if(n <= next_poll.size){
227 if(next_poll.poll != NULL) kfree(next_poll.poll);
228 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
229 if(next_poll.poll == NULL){
230 printk("need_poll : failed to allocate new pollfds\n");
240 static void update_thread(void)
246 flags = set_signals(0);
247 n = os_write_file(sigio_private[0], &c, sizeof(c));
249 printk("update_thread : write failed, err = %d\n", -n);
253 n = os_read_file(sigio_private[0], &c, sizeof(c));
255 printk("update_thread : read failed, err = %d\n", -n);
263 if(write_sigio_pid != -1)
264 os_kill_process(write_sigio_pid, 1);
265 write_sigio_pid = -1;
266 os_close_file(sigio_private[0]);
267 os_close_file(sigio_private[1]);
268 os_close_file(write_sigio_fds[0]);
269 os_close_file(write_sigio_fds[1]);
274 int add_sigio_fd(int fd, int read)
276 int err = 0, i, n, events;
279 for(i = 0; i < current_poll.used; i++){
280 if(current_poll.poll[i].fd == fd)
284 n = current_poll.used + 1;
289 for(i = 0; i < current_poll.used; i++)
290 next_poll.poll[i] = current_poll.poll[i];
292 if(read) events = POLLIN;
293 else events = POLLOUT;
295 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
304 int ignore_sigio_fd(int fd)
307 int err = 0, i, n = 0;
310 for(i = 0; i < current_poll.used; i++){
311 if(current_poll.poll[i].fd == fd) break;
313 if(i == current_poll.used)
316 err = need_poll(current_poll.used - 1);
320 for(i = 0; i < current_poll.used; i++){
321 p = ¤t_poll.poll[i];
322 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
325 printk("ignore_sigio_fd : fd %d not found\n", fd);
336 static int setup_initial_poll(int fd)
340 p = um_kmalloc(sizeof(struct pollfd));
342 printk("setup_initial_poll : failed to allocate poll\n");
345 *p = ((struct pollfd) { .fd = fd,
348 current_poll = ((struct pollfds) { .poll = p,
354 void write_sigio_workaround(void)
360 if(write_sigio_pid != -1)
363 err = os_pipe(write_sigio_fds, 1, 1);
365 printk("write_sigio_workaround - os_pipe 1 failed, "
369 err = os_pipe(sigio_private, 1, 1);
371 printk("write_sigio_workaround - os_pipe 2 failed, "
375 if(setup_initial_poll(sigio_private[1]))
378 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
379 CLONE_FILES | CLONE_VM, &stack, 0);
381 if(write_sigio_pid < 0) goto out_close2;
383 if(write_sigio_irq(write_sigio_fds[0]))
391 os_kill_process(write_sigio_pid, 1);
392 write_sigio_pid = -1;
394 os_close_file(sigio_private[0]);
395 os_close_file(sigio_private[1]);
397 os_close_file(write_sigio_fds[0]);
398 os_close_file(write_sigio_fds[1]);
402 int read_sigio_fd(int fd)
407 n = os_read_file(fd, &c, sizeof(c));
410 printk("read_sigio_fd - read failed, err = %d\n", -n);
414 printk("read_sigio_fd - short read, bytes = %d\n", n);
421 static void sigio_cleanup(void)
423 if(write_sigio_pid != -1)
424 os_kill_process(write_sigio_pid, 1);
427 __uml_exitcall(sigio_cleanup);
430 * Overrides for Emacs so that we follow Linus's tabbing style.
431 * Emacs will notice this stuff at the end of the file and automatically
432 * adjust the settings for this buffer only. This must remain at the end
434 * ---------------------------------------------------------------------------
436 * c-file-style: "linux"