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