[PATCH] uml: move sigio_user.c to os-Linux/sigio.c
[linux-2.6.22.y-op.git] / arch / um / os-Linux / sigio.c
blob7604c404c4c2f390fe6d360c6ef8ebbfcc4d1c2c
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
21 #include "os.h"
23 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
24 * exitcall.
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 };
35 struct pollfds {
36 struct pollfd *poll;
37 int size;
38 int used;
41 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
42 * synchronizes with it.
44 struct pollfds current_poll = {
45 .poll = NULL,
46 .size = 0,
47 .used = 0
50 struct pollfds next_poll = {
51 .poll = NULL,
52 .size = 0,
53 .used = 0
56 static int write_sigio_thread(void *unused)
58 struct pollfds *fds, tmp;
59 struct pollfd *p;
60 int i, n, respond_fd;
61 char c;
63 signal(SIGWINCH, SIG_IGN);
64 fds = &current_poll;
65 while(1){
66 n = poll(fds->poll, fds->used, -1);
67 if(n < 0){
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++){
73 p = &fds->poll[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));
77 if(n != sizeof(c))
78 printk("write_sigio_thread : "
79 "read failed, err = %d\n", -n);
80 tmp = current_poll;
81 current_poll = next_poll;
82 next_poll = tmp;
83 respond_fd = sigio_private[1];
85 else {
86 respond_fd = write_sigio_fds[1];
87 fds->used--;
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));
93 if(n != sizeof(c))
94 printk("write_sigio_thread : write failed, "
95 "err = %d\n", -n);
99 return 0;
102 static int need_poll(int n)
104 if(n <= next_poll.size){
105 next_poll.used = n;
106 return(0);
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");
112 next_poll.size = 0;
113 next_poll.used = 0;
114 return(-1);
116 next_poll.size = n;
117 next_poll.used = n;
118 return(0);
121 /* Must be called with sigio_lock held, because it's needed by the marked
122 * critical section. */
123 static void update_thread(void)
125 unsigned long flags;
126 int n;
127 char c;
129 flags = set_signals(0);
130 n = os_write_file(sigio_private[0], &c, sizeof(c));
131 if(n != sizeof(c)){
132 printk("update_thread : write failed, err = %d\n", -n);
133 goto fail;
136 n = os_read_file(sigio_private[0], &c, sizeof(c));
137 if(n != sizeof(c)){
138 printk("update_thread : read failed, err = %d\n", -n);
139 goto fail;
142 set_signals(flags);
143 return;
144 fail:
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 */
154 set_signals(flags);
157 int add_sigio_fd(int fd, int read)
159 int err = 0, i, n, events;
161 sigio_lock();
162 for(i = 0; i < current_poll.used; i++){
163 if(current_poll.poll[i].fd == fd)
164 goto out;
167 n = current_poll.used + 1;
168 err = need_poll(n);
169 if(err)
170 goto out;
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,
179 .events = events,
180 .revents = 0 });
181 update_thread();
182 out:
183 sigio_unlock();
184 return(err);
187 int ignore_sigio_fd(int fd)
189 struct pollfd *p;
190 int err = 0, i, n = 0;
192 sigio_lock();
193 for(i = 0; i < current_poll.used; i++){
194 if(current_poll.poll[i].fd == fd) break;
196 if(i == current_poll.used)
197 goto out;
199 err = need_poll(current_poll.used - 1);
200 if(err)
201 goto out;
203 for(i = 0; i < current_poll.used; i++){
204 p = &current_poll.poll[i];
205 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
207 if(n == i){
208 printk("ignore_sigio_fd : fd %d not found\n", fd);
209 err = -1;
210 goto out;
213 update_thread();
214 out:
215 sigio_unlock();
216 return(err);
219 static struct pollfd *setup_initial_poll(int fd)
221 struct pollfd *p;
223 p = um_kmalloc(sizeof(struct pollfd));
224 if (p == NULL) {
225 printk("setup_initial_poll : failed to allocate poll\n");
226 return NULL;
228 *p = ((struct pollfd) { .fd = fd,
229 .events = POLLIN,
230 .revents = 0 });
231 return p;
234 void write_sigio_workaround(void)
236 unsigned long stack;
237 struct pollfd *p;
238 int err;
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. */
244 sigio_lock();
245 l_write_sigio_pid = write_sigio_pid;
246 sigio_unlock();
248 if (l_write_sigio_pid != -1)
249 return;
251 err = os_pipe(l_write_sigio_fds, 1, 1);
252 if(err < 0){
253 printk("write_sigio_workaround - os_pipe 1 failed, "
254 "err = %d\n", -err);
255 return;
257 err = os_pipe(l_sigio_private, 1, 1);
258 if(err < 0){
259 printk("write_sigio_workaround - os_pipe 2 failed, "
260 "err = %d\n", -err);
261 goto out_close1;
264 p = setup_initial_poll(l_sigio_private[1]);
265 if(!p)
266 goto out_close2;
268 sigio_lock();
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)
273 goto out_unlock;
275 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
276 CLONE_FILES | CLONE_VM, &stack, 0);
278 if (write_sigio_pid < 0)
279 goto out_clear;
281 if (write_sigio_irq(l_write_sigio_fds[0]))
282 goto out_kill;
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,
289 .used = 1,
290 .size = 1 });
292 sigio_unlock();
293 return;
295 out_kill:
296 l_write_sigio_pid = write_sigio_pid;
297 write_sigio_pid = -1;
298 sigio_unlock();
299 /* Going to call waitpid, avoid holding the lock. */
300 os_kill_process(l_write_sigio_pid, 1);
301 goto out_free;
303 out_clear:
304 write_sigio_pid = -1;
305 out_unlock:
306 sigio_unlock();
307 out_free:
308 kfree(p);
309 out_close2:
310 close(l_sigio_private[0]);
311 close(l_sigio_private[1]);
312 out_close1:
313 close(l_write_sigio_fds[0]);
314 close(l_write_sigio_fds[1]);
315 return;
318 void sigio_cleanup(void)
320 if(write_sigio_pid != -1){
321 os_kill_process(write_sigio_pid, 1);
322 write_sigio_pid = -1;