blob: 925a65240cfec96c0f6c424e6009bf24a2012f90 [file] [log] [blame]
Jeff Dike8e367062006-03-27 01:14:32 -08001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 "user_util.h"
20#include "sigio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "os.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070022#include "um_malloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jeff Dikef206aab2006-03-27 01:14:33 -080024/* Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * exitcall.
26 */
27static int write_sigio_pid = -1;
28
29/* These arrays are initialized before the sigio thread is started, and
30 * the descriptors closed after it is killed. So, it can't see them change.
31 * On the UML side, they are changed under the sigio_lock.
32 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080033#define SIGIO_FDS_INIT {-1, -1}
34
35static int write_sigio_fds[2] = SIGIO_FDS_INIT;
36static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct pollfds {
39 struct pollfd *poll;
40 int size;
41 int used;
42};
43
44/* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
45 * synchronizes with it.
46 */
Jeff Dike19bdf042006-09-25 23:33:04 -070047static struct pollfds current_poll;
48static struct pollfds next_poll;
49static struct pollfds all_sigio_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static int write_sigio_thread(void *unused)
52{
53 struct pollfds *fds, tmp;
54 struct pollfd *p;
55 int i, n, respond_fd;
56 char c;
57
Jeff Dikecd2ee4a2005-05-05 16:15:32 -070058 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 fds = &current_poll;
60 while(1){
61 n = poll(fds->poll, fds->used, -1);
62 if(n < 0){
63 if(errno == EINTR) continue;
64 printk("write_sigio_thread : poll returned %d, "
65 "errno = %d\n", n, errno);
66 }
67 for(i = 0; i < fds->used; i++){
68 p = &fds->poll[i];
69 if(p->revents == 0) continue;
70 if(p->fd == sigio_private[1]){
71 n = os_read_file(sigio_private[1], &c, sizeof(c));
72 if(n != sizeof(c))
73 printk("write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070074 "read on socket failed, "
75 "err = %d\n", -n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 tmp = current_poll;
77 current_poll = next_poll;
78 next_poll = tmp;
79 respond_fd = sigio_private[1];
80 }
81 else {
82 respond_fd = write_sigio_fds[1];
83 fds->used--;
84 memmove(&fds->poll[i], &fds->poll[i + 1],
85 (fds->used - i) * sizeof(*fds->poll));
86 }
87
88 n = os_write_file(respond_fd, &c, sizeof(c));
89 if(n != sizeof(c))
Jeff Dike19bdf042006-09-25 23:33:04 -070090 printk("write_sigio_thread : write on socket "
91 "failed, err = %d\n", -n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -080094
95 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Jeff Dike19bdf042006-09-25 23:33:04 -070098static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Jeff Dike19bdf042006-09-25 23:33:04 -0700100 if(n <= polls->size){
101 polls->used = n;
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700104 kfree(polls->poll);
105 polls->poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
106 if(polls->poll == NULL){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 printk("need_poll : failed to allocate new pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700108 polls->size = 0;
109 polls->used = 0;
110 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700112 polls->size = n;
113 polls->used = n;
114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117/* Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700118 * critical section.
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void update_thread(void)
121{
122 unsigned long flags;
123 int n;
124 char c;
125
126 flags = set_signals(0);
127 n = os_write_file(sigio_private[0], &c, sizeof(c));
128 if(n != sizeof(c)){
129 printk("update_thread : write failed, err = %d\n", -n);
130 goto fail;
131 }
132
133 n = os_read_file(sigio_private[0], &c, sizeof(c));
134 if(n != sizeof(c)){
135 printk("update_thread : read failed, err = %d\n", -n);
136 goto fail;
137 }
138
139 set_signals(flags);
140 return;
141 fail:
142 /* Critical section start */
Jeff Dikef206aab2006-03-27 01:14:33 -0800143 if(write_sigio_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 os_kill_process(write_sigio_pid, 1);
145 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800146 close(sigio_private[0]);
147 close(sigio_private[1]);
148 close(write_sigio_fds[0]);
149 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* Critical section end */
151 set_signals(flags);
152}
153
Jeff Dike19bdf042006-09-25 23:33:04 -0700154int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jeff Dike19bdf042006-09-25 23:33:04 -0700156 struct pollfd *p;
157 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 sigio_lock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700160 for(i = 0; i < all_sigio_fds.used; i++){
161 if(all_sigio_fds.poll[i].fd == fd)
162 break;
163 }
164 if(i == all_sigio_fds.used)
165 goto out;
166
167 p = &all_sigio_fds.poll[i];
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 for(i = 0; i < current_poll.used; i++){
Jeff Dikef206aab2006-03-27 01:14:33 -0800170 if(current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 goto out;
172 }
173
174 n = current_poll.used + 1;
Jeff Dike19bdf042006-09-25 23:33:04 -0700175 err = need_poll(&next_poll, n);
Jeff Dikef206aab2006-03-27 01:14:33 -0800176 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto out;
178
179 for(i = 0; i < current_poll.used; i++)
180 next_poll.poll[i] = current_poll.poll[i];
181
Jeff Dike19bdf042006-09-25 23:33:04 -0700182 next_poll.poll[n - 1] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 update_thread();
184 out:
185 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189int ignore_sigio_fd(int fd)
190{
191 struct pollfd *p;
192 int err = 0, i, n = 0;
193
Jeff Dike61232f22006-07-10 04:45:11 -0700194 /* This is called from exitcalls elsewhere in UML - if
195 * sigio_cleanup has already run, then update_thread will hang
196 * or fail because the thread is no longer running.
197 */
198 if(write_sigio_pid == -1)
199 return -EIO;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 sigio_lock();
202 for(i = 0; i < current_poll.used; i++){
203 if(current_poll.poll[i].fd == fd) break;
204 }
205 if(i == current_poll.used)
206 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800207
Jeff Dike19bdf042006-09-25 23:33:04 -0700208 err = need_poll(&next_poll, current_poll.used - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if(err)
210 goto out;
211
212 for(i = 0; i < current_poll.used; i++){
213 p = &current_poll.poll[i];
Jeff Dike19bdf042006-09-25 23:33:04 -0700214 if(p->fd != fd)
215 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 update_thread();
219 out:
220 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Jeff Dikef206aab2006-03-27 01:14:33 -0800224static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct pollfd *p;
227
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800228 p = um_kmalloc(sizeof(struct pollfd));
229 if (p == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 printk("setup_initial_poll : failed to allocate poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800231 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700233 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .events = POLLIN,
235 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800236 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700239static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 unsigned long stack;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800242 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800244 int l_write_sigio_fds[2];
245 int l_sigio_private[2];
246 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800248 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800250 l_write_sigio_pid = write_sigio_pid;
251 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800253 if (l_write_sigio_pid != -1)
254 return;
255
256 err = os_pipe(l_write_sigio_fds, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if(err < 0){
258 printk("write_sigio_workaround - os_pipe 1 failed, "
259 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800260 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800262 err = os_pipe(l_sigio_private, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if(err < 0){
Jeff Dikef206aab2006-03-27 01:14:33 -0800264 printk("write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 "err = %d\n", -err);
266 goto out_close1;
267 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800268
269 p = setup_initial_poll(l_sigio_private[1]);
270 if(!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto out_close2;
272
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800273 sigio_lock();
274
275 /* Did we race? Don't try to optimize this, please, it's not so likely
276 * to happen, and no more than once at the boot. */
277 if(write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800278 goto out_free;
279
280 current_poll = ((struct pollfds) { .poll = p,
281 .used = 1,
282 .size = 1 });
283
284 if (write_sigio_irq(l_write_sigio_fds[0]))
285 goto out_clear_poll;
286
287 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
288 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800289
290 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 CLONE_FILES | CLONE_VM, &stack, 0);
292
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800293 if (write_sigio_pid < 0)
294 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 sigio_unlock();
297 return;
298
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800299out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800301 write_sigio_fds[0] = -1;
302 write_sigio_fds[1] = -1;
303 sigio_private[0] = -1;
304 sigio_private[1] = -1;
305out_clear_poll:
306 current_poll = ((struct pollfds) { .poll = NULL,
307 .size = 0,
308 .used = 0 });
309out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800310 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700311 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800312out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800313 close(l_sigio_private[0]);
314 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800315out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800316 close(l_write_sigio_fds[0]);
317 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700320void maybe_sigio_broken(int fd, int read)
321{
Jeff Dike19bdf042006-09-25 23:33:04 -0700322 int err;
323
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700324 if(!isatty(fd))
325 return;
326
327 if((read || pty_output_sigio) && (!read || pty_close_sigio))
328 return;
329
330 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700331
332 sigio_lock();
333 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
334 if(err){
335 printk("maybe_sigio_broken - failed to add pollfd\n");
336 goto out;
337 }
338 all_sigio_fds.poll[all_sigio_fds.used++] =
339 ((struct pollfd) { .fd = fd,
340 .events = read ? POLLIN : POLLOUT,
341 .revents = 0 });
342out:
343 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700344}
345
Jeff Dike29ac1c22006-07-10 04:45:12 -0700346static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jeff Dikef206aab2006-03-27 01:14:33 -0800348 if(write_sigio_pid != -1){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 os_kill_process(write_sigio_pid, 1);
350 write_sigio_pid = -1;
351 }
352}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700353
354__uml_exitcall(sigio_cleanup);