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;
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 err = os_make_pty_raw(master);
72 panic("check_sigio : os_make_pty_raw failed, errno = %d\n", -err);
74 err = os_sigio_async(master, slave);
76 panic("tty_fds : sigio_async failed, err = %d\n", -err);
78 if(sigaction(SIGIO, NULL, &old) < 0)
79 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
81 new.sa_handler = handler;
82 if(sigaction(SIGIO, &new, NULL) < 0)
83 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
86 (*proc)(master, slave);
88 os_close_file(master);
91 if(sigaction(SIGIO, &old, NULL) < 0)
92 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
95 static void tty_output(int master, int slave)
100 printk("Checking that host ptys support output SIGIO...");
102 memset(buf, 0, sizeof(buf));
104 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
106 panic("check_sigio : write failed, errno = %d\n", errno);
107 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
111 pty_output_sigio = 1;
113 else if(n == -EAGAIN) printk("No, enabling workaround\n");
114 else panic("check_sigio : read failed, err = %d\n", n);
117 static void tty_close(int master, int slave)
119 printk("Checking that host ptys support SIGIO on close...");
121 os_close_file(slave);
126 else printk("No, enabling workaround\n");
129 void __init check_sigio(void)
131 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
132 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
133 printk("No pseudo-terminals available - skipping pty SIGIO "
137 check_one_sigio(tty_output);
138 check_one_sigio(tty_close);
141 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
144 static int write_sigio_pid = -1;
146 /* These arrays are initialized before the sigio thread is started, and
147 * the descriptors closed after it is killed. So, it can't see them change.
148 * On the UML side, they are changed under the sigio_lock.
150 static int write_sigio_fds[2] = { -1, -1 };
151 static int sigio_private[2] = { -1, -1 };
159 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
160 * synchronizes with it.
162 struct pollfds current_poll = {
168 struct pollfds next_poll = {
174 static int write_sigio_thread(void *unused)
176 struct pollfds *fds, tmp;
178 int i, n, respond_fd;
183 n = poll(fds->poll, fds->used, -1);
185 if(errno == EINTR) continue;
186 printk("write_sigio_thread : poll returned %d, "
187 "errno = %d\n", n, errno);
189 for(i = 0; i < fds->used; i++){
191 if(p->revents == 0) continue;
192 if(p->fd == sigio_private[1]){
193 n = os_read_file(sigio_private[1], &c, sizeof(c));
195 printk("write_sigio_thread : "
196 "read failed, err = %d\n", -n);
198 current_poll = next_poll;
200 respond_fd = sigio_private[1];
203 respond_fd = write_sigio_fds[1];
205 memmove(&fds->poll[i], &fds->poll[i + 1],
206 (fds->used - i) * sizeof(*fds->poll));
209 n = os_write_file(respond_fd, &c, sizeof(c));
211 printk("write_sigio_thread : write failed, "
217 static int need_poll(int n)
219 if(n <= next_poll.size){
223 if(next_poll.poll != NULL) kfree(next_poll.poll);
224 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
225 if(next_poll.poll == NULL){
226 printk("need_poll : failed to allocate new pollfds\n");
236 static void update_thread(void)
242 flags = set_signals(0);
243 n = os_write_file(sigio_private[0], &c, sizeof(c));
245 printk("update_thread : write failed, err = %d\n", -n);
249 n = os_read_file(sigio_private[0], &c, sizeof(c));
251 printk("update_thread : read failed, err = %d\n", -n);
259 if(write_sigio_pid != -1)
260 os_kill_process(write_sigio_pid, 1);
261 write_sigio_pid = -1;
262 os_close_file(sigio_private[0]);
263 os_close_file(sigio_private[1]);
264 os_close_file(write_sigio_fds[0]);
265 os_close_file(write_sigio_fds[1]);
270 int add_sigio_fd(int fd, int read)
272 int err = 0, i, n, events;
275 for(i = 0; i < current_poll.used; i++){
276 if(current_poll.poll[i].fd == fd)
280 n = current_poll.used + 1;
285 for(i = 0; i < current_poll.used; i++)
286 next_poll.poll[i] = current_poll.poll[i];
288 if(read) events = POLLIN;
289 else events = POLLOUT;
291 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
300 int ignore_sigio_fd(int fd)
303 int err = 0, i, n = 0;
306 for(i = 0; i < current_poll.used; i++){
307 if(current_poll.poll[i].fd == fd) break;
309 if(i == current_poll.used)
312 err = need_poll(current_poll.used - 1);
316 for(i = 0; i < current_poll.used; i++){
317 p = ¤t_poll.poll[i];
318 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
321 printk("ignore_sigio_fd : fd %d not found\n", fd);
332 static int setup_initial_poll(int fd)
336 p = um_kmalloc(sizeof(struct pollfd));
338 printk("setup_initial_poll : failed to allocate poll\n");
341 *p = ((struct pollfd) { .fd = fd,
344 current_poll = ((struct pollfds) { .poll = p,
350 void write_sigio_workaround(void)
356 if(write_sigio_pid != -1)
359 err = os_pipe(write_sigio_fds, 1, 1);
361 printk("write_sigio_workaround - os_pipe 1 failed, "
365 err = os_pipe(sigio_private, 1, 1);
367 printk("write_sigio_workaround - os_pipe 2 failed, "
371 if(setup_initial_poll(sigio_private[1]))
374 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
375 CLONE_FILES | CLONE_VM, &stack, 0);
377 if(write_sigio_pid < 0) goto out_close2;
379 if(write_sigio_irq(write_sigio_fds[0]))
387 os_kill_process(write_sigio_pid, 1);
388 write_sigio_pid = -1;
390 os_close_file(sigio_private[0]);
391 os_close_file(sigio_private[1]);
393 os_close_file(write_sigio_fds[0]);
394 os_close_file(write_sigio_fds[1]);
398 int read_sigio_fd(int fd)
403 n = os_read_file(fd, &c, sizeof(c));
406 printk("read_sigio_fd - read failed, err = %d\n", -n);
410 printk("read_sigio_fd - short read, bytes = %d\n", n);
417 static void sigio_cleanup(void)
419 if(write_sigio_pid != -1)
420 os_kill_process(write_sigio_pid, 1);
423 __uml_exitcall(sigio_cleanup);
426 * Overrides for Emacs so that we follow Linus's tabbing style.
427 * Emacs will notice this stuff at the end of the file and automatically
428 * adjust the settings for this buffer only. This must remain at the end
430 * ---------------------------------------------------------------------------
432 * c-file-style: "linux"