blob: 63d299df152bb266f614d576b47e9039df531cf4 [file] [log] [blame]
Jeff Dike8e367062006-03-27 01:14:32 -08001/*
Jeff Dike5d33e4d2008-05-12 14:01:58 -07002 * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <errno.h>
Jeff Dikefee64d32008-02-04 22:31:02 -08008#include <fcntl.h>
9#include <poll.h>
10#include <pty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <sched.h>
Jeff Dikefee64d32008-02-04 22:31:02 -080012#include <signal.h>
13#include <string.h>
14#include "kern_constants.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "kern_util.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080016#include "init.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "os.h"
Jeff Dike5d33e4d2008-05-12 14:01:58 -070018#include "process.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080019#include "sigio.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070020#include "um_malloc.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080021#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jeff Dikefee64d32008-02-04 22:31:02 -080023/*
24 * 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;
Jeff Dikec4399012007-07-15 23:38:56 -070028static unsigned long write_sigio_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Jeff Dikefee64d32008-02-04 22:31:02 -080030/*
31 * These arrays are initialized before the sigio thread is started, and
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 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 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080035#define SIGIO_FDS_INIT {-1, -1}
36
37static int write_sigio_fds[2] = SIGIO_FDS_INIT;
38static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40struct pollfds {
41 struct pollfd *poll;
42 int size;
43 int used;
44};
45
Jeff Dikefee64d32008-02-04 22:31:02 -080046/*
47 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * synchronizes with it.
49 */
Jeff Dike19bdf042006-09-25 23:33:04 -070050static struct pollfds current_poll;
51static struct pollfds next_poll;
52static struct pollfds all_sigio_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
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
Jeff Dikefee64d32008-02-04 22:31:02 -080061 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 fds = &current_poll;
Jeff Dikefee64d32008-02-04 22:31:02 -080063 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 n = poll(fds->poll, fds->used, -1);
Jeff Dikefee64d32008-02-04 22:31:02 -080065 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
Jeff Dikefee64d32008-02-04 22:31:02 -080071 for (i = 0; i < fds->used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 p = &fds->poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -080073 if (p->revents == 0)
74 continue;
75 if (p->fd == sigio_private[1]) {
Jeff Dikea61f3342007-05-06 14:51:35 -070076 CATCH_EINTR(n = read(sigio_private[1], &c,
77 sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080078 if (n != sizeof(c))
79 printk(UM_KERN_ERR
80 "write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070081 "read on socket failed, "
Jeff Dikea61f3342007-05-06 14:51:35 -070082 "err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 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
Jeff Dikea61f3342007-05-06 14:51:35 -070095 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080096 if (n != sizeof(c))
97 printk(UM_KERN_ERR "write_sigio_thread : "
98 "write on socket failed, err = %d\n",
99 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -0800102
103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Jeff Dike19bdf042006-09-25 23:33:04 -0700106static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Jeff Dike838e56a2007-02-16 01:27:21 -0800108 struct pollfd *new;
109
Jeff Dikefee64d32008-02-04 22:31:02 -0800110 if (n <= polls->size)
Jeff Dike19bdf042006-09-25 23:33:04 -0700111 return 0;
Jeff Dike838e56a2007-02-16 01:27:21 -0800112
Jeff Dike43f5b302008-05-12 14:01:52 -0700113 new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
Jeff Dikefee64d32008-02-04 22:31:02 -0800114 if (new == NULL) {
115 printk(UM_KERN_ERR "need_poll : failed to allocate new "
116 "pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700117 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800119
120 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
121 kfree(polls->poll);
122
123 polls->poll = new;
Jeff Dike19bdf042006-09-25 23:33:04 -0700124 polls->size = n;
Jeff Dike19bdf042006-09-25 23:33:04 -0700125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jeff Dikefee64d32008-02-04 22:31:02 -0800128/*
129 * Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700130 * critical section.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static void update_thread(void)
133{
134 unsigned long flags;
135 int n;
136 char c;
137
138 flags = set_signals(0);
Jeff Dikefee64d32008-02-04 22:31:02 -0800139 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto fail;
144 }
145
Jeff Dikea61f3342007-05-06 14:51:35 -0700146 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -0800147 if (n != sizeof(c)) {
148 printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
149 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto fail;
151 }
152
153 set_signals(flags);
154 return;
155 fail:
156 /* Critical section start */
Jeff Dikec4399012007-07-15 23:38:56 -0700157 if (write_sigio_pid != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 os_kill_process(write_sigio_pid, 1);
Jeff Dikec4399012007-07-15 23:38:56 -0700159 free_stack(write_sigio_stack, 0);
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800162 close(sigio_private[0]);
163 close(sigio_private[1]);
164 close(write_sigio_fds[0]);
165 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* Critical section end */
167 set_signals(flags);
168}
169
Jeff Dike19bdf042006-09-25 23:33:04 -0700170int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Jeff Dike19bdf042006-09-25 23:33:04 -0700172 struct pollfd *p;
173 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800176 for (i = 0; i < all_sigio_fds.used; i++) {
177 if (all_sigio_fds.poll[i].fd == fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700178 break;
179 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800180 if (i == all_sigio_fds.used)
Jeff Dike19bdf042006-09-25 23:33:04 -0700181 goto out;
182
183 p = &all_sigio_fds.poll[i];
184
Jeff Dikefee64d32008-02-04 22:31:02 -0800185 for (i = 0; i < current_poll.used; i++) {
186 if (current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto out;
188 }
189
Jeff Dike838e56a2007-02-16 01:27:21 -0800190 n = current_poll.used;
191 err = need_poll(&next_poll, n + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800192 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto out;
194
Jeff Dike838e56a2007-02-16 01:27:21 -0800195 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 update_thread();
200 out:
201 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205int ignore_sigio_fd(int fd)
206{
207 struct pollfd *p;
208 int err = 0, i, n = 0;
209
Jeff Dikefee64d32008-02-04 22:31:02 -0800210 /*
211 * This is called from exitcalls elsewhere in UML - if
Jeff Dike61232f22006-07-10 04:45:11 -0700212 * sigio_cleanup has already run, then update_thread will hang
213 * or fail because the thread is no longer running.
214 */
Jeff Dikefee64d32008-02-04 22:31:02 -0800215 if (write_sigio_pid == -1)
Jeff Dike61232f22006-07-10 04:45:11 -0700216 return -EIO;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800219 for (i = 0; i < current_poll.used; i++) {
220 if (current_poll.poll[i].fd == fd)
221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800223 if (i == current_poll.used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800225
Jeff Dike19bdf042006-09-25 23:33:04 -0700226 err = need_poll(&next_poll, current_poll.used - 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800227 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto out;
229
Jeff Dikefee64d32008-02-04 22:31:02 -0800230 for (i = 0; i < current_poll.used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 p = &current_poll.poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -0800232 if (p->fd != fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700233 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800235 next_poll.used = current_poll.used - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 update_thread();
238 out:
239 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Jeff Dikef206aab2006-03-27 01:14:33 -0800243static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct pollfd *p;
246
Jeff Dike43f5b302008-05-12 14:01:52 -0700247 p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800248 if (p == NULL) {
Jeff Dikefee64d32008-02-04 22:31:02 -0800249 printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
250 "poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800251 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700253 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .events = POLLIN,
255 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800256 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700259static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800261 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800263 int l_write_sigio_fds[2];
264 int l_sigio_private[2];
265 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800267 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800269 l_write_sigio_pid = write_sigio_pid;
270 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800272 if (l_write_sigio_pid != -1)
273 return;
274
275 err = os_pipe(l_write_sigio_fds, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800276 if (err < 0) {
277 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800281 err = os_pipe(l_sigio_private, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800282 if (err < 0) {
283 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 "err = %d\n", -err);
285 goto out_close1;
286 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800287
288 p = setup_initial_poll(l_sigio_private[1]);
Jeff Dikefee64d32008-02-04 22:31:02 -0800289 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 goto out_close2;
291
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800292 sigio_lock();
293
Jeff Dikefee64d32008-02-04 22:31:02 -0800294 /*
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)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800299 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));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800310
311 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Jeff Dikec4399012007-07-15 23:38:56 -0700312 CLONE_FILES | CLONE_VM,
313 &write_sigio_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800315 if (write_sigio_pid < 0)
316 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 sigio_unlock();
319 return;
320
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800321out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800323 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:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800332 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700333 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800334out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800335 close(l_sigio_private[0]);
336 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800337out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800338 close(l_write_sigio_fds[0]);
339 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700342void sigio_broken(int fd, int read)
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700343{
Jeff Dike19bdf042006-09-25 23:33:04 -0700344 int err;
345
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700346 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700347
348 sigio_lock();
349 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800350 if (err) {
351 printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
352 "for descriptor %d\n", fd);
Jeff Dike19bdf042006-09-25 23:33:04 -0700353 goto out;
Jeff Dike04a51e62007-02-28 20:13:11 -0800354 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800355
Jeff Dike19bdf042006-09-25 23:33:04 -0700356 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();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700362}
363
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700364/* 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
Jeff Dike29ac1c22006-07-10 04:45:12 -0700379static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Jeff Dikec4399012007-07-15 23:38:56 -0700381 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700388
389__uml_exitcall(sigio_cleanup);
Jeff Dikec65badb2007-05-06 14:51:06 -0700390
391/* Used as a flag during SIGIO testing early in boot */
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700392static int got_sigio;
Jeff Dikec65badb2007-05-06 14:51:06 -0700393
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;
Jeff Dikefee64d32008-02-04 22:31:02 -0800410 if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
Jeff Dikec65badb2007-05-06 14:51:06 -0700411 info->err = -errno;
412}
413
414static int async_pty(int master, int slave)
415{
416 int flags;
417
418 flags = fcntl(master, F_GETFL);
Jeff Dikefee64d32008-02-04 22:31:02 -0800419 if (flags < 0)
Jeff Dikec65badb2007-05-06 14:51:06 -0700420 return -errno;
421
Jeff Dikefee64d32008-02-04 22:31:02 -0800422 if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
423 (fcntl(master, F_SETOWN, os_getpid()) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700424 return -errno;
425
Jeff Dikefee64d32008-02-04 22:31:02 -0800426 if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700427 return -errno;
428
WANG Congc0a92902008-02-04 22:30:41 -0800429 return 0;
Jeff Dikec65badb2007-05-06 14:51:06 -0700430}
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);
Jeff Dikefee64d32008-02-04 22:31:02 -0800439 if (pty.err) {
440 printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
441 -pty.err);
Jeff Dikec65badb2007-05-06 14:51:06 -0700442 return;
443 }
444
445 master = pty.master;
446 slave = pty.slave;
447
Jeff Dikefee64d32008-02-04 22:31:02 -0800448 if ((master == -1) || (slave == -1)) {
449 printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
450 "pty\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700451 return;
452 }
453
454 /* Not now, but complain so we now where we failed. */
455 err = raw(master);
Jeff Dikefee64d32008-02-04 22:31:02 -0800456 if (err < 0) {
457 printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
458 -err);
459 return;
460 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700461
462 err = async_pty(master, slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800463 if (err < 0) {
464 printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
465 "err = %d\n", -err);
466 return;
467 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700468
Jeff Dikefee64d32008-02-04 22:31:02 -0800469 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
Jeff Dikec65badb2007-05-06 14:51:06 -0700475 new = old;
476 new.sa_handler = handler;
Jeff Dikefee64d32008-02-04 22:31:02 -0800477 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 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700482
483 got_sigio = 0;
484 (*proc)(master, slave);
485
486 close(master);
487 close(slave);
488
Jeff Dikefee64d32008-02-04 22:31:02 -0800489 if (sigaction(SIGIO, &old, NULL) < 0)
490 printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
491 "errno = %d\n", errno);
Jeff Dikec65badb2007-05-06 14:51:06 -0700492}
493
494static void tty_output(int master, int slave)
495{
496 int n;
497 char buf[512];
498
Jeff Dikefee64d32008-02-04 22:31:02 -0800499 printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700500
501 memset(buf, 0, sizeof(buf));
502
Jeff Dikefee64d32008-02-04 22:31:02 -0800503 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);
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700507 while (((n = read(slave, buf, sizeof(buf))) > 0) &&
508 !({ barrier(); got_sigio; }))
Jeff Dikefee64d32008-02-04 22:31:02 -0800509 ;
Jeff Dikec65badb2007-05-06 14:51:06 -0700510
Jeff Dikefee64d32008-02-04 22:31:02 -0800511 if (got_sigio) {
512 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700513 pty_output_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800514 } 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);
Jeff Dikec65badb2007-05-06 14:51:06 -0700518}
519
520static void tty_close(int master, int slave)
521{
Jeff Dikefee64d32008-02-04 22:31:02 -0800522 printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
523 "close...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700524
525 close(slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800526 if (got_sigio) {
527 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700528 pty_close_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800529 } else
530 printk(UM_KERN_CONT "No, enabling workaround\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700531}
532
WANG Cong99764fa2008-07-23 21:28:49 -0700533static void __init check_sigio(void)
Jeff Dikec65badb2007-05-06 14:51:06 -0700534{
Jeff Dikefee64d32008-02-04 22:31:02 -0800535 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");
Jeff Dikec65badb2007-05-06 14:51:06 -0700539 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}