blob: 46e762f926eb9def51ad29fbc8dc6ac17f7bc485 [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>
Al Viro37185b32012-10-08 03:27:32 +010014#include <kern_util.h>
15#include <init.h>
16#include <os.h>
17#include <sigio.h>
18#include <um_malloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Jeff Dikefee64d32008-02-04 22:31:02 -080020/*
21 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * exitcall.
23 */
24static int write_sigio_pid = -1;
Jeff Dikec4399012007-07-15 23:38:56 -070025static unsigned long write_sigio_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Jeff Dikefee64d32008-02-04 22:31:02 -080027/*
28 * These arrays are initialized before the sigio thread is started, and
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * the descriptors closed after it is killed. So, it can't see them change.
30 * On the UML side, they are changed under the sigio_lock.
31 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080032#define SIGIO_FDS_INIT {-1, -1}
33
34static int write_sigio_fds[2] = SIGIO_FDS_INIT;
35static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct pollfds {
38 struct pollfd *poll;
39 int size;
40 int used;
41};
42
Jeff Dikefee64d32008-02-04 22:31:02 -080043/*
44 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * 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
Richard Weinberger91d44ff2013-08-18 13:30:08 +020058 os_fix_helper_signals();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 fds = &current_poll;
Jeff Dikefee64d32008-02-04 22:31:02 -080060 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 n = poll(fds->poll, fds->used, -1);
Jeff Dikefee64d32008-02-04 22:31:02 -080062 if (n < 0) {
63 if (errno == EINTR)
64 continue;
65 printk(UM_KERN_ERR "write_sigio_thread : poll returned "
66 "%d, errno = %d\n", n, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Jeff Dikefee64d32008-02-04 22:31:02 -080068 for (i = 0; i < fds->used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 p = &fds->poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -080070 if (p->revents == 0)
71 continue;
72 if (p->fd == sigio_private[1]) {
Jeff Dikea61f3342007-05-06 14:51:35 -070073 CATCH_EINTR(n = read(sigio_private[1], &c,
74 sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080075 if (n != sizeof(c))
76 printk(UM_KERN_ERR
77 "write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070078 "read on socket failed, "
Jeff Dikea61f3342007-05-06 14:51:35 -070079 "err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 tmp = current_poll;
81 current_poll = next_poll;
82 next_poll = tmp;
83 respond_fd = sigio_private[1];
84 }
85 else {
86 respond_fd = write_sigio_fds[1];
87 fds->used--;
88 memmove(&fds->poll[i], &fds->poll[i + 1],
89 (fds->used - i) * sizeof(*fds->poll));
90 }
91
Jeff Dikea61f3342007-05-06 14:51:35 -070092 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080093 if (n != sizeof(c))
94 printk(UM_KERN_ERR "write_sigio_thread : "
95 "write on socket failed, err = %d\n",
96 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -080099
100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Jeff Dike19bdf042006-09-25 23:33:04 -0700103static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Jeff Dike838e56a2007-02-16 01:27:21 -0800105 struct pollfd *new;
106
Jeff Dikefee64d32008-02-04 22:31:02 -0800107 if (n <= polls->size)
Jeff Dike19bdf042006-09-25 23:33:04 -0700108 return 0;
Jeff Dike838e56a2007-02-16 01:27:21 -0800109
Jeff Dike43f5b302008-05-12 14:01:52 -0700110 new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
Jeff Dikefee64d32008-02-04 22:31:02 -0800111 if (new == NULL) {
112 printk(UM_KERN_ERR "need_poll : failed to allocate new "
113 "pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700114 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800116
117 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
118 kfree(polls->poll);
119
120 polls->poll = new;
Jeff Dike19bdf042006-09-25 23:33:04 -0700121 polls->size = n;
Jeff Dike19bdf042006-09-25 23:33:04 -0700122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Jeff Dikefee64d32008-02-04 22:31:02 -0800125/*
126 * Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700127 * critical section.
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static void update_thread(void)
130{
131 unsigned long flags;
132 int n;
133 char c;
134
135 flags = set_signals(0);
Jeff Dikefee64d32008-02-04 22:31:02 -0800136 CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
137 if (n != sizeof(c)) {
138 printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
139 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto fail;
141 }
142
Jeff Dikea61f3342007-05-06 14:51:35 -0700143 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -0800144 if (n != sizeof(c)) {
145 printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
146 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto fail;
148 }
149
150 set_signals(flags);
151 return;
152 fail:
153 /* Critical section start */
Jeff Dikec4399012007-07-15 23:38:56 -0700154 if (write_sigio_pid != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 os_kill_process(write_sigio_pid, 1);
Jeff Dikec4399012007-07-15 23:38:56 -0700156 free_stack(write_sigio_stack, 0);
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800159 close(sigio_private[0]);
160 close(sigio_private[1]);
161 close(write_sigio_fds[0]);
162 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* Critical section end */
164 set_signals(flags);
165}
166
Jeff Dike19bdf042006-09-25 23:33:04 -0700167int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jeff Dike19bdf042006-09-25 23:33:04 -0700169 struct pollfd *p;
170 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800173 for (i = 0; i < all_sigio_fds.used; i++) {
174 if (all_sigio_fds.poll[i].fd == fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700175 break;
176 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800177 if (i == all_sigio_fds.used)
Jeff Dike19bdf042006-09-25 23:33:04 -0700178 goto out;
179
180 p = &all_sigio_fds.poll[i];
181
Jeff Dikefee64d32008-02-04 22:31:02 -0800182 for (i = 0; i < current_poll.used; i++) {
183 if (current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
185 }
186
Jeff Dike838e56a2007-02-16 01:27:21 -0800187 n = current_poll.used;
188 err = need_poll(&next_poll, n + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800189 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 goto out;
191
Jeff Dike838e56a2007-02-16 01:27:21 -0800192 memcpy(next_poll.poll, current_poll.poll,
193 current_poll.used * sizeof(struct pollfd));
194 next_poll.poll[n] = *p;
195 next_poll.used = n + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 update_thread();
197 out:
198 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202int ignore_sigio_fd(int fd)
203{
204 struct pollfd *p;
205 int err = 0, i, n = 0;
206
Jeff Dikefee64d32008-02-04 22:31:02 -0800207 /*
208 * This is called from exitcalls elsewhere in UML - if
Jeff Dike61232f22006-07-10 04:45:11 -0700209 * sigio_cleanup has already run, then update_thread will hang
210 * or fail because the thread is no longer running.
211 */
Jeff Dikefee64d32008-02-04 22:31:02 -0800212 if (write_sigio_pid == -1)
Jeff Dike61232f22006-07-10 04:45:11 -0700213 return -EIO;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800216 for (i = 0; i < current_poll.used; i++) {
217 if (current_poll.poll[i].fd == fd)
218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800220 if (i == current_poll.used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800222
Jeff Dike19bdf042006-09-25 23:33:04 -0700223 err = need_poll(&next_poll, current_poll.used - 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800224 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 goto out;
226
Jeff Dikefee64d32008-02-04 22:31:02 -0800227 for (i = 0; i < current_poll.used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 p = &current_poll.poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -0800229 if (p->fd != fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700230 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800232 next_poll.used = current_poll.used - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 update_thread();
235 out:
236 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Jeff Dikef206aab2006-03-27 01:14:33 -0800240static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct pollfd *p;
243
Jeff Dike43f5b302008-05-12 14:01:52 -0700244 p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800245 if (p == NULL) {
Jeff Dikefee64d32008-02-04 22:31:02 -0800246 printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
247 "poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800248 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700250 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .events = POLLIN,
252 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800253 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700256static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800258 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800260 int l_write_sigio_fds[2];
261 int l_sigio_private[2];
262 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800264 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800266 l_write_sigio_pid = write_sigio_pid;
267 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800269 if (l_write_sigio_pid != -1)
270 return;
271
272 err = os_pipe(l_write_sigio_fds, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800273 if (err < 0) {
274 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800276 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800278 err = os_pipe(l_sigio_private, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800279 if (err < 0) {
280 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 "err = %d\n", -err);
282 goto out_close1;
283 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800284
285 p = setup_initial_poll(l_sigio_private[1]);
Jeff Dikefee64d32008-02-04 22:31:02 -0800286 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto out_close2;
288
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800289 sigio_lock();
290
Jeff Dikefee64d32008-02-04 22:31:02 -0800291 /*
292 * Did we race? Don't try to optimize this, please, it's not so likely
293 * to happen, and no more than once at the boot.
294 */
295 if (write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800296 goto out_free;
297
298 current_poll = ((struct pollfds) { .poll = p,
299 .used = 1,
300 .size = 1 });
301
302 if (write_sigio_irq(l_write_sigio_fds[0]))
303 goto out_clear_poll;
304
305 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
306 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800307
308 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Jeff Dikec4399012007-07-15 23:38:56 -0700309 CLONE_FILES | CLONE_VM,
310 &write_sigio_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800312 if (write_sigio_pid < 0)
313 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 sigio_unlock();
316 return;
317
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800318out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800320 write_sigio_fds[0] = -1;
321 write_sigio_fds[1] = -1;
322 sigio_private[0] = -1;
323 sigio_private[1] = -1;
324out_clear_poll:
325 current_poll = ((struct pollfds) { .poll = NULL,
326 .size = 0,
327 .used = 0 });
328out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800329 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700330 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800331out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800332 close(l_sigio_private[0]);
333 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800334out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800335 close(l_write_sigio_fds[0]);
336 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700339void sigio_broken(int fd, int read)
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700340{
Jeff Dike19bdf042006-09-25 23:33:04 -0700341 int err;
342
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700343 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700344
345 sigio_lock();
346 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800347 if (err) {
348 printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
349 "for descriptor %d\n", fd);
Jeff Dike19bdf042006-09-25 23:33:04 -0700350 goto out;
Jeff Dike04a51e62007-02-28 20:13:11 -0800351 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800352
Jeff Dike19bdf042006-09-25 23:33:04 -0700353 all_sigio_fds.poll[all_sigio_fds.used++] =
354 ((struct pollfd) { .fd = fd,
355 .events = read ? POLLIN : POLLOUT,
356 .revents = 0 });
357out:
358 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700359}
360
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700361/* Changed during early boot */
362static int pty_output_sigio;
363static int pty_close_sigio;
364
365void maybe_sigio_broken(int fd, int read)
366{
367 if (!isatty(fd))
368 return;
369
370 if ((read || pty_output_sigio) && (!read || pty_close_sigio))
371 return;
372
373 sigio_broken(fd, read);
374}
375
Jeff Dike29ac1c22006-07-10 04:45:12 -0700376static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Jeff Dikec4399012007-07-15 23:38:56 -0700378 if (write_sigio_pid == -1)
379 return;
380
381 os_kill_process(write_sigio_pid, 1);
382 free_stack(write_sigio_stack, 0);
383 write_sigio_pid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700385
386__uml_exitcall(sigio_cleanup);
Jeff Dikec65badb2007-05-06 14:51:06 -0700387
388/* Used as a flag during SIGIO testing early in boot */
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700389static int got_sigio;
Jeff Dikec65badb2007-05-06 14:51:06 -0700390
391static void __init handler(int sig)
392{
393 got_sigio = 1;
394}
395
396struct openpty_arg {
397 int master;
398 int slave;
399 int err;
400};
401
402static void openpty_cb(void *arg)
403{
404 struct openpty_arg *info = arg;
405
406 info->err = 0;
Jeff Dikefee64d32008-02-04 22:31:02 -0800407 if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
Jeff Dikec65badb2007-05-06 14:51:06 -0700408 info->err = -errno;
409}
410
411static int async_pty(int master, int slave)
412{
413 int flags;
414
415 flags = fcntl(master, F_GETFL);
Jeff Dikefee64d32008-02-04 22:31:02 -0800416 if (flags < 0)
Jeff Dikec65badb2007-05-06 14:51:06 -0700417 return -errno;
418
Jeff Dikefee64d32008-02-04 22:31:02 -0800419 if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
420 (fcntl(master, F_SETOWN, os_getpid()) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700421 return -errno;
422
Jeff Dikefee64d32008-02-04 22:31:02 -0800423 if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700424 return -errno;
425
WANG Congc0a92902008-02-04 22:30:41 -0800426 return 0;
Jeff Dikec65badb2007-05-06 14:51:06 -0700427}
428
429static void __init check_one_sigio(void (*proc)(int, int))
430{
431 struct sigaction old, new;
432 struct openpty_arg pty = { .master = -1, .slave = -1 };
433 int master, slave, err;
434
435 initial_thread_cb(openpty_cb, &pty);
Jeff Dikefee64d32008-02-04 22:31:02 -0800436 if (pty.err) {
437 printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
438 -pty.err);
Jeff Dikec65badb2007-05-06 14:51:06 -0700439 return;
440 }
441
442 master = pty.master;
443 slave = pty.slave;
444
Jeff Dikefee64d32008-02-04 22:31:02 -0800445 if ((master == -1) || (slave == -1)) {
446 printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
447 "pty\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700448 return;
449 }
450
451 /* Not now, but complain so we now where we failed. */
452 err = raw(master);
Jeff Dikefee64d32008-02-04 22:31:02 -0800453 if (err < 0) {
454 printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
455 -err);
456 return;
457 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700458
459 err = async_pty(master, slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800460 if (err < 0) {
461 printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
462 "err = %d\n", -err);
463 return;
464 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700465
Jeff Dikefee64d32008-02-04 22:31:02 -0800466 if (sigaction(SIGIO, NULL, &old) < 0) {
467 printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
468 "errno = %d\n", errno);
469 return;
470 }
471
Jeff Dikec65badb2007-05-06 14:51:06 -0700472 new = old;
473 new.sa_handler = handler;
Jeff Dikefee64d32008-02-04 22:31:02 -0800474 if (sigaction(SIGIO, &new, NULL) < 0) {
475 printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
476 "errno = %d\n", errno);
477 return;
478 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700479
480 got_sigio = 0;
481 (*proc)(master, slave);
482
483 close(master);
484 close(slave);
485
Jeff Dikefee64d32008-02-04 22:31:02 -0800486 if (sigaction(SIGIO, &old, NULL) < 0)
487 printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
488 "errno = %d\n", errno);
Jeff Dikec65badb2007-05-06 14:51:06 -0700489}
490
491static void tty_output(int master, int slave)
492{
493 int n;
494 char buf[512];
495
Jeff Dikefee64d32008-02-04 22:31:02 -0800496 printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700497
498 memset(buf, 0, sizeof(buf));
499
Jeff Dikefee64d32008-02-04 22:31:02 -0800500 while (write(master, buf, sizeof(buf)) > 0) ;
501 if (errno != EAGAIN)
502 printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
503 errno);
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700504 while (((n = read(slave, buf, sizeof(buf))) > 0) &&
505 !({ barrier(); got_sigio; }))
Jeff Dikefee64d32008-02-04 22:31:02 -0800506 ;
Jeff Dikec65badb2007-05-06 14:51:06 -0700507
Jeff Dikefee64d32008-02-04 22:31:02 -0800508 if (got_sigio) {
509 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700510 pty_output_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800511 } else if (n == -EAGAIN)
512 printk(UM_KERN_CONT "No, enabling workaround\n");
513 else
514 printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
Jeff Dikec65badb2007-05-06 14:51:06 -0700515}
516
517static void tty_close(int master, int slave)
518{
Jeff Dikefee64d32008-02-04 22:31:02 -0800519 printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
520 "close...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700521
522 close(slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800523 if (got_sigio) {
524 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700525 pty_close_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800526 } else
527 printk(UM_KERN_CONT "No, enabling workaround\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700528}
529
WANG Cong99764fa2008-07-23 21:28:49 -0700530static void __init check_sigio(void)
Jeff Dikec65badb2007-05-06 14:51:06 -0700531{
Jeff Dikefee64d32008-02-04 22:31:02 -0800532 if ((access("/dev/ptmx", R_OK) < 0) &&
533 (access("/dev/ptyp0", R_OK) < 0)) {
534 printk(UM_KERN_WARNING "No pseudo-terminals available - "
535 "skipping pty SIGIO check\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700536 return;
537 }
538 check_one_sigio(tty_output);
539 check_one_sigio(tty_close);
540}
541
542/* Here because it only does the SIGIO testing for now */
543void __init os_check_bugs(void)
544{
545 check_sigio();
546}