MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / um / kernel / sigio_user.c
blob82d19b451d5eaedea9420cfc67ac35f0f8efcc70
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <termios.h>
9 #include <pty.h>
10 #include <signal.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <sched.h>
14 #include <sys/socket.h>
15 #include <sys/poll.h>
16 #include "init.h"
17 #include "user.h"
18 #include "kern_util.h"
19 #include "user_util.h"
20 #include "sigio.h"
21 #include "helper.h"
22 #include "os.h"
24 /* Changed during early boot */
25 int pty_output_sigio = 0;
26 int pty_close_sigio = 0;
28 /* Used as a flag during SIGIO testing early in boot */
29 static volatile int got_sigio = 0;
31 void __init handler(int sig)
33 got_sigio = 1;
36 struct openpty_arg {
37 int master;
38 int slave;
39 int err;
42 static void openpty_cb(void *arg)
44 struct openpty_arg *info = arg;
46 info->err = 0;
47 if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
48 info->err = -errno;
51 void __init check_one_sigio(void (*proc)(int, int))
53 struct sigaction old, new;
54 struct openpty_arg pty = { .master = -1, .slave = -1 };
55 int master, slave, err;
57 initial_thread_cb(openpty_cb, &pty);
58 if(pty.err){
59 printk("openpty failed, errno = %d\n", -pty.err);
60 return;
63 master = pty.master;
64 slave = pty.slave;
66 if((master == -1) || (slave == -1)){
67 printk("openpty failed to allocate a pty\n");
68 return;
71 /* Not now, but complain so we now where we failed. */
72 err = raw(master);
73 if (err < 0)
74 panic("check_sigio : __raw failed, errno = %d\n", -err);
76 err = os_sigio_async(master, slave);
77 if(err < 0)
78 panic("tty_fds : sigio_async failed, err = %d\n", -err);
80 if(sigaction(SIGIO, NULL, &old) < 0)
81 panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
82 new = old;
83 new.sa_handler = handler;
84 if(sigaction(SIGIO, &new, NULL) < 0)
85 panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
87 got_sigio = 0;
88 (*proc)(master, slave);
90 os_close_file(master);
91 os_close_file(slave);
93 if(sigaction(SIGIO, &old, NULL) < 0)
94 panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
97 static void tty_output(int master, int slave)
99 int n;
100 char buf[512];
102 printk("Checking that host ptys support output SIGIO...");
104 memset(buf, 0, sizeof(buf));
106 while(os_write_file(master, buf, sizeof(buf)) > 0) ;
107 if(errno != EAGAIN)
108 panic("check_sigio : write failed, errno = %d\n", errno);
109 while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
111 if(got_sigio){
112 printk("Yes\n");
113 pty_output_sigio = 1;
115 else if(n == -EAGAIN) printk("No, enabling workaround\n");
116 else panic("check_sigio : read failed, err = %d\n", n);
119 static void tty_close(int master, int slave)
121 printk("Checking that host ptys support SIGIO on close...");
123 os_close_file(slave);
124 if(got_sigio){
125 printk("Yes\n");
126 pty_close_sigio = 1;
128 else printk("No, enabling workaround\n");
131 void __init check_sigio(void)
133 if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
134 (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
135 printk("No pseudo-terminals available - skipping pty SIGIO "
136 "check\n");
137 return;
139 check_one_sigio(tty_output);
140 check_one_sigio(tty_close);
143 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
144 * exitcall.
146 static int write_sigio_pid = -1;
148 /* These arrays are initialized before the sigio thread is started, and
149 * the descriptors closed after it is killed. So, it can't see them change.
150 * On the UML side, they are changed under the sigio_lock.
152 static int write_sigio_fds[2] = { -1, -1 };
153 static int sigio_private[2] = { -1, -1 };
155 struct pollfds {
156 struct pollfd *poll;
157 int size;
158 int used;
161 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
162 * synchronizes with it.
164 struct pollfds current_poll = {
165 .poll = NULL,
166 .size = 0,
167 .used = 0
170 struct pollfds next_poll = {
171 .poll = NULL,
172 .size = 0,
173 .used = 0
176 static int write_sigio_thread(void *unused)
178 struct pollfds *fds, tmp;
179 struct pollfd *p;
180 int i, n, respond_fd;
181 char c;
183 fds = &current_poll;
184 while(1){
185 n = poll(fds->poll, fds->used, -1);
186 if(n < 0){
187 if(errno == EINTR) continue;
188 printk("write_sigio_thread : poll returned %d, "
189 "errno = %d\n", n, errno);
191 for(i = 0; i < fds->used; i++){
192 p = &fds->poll[i];
193 if(p->revents == 0) continue;
194 if(p->fd == sigio_private[1]){
195 n = os_read_file(sigio_private[1], &c, sizeof(c));
196 if(n != sizeof(c))
197 printk("write_sigio_thread : "
198 "read failed, err = %d\n", -n);
199 tmp = current_poll;
200 current_poll = next_poll;
201 next_poll = tmp;
202 respond_fd = sigio_private[1];
204 else {
205 respond_fd = write_sigio_fds[1];
206 fds->used--;
207 memmove(&fds->poll[i], &fds->poll[i + 1],
208 (fds->used - i) * sizeof(*fds->poll));
211 n = os_write_file(respond_fd, &c, sizeof(c));
212 if(n != sizeof(c))
213 printk("write_sigio_thread : write failed, "
214 "err = %d\n", -n);
219 static int need_poll(int n)
221 if(n <= next_poll.size){
222 next_poll.used = n;
223 return(0);
225 if(next_poll.poll != NULL) kfree(next_poll.poll);
226 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
227 if(next_poll.poll == NULL){
228 printk("need_poll : failed to allocate new pollfds\n");
229 next_poll.size = 0;
230 next_poll.used = 0;
231 return(-1);
233 next_poll.size = n;
234 next_poll.used = n;
235 return(0);
238 static void update_thread(void)
240 unsigned long flags;
241 int n;
242 char c;
244 flags = set_signals(0);
245 n = os_write_file(sigio_private[0], &c, sizeof(c));
246 if(n != sizeof(c)){
247 printk("update_thread : write failed, err = %d\n", -n);
248 goto fail;
251 n = os_read_file(sigio_private[0], &c, sizeof(c));
252 if(n != sizeof(c)){
253 printk("update_thread : read failed, err = %d\n", -n);
254 goto fail;
257 set_signals(flags);
258 return;
259 fail:
260 sigio_lock();
261 if(write_sigio_pid != -1)
262 os_kill_process(write_sigio_pid, 1);
263 write_sigio_pid = -1;
264 os_close_file(sigio_private[0]);
265 os_close_file(sigio_private[1]);
266 os_close_file(write_sigio_fds[0]);
267 os_close_file(write_sigio_fds[1]);
268 sigio_unlock();
269 set_signals(flags);
272 int add_sigio_fd(int fd, int read)
274 int err = 0, i, n, events;
276 sigio_lock();
277 for(i = 0; i < current_poll.used; i++){
278 if(current_poll.poll[i].fd == fd)
279 goto out;
282 n = current_poll.used + 1;
283 err = need_poll(n);
284 if(err)
285 goto out;
287 for(i = 0; i < current_poll.used; i++)
288 next_poll.poll[i] = current_poll.poll[i];
290 if(read) events = POLLIN;
291 else events = POLLOUT;
293 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
294 .events = events,
295 .revents = 0 });
296 update_thread();
297 out:
298 sigio_unlock();
299 return(err);
302 int ignore_sigio_fd(int fd)
304 struct pollfd *p;
305 int err = 0, i, n = 0;
307 sigio_lock();
308 for(i = 0; i < current_poll.used; i++){
309 if(current_poll.poll[i].fd == fd) break;
311 if(i == current_poll.used)
312 goto out;
314 err = need_poll(current_poll.used - 1);
315 if(err)
316 goto out;
318 for(i = 0; i < current_poll.used; i++){
319 p = &current_poll.poll[i];
320 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
322 if(n == i){
323 printk("ignore_sigio_fd : fd %d not found\n", fd);
324 err = -1;
325 goto out;
328 update_thread();
329 out:
330 sigio_unlock();
331 return(err);
334 static int setup_initial_poll(int fd)
336 struct pollfd *p;
338 p = um_kmalloc(sizeof(struct pollfd));
339 if(p == NULL){
340 printk("setup_initial_poll : failed to allocate poll\n");
341 return(-1);
343 *p = ((struct pollfd) { .fd = fd,
344 .events = POLLIN,
345 .revents = 0 });
346 current_poll = ((struct pollfds) { .poll = p,
347 .used = 1,
348 .size = 1 });
349 return(0);
352 void write_sigio_workaround(void)
354 unsigned long stack;
355 int err;
357 sigio_lock();
358 if(write_sigio_pid != -1)
359 goto out;
361 err = os_pipe(write_sigio_fds, 1, 1);
362 if(err < 0){
363 printk("write_sigio_workaround - os_pipe 1 failed, "
364 "err = %d\n", -err);
365 goto out;
367 err = os_pipe(sigio_private, 1, 1);
368 if(err < 0){
369 printk("write_sigio_workaround - os_pipe 2 failed, "
370 "err = %d\n", -err);
371 goto out_close1;
373 if(setup_initial_poll(sigio_private[1]))
374 goto out_close2;
376 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
377 CLONE_FILES | CLONE_VM, &stack, 0);
379 if(write_sigio_pid < 0) goto out_close2;
381 if(write_sigio_irq(write_sigio_fds[0]))
382 goto out_kill;
384 out:
385 sigio_unlock();
386 return;
388 out_kill:
389 os_kill_process(write_sigio_pid, 1);
390 write_sigio_pid = -1;
391 out_close2:
392 os_close_file(sigio_private[0]);
393 os_close_file(sigio_private[1]);
394 out_close1:
395 os_close_file(write_sigio_fds[0]);
396 os_close_file(write_sigio_fds[1]);
397 sigio_unlock();
400 int read_sigio_fd(int fd)
402 int n;
403 char c;
405 n = os_read_file(fd, &c, sizeof(c));
406 if(n != sizeof(c)){
407 if(n < 0) {
408 printk("read_sigio_fd - read failed, err = %d\n", -n);
409 return(n);
411 else {
412 printk("read_sigio_fd - short read, bytes = %d\n", n);
413 return(-EIO);
416 return(n);
419 static void sigio_cleanup(void)
421 if(write_sigio_pid != -1)
422 os_kill_process(write_sigio_pid, 1);
425 __uml_exitcall(sigio_cleanup);
428 * Overrides for Emacs so that we follow Linus's tabbing style.
429 * Emacs will notice this stuff at the end of the file and automatically
430 * adjust the settings for this buffer only. This must remain at the end
431 * of the file.
432 * ---------------------------------------------------------------------------
433 * Local variables:
434 * c-file-style: "linux"
435 * End: