blob: abf47a7c4abd795c039f4c98136163819adb92a4 [file] [log] [blame]
Jeff Dike8e367062006-03-27 01:14:32 -08001/*
Jeff Dikefee64d32008-02-04 22:31:02 -08002 * Copyright (C) 2002 - 2007 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 Dikefee64d32008-02-04 22:31:02 -080018#include "sigio.h"
Paolo 'Blaisorblade' Giarrussoc13e5692006-10-19 23:28:20 -070019#include "um_malloc.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080020#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Jeff Dikefee64d32008-02-04 22:31:02 -080022/*
23 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * exitcall.
25 */
26static int write_sigio_pid = -1;
Jeff Dikec4399012007-07-15 23:38:56 -070027static unsigned long write_sigio_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Jeff Dikefee64d32008-02-04 22:31:02 -080029/*
30 * These arrays are initialized before the sigio thread is started, and
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * the descriptors closed after it is killed. So, it can't see them change.
32 * On the UML side, they are changed under the sigio_lock.
33 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080034#define SIGIO_FDS_INIT {-1, -1}
35
36static int write_sigio_fds[2] = SIGIO_FDS_INIT;
37static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct pollfds {
40 struct pollfd *poll;
41 int size;
42 int used;
43};
44
Jeff Dikefee64d32008-02-04 22:31:02 -080045/*
46 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * synchronizes with it.
48 */
Jeff Dike19bdf042006-09-25 23:33:04 -070049static struct pollfds current_poll;
50static struct pollfds next_poll;
51static struct pollfds all_sigio_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static int write_sigio_thread(void *unused)
54{
55 struct pollfds *fds, tmp;
56 struct pollfd *p;
57 int i, n, respond_fd;
58 char c;
59
Jeff Dikefee64d32008-02-04 22:31:02 -080060 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 fds = &current_poll;
Jeff Dikefee64d32008-02-04 22:31:02 -080062 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 n = poll(fds->poll, fds->used, -1);
Jeff Dikefee64d32008-02-04 22:31:02 -080064 if (n < 0) {
65 if (errno == EINTR)
66 continue;
67 printk(UM_KERN_ERR "write_sigio_thread : poll returned "
68 "%d, errno = %d\n", n, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Jeff Dikefee64d32008-02-04 22:31:02 -080070 for (i = 0; i < fds->used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 p = &fds->poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -080072 if (p->revents == 0)
73 continue;
74 if (p->fd == sigio_private[1]) {
Jeff Dikea61f3342007-05-06 14:51:35 -070075 CATCH_EINTR(n = read(sigio_private[1], &c,
76 sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080077 if (n != sizeof(c))
78 printk(UM_KERN_ERR
79 "write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070080 "read on socket failed, "
Jeff Dikea61f3342007-05-06 14:51:35 -070081 "err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 tmp = current_poll;
83 current_poll = next_poll;
84 next_poll = tmp;
85 respond_fd = sigio_private[1];
86 }
87 else {
88 respond_fd = write_sigio_fds[1];
89 fds->used--;
90 memmove(&fds->poll[i], &fds->poll[i + 1],
91 (fds->used - i) * sizeof(*fds->poll));
92 }
93
Jeff Dikea61f3342007-05-06 14:51:35 -070094 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080095 if (n != sizeof(c))
96 printk(UM_KERN_ERR "write_sigio_thread : "
97 "write on socket failed, err = %d\n",
98 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -0800101
102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Jeff Dike19bdf042006-09-25 23:33:04 -0700105static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jeff Dike838e56a2007-02-16 01:27:21 -0800107 struct pollfd *new;
108
Jeff Dikefee64d32008-02-04 22:31:02 -0800109 if (n <= polls->size)
Jeff Dike19bdf042006-09-25 23:33:04 -0700110 return 0;
Jeff Dike838e56a2007-02-16 01:27:21 -0800111
Jeff Dikee4c4bf992007-07-15 23:38:56 -0700112 new = kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
Jeff Dikefee64d32008-02-04 22:31:02 -0800113 if (new == NULL) {
114 printk(UM_KERN_ERR "need_poll : failed to allocate new "
115 "pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700116 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800118
119 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
120 kfree(polls->poll);
121
122 polls->poll = new;
Jeff Dike19bdf042006-09-25 23:33:04 -0700123 polls->size = n;
Jeff Dike19bdf042006-09-25 23:33:04 -0700124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Jeff Dikefee64d32008-02-04 22:31:02 -0800127/*
128 * Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700129 * critical section.
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static void update_thread(void)
132{
133 unsigned long flags;
134 int n;
135 char c;
136
137 flags = set_signals(0);
Jeff Dikefee64d32008-02-04 22:31:02 -0800138 CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
139 if (n != sizeof(c)) {
140 printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
141 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 goto fail;
143 }
144
Jeff Dikea61f3342007-05-06 14:51:35 -0700145 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -0800146 if (n != sizeof(c)) {
147 printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
148 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto fail;
150 }
151
152 set_signals(flags);
153 return;
154 fail:
155 /* Critical section start */
Jeff Dikec4399012007-07-15 23:38:56 -0700156 if (write_sigio_pid != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 os_kill_process(write_sigio_pid, 1);
Jeff Dikec4399012007-07-15 23:38:56 -0700158 free_stack(write_sigio_stack, 0);
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800161 close(sigio_private[0]);
162 close(sigio_private[1]);
163 close(write_sigio_fds[0]);
164 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* Critical section end */
166 set_signals(flags);
167}
168
Jeff Dike19bdf042006-09-25 23:33:04 -0700169int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Jeff Dike19bdf042006-09-25 23:33:04 -0700171 struct pollfd *p;
172 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800175 for (i = 0; i < all_sigio_fds.used; i++) {
176 if (all_sigio_fds.poll[i].fd == fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700177 break;
178 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800179 if (i == all_sigio_fds.used)
Jeff Dike19bdf042006-09-25 23:33:04 -0700180 goto out;
181
182 p = &all_sigio_fds.poll[i];
183
Jeff Dikefee64d32008-02-04 22:31:02 -0800184 for (i = 0; i < current_poll.used; i++) {
185 if (current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto out;
187 }
188
Jeff Dike838e56a2007-02-16 01:27:21 -0800189 n = current_poll.used;
190 err = need_poll(&next_poll, n + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800191 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto out;
193
Jeff Dike838e56a2007-02-16 01:27:21 -0800194 memcpy(next_poll.poll, current_poll.poll,
195 current_poll.used * sizeof(struct pollfd));
196 next_poll.poll[n] = *p;
197 next_poll.used = n + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 update_thread();
199 out:
200 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204int ignore_sigio_fd(int fd)
205{
206 struct pollfd *p;
207 int err = 0, i, n = 0;
208
Jeff Dikefee64d32008-02-04 22:31:02 -0800209 /*
210 * This is called from exitcalls elsewhere in UML - if
Jeff Dike61232f22006-07-10 04:45:11 -0700211 * sigio_cleanup has already run, then update_thread will hang
212 * or fail because the thread is no longer running.
213 */
Jeff Dikefee64d32008-02-04 22:31:02 -0800214 if (write_sigio_pid == -1)
Jeff Dike61232f22006-07-10 04:45:11 -0700215 return -EIO;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800218 for (i = 0; i < current_poll.used; i++) {
219 if (current_poll.poll[i].fd == fd)
220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800222 if (i == current_poll.used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800224
Jeff Dike19bdf042006-09-25 23:33:04 -0700225 err = need_poll(&next_poll, current_poll.used - 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800226 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto out;
228
Jeff Dikefee64d32008-02-04 22:31:02 -0800229 for (i = 0; i < current_poll.used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 p = &current_poll.poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -0800231 if (p->fd != fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700232 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800234 next_poll.used = current_poll.used - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 update_thread();
237 out:
238 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700239 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jeff Dikef206aab2006-03-27 01:14:33 -0800242static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct pollfd *p;
245
Jeff Dikee4c4bf992007-07-15 23:38:56 -0700246 p = kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800247 if (p == NULL) {
Jeff Dikefee64d32008-02-04 22:31:02 -0800248 printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
249 "poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800250 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700252 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .events = POLLIN,
254 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800255 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700258static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800260 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800262 int l_write_sigio_fds[2];
263 int l_sigio_private[2];
264 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800266 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800268 l_write_sigio_pid = write_sigio_pid;
269 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800271 if (l_write_sigio_pid != -1)
272 return;
273
274 err = os_pipe(l_write_sigio_fds, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800275 if (err < 0) {
276 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800280 err = os_pipe(l_sigio_private, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800281 if (err < 0) {
282 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 "err = %d\n", -err);
284 goto out_close1;
285 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800286
287 p = setup_initial_poll(l_sigio_private[1]);
Jeff Dikefee64d32008-02-04 22:31:02 -0800288 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 goto out_close2;
290
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800291 sigio_lock();
292
Jeff Dikefee64d32008-02-04 22:31:02 -0800293 /*
294 * Did we race? Don't try to optimize this, please, it's not so likely
295 * to happen, and no more than once at the boot.
296 */
297 if (write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800298 goto out_free;
299
300 current_poll = ((struct pollfds) { .poll = p,
301 .used = 1,
302 .size = 1 });
303
304 if (write_sigio_irq(l_write_sigio_fds[0]))
305 goto out_clear_poll;
306
307 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
308 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800309
310 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Jeff Dikec4399012007-07-15 23:38:56 -0700311 CLONE_FILES | CLONE_VM,
312 &write_sigio_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800314 if (write_sigio_pid < 0)
315 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 sigio_unlock();
318 return;
319
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800320out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800322 write_sigio_fds[0] = -1;
323 write_sigio_fds[1] = -1;
324 sigio_private[0] = -1;
325 sigio_private[1] = -1;
326out_clear_poll:
327 current_poll = ((struct pollfds) { .poll = NULL,
328 .size = 0,
329 .used = 0 });
330out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800331 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700332 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800333out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800334 close(l_sigio_private[0]);
335 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800336out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800337 close(l_write_sigio_fds[0]);
338 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Jeff Dikec65badb2007-05-06 14:51:06 -0700341/* Changed during early boot */
342static int pty_output_sigio = 0;
343static int pty_close_sigio = 0;
344
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700345void maybe_sigio_broken(int fd, int read)
346{
Jeff Dike19bdf042006-09-25 23:33:04 -0700347 int err;
348
Jeff Dikefee64d32008-02-04 22:31:02 -0800349 if (!isatty(fd))
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700350 return;
351
Jeff Dikefee64d32008-02-04 22:31:02 -0800352 if ((read || pty_output_sigio) && (!read || pty_close_sigio))
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700353 return;
354
355 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700356
357 sigio_lock();
358 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800359 if (err) {
360 printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
361 "for descriptor %d\n", fd);
Jeff Dike19bdf042006-09-25 23:33:04 -0700362 goto out;
Jeff Dike04a51e62007-02-28 20:13:11 -0800363 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800364
Jeff Dike19bdf042006-09-25 23:33:04 -0700365 all_sigio_fds.poll[all_sigio_fds.used++] =
366 ((struct pollfd) { .fd = fd,
367 .events = read ? POLLIN : POLLOUT,
368 .revents = 0 });
369out:
370 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700371}
372
Jeff Dike29ac1c22006-07-10 04:45:12 -0700373static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jeff Dikec4399012007-07-15 23:38:56 -0700375 if (write_sigio_pid == -1)
376 return;
377
378 os_kill_process(write_sigio_pid, 1);
379 free_stack(write_sigio_stack, 0);
380 write_sigio_pid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700382
383__uml_exitcall(sigio_cleanup);
Jeff Dikec65badb2007-05-06 14:51:06 -0700384
385/* Used as a flag during SIGIO testing early in boot */
386static volatile int got_sigio = 0;
387
388static void __init handler(int sig)
389{
390 got_sigio = 1;
391}
392
393struct openpty_arg {
394 int master;
395 int slave;
396 int err;
397};
398
399static void openpty_cb(void *arg)
400{
401 struct openpty_arg *info = arg;
402
403 info->err = 0;
Jeff Dikefee64d32008-02-04 22:31:02 -0800404 if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
Jeff Dikec65badb2007-05-06 14:51:06 -0700405 info->err = -errno;
406}
407
408static int async_pty(int master, int slave)
409{
410 int flags;
411
412 flags = fcntl(master, F_GETFL);
Jeff Dikefee64d32008-02-04 22:31:02 -0800413 if (flags < 0)
Jeff Dikec65badb2007-05-06 14:51:06 -0700414 return -errno;
415
Jeff Dikefee64d32008-02-04 22:31:02 -0800416 if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
417 (fcntl(master, F_SETOWN, os_getpid()) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700418 return -errno;
419
Jeff Dikefee64d32008-02-04 22:31:02 -0800420 if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700421 return -errno;
422
WANG Congc0a92902008-02-04 22:30:41 -0800423 return 0;
Jeff Dikec65badb2007-05-06 14:51:06 -0700424}
425
426static void __init check_one_sigio(void (*proc)(int, int))
427{
428 struct sigaction old, new;
429 struct openpty_arg pty = { .master = -1, .slave = -1 };
430 int master, slave, err;
431
432 initial_thread_cb(openpty_cb, &pty);
Jeff Dikefee64d32008-02-04 22:31:02 -0800433 if (pty.err) {
434 printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
435 -pty.err);
Jeff Dikec65badb2007-05-06 14:51:06 -0700436 return;
437 }
438
439 master = pty.master;
440 slave = pty.slave;
441
Jeff Dikefee64d32008-02-04 22:31:02 -0800442 if ((master == -1) || (slave == -1)) {
443 printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
444 "pty\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700445 return;
446 }
447
448 /* Not now, but complain so we now where we failed. */
449 err = raw(master);
Jeff Dikefee64d32008-02-04 22:31:02 -0800450 if (err < 0) {
451 printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
452 -err);
453 return;
454 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700455
456 err = async_pty(master, slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800457 if (err < 0) {
458 printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
459 "err = %d\n", -err);
460 return;
461 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700462
Jeff Dikefee64d32008-02-04 22:31:02 -0800463 if (sigaction(SIGIO, NULL, &old) < 0) {
464 printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
465 "errno = %d\n", errno);
466 return;
467 }
468
Jeff Dikec65badb2007-05-06 14:51:06 -0700469 new = old;
470 new.sa_handler = handler;
Jeff Dikefee64d32008-02-04 22:31:02 -0800471 if (sigaction(SIGIO, &new, NULL) < 0) {
472 printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
473 "errno = %d\n", errno);
474 return;
475 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700476
477 got_sigio = 0;
478 (*proc)(master, slave);
479
480 close(master);
481 close(slave);
482
Jeff Dikefee64d32008-02-04 22:31:02 -0800483 if (sigaction(SIGIO, &old, NULL) < 0)
484 printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
485 "errno = %d\n", errno);
Jeff Dikec65badb2007-05-06 14:51:06 -0700486}
487
488static void tty_output(int master, int slave)
489{
490 int n;
491 char buf[512];
492
Jeff Dikefee64d32008-02-04 22:31:02 -0800493 printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700494
495 memset(buf, 0, sizeof(buf));
496
Jeff Dikefee64d32008-02-04 22:31:02 -0800497 while (write(master, buf, sizeof(buf)) > 0) ;
498 if (errno != EAGAIN)
499 printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
500 errno);
501 while (((n = read(slave, buf, sizeof(buf))) > 0) && !got_sigio)
502 ;
Jeff Dikec65badb2007-05-06 14:51:06 -0700503
Jeff Dikefee64d32008-02-04 22:31:02 -0800504 if (got_sigio) {
505 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700506 pty_output_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800507 } else if (n == -EAGAIN)
508 printk(UM_KERN_CONT "No, enabling workaround\n");
509 else
510 printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
Jeff Dikec65badb2007-05-06 14:51:06 -0700511}
512
513static void tty_close(int master, int slave)
514{
Jeff Dikefee64d32008-02-04 22:31:02 -0800515 printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
516 "close...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700517
518 close(slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800519 if (got_sigio) {
520 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700521 pty_close_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800522 } else
523 printk(UM_KERN_CONT "No, enabling workaround\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700524}
525
526void __init check_sigio(void)
527{
Jeff Dikefee64d32008-02-04 22:31:02 -0800528 if ((access("/dev/ptmx", R_OK) < 0) &&
529 (access("/dev/ptyp0", R_OK) < 0)) {
530 printk(UM_KERN_WARNING "No pseudo-terminals available - "
531 "skipping pty SIGIO check\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700532 return;
533 }
534 check_one_sigio(tty_output);
535 check_one_sigio(tty_close);
536}
537
538/* Here because it only does the SIGIO testing for now */
539void __init os_check_bugs(void)
540{
541 check_sigio();
542}