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