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 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
26 static int write_sigio_pid = -1;
28 /* These arrays are initialized before the sigio thread is started, and
29 * the descriptors closed after it is killed. So, it can't see them change.
30 * On the UML side, they are changed under the sigio_lock.
32 #define SIGIO_FDS_INIT {-1, -1}
34 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
35 static int sigio_private[2] = SIGIO_FDS_INIT;
43 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
44 * synchronizes with it.
46 static struct pollfds current_poll;
47 static struct pollfds next_poll;
48 static struct pollfds all_sigio_fds;
50 static int write_sigio_thread(void *unused)
52 struct pollfds *fds, tmp;
57 signal(SIGWINCH, SIG_IGN);
60 n = poll(fds->poll, fds->used, -1);
62 if(errno == EINTR) continue;
63 printk("write_sigio_thread : poll returned %d, "
64 "errno = %d\n", n, errno);
66 for(i = 0; i < fds->used; i++){
68 if(p->revents == 0) continue;
69 if(p->fd == sigio_private[1]){
70 n = os_read_file(sigio_private[1], &c, sizeof(c));
72 printk("write_sigio_thread : "
73 "read on socket failed, "
76 current_poll = next_poll;
78 respond_fd = sigio_private[1];
81 respond_fd = write_sigio_fds[1];
83 memmove(&fds->poll[i], &fds->poll[i + 1],
84 (fds->used - i) * sizeof(*fds->poll));
87 n = os_write_file(respond_fd, &c, sizeof(c));
89 printk("write_sigio_thread : write on socket "
90 "failed, err = %d\n", -n);
97 static int need_poll(struct pollfds *polls, int n)
104 polls->poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
105 if(polls->poll == NULL){
106 printk("need_poll : failed to allocate new pollfds\n");
116 /* Must be called with sigio_lock held, because it's needed by the marked
119 static void update_thread(void)
125 flags = set_signals(0);
126 n = os_write_file(sigio_private[0], &c, sizeof(c));
128 printk("update_thread : write failed, err = %d\n", -n);
132 n = os_read_file(sigio_private[0], &c, sizeof(c));
134 printk("update_thread : read failed, err = %d\n", -n);
141 /* Critical section start */
142 if(write_sigio_pid != -1)
143 os_kill_process(write_sigio_pid, 1);
144 write_sigio_pid = -1;
145 close(sigio_private[0]);
146 close(sigio_private[1]);
147 close(write_sigio_fds[0]);
148 close(write_sigio_fds[1]);
149 /* Critical section end */
153 int add_sigio_fd(int fd)
159 for(i = 0; i < all_sigio_fds.used; i++){
160 if(all_sigio_fds.poll[i].fd == fd)
163 if(i == all_sigio_fds.used)
166 p = &all_sigio_fds.poll[i];
168 for(i = 0; i < current_poll.used; i++){
169 if(current_poll.poll[i].fd == fd)
173 n = current_poll.used + 1;
174 err = need_poll(&next_poll, n);
178 for(i = 0; i < current_poll.used; i++)
179 next_poll.poll[i] = current_poll.poll[i];
181 next_poll.poll[n - 1] = *p;
188 int ignore_sigio_fd(int fd)
191 int err = 0, i, n = 0;
193 /* This is called from exitcalls elsewhere in UML - if
194 * sigio_cleanup has already run, then update_thread will hang
195 * or fail because the thread is no longer running.
197 if(write_sigio_pid == -1)
201 for(i = 0; i < current_poll.used; i++){
202 if(current_poll.poll[i].fd == fd) break;
204 if(i == current_poll.used)
207 err = need_poll(&next_poll, current_poll.used - 1);
211 for(i = 0; i < current_poll.used; i++){
212 p = ¤t_poll.poll[i];
214 next_poll.poll[n++] = *p;
223 static struct pollfd *setup_initial_poll(int fd)
227 p = um_kmalloc(sizeof(struct pollfd));
229 printk("setup_initial_poll : failed to allocate poll\n");
232 *p = ((struct pollfd) { .fd = fd,
238 static void write_sigio_workaround(void)
243 int l_write_sigio_fds[2];
244 int l_sigio_private[2];
245 int l_write_sigio_pid;
247 /* We call this *tons* of times - and most ones we must just fail. */
249 l_write_sigio_pid = write_sigio_pid;
252 if (l_write_sigio_pid != -1)
255 err = os_pipe(l_write_sigio_fds, 1, 1);
257 printk("write_sigio_workaround - os_pipe 1 failed, "
261 err = os_pipe(l_sigio_private, 1, 1);
263 printk("write_sigio_workaround - os_pipe 2 failed, "
268 p = setup_initial_poll(l_sigio_private[1]);
274 /* Did we race? Don't try to optimize this, please, it's not so likely
275 * to happen, and no more than once at the boot. */
276 if(write_sigio_pid != -1)
279 current_poll = ((struct pollfds) { .poll = p,
283 if (write_sigio_irq(l_write_sigio_fds[0]))
286 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
287 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
289 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
290 CLONE_FILES | CLONE_VM, &stack, 0);
292 if (write_sigio_pid < 0)
299 write_sigio_pid = -1;
300 write_sigio_fds[0] = -1;
301 write_sigio_fds[1] = -1;
302 sigio_private[0] = -1;
303 sigio_private[1] = -1;
305 current_poll = ((struct pollfds) { .poll = NULL,
312 close(l_sigio_private[0]);
313 close(l_sigio_private[1]);
315 close(l_write_sigio_fds[0]);
316 close(l_write_sigio_fds[1]);
319 void maybe_sigio_broken(int fd, int read)
326 if((read || pty_output_sigio) && (!read || pty_close_sigio))
329 write_sigio_workaround();
332 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
334 printk("maybe_sigio_broken - failed to add pollfd\n");
337 all_sigio_fds.poll[all_sigio_fds.used++] =
338 ((struct pollfd) { .fd = fd,
339 .events = read ? POLLIN : POLLOUT,
345 static void sigio_cleanup(void)
347 if(write_sigio_pid != -1){
348 os_kill_process(write_sigio_pid, 1);
349 write_sigio_pid = -1;
353 __uml_exitcall(sigio_cleanup);