ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / kernel / sigio_user.c
1 /* 
2  * 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 <fcntl.h>
11 #include <signal.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <sched.h>
15 #include <sys/socket.h>
16 #include <sys/poll.h>
17 #include "init.h"
18 #include "user.h"
19 #include "kern_util.h"
20 #include "sigio.h"
21 #include "helper.h"
22 #include "os.h"
23
24 /* Changed during early boot */
25 int pty_output_sigio = 0;
26 int pty_close_sigio = 0;
27
28 /* Used as a flag during SIGIO testing early in boot */
29 static int got_sigio = 0;
30
31 void __init handler(int sig)
32 {
33         got_sigio = 1;
34 }
35
36 struct openpty_arg {
37         int master;
38         int slave;
39         int err;
40 };
41
42 static void openpty_cb(void *arg)
43 {
44         struct openpty_arg *info = arg;
45
46         info->err = 0;
47         if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
48                 info->err = errno;
49 }
50
51 void __init check_one_sigio(void (*proc)(int, int))
52 {
53         struct sigaction old, new;
54         struct termios tt;
55         struct openpty_arg pty = { .master = -1, .slave = -1 };
56         int master, slave, flags;
57
58         initial_thread_cb(openpty_cb, &pty);
59         if(pty.err){
60                 printk("openpty failed, errno = %d\n", pty.err);
61                 return;
62         }
63
64         master = pty.master;
65         slave = pty.slave;
66
67         if((master == -1) || (slave == -1)){
68                 printk("openpty failed to allocate a pty\n");
69                 return;
70         }
71
72         if(tcgetattr(master, &tt) < 0)
73                 panic("check_sigio : tcgetattr failed, errno = %d\n", errno);
74         cfmakeraw(&tt);
75         if(tcsetattr(master, TCSADRAIN, &tt) < 0)
76                 panic("check_sigio : tcsetattr failed, errno = %d\n", errno);
77
78         if((flags = fcntl(master, F_GETFL)) < 0)
79                 panic("tty_fds : fcntl F_GETFL failed, errno = %d\n", errno);
80
81         if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
82            (fcntl(master, F_SETOWN, os_getpid()) < 0))
83                 panic("check_sigio : fcntl F_SETFL or F_SETOWN failed, "
84                       "errno = %d\n", errno);
85
86         if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
87                 panic("check_sigio : fcntl F_SETFL failed, errno = %d\n", 
88                       errno);
89
90         if(sigaction(SIGIO, NULL, &old) < 0)
91                 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
92         new = old;
93         new.sa_handler = handler;
94         if(sigaction(SIGIO, &new, NULL) < 0)
95                 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
96
97         got_sigio = 0;
98         (*proc)(master, slave);
99                 
100         close(master);
101         close(slave);
102
103         if(sigaction(SIGIO, &old, NULL) < 0)
104                 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
105 }
106
107 static void tty_output(int master, int slave)
108 {
109         int n;
110         char buf[512];
111
112         printk("Checking that host ptys support output SIGIO...");
113
114         memset(buf, 0, sizeof(buf));
115         while(write(master, buf, sizeof(buf)) > 0) ;
116         if(errno != EAGAIN)
117                 panic("check_sigio : write failed, errno = %d\n", errno);
118
119         while(((n = read(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
120
121         if(got_sigio){
122                 printk("Yes\n");
123                 pty_output_sigio = 1;
124         }
125         else if(errno == EAGAIN) printk("No, enabling workaround\n");
126         else panic("check_sigio : read failed, errno = %d\n", errno);
127 }
128
129 static void tty_close(int master, int slave)
130 {
131         printk("Checking that host ptys support SIGIO on close...");
132
133         close(slave);
134         if(got_sigio){
135                 printk("Yes\n");
136                 pty_close_sigio = 1;
137         }
138         else printk("No, enabling workaround\n");
139 }
140
141 void __init check_sigio(void)
142 {
143         if(access("/dev/ptmx", R_OK) && access("/dev/ptyp0", R_OK)){
144                 printk("No pseudo-terminals available - skipping pty SIGIO "
145                        "check\n");
146                 return;
147         }
148         check_one_sigio(tty_output);
149         check_one_sigio(tty_close);
150 }
151
152 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an 
153  * exitcall.
154  */
155 static int write_sigio_pid = -1;
156
157 /* These arrays are initialized before the sigio thread is started, and
158  * the descriptors closed after it is killed.  So, it can't see them change.
159  * On the UML side, they are changed under the sigio_lock.
160  */
161 static int write_sigio_fds[2] = { -1, -1 };
162 static int sigio_private[2] = { -1, -1 };
163
164 struct pollfds {
165         struct pollfd *poll;
166         int size;
167         int used;
168 };
169
170 /* Protected by sigio_lock().  Used by the sigio thread, but the UML thread
171  * synchronizes with it.
172  */
173 struct pollfds current_poll = {
174         .poll           = NULL,
175         .size           = 0,
176         .used           = 0
177 };
178
179 struct pollfds next_poll = {
180         .poll           = NULL,
181         .size           = 0,
182         .used           = 0
183 };
184
185 static int write_sigio_thread(void *unused)
186 {
187         struct pollfds *fds, tmp;
188         struct pollfd *p;
189         int i, n, respond_fd;
190         char c;
191
192         fds = &current_poll;
193         while(1){
194                 n = poll(fds->poll, fds->used, -1);
195                 if(n < 0){
196                         if(errno == EINTR) continue;
197                         printk("write_sigio_thread : poll returned %d, "
198                                "errno = %d\n", n, errno);
199                 }
200                 for(i = 0; i < fds->used; i++){
201                         p = &fds->poll[i];
202                         if(p->revents == 0) continue;
203                         if(p->fd == sigio_private[1]){
204                                 n = read(sigio_private[1], &c, sizeof(c));
205                                 if(n != sizeof(c))
206                                         printk("write_sigio_thread : "
207                                                "read failed, errno = %d\n",
208                                                errno);
209                                 tmp = current_poll;
210                                 current_poll = next_poll;
211                                 next_poll = tmp;
212                                 respond_fd = sigio_private[1];
213                         }
214                         else {
215                                 respond_fd = write_sigio_fds[1];
216                                 fds->used--;
217                                 memmove(&fds->poll[i], &fds->poll[i + 1],
218                                         (fds->used - i) * sizeof(*fds->poll));
219                         }
220
221                         n = write(respond_fd, &c, sizeof(c));
222                         if(n != sizeof(c))
223                                 printk("write_sigio_thread : write failed, "
224                                        "errno = %d\n", errno);
225                 }
226         }
227 }
228
229 static int need_poll(int n)
230 {
231         if(n <= next_poll.size){
232                 next_poll.used = n;
233                 return(0);
234         }
235         if(next_poll.poll != NULL) kfree(next_poll.poll);
236         next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
237         if(next_poll.poll == NULL){
238                 printk("need_poll : failed to allocate new pollfds\n");
239                 next_poll.size = 0;
240                 next_poll.used = 0;
241                 return(-1);
242         }
243         next_poll.size = n;
244         next_poll.used = n;
245         return(0);
246 }
247
248 static void update_thread(void)
249 {
250         unsigned long flags;
251         int n;
252         char c;
253
254         flags = set_signals(0);
255         n = write(sigio_private[0], &c, sizeof(c));
256         if(n != sizeof(c)){
257                 printk("update_thread : write failed, errno = %d\n", errno);
258                 goto fail;
259         }
260
261         n = read(sigio_private[0], &c, sizeof(c));
262         if(n != sizeof(c)){
263                 printk("update_thread : read failed, errno = %d\n", errno);
264                 goto fail;
265         }
266
267         set_signals(flags);
268         return;
269  fail:
270         sigio_lock();
271         if(write_sigio_pid != -1) 
272                 os_kill_process(write_sigio_pid, 1);
273         write_sigio_pid = -1;
274         close(sigio_private[0]);
275         close(sigio_private[1]);        
276         close(write_sigio_fds[0]);
277         close(write_sigio_fds[1]);
278         sigio_unlock();
279         set_signals(flags);
280 }
281
282 int add_sigio_fd(int fd, int read)
283 {
284         int err = 0, i, n, events;
285
286         sigio_lock();
287         for(i = 0; i < current_poll.used; i++){
288                 if(current_poll.poll[i].fd == fd) 
289                         goto out;
290         }
291
292         n = current_poll.used + 1;
293         err = need_poll(n);
294         if(err) 
295                 goto out;
296
297         for(i = 0; i < current_poll.used; i++)
298                 next_poll.poll[i] = current_poll.poll[i];
299
300         if(read) events = POLLIN;
301         else events = POLLOUT;
302
303         next_poll.poll[n - 1] = ((struct pollfd) { .fd          = fd,
304                                                    .events      = events,
305                                                    .revents     = 0 });
306         update_thread();
307  out:
308         sigio_unlock();
309         return(err);
310 }
311
312 int ignore_sigio_fd(int fd)
313 {
314         struct pollfd *p;
315         int err = 0, i, n = 0;
316
317         sigio_lock();
318         for(i = 0; i < current_poll.used; i++){
319                 if(current_poll.poll[i].fd == fd) break;
320         }
321         if(i == current_poll.used)
322                 goto out;
323         
324         err = need_poll(current_poll.used - 1);
325         if(err)
326                 goto out;
327
328         for(i = 0; i < current_poll.used; i++){
329                 p = &current_poll.poll[i];
330                 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
331         }
332         if(n == i){
333                 printk("ignore_sigio_fd : fd %d not found\n", fd);
334                 err = -1;
335                 goto out;
336         }
337
338         update_thread();
339  out:
340         sigio_unlock();
341         return(err);
342 }
343
344 static int setup_initial_poll(int fd)
345 {
346         struct pollfd *p;
347
348         p = um_kmalloc(sizeof(struct pollfd));
349         if(p == NULL){
350                 printk("setup_initial_poll : failed to allocate poll\n");
351                 return(-1);
352         }
353         *p = ((struct pollfd) { .fd     = fd,
354                                 .events         = POLLIN,
355                                 .revents        = 0 });
356         current_poll = ((struct pollfds) { .poll        = p,
357                                            .used        = 1,
358                                            .size        = 1 });
359         return(0);
360 }
361
362 void write_sigio_workaround(void)
363 {
364         unsigned long stack;
365         int err;
366
367         sigio_lock();
368         if(write_sigio_pid != -1)
369                 goto out;
370
371         err = os_pipe(write_sigio_fds, 1, 1);
372         if(err){
373                 printk("write_sigio_workaround - os_pipe 1 failed, "
374                        "errno = %d\n", -err);
375                 goto out;
376         }
377         err = os_pipe(sigio_private, 1, 1);
378         if(err){
379                 printk("write_sigio_workaround - os_pipe 2 failed, "
380                        "errno = %d\n", -err);
381                 goto out_close1;
382         }
383         if(setup_initial_poll(sigio_private[1]))
384                 goto out_close2;
385
386         write_sigio_pid = run_helper_thread(write_sigio_thread, NULL, 
387                                             CLONE_FILES | CLONE_VM, &stack, 0);
388
389         if(write_sigio_pid < 0) goto out_close2;
390
391         if(write_sigio_irq(write_sigio_fds[0])) 
392                 goto out_kill;
393
394  out:
395         sigio_unlock();
396         return;
397
398  out_kill:
399         os_kill_process(write_sigio_pid, 1);
400         write_sigio_pid = -1;
401  out_close2:
402         close(sigio_private[0]);
403         close(sigio_private[1]);        
404  out_close1:
405         close(write_sigio_fds[0]);
406         close(write_sigio_fds[1]);
407         sigio_unlock();
408 }
409
410 int read_sigio_fd(int fd)
411 {
412         int n;
413         char c;
414
415         n = read(fd, &c, sizeof(c));
416         if(n != sizeof(c)){
417                 printk("read_sigio_fd - read failed, errno = %d\n", errno);
418                 return(-errno);
419         }
420         return(n);
421 }
422
423 static void sigio_cleanup(void)
424 {
425         if(write_sigio_pid != -1)
426                 os_kill_process(write_sigio_pid, 1);
427 }
428
429 __uml_exitcall(sigio_cleanup);
430
431 /*
432  * Overrides for Emacs so that we follow Linus's tabbing style.
433  * Emacs will notice this stuff at the end of the file and automatically
434  * adjust the settings for this buffer only.  This must remain at the end
435  * of the file.
436  * ---------------------------------------------------------------------------
437  * Local variables:
438  * c-file-style: "linux"
439  * End:
440  */