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