blob: b2e1fd8e35712504c1b8190acd2175f923ce7e5e [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 Dike838e56a2007-02-16 01:27:21 -0800100 struct pollfd *new;
101
102 if(n <= polls->size)
Jeff Dike19bdf042006-09-25 23:33:04 -0700103 return 0;
Jeff Dike838e56a2007-02-16 01:27:21 -0800104
105 new = um_kmalloc_atomic(n * sizeof(struct pollfd));
106 if(new == 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 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800110
111 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
112 kfree(polls->poll);
113
114 polls->poll = new;
Jeff Dike19bdf042006-09-25 23:33:04 -0700115 polls->size = n;
Jeff Dike19bdf042006-09-25 23:33:04 -0700116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119/* Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700120 * critical section.
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static void update_thread(void)
123{
124 unsigned long flags;
125 int n;
126 char c;
127
128 flags = set_signals(0);
129 n = os_write_file(sigio_private[0], &c, sizeof(c));
130 if(n != sizeof(c)){
131 printk("update_thread : write failed, err = %d\n", -n);
132 goto fail;
133 }
134
135 n = os_read_file(sigio_private[0], &c, sizeof(c));
136 if(n != sizeof(c)){
137 printk("update_thread : read failed, err = %d\n", -n);
138 goto fail;
139 }
140
141 set_signals(flags);
142 return;
143 fail:
144 /* Critical section start */
Jeff Dikef206aab2006-03-27 01:14:33 -0800145 if(write_sigio_pid != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 os_kill_process(write_sigio_pid, 1);
147 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800148 close(sigio_private[0]);
149 close(sigio_private[1]);
150 close(write_sigio_fds[0]);
151 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Critical section end */
153 set_signals(flags);
154}
155
Jeff Dike19bdf042006-09-25 23:33:04 -0700156int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jeff Dike19bdf042006-09-25 23:33:04 -0700158 struct pollfd *p;
159 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 sigio_lock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700162 for(i = 0; i < all_sigio_fds.used; i++){
163 if(all_sigio_fds.poll[i].fd == fd)
164 break;
165 }
166 if(i == all_sigio_fds.used)
167 goto out;
168
169 p = &all_sigio_fds.poll[i];
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 for(i = 0; i < current_poll.used; i++){
Jeff Dikef206aab2006-03-27 01:14:33 -0800172 if(current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto out;
174 }
175
Jeff Dike838e56a2007-02-16 01:27:21 -0800176 n = current_poll.used;
177 err = need_poll(&next_poll, n + 1);
Jeff Dikef206aab2006-03-27 01:14:33 -0800178 if(err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto out;
180
Jeff Dike838e56a2007-02-16 01:27:21 -0800181 memcpy(next_poll.poll, current_poll.poll,
182 current_poll.used * sizeof(struct pollfd));
183 next_poll.poll[n] = *p;
184 next_poll.used = n + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 update_thread();
186 out:
187 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191int ignore_sigio_fd(int fd)
192{
193 struct pollfd *p;
194 int err = 0, i, n = 0;
195
Jeff Dike61232f22006-07-10 04:45:11 -0700196 /* This is called from exitcalls elsewhere in UML - if
197 * sigio_cleanup has already run, then update_thread will hang
198 * or fail because the thread is no longer running.
199 */
200 if(write_sigio_pid == -1)
201 return -EIO;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 sigio_lock();
204 for(i = 0; i < current_poll.used; i++){
205 if(current_poll.poll[i].fd == fd) break;
206 }
207 if(i == current_poll.used)
208 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800209
Jeff Dike19bdf042006-09-25 23:33:04 -0700210 err = need_poll(&next_poll, current_poll.used - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if(err)
212 goto out;
213
214 for(i = 0; i < current_poll.used; i++){
215 p = &current_poll.poll[i];
Jeff Dike19bdf042006-09-25 23:33:04 -0700216 if(p->fd != fd)
217 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800219 next_poll.used = current_poll.used - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 update_thread();
222 out:
223 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700224 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Jeff Dikef206aab2006-03-27 01:14:33 -0800227static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct pollfd *p;
230
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800231 p = um_kmalloc(sizeof(struct pollfd));
232 if (p == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 printk("setup_initial_poll : failed to allocate poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800234 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700236 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .events = POLLIN,
238 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800239 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700242static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 unsigned long stack;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800245 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800247 int l_write_sigio_fds[2];
248 int l_sigio_private[2];
249 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800251 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800253 l_write_sigio_pid = write_sigio_pid;
254 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800256 if (l_write_sigio_pid != -1)
257 return;
258
259 err = os_pipe(l_write_sigio_fds, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if(err < 0){
261 printk("write_sigio_workaround - os_pipe 1 failed, "
262 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800263 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800265 err = os_pipe(l_sigio_private, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if(err < 0){
Jeff Dikef206aab2006-03-27 01:14:33 -0800267 printk("write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 "err = %d\n", -err);
269 goto out_close1;
270 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800271
272 p = setup_initial_poll(l_sigio_private[1]);
273 if(!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto out_close2;
275
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800276 sigio_lock();
277
278 /* Did we race? Don't try to optimize this, please, it's not so likely
279 * to happen, and no more than once at the boot. */
280 if(write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800281 goto out_free;
282
283 current_poll = ((struct pollfds) { .poll = p,
284 .used = 1,
285 .size = 1 });
286
287 if (write_sigio_irq(l_write_sigio_fds[0]))
288 goto out_clear_poll;
289
290 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
291 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800292
293 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 CLONE_FILES | CLONE_VM, &stack, 0);
295
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800296 if (write_sigio_pid < 0)
297 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 sigio_unlock();
300 return;
301
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800302out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800304 write_sigio_fds[0] = -1;
305 write_sigio_fds[1] = -1;
306 sigio_private[0] = -1;
307 sigio_private[1] = -1;
308out_clear_poll:
309 current_poll = ((struct pollfds) { .poll = NULL,
310 .size = 0,
311 .used = 0 });
312out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800313 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700314 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800315out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800316 close(l_sigio_private[0]);
317 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800318out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800319 close(l_write_sigio_fds[0]);
320 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700323void maybe_sigio_broken(int fd, int read)
324{
Jeff Dike19bdf042006-09-25 23:33:04 -0700325 int err;
326
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700327 if(!isatty(fd))
328 return;
329
330 if((read || pty_output_sigio) && (!read || pty_close_sigio))
331 return;
332
333 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700334
335 sigio_lock();
336 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
Jeff Dike838e56a2007-02-16 01:27:21 -0800337 if(err)
Jeff Dike19bdf042006-09-25 23:33:04 -0700338 goto out;
Jeff Dike838e56a2007-02-16 01:27:21 -0800339
Jeff Dike19bdf042006-09-25 23:33:04 -0700340 all_sigio_fds.poll[all_sigio_fds.used++] =
341 ((struct pollfd) { .fd = fd,
342 .events = read ? POLLIN : POLLOUT,
343 .revents = 0 });
344out:
345 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700346}
347
Jeff Dike29ac1c22006-07-10 04:45:12 -0700348static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Jeff Dikef206aab2006-03-27 01:14:33 -0800350 if(write_sigio_pid != -1){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 os_kill_process(write_sigio_pid, 1);
352 write_sigio_pid = -1;
353 }
354}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700355
356__uml_exitcall(sigio_cleanup);