This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / um / drivers / chan_user.c
1 /* 
2  * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.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 <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"
23
24 void generic_close(int fd, void *unused)
25 {
26         os_close_file(fd);
27 }
28
29 int generic_read(int fd, char *c_out, void *unused)
30 {
31         int n;
32
33         n = os_read_file(fd, c_out, sizeof(*c_out));
34
35         if(n == -EAGAIN) 
36                 return(0);
37         else if(n == 0) 
38                 return(-EIO);
39         return(n);
40 }
41
42 /* XXX Trivial wrapper around os_write_file */
43
44 int generic_write(int fd, const char *buf, int n, void *unused)
45 {
46         return(os_write_file(fd, buf, n));
47 }
48
49 int generic_console_write(int fd, const char *buf, int n, void *unused)
50 {
51         struct termios save, new;
52         int err;
53
54         if(isatty(fd)){
55                 tcgetattr(fd, &save);
56                 new = save;
57                 new.c_oflag |= OPOST;
58                 tcsetattr(fd, TCSAFLUSH, &new);
59         }
60         err = generic_write(fd, buf, n, NULL);
61         if(isatty(fd)) tcsetattr(fd, TCSAFLUSH, &save);
62         return(err);
63 }
64
65 int generic_window_size(int fd, void *unused, unsigned short *rows_out,
66                         unsigned short *cols_out)
67 {
68         int rows, cols;
69         int ret;
70
71         ret = os_window_size(fd, &rows, &cols);
72         if(ret < 0)
73                 return(ret);
74
75         ret = ((*rows_out != rows) || (*cols_out != cols));
76
77         *rows_out = rows;
78         *cols_out = cols;
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         int count, err;
104         char c = 1;
105
106         os_close_file(data->close_me);
107         pty_fd = data->pty_fd;
108         pipe_fd = data->pipe_fd;
109         count = os_write_file(pipe_fd, &c, sizeof(c));
110         if(count != sizeof(c))
111                 printk("winch_thread : failed to write synchronization "
112                        "byte, err = %d\n", -count);
113
114         signal(SIGWINCH, winch_handler);
115         sigfillset(&sigs);
116         sigdelset(&sigs, SIGWINCH);
117         if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
118                 printk("winch_thread : sigprocmask failed, errno = %d\n", 
119                        errno);
120                 exit(1);
121         }
122
123         if(setsid() < 0){
124                 printk("winch_thread : setsid failed, errno = %d\n", errno);
125                 exit(1);
126         }
127
128         err = os_new_tty_pgrp(pty_fd, os_getpid());
129         if(err < 0){
130                 printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err);
131                 exit(1);
132         }
133
134         count = os_read_file(pipe_fd, &c, sizeof(c));
135         if(count != sizeof(c))
136                 printk("winch_thread : failed to read synchronization byte, "
137                        "err = %d\n", -count);
138
139         while(1){
140                 pause();
141
142                 count = os_write_file(pipe_fd, &c, sizeof(c));
143                 if(count != sizeof(c))
144                         printk("winch_thread : write failed, err = %d\n",
145                                -count);
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 < 0){
158                 printk("winch_tramp : os_pipe failed, err = %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         os_close_file(fds[1]);
172         *fd_out = fds[0];
173         n = os_read_file(fds[0], &c, sizeof(c));
174         if(n != sizeof(c)){
175                 printk("winch_tramp : failed to read synchronization byte\n");
176                 printk("read failed, err = %d\n", -n);
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         int count;
187         char c = 1;
188
189         if(!isatty(fd)) 
190                 return;
191
192         pid = tcgetpgrp(fd);
193         if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, 
194                              device_data) && (pid == -1)){
195                 thread = winch_tramp(fd, device_data, &thread_fd);
196                 if(fd != -1){
197                         register_winch_irq(thread_fd, fd, thread, device_data);
198
199                         count = os_write_file(thread_fd, &c, sizeof(c));
200                         if(count != sizeof(c))
201                                 printk("register_winch : failed to write "
202                                        "synchronization byte, err = %d\n",
203                                         -count);
204                 }
205         }
206 }
207
208 /*
209  * Overrides for Emacs so that we follow Linus's tabbing style.
210  * Emacs will notice this stuff at the end of the file and automatically
211  * adjust the settings for this buffer only.  This must remain at the end
212  * of the file.
213  * ---------------------------------------------------------------------------
214  * Local variables:
215  * c-file-style: "linux"
216  * End:
217  */