blob: 00e9388e947af4fa59442a3fdad627a82b7384e2 [file] [log] [blame]
Jeff Dike8e367062006-03-27 01:14:32 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
6#include <unistd.h>
7#include <stdlib.h>
8#include <termios.h>
9#include <pty.h>
10#include <signal.h>
11#include <errno.h>
12#include <string.h>
13#include <sched.h>
14#include <sys/socket.h>
15#include <sys/poll.h>
16#include "init.h"
17#include "user.h"
18#include "kern_util.h"
19#include "user_util.h"
20#include "sigio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "os.h"
22
Jeff Dikef206aab2006-03-27 01:14:33 -080023/* Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * exitcall.
25 */
26static int write_sigio_pid = -1;
27
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.
31 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080032#define SIGIO_FDS_INIT {-1, -1}
33
34static int write_sigio_fds[2] = SIGIO_FDS_INIT;
35static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct pollfds {
38 struct pollfd *poll;
39 int size;
40 int used;
41};
42
43/* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
44 * synchronizes with it.
45 */
46struct pollfds current_poll = {
47 .poll = NULL,
48 .size = 0,
49 .used = 0
50};
51
52struct pollfds next_poll = {
53 .poll = NULL,
54 .size = 0,
55 .used = 0
56};
57
58static int write_sigio_thread(void *unused)
59{
60 struct pollfds *fds, tmp;
61 struct pollfd *p;
62 int i, n, respond_fd;
63 char c;
64
Jeff Dikecd2ee4a2005-05-05 16:15:32 -070065 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 fds = &current_poll;
67 while(1){
68 n = poll(fds->poll, fds->used, -1);
69 if(n < 0){
70 if(errno == EINTR) continue;
71 printk("write_sigio_thread : poll returned %d, "
72 "errno = %d\n", n, errno);
73 }
74 for(i = 0; i < fds->used; i++){
75 p = &fds->poll[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));
79 if(n != sizeof(c))
80 printk("write_sigio_thread : "
81 "read failed, err = %d\n", -n);
82 tmp = current_poll;
83 current_poll = next_poll;
84 next_poll = tmp;
85 respond_fd = sigio_private[1];
86 }
87 else {
88 respond_fd = write_sigio_fds[1];
89 fds->used--;
90 memmove(&fds->poll[i], &fds->poll[i + 1],
91 (fds->used - i) * sizeof(*fds->poll));
92 }
93
94 n = os_write_file(respond_fd, &c, sizeof(c));
95 if(n != sizeof(c))
96 printk("write_sigio_thread : write failed, "
97 "err = %d\n", -n);
98 }
99 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -0800100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static int need_poll(int n)
105{
106 if(n <= next_poll.size){
107 next_poll.used = n;
108 return(0);
109 }
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800110 kfree(next_poll.poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 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");
114 next_poll.size = 0;
115 next_poll.used = 0;
116 return(-1);
117 }
118 next_poll.size = n;
119 next_poll.used = n;
120 return(0);
121}
122
123/* Must be called with sigio_lock held, because it's needed by the marked
124 * critical section. */
125static void update_thread(void)
126{
127 unsigned long flags;
128 int n;
129 char c;
130
131 flags = set_signals(0);
132 n = os_write_file(sigio_private[0], &c, sizeof(c));
133 if(n != sizeof(c)){
134 printk("update_thread : write failed, err = %d\n", -n);
135 goto fail;
136 }
137
138 n = os_read_file(sigio_private[0], &c, sizeof(c));
139 if(n != sizeof(c)){
140 printk("update_thread : read failed, err = %d\n", -n);
141 goto fail;
142 }
143
144 set_signals(flags);
145 return;
146 fail:
147 /* Critical section start */
Jeff Dikef206aab2006-03-27 01:14:33 -0800148 if(write_sigio_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 os_kill_process(write_sigio_pid, 1);
150 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800151 close(sigio_private[0]);
152 close(sigio_private[1]);
153 close(write_sigio_fds[0]);
154 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* Critical section end */
156 set_signals(flags);
157}
158
159int add_sigio_fd(int fd, int read)
160{
161 int err = 0, i, n, events;
162
163 sigio_lock();
164 for(i = 0; i < current_poll.used; i++){
Jeff Dikef206aab2006-03-27 01:14:33 -0800165 if(current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto out;
167 }
168
169 n = current_poll.used + 1;
170 err = need_poll(n);
Jeff Dikef206aab2006-03-27 01:14:33 -0800171 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto out;
173
174 for(i = 0; i < current_poll.used; i++)
175 next_poll.poll[i] = current_poll.poll[i];
176
177 if(read) events = POLLIN;
178 else events = POLLOUT;
179
180 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
181 .events = events,
182 .revents = 0 });
183 update_thread();
184 out:
185 sigio_unlock();
186 return(err);
187}
188
189int ignore_sigio_fd(int fd)
190{
191 struct pollfd *p;
192 int err = 0, i, n = 0;
193
194 sigio_lock();
195 for(i = 0; i < current_poll.used; i++){
196 if(current_poll.poll[i].fd == fd) break;
197 }
198 if(i == current_poll.used)
199 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 err = need_poll(current_poll.used - 1);
202 if(err)
203 goto out;
204
205 for(i = 0; i < current_poll.used; i++){
206 p = &current_poll.poll[i];
207 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
208 }
209 if(n == i){
210 printk("ignore_sigio_fd : fd %d not found\n", fd);
211 err = -1;
212 goto out;
213 }
214
215 update_thread();
216 out:
217 sigio_unlock();
218 return(err);
219}
220
Jeff Dikef206aab2006-03-27 01:14:33 -0800221static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct pollfd *p;
224
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800225 p = um_kmalloc(sizeof(struct pollfd));
226 if (p == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 printk("setup_initial_poll : failed to allocate poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800228 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 *p = ((struct pollfd) { .fd = fd,
231 .events = POLLIN,
232 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800233 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236void write_sigio_workaround(void)
237{
238 unsigned long stack;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800239 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800241 int l_write_sigio_fds[2];
242 int l_sigio_private[2];
243 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800245 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800247 l_write_sigio_pid = write_sigio_pid;
248 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800250 if (l_write_sigio_pid != -1)
251 return;
252
253 err = os_pipe(l_write_sigio_fds, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if(err < 0){
255 printk("write_sigio_workaround - os_pipe 1 failed, "
256 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800257 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800259 err = os_pipe(l_sigio_private, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if(err < 0){
Jeff Dikef206aab2006-03-27 01:14:33 -0800261 printk("write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 "err = %d\n", -err);
263 goto out_close1;
264 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800265
266 p = setup_initial_poll(l_sigio_private[1]);
267 if(!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out_close2;
269
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800270 sigio_lock();
271
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)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800275 goto out_free;
276
277 current_poll = ((struct pollfds) { .poll = p,
278 .used = 1,
279 .size = 1 });
280
281 if (write_sigio_irq(l_write_sigio_fds[0]))
282 goto out_clear_poll;
283
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));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800286
287 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 CLONE_FILES | CLONE_VM, &stack, 0);
289
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800290 if (write_sigio_pid < 0)
291 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 sigio_unlock();
294 return;
295
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800296out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800298 write_sigio_fds[0] = -1;
299 write_sigio_fds[1] = -1;
300 sigio_private[0] = -1;
301 sigio_private[1] = -1;
302out_clear_poll:
303 current_poll = ((struct pollfds) { .poll = NULL,
304 .size = 0,
305 .used = 0 });
306out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800307 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700308 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800309out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800310 close(l_sigio_private[0]);
311 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800312out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800313 close(l_write_sigio_fds[0]);
314 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Jeff Dike8e367062006-03-27 01:14:32 -0800317void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Jeff Dikef206aab2006-03-27 01:14:33 -0800319 if(write_sigio_pid != -1){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 os_kill_process(write_sigio_pid, 1);
321 write_sigio_pid = -1;
322 }
323}