blob: 34bb59bfa00be5dd81882336ec81fe217485546a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "kern_util.h"
Jeff Dikefee64d32008-02-04 22:31:02 -080015#include "init.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "os.h"
Jeff Dike5d33e4d2008-05-12 14:01:58 -070017#include "process.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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jeff Dikefee64d32008-02-04 22:31:02 -080021/*
22 * Protected by sigio_lock(), also used by sigio_cleanup, which is an
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * exitcall.
24 */
25static int write_sigio_pid = -1;
Jeff Dikec4399012007-07-15 23:38:56 -070026static unsigned long write_sigio_stack;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Jeff Dikefee64d32008-02-04 22:31:02 -080028/*
29 * These arrays are initialized before the sigio thread is started, and
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * the descriptors closed after it is killed. So, it can't see them change.
31 * On the UML side, they are changed under the sigio_lock.
32 */
Jeff Dike5f4e8fd2006-03-27 01:14:40 -080033#define SIGIO_FDS_INIT {-1, -1}
34
35static int write_sigio_fds[2] = SIGIO_FDS_INIT;
36static int sigio_private[2] = SIGIO_FDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct pollfds {
39 struct pollfd *poll;
40 int size;
41 int used;
42};
43
Jeff Dikefee64d32008-02-04 22:31:02 -080044/*
45 * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * synchronizes with it.
47 */
Jeff Dike19bdf042006-09-25 23:33:04 -070048static struct pollfds current_poll;
49static struct pollfds next_poll;
50static struct pollfds all_sigio_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static int write_sigio_thread(void *unused)
53{
54 struct pollfds *fds, tmp;
55 struct pollfd *p;
56 int i, n, respond_fd;
57 char c;
58
Jeff Dikefee64d32008-02-04 22:31:02 -080059 signal(SIGWINCH, SIG_IGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 fds = &current_poll;
Jeff Dikefee64d32008-02-04 22:31:02 -080061 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 n = poll(fds->poll, fds->used, -1);
Jeff Dikefee64d32008-02-04 22:31:02 -080063 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Jeff Dikefee64d32008-02-04 22:31:02 -080069 for (i = 0; i < fds->used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 p = &fds->poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -080071 if (p->revents == 0)
72 continue;
73 if (p->fd == sigio_private[1]) {
Jeff Dikea61f3342007-05-06 14:51:35 -070074 CATCH_EINTR(n = read(sigio_private[1], &c,
75 sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080076 if (n != sizeof(c))
77 printk(UM_KERN_ERR
78 "write_sigio_thread : "
Jeff Dike19bdf042006-09-25 23:33:04 -070079 "read on socket failed, "
Jeff Dikea61f3342007-05-06 14:51:35 -070080 "err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 tmp = current_poll;
82 current_poll = next_poll;
83 next_poll = tmp;
84 respond_fd = sigio_private[1];
85 }
86 else {
87 respond_fd = write_sigio_fds[1];
88 fds->used--;
89 memmove(&fds->poll[i], &fds->poll[i + 1],
90 (fds->used - i) * sizeof(*fds->poll));
91 }
92
Jeff Dikea61f3342007-05-06 14:51:35 -070093 CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -080094 if (n != sizeof(c))
95 printk(UM_KERN_ERR "write_sigio_thread : "
96 "write on socket failed, err = %d\n",
97 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 }
Jeff Dike1b57e9c2006-01-06 00:18:49 -0800100
101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Jeff Dike19bdf042006-09-25 23:33:04 -0700104static int need_poll(struct pollfds *polls, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Jeff Dike838e56a2007-02-16 01:27:21 -0800106 struct pollfd *new;
107
Jeff Dikefee64d32008-02-04 22:31:02 -0800108 if (n <= polls->size)
Jeff Dike19bdf042006-09-25 23:33:04 -0700109 return 0;
Jeff Dike838e56a2007-02-16 01:27:21 -0800110
Jeff Dike43f5b302008-05-12 14:01:52 -0700111 new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
Jeff Dikefee64d32008-02-04 22:31:02 -0800112 if (new == NULL) {
113 printk(UM_KERN_ERR "need_poll : failed to allocate new "
114 "pollfds\n");
Jeff Dike19bdf042006-09-25 23:33:04 -0700115 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800117
118 memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
119 kfree(polls->poll);
120
121 polls->poll = new;
Jeff Dike19bdf042006-09-25 23:33:04 -0700122 polls->size = n;
Jeff Dike19bdf042006-09-25 23:33:04 -0700123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Jeff Dikefee64d32008-02-04 22:31:02 -0800126/*
127 * Must be called with sigio_lock held, because it's needed by the marked
Jeff Dike19bdf042006-09-25 23:33:04 -0700128 * critical section.
129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void update_thread(void)
131{
132 unsigned long flags;
133 int n;
134 char c;
135
136 flags = set_signals(0);
Jeff Dikefee64d32008-02-04 22:31:02 -0800137 CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
138 if (n != sizeof(c)) {
139 printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
140 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto fail;
142 }
143
Jeff Dikea61f3342007-05-06 14:51:35 -0700144 CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
Jeff Dikefee64d32008-02-04 22:31:02 -0800145 if (n != sizeof(c)) {
146 printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
147 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto fail;
149 }
150
151 set_signals(flags);
152 return;
153 fail:
154 /* Critical section start */
Jeff Dikec4399012007-07-15 23:38:56 -0700155 if (write_sigio_pid != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 os_kill_process(write_sigio_pid, 1);
Jeff Dikec4399012007-07-15 23:38:56 -0700157 free_stack(write_sigio_stack, 0);
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 write_sigio_pid = -1;
Jeff Dike8e367062006-03-27 01:14:32 -0800160 close(sigio_private[0]);
161 close(sigio_private[1]);
162 close(write_sigio_fds[0]);
163 close(write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Critical section end */
165 set_signals(flags);
166}
167
Jeff Dike19bdf042006-09-25 23:33:04 -0700168int add_sigio_fd(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Jeff Dike19bdf042006-09-25 23:33:04 -0700170 struct pollfd *p;
171 int err = 0, i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800174 for (i = 0; i < all_sigio_fds.used; i++) {
175 if (all_sigio_fds.poll[i].fd == fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700176 break;
177 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800178 if (i == all_sigio_fds.used)
Jeff Dike19bdf042006-09-25 23:33:04 -0700179 goto out;
180
181 p = &all_sigio_fds.poll[i];
182
Jeff Dikefee64d32008-02-04 22:31:02 -0800183 for (i = 0; i < current_poll.used; i++) {
184 if (current_poll.poll[i].fd == fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto out;
186 }
187
Jeff Dike838e56a2007-02-16 01:27:21 -0800188 n = current_poll.used;
189 err = need_poll(&next_poll, n + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800190 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto out;
192
Jeff Dike838e56a2007-02-16 01:27:21 -0800193 memcpy(next_poll.poll, current_poll.poll,
194 current_poll.used * sizeof(struct pollfd));
195 next_poll.poll[n] = *p;
196 next_poll.used = n + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 update_thread();
198 out:
199 sigio_unlock();
Jeff Dike19bdf042006-09-25 23:33:04 -0700200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203int ignore_sigio_fd(int fd)
204{
205 struct pollfd *p;
206 int err = 0, i, n = 0;
207
Jeff Dikefee64d32008-02-04 22:31:02 -0800208 /*
209 * This is called from exitcalls elsewhere in UML - if
Jeff Dike61232f22006-07-10 04:45:11 -0700210 * sigio_cleanup has already run, then update_thread will hang
211 * or fail because the thread is no longer running.
212 */
Jeff Dikefee64d32008-02-04 22:31:02 -0800213 if (write_sigio_pid == -1)
Jeff Dike61232f22006-07-10 04:45:11 -0700214 return -EIO;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 sigio_lock();
Jeff Dikefee64d32008-02-04 22:31:02 -0800217 for (i = 0; i < current_poll.used; i++) {
218 if (current_poll.poll[i].fd == fd)
219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Jeff Dikefee64d32008-02-04 22:31:02 -0800221 if (i == current_poll.used)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto out;
Jeff Dikef206aab2006-03-27 01:14:33 -0800223
Jeff Dike19bdf042006-09-25 23:33:04 -0700224 err = need_poll(&next_poll, current_poll.used - 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800225 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto out;
227
Jeff Dikefee64d32008-02-04 22:31:02 -0800228 for (i = 0; i < current_poll.used; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 p = &current_poll.poll[i];
Jeff Dikefee64d32008-02-04 22:31:02 -0800230 if (p->fd != fd)
Jeff Dike19bdf042006-09-25 23:33:04 -0700231 next_poll.poll[n++] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800233 next_poll.used = current_poll.used - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 update_thread();
236 out:
237 sigio_unlock();
Jeff Dike61232f22006-07-10 04:45:11 -0700238 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Jeff Dikef206aab2006-03-27 01:14:33 -0800241static struct pollfd *setup_initial_poll(int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct pollfd *p;
244
Jeff Dike43f5b302008-05-12 14:01:52 -0700245 p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800246 if (p == NULL) {
Jeff Dikefee64d32008-02-04 22:31:02 -0800247 printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
248 "poll\n");
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800249 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Jeff Dike19bdf042006-09-25 23:33:04 -0700251 *p = ((struct pollfd) { .fd = fd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .events = POLLIN,
253 .revents = 0 });
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800254 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700257static void write_sigio_workaround(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800259 struct pollfd *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 int err;
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800261 int l_write_sigio_fds[2];
262 int l_sigio_private[2];
263 int l_write_sigio_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800265 /* We call this *tons* of times - and most ones we must just fail. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 sigio_lock();
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800267 l_write_sigio_pid = write_sigio_pid;
268 sigio_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800270 if (l_write_sigio_pid != -1)
271 return;
272
273 err = os_pipe(l_write_sigio_fds, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800274 if (err < 0) {
275 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 "err = %d\n", -err);
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800277 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800279 err = os_pipe(l_sigio_private, 1, 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800280 if (err < 0) {
281 printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 "err = %d\n", -err);
283 goto out_close1;
284 }
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800285
286 p = setup_initial_poll(l_sigio_private[1]);
Jeff Dikefee64d32008-02-04 22:31:02 -0800287 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 goto out_close2;
289
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800290 sigio_lock();
291
Jeff Dikefee64d32008-02-04 22:31:02 -0800292 /*
293 * Did we race? Don't try to optimize this, please, it's not so likely
294 * to happen, and no more than once at the boot.
295 */
296 if (write_sigio_pid != -1)
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800297 goto out_free;
298
299 current_poll = ((struct pollfds) { .poll = p,
300 .used = 1,
301 .size = 1 });
302
303 if (write_sigio_irq(l_write_sigio_fds[0]))
304 goto out_clear_poll;
305
306 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
307 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800308
309 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
Jeff Dikec4399012007-07-15 23:38:56 -0700310 CLONE_FILES | CLONE_VM,
311 &write_sigio_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Paolo 'Blaisorblade' Giarrussob6a2b132006-01-18 17:42:57 -0800313 if (write_sigio_pid < 0)
314 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 sigio_unlock();
317 return;
318
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800319out_clear:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 write_sigio_pid = -1;
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800321 write_sigio_fds[0] = -1;
322 write_sigio_fds[1] = -1;
323 sigio_private[0] = -1;
324 sigio_private[1] = -1;
325out_clear_poll:
326 current_poll = ((struct pollfds) { .poll = NULL,
327 .size = 0,
328 .used = 0 });
329out_free:
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800330 sigio_unlock();
Paolo 'Blaisorblade' Giarrussoe6fb54a2006-04-10 22:53:36 -0700331 kfree(p);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800332out_close2:
Jeff Dike8e367062006-03-27 01:14:32 -0800333 close(l_sigio_private[0]);
334 close(l_sigio_private[1]);
Jeff Dike5f4e8fd2006-03-27 01:14:40 -0800335out_close1:
Jeff Dike8e367062006-03-27 01:14:32 -0800336 close(l_write_sigio_fds[0]);
337 close(l_write_sigio_fds[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700340void sigio_broken(int fd, int read)
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700341{
Jeff Dike19bdf042006-09-25 23:33:04 -0700342 int err;
343
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700344 write_sigio_workaround();
Jeff Dike19bdf042006-09-25 23:33:04 -0700345
346 sigio_lock();
347 err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
Jeff Dikefee64d32008-02-04 22:31:02 -0800348 if (err) {
349 printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
350 "for descriptor %d\n", fd);
Jeff Dike19bdf042006-09-25 23:33:04 -0700351 goto out;
Jeff Dike04a51e62007-02-28 20:13:11 -0800352 }
Jeff Dike838e56a2007-02-16 01:27:21 -0800353
Jeff Dike19bdf042006-09-25 23:33:04 -0700354 all_sigio_fds.poll[all_sigio_fds.used++] =
355 ((struct pollfd) { .fd = fd,
356 .events = read ? POLLIN : POLLOUT,
357 .revents = 0 });
358out:
359 sigio_unlock();
Jeff Dike8e64d96a2006-07-10 04:45:11 -0700360}
361
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700362/* Changed during early boot */
363static int pty_output_sigio;
364static int pty_close_sigio;
365
366void maybe_sigio_broken(int fd, int read)
367{
368 if (!isatty(fd))
369 return;
370
371 if ((read || pty_output_sigio) && (!read || pty_close_sigio))
372 return;
373
374 sigio_broken(fd, read);
375}
376
Jeff Dike29ac1c22006-07-10 04:45:12 -0700377static void sigio_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Jeff Dikec4399012007-07-15 23:38:56 -0700379 if (write_sigio_pid == -1)
380 return;
381
382 os_kill_process(write_sigio_pid, 1);
383 free_stack(write_sigio_stack, 0);
384 write_sigio_pid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
Jeff Dike29ac1c22006-07-10 04:45:12 -0700386
387__uml_exitcall(sigio_cleanup);
Jeff Dikec65badb2007-05-06 14:51:06 -0700388
389/* Used as a flag during SIGIO testing early in boot */
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700390static int got_sigio;
Jeff Dikec65badb2007-05-06 14:51:06 -0700391
392static void __init handler(int sig)
393{
394 got_sigio = 1;
395}
396
397struct openpty_arg {
398 int master;
399 int slave;
400 int err;
401};
402
403static void openpty_cb(void *arg)
404{
405 struct openpty_arg *info = arg;
406
407 info->err = 0;
Jeff Dikefee64d32008-02-04 22:31:02 -0800408 if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
Jeff Dikec65badb2007-05-06 14:51:06 -0700409 info->err = -errno;
410}
411
412static int async_pty(int master, int slave)
413{
414 int flags;
415
416 flags = fcntl(master, F_GETFL);
Jeff Dikefee64d32008-02-04 22:31:02 -0800417 if (flags < 0)
Jeff Dikec65badb2007-05-06 14:51:06 -0700418 return -errno;
419
Jeff Dikefee64d32008-02-04 22:31:02 -0800420 if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
421 (fcntl(master, F_SETOWN, os_getpid()) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700422 return -errno;
423
Jeff Dikefee64d32008-02-04 22:31:02 -0800424 if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
Jeff Dikec65badb2007-05-06 14:51:06 -0700425 return -errno;
426
WANG Congc0a92902008-02-04 22:30:41 -0800427 return 0;
Jeff Dikec65badb2007-05-06 14:51:06 -0700428}
429
430static void __init check_one_sigio(void (*proc)(int, int))
431{
432 struct sigaction old, new;
433 struct openpty_arg pty = { .master = -1, .slave = -1 };
434 int master, slave, err;
435
436 initial_thread_cb(openpty_cb, &pty);
Jeff Dikefee64d32008-02-04 22:31:02 -0800437 if (pty.err) {
438 printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
439 -pty.err);
Jeff Dikec65badb2007-05-06 14:51:06 -0700440 return;
441 }
442
443 master = pty.master;
444 slave = pty.slave;
445
Jeff Dikefee64d32008-02-04 22:31:02 -0800446 if ((master == -1) || (slave == -1)) {
447 printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
448 "pty\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700449 return;
450 }
451
452 /* Not now, but complain so we now where we failed. */
453 err = raw(master);
Jeff Dikefee64d32008-02-04 22:31:02 -0800454 if (err < 0) {
455 printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
456 -err);
457 return;
458 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700459
460 err = async_pty(master, slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800461 if (err < 0) {
462 printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
463 "err = %d\n", -err);
464 return;
465 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700466
Jeff Dikefee64d32008-02-04 22:31:02 -0800467 if (sigaction(SIGIO, NULL, &old) < 0) {
468 printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
469 "errno = %d\n", errno);
470 return;
471 }
472
Jeff Dikec65badb2007-05-06 14:51:06 -0700473 new = old;
474 new.sa_handler = handler;
Jeff Dikefee64d32008-02-04 22:31:02 -0800475 if (sigaction(SIGIO, &new, NULL) < 0) {
476 printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
477 "errno = %d\n", errno);
478 return;
479 }
Jeff Dikec65badb2007-05-06 14:51:06 -0700480
481 got_sigio = 0;
482 (*proc)(master, slave);
483
484 close(master);
485 close(slave);
486
Jeff Dikefee64d32008-02-04 22:31:02 -0800487 if (sigaction(SIGIO, &old, NULL) < 0)
488 printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
489 "errno = %d\n", errno);
Jeff Dikec65badb2007-05-06 14:51:06 -0700490}
491
492static void tty_output(int master, int slave)
493{
494 int n;
495 char buf[512];
496
Jeff Dikefee64d32008-02-04 22:31:02 -0800497 printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700498
499 memset(buf, 0, sizeof(buf));
500
Jeff Dikefee64d32008-02-04 22:31:02 -0800501 while (write(master, buf, sizeof(buf)) > 0) ;
502 if (errno != EAGAIN)
503 printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
504 errno);
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700505 while (((n = read(slave, buf, sizeof(buf))) > 0) &&
506 !({ barrier(); got_sigio; }))
Jeff Dikefee64d32008-02-04 22:31:02 -0800507 ;
Jeff Dikec65badb2007-05-06 14:51:06 -0700508
Jeff Dikefee64d32008-02-04 22:31:02 -0800509 if (got_sigio) {
510 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700511 pty_output_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800512 } else if (n == -EAGAIN)
513 printk(UM_KERN_CONT "No, enabling workaround\n");
514 else
515 printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
Jeff Dikec65badb2007-05-06 14:51:06 -0700516}
517
518static void tty_close(int master, int slave)
519{
Jeff Dikefee64d32008-02-04 22:31:02 -0800520 printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
521 "close...");
Jeff Dikec65badb2007-05-06 14:51:06 -0700522
523 close(slave);
Jeff Dikefee64d32008-02-04 22:31:02 -0800524 if (got_sigio) {
525 printk(UM_KERN_CONT "Yes\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700526 pty_close_sigio = 1;
Jeff Dikefee64d32008-02-04 22:31:02 -0800527 } else
528 printk(UM_KERN_CONT "No, enabling workaround\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700529}
530
WANG Cong99764fa2008-07-23 21:28:49 -0700531static void __init check_sigio(void)
Jeff Dikec65badb2007-05-06 14:51:06 -0700532{
Jeff Dikefee64d32008-02-04 22:31:02 -0800533 if ((access("/dev/ptmx", R_OK) < 0) &&
534 (access("/dev/ptyp0", R_OK) < 0)) {
535 printk(UM_KERN_WARNING "No pseudo-terminals available - "
536 "skipping pty SIGIO check\n");
Jeff Dikec65badb2007-05-06 14:51:06 -0700537 return;
538 }
539 check_one_sigio(tty_output);
540 check_one_sigio(tty_close);
541}
542
543/* Here because it only does the SIGIO testing for now */
544void __init os_check_bugs(void)
545{
546 check_sigio();
547}