uml: tidy libc code
[powerpc.git] / arch / um / os-Linux / sigio.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 <fcntl.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 "os.h"
22 #include "um_malloc.h"
23 #include "init.h"
24
25 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
26  * exitcall.
27  */
28 static int write_sigio_pid = -1;
29
30 /* These arrays are initialized before the sigio thread is started, and
31  * the descriptors closed after it is killed.  So, it can't see them change.
32  * On the UML side, they are changed under the sigio_lock.
33  */
34 #define SIGIO_FDS_INIT {-1, -1}
35
36 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
37 static int sigio_private[2] = SIGIO_FDS_INIT;
38
39 struct pollfds {
40         struct pollfd *poll;
41         int size;
42         int used;
43 };
44
45 /* Protected by sigio_lock().  Used by the sigio thread, but the UML thread
46  * synchronizes with it.
47  */
48 static struct pollfds current_poll;
49 static struct pollfds next_poll;
50 static struct pollfds all_sigio_fds;
51
52 static int write_sigio_thread(void *unused)
53 {
54         struct pollfds *fds, tmp;
55         struct pollfd *p;
56         int i, n, respond_fd;
57         char c;
58
59         signal(SIGWINCH, SIG_IGN);
60         fds = &current_poll;
61         while(1){
62                 n = poll(fds->poll, fds->used, -1);
63                 if(n < 0){
64                         if(errno == EINTR) continue;
65                         printk("write_sigio_thread : poll returned %d, "
66                                "errno = %d\n", n, errno);
67                 }
68                 for(i = 0; i < fds->used; i++){
69                         p = &fds->poll[i];
70                         if(p->revents == 0) continue;
71                         if(p->fd == sigio_private[1]){
72                                 n = os_read_file(sigio_private[1], &c, sizeof(c));
73                                 if(n != sizeof(c))
74                                         printk("write_sigio_thread : "
75                                                "read on socket failed, "
76                                                "err = %d\n", -n);
77                                 tmp = current_poll;
78                                 current_poll = next_poll;
79                                 next_poll = tmp;
80                                 respond_fd = sigio_private[1];
81                         }
82                         else {
83                                 respond_fd = write_sigio_fds[1];
84                                 fds->used--;
85                                 memmove(&fds->poll[i], &fds->poll[i + 1],
86                                         (fds->used - i) * sizeof(*fds->poll));
87                         }
88
89                         n = os_write_file(respond_fd, &c, sizeof(c));
90                         if(n != sizeof(c))
91                                 printk("write_sigio_thread : write on socket "
92                                        "failed, err = %d\n", -n);
93                 }
94         }
95
96         return 0;
97 }
98
99 static int need_poll(struct pollfds *polls, int n)
100 {
101         struct pollfd *new;
102
103         if(n <= polls->size)
104                 return 0;
105
106         new = um_kmalloc_atomic(n * sizeof(struct pollfd));
107         if(new == NULL){
108                 printk("need_poll : failed to allocate new pollfds\n");
109                 return -ENOMEM;
110         }
111
112         memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
113         kfree(polls->poll);
114
115         polls->poll = new;
116         polls->size = n;
117         return 0;
118 }
119
120 /* Must be called with sigio_lock held, because it's needed by the marked
121  * critical section.
122  */
123 static void update_thread(void)
124 {
125         unsigned long flags;
126         int n;
127         char c;
128
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;
134         }
135
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;
140         }
141
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);
155 }
156
157 int add_sigio_fd(int fd)
158 {
159         struct pollfd *p;
160         int err = 0, i, n;
161
162         sigio_lock();
163         for(i = 0; i < all_sigio_fds.used; i++){
164                 if(all_sigio_fds.poll[i].fd == fd)
165                         break;
166         }
167         if(i == all_sigio_fds.used)
168                 goto out;
169
170         p = &all_sigio_fds.poll[i];
171
172         for(i = 0; i < current_poll.used; i++){
173                 if(current_poll.poll[i].fd == fd)
174                         goto out;
175         }
176
177         n = current_poll.used;
178         err = need_poll(&next_poll, n + 1);
179         if(err)
180                 goto out;
181
182         memcpy(next_poll.poll, current_poll.poll,
183                current_poll.used * sizeof(struct pollfd));
184         next_poll.poll[n] = *p;
185         next_poll.used = n + 1;
186         update_thread();
187  out:
188         sigio_unlock();
189         return err;
190 }
191
192 int ignore_sigio_fd(int fd)
193 {
194         struct pollfd *p;
195         int err = 0, i, n = 0;
196
197         /* This is called from exitcalls elsewhere in UML - if
198          * sigio_cleanup has already run, then update_thread will hang
199          * or fail because the thread is no longer running.
200          */
201         if(write_sigio_pid == -1)
202                 return -EIO;
203
204         sigio_lock();
205         for(i = 0; i < current_poll.used; i++){
206                 if(current_poll.poll[i].fd == fd) break;
207         }
208         if(i == current_poll.used)
209                 goto out;
210
211         err = need_poll(&next_poll, current_poll.used - 1);
212         if(err)
213                 goto out;
214
215         for(i = 0; i < current_poll.used; i++){
216                 p = &current_poll.poll[i];
217                 if(p->fd != fd)
218                         next_poll.poll[n++] = *p;
219         }
220         next_poll.used = current_poll.used - 1;
221
222         update_thread();
223  out:
224         sigio_unlock();
225         return err;
226 }
227
228 static struct pollfd *setup_initial_poll(int fd)
229 {
230         struct pollfd *p;
231
232         p = um_kmalloc(sizeof(struct pollfd));
233         if (p == NULL) {
234                 printk("setup_initial_poll : failed to allocate poll\n");
235                 return NULL;
236         }
237         *p = ((struct pollfd) { .fd             = fd,
238                                 .events         = POLLIN,
239                                 .revents        = 0 });
240         return p;
241 }
242
243 static void write_sigio_workaround(void)
244 {
245         unsigned long stack;
246         struct pollfd *p;
247         int err;
248         int l_write_sigio_fds[2];
249         int l_sigio_private[2];
250         int l_write_sigio_pid;
251
252         /* We call this *tons* of times - and most ones we must just fail. */
253         sigio_lock();
254         l_write_sigio_pid = write_sigio_pid;
255         sigio_unlock();
256
257         if (l_write_sigio_pid != -1)
258                 return;
259
260         err = os_pipe(l_write_sigio_fds, 1, 1);
261         if(err < 0){
262                 printk("write_sigio_workaround - os_pipe 1 failed, "
263                        "err = %d\n", -err);
264                 return;
265         }
266         err = os_pipe(l_sigio_private, 1, 1);
267         if(err < 0){
268                 printk("write_sigio_workaround - os_pipe 2 failed, "
269                        "err = %d\n", -err);
270                 goto out_close1;
271         }
272
273         p = setup_initial_poll(l_sigio_private[1]);
274         if(!p)
275                 goto out_close2;
276
277         sigio_lock();
278
279         /* Did we race? Don't try to optimize this, please, it's not so likely
280          * to happen, and no more than once at the boot. */
281         if(write_sigio_pid != -1)
282                 goto out_free;
283
284         current_poll = ((struct pollfds) { .poll        = p,
285                                            .used        = 1,
286                                            .size        = 1 });
287
288         if (write_sigio_irq(l_write_sigio_fds[0]))
289                 goto out_clear_poll;
290
291         memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
292         memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
293
294         write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
295                                             CLONE_FILES | CLONE_VM, &stack, 0);
296
297         if (write_sigio_pid < 0)
298                 goto out_clear;
299
300         sigio_unlock();
301         return;
302
303 out_clear:
304         write_sigio_pid = -1;
305         write_sigio_fds[0] = -1;
306         write_sigio_fds[1] = -1;
307         sigio_private[0] = -1;
308         sigio_private[1] = -1;
309 out_clear_poll:
310         current_poll = ((struct pollfds) { .poll        = NULL,
311                                            .size        = 0,
312                                            .used        = 0 });
313 out_free:
314         sigio_unlock();
315         kfree(p);
316 out_close2:
317         close(l_sigio_private[0]);
318         close(l_sigio_private[1]);
319 out_close1:
320         close(l_write_sigio_fds[0]);
321         close(l_write_sigio_fds[1]);
322 }
323
324 /* Changed during early boot */
325 static int pty_output_sigio = 0;
326 static int pty_close_sigio = 0;
327
328 void maybe_sigio_broken(int fd, int read)
329 {
330         int err;
331
332         if(!isatty(fd))
333                 return;
334
335         if((read || pty_output_sigio) && (!read || pty_close_sigio))
336                 return;
337
338         write_sigio_workaround();
339
340         sigio_lock();
341         err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
342         if(err){
343                 printk("maybe_sigio_broken - failed to add pollfd for "
344                        "descriptor %d\n", fd);
345                 goto out;
346         }
347
348         all_sigio_fds.poll[all_sigio_fds.used++] =
349                 ((struct pollfd) { .fd          = fd,
350                                    .events      = read ? POLLIN : POLLOUT,
351                                    .revents     = 0 });
352 out:
353         sigio_unlock();
354 }
355
356 static void sigio_cleanup(void)
357 {
358         if(write_sigio_pid != -1){
359                 os_kill_process(write_sigio_pid, 1);
360                 write_sigio_pid = -1;
361         }
362 }
363
364 __uml_exitcall(sigio_cleanup);
365
366 /* Used as a flag during SIGIO testing early in boot */
367 static volatile int got_sigio = 0;
368
369 static void __init handler(int sig)
370 {
371         got_sigio = 1;
372 }
373
374 struct openpty_arg {
375         int master;
376         int slave;
377         int err;
378 };
379
380 static void openpty_cb(void *arg)
381 {
382         struct openpty_arg *info = arg;
383
384         info->err = 0;
385         if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
386                 info->err = -errno;
387 }
388
389 static int async_pty(int master, int slave)
390 {
391         int flags;
392
393         flags = fcntl(master, F_GETFL);
394         if(flags < 0)
395                 return -errno;
396
397         if((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
398            (fcntl(master, F_SETOWN, os_getpid()) < 0))
399                 return -errno;
400
401         if((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
402                 return -errno;
403
404         return(0);
405 }
406
407 static void __init check_one_sigio(void (*proc)(int, int))
408 {
409         struct sigaction old, new;
410         struct openpty_arg pty = { .master = -1, .slave = -1 };
411         int master, slave, err;
412
413         initial_thread_cb(openpty_cb, &pty);
414         if(pty.err){
415                 printk("openpty failed, errno = %d\n", -pty.err);
416                 return;
417         }
418
419         master = pty.master;
420         slave = pty.slave;
421
422         if((master == -1) || (slave == -1)){
423                 printk("openpty failed to allocate a pty\n");
424                 return;
425         }
426
427         /* Not now, but complain so we now where we failed. */
428         err = raw(master);
429         if (err < 0)
430                 panic("check_sigio : __raw failed, errno = %d\n", -err);
431
432         err = async_pty(master, slave);
433         if(err < 0)
434                 panic("tty_fds : sigio_async failed, err = %d\n", -err);
435
436         if(sigaction(SIGIO, NULL, &old) < 0)
437                 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
438         new = old;
439         new.sa_handler = handler;
440         if(sigaction(SIGIO, &new, NULL) < 0)
441                 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
442
443         got_sigio = 0;
444         (*proc)(master, slave);
445
446         close(master);
447         close(slave);
448
449         if(sigaction(SIGIO, &old, NULL) < 0)
450                 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
451 }
452
453 static void tty_output(int master, int slave)
454 {
455         int n;
456         char buf[512];
457
458         printk("Checking that host ptys support output SIGIO...");
459
460         memset(buf, 0, sizeof(buf));
461
462         while(os_write_file(master, buf, sizeof(buf)) > 0) ;
463         if(errno != EAGAIN)
464                 panic("tty_output : write failed, errno = %d\n", errno);
465         while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
466
467         if(got_sigio){
468                 printk("Yes\n");
469                 pty_output_sigio = 1;
470         }
471         else if(n == -EAGAIN)
472                 printk("No, enabling workaround\n");
473         else panic("tty_output : read failed, err = %d\n", n);
474 }
475
476 static void tty_close(int master, int slave)
477 {
478         printk("Checking that host ptys support SIGIO on close...");
479
480         close(slave);
481         if(got_sigio){
482                 printk("Yes\n");
483                 pty_close_sigio = 1;
484         }
485         else printk("No, enabling workaround\n");
486 }
487
488 void __init check_sigio(void)
489 {
490         if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
491            (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
492                 printk("No pseudo-terminals available - skipping pty SIGIO "
493                        "check\n");
494                 return;
495         }
496         check_one_sigio(tty_output);
497         check_one_sigio(tty_close);
498 }
499
500 /* Here because it only does the SIGIO testing for now */
501 void __init os_check_bugs(void)
502 {
503         check_sigio();
504 }