MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / arch / um / drivers / chan_user.c
blob8a5180a779e57bb7e1d11598ee51945bb9022c3c
1 /*
2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <termios.h>
10 #include <string.h>
11 #include <signal.h>
12 #include <sys/stat.h>
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
15 #include "kern_util.h"
16 #include "user_util.h"
17 #include "chan_user.h"
18 #include "user.h"
19 #include "helper.h"
20 #include "os.h"
21 #include "choose-mode.h"
22 #include "mode.h"
24 int generic_console_write(int fd, const char *buf, int n, void *unused)
26 struct termios save, new;
27 int err;
29 if(isatty(fd)){
30 tcgetattr(fd, &save);
31 new = save;
32 new.c_oflag |= OPOST;
33 tcsetattr(fd, TCSAFLUSH, &new);
35 err = generic_write(fd, buf, n, NULL);
36 if(isatty(fd)) tcsetattr(fd, TCSAFLUSH, &save);
37 return(err);
40 static void winch_handler(int sig)
44 struct winch_data {
45 int pty_fd;
46 int pipe_fd;
47 int close_me;
50 static int winch_thread(void *arg)
52 struct winch_data *data = arg;
53 sigset_t sigs;
54 int pty_fd, pipe_fd;
55 int count, err;
56 char c = 1;
58 os_close_file(data->close_me);
59 pty_fd = data->pty_fd;
60 pipe_fd = data->pipe_fd;
61 count = os_write_file(pipe_fd, &c, sizeof(c));
62 if(count != sizeof(c))
63 printk("winch_thread : failed to write synchronization "
64 "byte, err = %d\n", -count);
66 signal(SIGWINCH, winch_handler);
67 sigfillset(&sigs);
68 sigdelset(&sigs, SIGWINCH);
69 if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
70 printk("winch_thread : sigprocmask failed, errno = %d\n",
71 errno);
72 exit(1);
75 if(setsid() < 0){
76 printk("winch_thread : setsid failed, errno = %d\n", errno);
77 exit(1);
80 err = os_new_tty_pgrp(pty_fd, os_getpid());
81 if(err < 0){
82 printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err);
83 exit(1);
86 count = os_read_file(pipe_fd, &c, sizeof(c));
87 if(count != sizeof(c))
88 printk("winch_thread : failed to read synchronization byte, "
89 "err = %d\n", -count);
91 while(1){
92 pause();
94 count = os_write_file(pipe_fd, &c, sizeof(c));
95 if(count != sizeof(c))
96 printk("winch_thread : write failed, err = %d\n",
97 -count);
101 static int winch_tramp(int fd, void *device_data, int *fd_out)
103 struct winch_data data;
104 unsigned long stack;
105 int fds[2], pid, n, err;
106 char c;
108 err = os_pipe(fds, 1, 1);
109 if(err < 0){
110 printk("winch_tramp : os_pipe failed, err = %d\n", -err);
111 return(err);
114 data = ((struct winch_data) { .pty_fd = fd,
115 .pipe_fd = fds[1],
116 .close_me = fds[0] } );
117 pid = run_helper_thread(winch_thread, &data, 0, &stack, 0);
118 if(pid < 0){
119 printk("fork of winch_thread failed - errno = %d\n", errno);
120 return(pid);
123 os_close_file(fds[1]);
124 *fd_out = fds[0];
125 n = os_read_file(fds[0], &c, sizeof(c));
126 if(n != sizeof(c)){
127 printk("winch_tramp : failed to read synchronization byte\n");
128 printk("read failed, err = %d\n", -n);
129 printk("fd %d will not support SIGWINCH\n", fd);
130 *fd_out = -1;
132 return(pid);
135 void register_winch(int fd, void *device_data)
137 int pid, thread, thread_fd;
138 int count;
139 char c = 1;
141 if(!isatty(fd))
142 return;
144 pid = tcgetpgrp(fd);
145 if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
146 device_data) && (pid == -1)){
147 thread = winch_tramp(fd, device_data, &thread_fd);
148 if(fd != -1){
149 register_winch_irq(thread_fd, fd, thread, device_data);
151 count = os_write_file(thread_fd, &c, sizeof(c));
152 if(count != sizeof(c))
153 printk("register_winch : failed to write "
154 "synchronization byte, err = %d\n",
155 -count);
161 * Overrides for Emacs so that we follow Linus's tabbing style.
162 * Emacs will notice this stuff at the end of the file and automatically
163 * adjust the settings for this buffer only. This must remain at the end
164 * of the file.
165 * ---------------------------------------------------------------------------
166 * Local variables:
167 * c-file-style: "linux"
168 * End: