ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / drivers / chan_user.c
1 /* 
2  * Copyright (C) 2000, 2001 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <termios.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <sys/stat.h>
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include "kern_util.h"
17 #include "user_util.h"
18 #include "chan_user.h"
19 #include "user.h"
20 #include "helper.h"
21 #include "os.h"
22 #include "choose-mode.h"
23 #include "mode.h"
24
25 void generic_close(int fd, void *unused)
26 {
27         close(fd);
28 }
29
30 int generic_read(int fd, char *c_out, void *unused)
31 {
32         int n;
33
34         n = read(fd, c_out, sizeof(*c_out));
35         if(n < 0){
36                 if(errno == EAGAIN) return(0);
37                 return(-errno);
38         }
39         else if(n == 0) return(-EIO);
40         return(1);
41 }
42
43 int generic_write(int fd, const char *buf, int n, void *unused)
44 {
45         int count;
46
47         count = write(fd, buf, n);
48         if(count < 0) return(-errno);
49         return(count);
50 }
51
52 int generic_console_write(int fd, const char *buf, int n, void *unused)
53 {
54         struct termios save, new;
55         int err;
56
57         if(isatty(fd)){
58                 tcgetattr(fd, &save);
59                 new = save;
60                 new.c_oflag |= OPOST;
61                 tcsetattr(fd, TCSAFLUSH, &new);
62         }
63         err = generic_write(fd, buf, n, NULL);
64         if(isatty(fd)) tcsetattr(fd, TCSAFLUSH, &save);
65         return(err);
66 }
67
68 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
69                         unsigned short *cols_out)
70 {
71         struct winsize size;
72         int ret = 0;
73
74         if(ioctl(fd, TIOCGWINSZ, &size) == 0){
75                 ret = ((*rows_out != size.ws_row) || 
76                        (*cols_out != size.ws_col));
77                 *rows_out = size.ws_row;
78                 *cols_out = size.ws_col;
79         }
80         return(ret);
81 }
82
83 void generic_free(void *data)
84 {
85         kfree(data);
86 }
87
88 static void winch_handler(int sig)
89 {
90 }
91
92 struct winch_data {
93         int pty_fd;
94         int pipe_fd;
95         int close_me;
96 };
97
98 static int winch_thread(void *arg)
99 {
100         struct winch_data *data = arg;
101         sigset_t sigs;
102         int pty_fd, pipe_fd;
103         char c = 1;
104
105         close(data->close_me);
106         pty_fd = data->pty_fd;
107         pipe_fd = data->pipe_fd;
108         if(write(pipe_fd, &c, sizeof(c)) != sizeof(c))
109                 printk("winch_thread : failed to write synchronization "
110                        "byte, errno = %d\n", errno);
111
112         signal(SIGWINCH, winch_handler);
113         sigfillset(&sigs);
114         sigdelset(&sigs, SIGWINCH);
115         if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
116                 printk("winch_thread : sigprocmask failed, errno = %d\n", 
117                        errno);
118                 exit(1);
119         }
120
121         if(setsid() < 0){
122                 printk("winch_thread : setsid failed, errno = %d\n", errno);
123                 exit(1);
124         }
125
126         if(ioctl(pty_fd, TIOCSCTTY, 0) < 0){
127                 printk("winch_thread : TIOCSCTTY failed, errno = %d\n", errno);
128                 exit(1);
129         }
130         if(tcsetpgrp(pty_fd, os_getpid()) < 0){
131                 printk("winch_thread : tcsetpgrp failed, errno = %d\n", errno);
132                 exit(1);
133         }
134
135         if(read(pipe_fd, &c, sizeof(c)) != sizeof(c))
136                 printk("winch_thread : failed to read synchronization byte, "
137                        "errno = %d\n", errno);
138
139         while(1){
140                 pause();
141
142                 if(write(pipe_fd, &c, sizeof(c)) != sizeof(c)){
143                         printk("winch_thread : write failed, errno = %d\n",
144                                errno);
145                 }
146         }
147 }
148
149 static int winch_tramp(int fd, void *device_data, int *fd_out)
150 {
151         struct winch_data data;
152         unsigned long stack;
153         int fds[2], pid, n, err;
154         char c;
155
156         err = os_pipe(fds, 1, 1);
157         if(err){
158                 printk("winch_tramp : os_pipe failed, errno = %d\n", -err);
159                 return(err);
160         }
161
162         data = ((struct winch_data) { .pty_fd           = fd,
163                                       .pipe_fd          = fds[1],
164                                       .close_me         = fds[0] } );
165         pid = run_helper_thread(winch_thread, &data, 0, &stack, 0);
166         if(pid < 0){
167                 printk("fork of winch_thread failed - errno = %d\n", errno);
168                 return(pid);
169         }
170
171         close(fds[1]);
172         *fd_out = fds[0];
173         n = read(fds[0], &c, sizeof(c));
174         if(n != sizeof(c)){
175                 printk("winch_tramp : failed to read synchronization byte\n");
176                 printk("read returned %d, errno = %d\n", n, errno);
177                 printk("fd %d will not support SIGWINCH\n", fd);
178                 *fd_out = -1;
179         }
180         return(pid);
181 }
182
183 void register_winch(int fd, void *device_data)
184 {
185         int pid, thread, thread_fd;
186         char c = 1;
187
188         if(!isatty(fd)) return;
189
190         pid = tcgetpgrp(fd);
191         if(!CHOOSE_MODE(is_tracer_winch(pid, fd, device_data), 0) && 
192            (pid == -1)){
193                 thread = winch_tramp(fd, device_data, &thread_fd);
194                 if(fd != -1){
195                         register_winch_irq(thread_fd, fd, thread, device_data);
196
197                         if(write(thread_fd, &c, sizeof(c)) != sizeof(c))
198                                 printk("register_winch : failed to write "
199                                        "synchronization byte\n");
200                 }
201         }
202 }
203
204 /*
205  * Overrides for Emacs so that we follow Linus's tabbing style.
206  * Emacs will notice this stuff at the end of the file and automatically
207  * adjust the settings for this buffer only.  This must remain at the end
208  * of the file.
209  * ---------------------------------------------------------------------------
210  * Local variables:
211  * c-file-style: "linux"
212  * End:
213  */