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 static int write_sigio_fds[2] = { -1, -1 };
33 static int sigio_private[2] = { -1, -1 };
41 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
42 * synchronizes with it.
44 struct pollfds current_poll = {
50 struct pollfds next_poll = {
56 static int write_sigio_thread(void *unused)
58 struct pollfds *fds, tmp;
63 signal(SIGWINCH, SIG_IGN);
66 n = poll(fds->poll, fds->used, -1);
68 if(errno == EINTR) continue;
69 printk("write_sigio_thread : poll returned %d, "
70 "errno = %d\n", n, errno);
72 for(i = 0; i < fds->used; i++){
74 if(p->revents == 0) continue;
75 if(p->fd == sigio_private[1]){
76 n = os_read_file(sigio_private[1], &c, sizeof(c));
78 printk("write_sigio_thread : "
79 "read failed, err = %d\n", -n);
81 current_poll = next_poll;
83 respond_fd = sigio_private[1];
86 respond_fd = write_sigio_fds[1];
88 memmove(&fds->poll[i], &fds->poll[i + 1],
89 (fds->used - i) * sizeof(*fds->poll));
92 n = os_write_file(respond_fd, &c, sizeof(c));
94 printk("write_sigio_thread : write failed, "
102 static int need_poll(int n)
104 if(n <= next_poll.size){
108 kfree(next_poll.poll);
109 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
110 if(next_poll.poll == NULL){
111 printk("need_poll : failed to allocate new pollfds\n");
121 /* Must be called with sigio_lock held, because it's needed by the marked
122 * critical section. */
123 static void update_thread(void)
129 flags = set_signals(0);
130 n = os_write_file(sigio_private[0], &c, sizeof(c));
132 printk("update_thread : write failed, err = %d\n", -n);
136 n = os_read_file(sigio_private[0], &c, sizeof(c));
138 printk("update_thread : read failed, err = %d\n", -n);
145 /* Critical section start */
146 if(write_sigio_pid != -1)
147 os_kill_process(write_sigio_pid, 1);
148 write_sigio_pid = -1;
149 close(sigio_private[0]);
150 close(sigio_private[1]);
151 close(write_sigio_fds[0]);
152 close(write_sigio_fds[1]);
153 /* Critical section end */
157 int add_sigio_fd(int fd, int read)
159 int err = 0, i, n, events;
162 for(i = 0; i < current_poll.used; i++){
163 if(current_poll.poll[i].fd == fd)
167 n = current_poll.used + 1;
172 for(i = 0; i < current_poll.used; i++)
173 next_poll.poll[i] = current_poll.poll[i];
175 if(read) events = POLLIN;
176 else events = POLLOUT;
178 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
187 int ignore_sigio_fd(int fd)
190 int err = 0, i, n = 0;
193 for(i = 0; i < current_poll.used; i++){
194 if(current_poll.poll[i].fd == fd) break;
196 if(i == current_poll.used)
199 err = need_poll(current_poll.used - 1);
203 for(i = 0; i < current_poll.used; i++){
204 p = ¤t_poll.poll[i];
205 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
208 printk("ignore_sigio_fd : fd %d not found\n", fd);
219 static struct pollfd *setup_initial_poll(int fd)
223 p = um_kmalloc(sizeof(struct pollfd));
225 printk("setup_initial_poll : failed to allocate poll\n");
228 *p = ((struct pollfd) { .fd = fd,
234 void write_sigio_workaround(void)
239 int l_write_sigio_fds[2];
240 int l_sigio_private[2];
241 int l_write_sigio_pid;
243 /* We call this *tons* of times - and most ones we must just fail. */
245 l_write_sigio_pid = write_sigio_pid;
248 if (l_write_sigio_pid != -1)
251 err = os_pipe(l_write_sigio_fds, 1, 1);
253 printk("write_sigio_workaround - os_pipe 1 failed, "
257 err = os_pipe(l_sigio_private, 1, 1);
259 printk("write_sigio_workaround - os_pipe 2 failed, "
264 p = setup_initial_poll(l_sigio_private[1]);
270 /* Did we race? Don't try to optimize this, please, it's not so likely
271 * to happen, and no more than once at the boot. */
272 if(write_sigio_pid != -1)
275 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
276 CLONE_FILES | CLONE_VM, &stack, 0);
278 if (write_sigio_pid < 0)
281 if (write_sigio_irq(l_write_sigio_fds[0]))
284 /* Success, finally. */
285 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
286 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
288 current_poll = ((struct pollfds) { .poll = p,
296 l_write_sigio_pid = write_sigio_pid;
297 write_sigio_pid = -1;
299 /* Going to call waitpid, avoid holding the lock. */
300 os_kill_process(l_write_sigio_pid, 1);
304 write_sigio_pid = -1;
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]);
318 void sigio_cleanup(void)
320 if(write_sigio_pid != -1){
321 os_kill_process(write_sigio_pid, 1);
322 write_sigio_pid = -1;