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 = {
52 static struct pollfds next_poll = {
58 static int write_sigio_thread(void *unused)
60 struct pollfds *fds, tmp;
65 signal(SIGWINCH, SIG_IGN);
68 n = poll(fds->poll, fds->used, -1);
70 if(errno == EINTR) continue;
71 printk("write_sigio_thread : poll returned %d, "
72 "errno = %d\n", n, errno);
74 for(i = 0; i < fds->used; i++){
76 if(p->revents == 0) continue;
77 if(p->fd == sigio_private[1]){
78 n = os_read_file(sigio_private[1], &c, sizeof(c));
80 printk("write_sigio_thread : "
81 "read failed, err = %d\n", -n);
83 current_poll = next_poll;
85 respond_fd = sigio_private[1];
88 respond_fd = write_sigio_fds[1];
90 memmove(&fds->poll[i], &fds->poll[i + 1],
91 (fds->used - i) * sizeof(*fds->poll));
94 n = os_write_file(respond_fd, &c, sizeof(c));
96 printk("write_sigio_thread : write failed, "
104 static int need_poll(int n)
106 if(n <= next_poll.size){
110 kfree(next_poll.poll);
111 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
112 if(next_poll.poll == NULL){
113 printk("need_poll : failed to allocate new pollfds\n");
123 /* Must be called with sigio_lock held, because it's needed by the marked
124 * critical section. */
125 static void update_thread(void)
131 flags = set_signals(0);
132 n = os_write_file(sigio_private[0], &c, sizeof(c));
134 printk("update_thread : write failed, err = %d\n", -n);
138 n = os_read_file(sigio_private[0], &c, sizeof(c));
140 printk("update_thread : read failed, err = %d\n", -n);
147 /* Critical section start */
148 if(write_sigio_pid != -1)
149 os_kill_process(write_sigio_pid, 1);
150 write_sigio_pid = -1;
151 close(sigio_private[0]);
152 close(sigio_private[1]);
153 close(write_sigio_fds[0]);
154 close(write_sigio_fds[1]);
155 /* Critical section end */
159 static int add_sigio_fd(int fd, int read)
161 int err = 0, i, n, events;
164 for(i = 0; i < current_poll.used; i++){
165 if(current_poll.poll[i].fd == fd)
169 n = current_poll.used + 1;
174 for(i = 0; i < current_poll.used; i++)
175 next_poll.poll[i] = current_poll.poll[i];
177 if(read) events = POLLIN;
178 else events = POLLOUT;
180 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
189 int ignore_sigio_fd(int fd)
192 int err = 0, i, n = 0;
194 /* This is called from exitcalls elsewhere in UML - if
195 * sigio_cleanup has already run, then update_thread will hang
196 * or fail because the thread is no longer running.
198 if(write_sigio_pid == -1)
202 for(i = 0; i < current_poll.used; i++){
203 if(current_poll.poll[i].fd == fd) break;
205 if(i == current_poll.used)
208 err = need_poll(current_poll.used - 1);
212 for(i = 0; i < current_poll.used; i++){
213 p = ¤t_poll.poll[i];
214 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
217 printk("ignore_sigio_fd : fd %d not found\n", fd);
228 static struct pollfd *setup_initial_poll(int fd)
232 p = um_kmalloc(sizeof(struct pollfd));
234 printk("setup_initial_poll : failed to allocate poll\n");
237 *p = ((struct pollfd) { .fd = fd,
243 static void write_sigio_workaround(void)
248 int l_write_sigio_fds[2];
249 int l_sigio_private[2];
250 int l_write_sigio_pid;
252 /* We call this *tons* of times - and most ones we must just fail. */
254 l_write_sigio_pid = write_sigio_pid;
257 if (l_write_sigio_pid != -1)
260 err = os_pipe(l_write_sigio_fds, 1, 1);
262 printk("write_sigio_workaround - os_pipe 1 failed, "
266 err = os_pipe(l_sigio_private, 1, 1);
268 printk("write_sigio_workaround - os_pipe 2 failed, "
273 p = setup_initial_poll(l_sigio_private[1]);
279 /* Did we race? Don't try to optimize this, please, it's not so likely
280 * to happen, and no more than once at the boot. */
281 if(write_sigio_pid != -1)
284 current_poll = ((struct pollfds) { .poll = p,
288 if (write_sigio_irq(l_write_sigio_fds[0]))
291 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
292 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
294 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
295 CLONE_FILES | CLONE_VM, &stack, 0);
297 if (write_sigio_pid < 0)
304 write_sigio_pid = -1;
305 write_sigio_fds[0] = -1;
306 write_sigio_fds[1] = -1;
307 sigio_private[0] = -1;
308 sigio_private[1] = -1;
310 current_poll = ((struct pollfds) { .poll = NULL,
317 close(l_sigio_private[0]);
318 close(l_sigio_private[1]);
320 close(l_write_sigio_fds[0]);
321 close(l_write_sigio_fds[1]);
324 void maybe_sigio_broken(int fd, int read)
329 if((read || pty_output_sigio) && (!read || pty_close_sigio))
332 write_sigio_workaround();
333 add_sigio_fd(fd, read);
336 static void sigio_cleanup(void)
338 if(write_sigio_pid != -1){
339 os_kill_process(write_sigio_pid, 1);
340 write_sigio_pid = -1;
344 __uml_exitcall(sigio_cleanup);