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 struct pollfds current_poll = {
52 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 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;
195 for(i = 0; i < current_poll.used; i++){
196 if(current_poll.poll[i].fd == fd) break;
198 if(i == current_poll.used)
201 err = need_poll(current_poll.used - 1);
205 for(i = 0; i < current_poll.used; i++){
206 p = ¤t_poll.poll[i];
207 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
210 printk("ignore_sigio_fd : fd %d not found\n", fd);
221 static struct pollfd *setup_initial_poll(int fd)
225 p = um_kmalloc(sizeof(struct pollfd));
227 printk("setup_initial_poll : failed to allocate poll\n");
230 *p = ((struct pollfd) { .fd = fd,
236 void write_sigio_workaround(void)
241 int l_write_sigio_fds[2];
242 int l_sigio_private[2];
243 int l_write_sigio_pid;
245 /* We call this *tons* of times - and most ones we must just fail. */
247 l_write_sigio_pid = write_sigio_pid;
250 if (l_write_sigio_pid != -1)
253 err = os_pipe(l_write_sigio_fds, 1, 1);
255 printk("write_sigio_workaround - os_pipe 1 failed, "
259 err = os_pipe(l_sigio_private, 1, 1);
261 printk("write_sigio_workaround - os_pipe 2 failed, "
266 p = setup_initial_poll(l_sigio_private[1]);
272 /* Did we race? Don't try to optimize this, please, it's not so likely
273 * to happen, and no more than once at the boot. */
274 if(write_sigio_pid != -1)
277 current_poll = ((struct pollfds) { .poll = p,
281 if (write_sigio_irq(l_write_sigio_fds[0]))
284 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
285 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
287 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
288 CLONE_FILES | CLONE_VM, &stack, 0);
290 if (write_sigio_pid < 0)
297 write_sigio_pid = -1;
298 write_sigio_fds[0] = -1;
299 write_sigio_fds[1] = -1;
300 sigio_private[0] = -1;
301 sigio_private[1] = -1;
303 current_poll = ((struct pollfds) { .poll = NULL,
310 close(l_sigio_private[0]);
311 close(l_sigio_private[1]);
313 close(l_write_sigio_fds[0]);
314 close(l_write_sigio_fds[1]);
317 void sigio_cleanup(void)
319 if(write_sigio_pid != -1){
320 os_kill_process(write_sigio_pid, 1);
321 write_sigio_pid = -1;