upgrade to linux 2.6.10-1.12_FC2
[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 int generic_console_write(int fd, const char *buf, int n, void *unused)
25 {
26         struct termios save, new;
27         int err;
28
29         if(isatty(fd)){
30                 CATCH_EINTR(err = tcgetattr(fd, &save));
31                 if (err)
32                         goto error;
33                 new = save;
34                 /* The terminal becomes a bit less raw, to handle \n also as
35                  * "Carriage Return", not only as "New Line". Otherwise, the new
36                  * line won't start at the first column.*/
37                 new.c_oflag |= OPOST;
38                 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
39                 if (err)
40                         goto error;
41         }
42         err = generic_write(fd, buf, n, NULL);
43         /* Restore raw mode, in any case; we *must* ignore any error apart
44          * EINTR, except for debug.*/
45         if(isatty(fd))
46                 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
47         return(err);
48 error:
49         return(-errno);
50 }
51
52 static void winch_handler(int sig)
53 {
54 }
55
56 struct winch_data {
57         int pty_fd;
58         int pipe_fd;
59         int close_me;
60 };
61
62 static int winch_thread(void *arg)
63 {
64         struct winch_data *data = arg;
65         sigset_t sigs;
66         int pty_fd, pipe_fd;
67         int count, err;
68         char c = 1;
69
70         os_close_file(data->close_me);
71         pty_fd = data->pty_fd;
72         pipe_fd = data->pipe_fd;
73         count = os_write_file(pipe_fd, &c, sizeof(c));
74         if(count != sizeof(c))
75                 printk("winch_thread : failed to write synchronization "
76                        "byte, err = %d\n", -count);
77
78         signal(SIGWINCH, winch_handler);
79         sigfillset(&sigs);
80         sigdelset(&sigs, SIGWINCH);
81         if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
82                 printk("winch_thread : sigprocmask failed, errno = %d\n", 
83                        errno);
84                 exit(1);
85         }
86
87         if(setsid() < 0){
88                 printk("winch_thread : setsid failed, errno = %d\n", errno);
89                 exit(1);
90         }
91
92         err = os_new_tty_pgrp(pty_fd, os_getpid());
93         if(err < 0){
94                 printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err);
95                 exit(1);
96         }
97
98         count = os_read_file(pipe_fd, &c, sizeof(c));
99         if(count != sizeof(c))
100                 printk("winch_thread : failed to read synchronization byte, "
101                        "err = %d\n", -count);
102
103         while(1){
104                 pause();
105
106                 count = os_write_file(pipe_fd, &c, sizeof(c));
107                 if(count != sizeof(c))
108                         printk("winch_thread : write failed, err = %d\n",
109                                -count);
110         }
111 }
112
113 static int winch_tramp(int fd, void *device_data, int *fd_out)
114 {
115         struct winch_data data;
116         unsigned long stack;
117         int fds[2], pid, n, err;
118         char c;
119
120         err = os_pipe(fds, 1, 1);
121         if(err < 0){
122                 printk("winch_tramp : os_pipe failed, err = %d\n", -err);
123                 return(err);
124         }
125
126         data = ((struct winch_data) { .pty_fd           = fd,
127                                       .pipe_fd          = fds[1],
128                                       .close_me         = fds[0] } );
129         pid = run_helper_thread(winch_thread, &data, 0, &stack, 0);
130         if(pid < 0){
131                 printk("fork of winch_thread failed - errno = %d\n", errno);
132                 return(pid);
133         }
134
135         os_close_file(fds[1]);
136         *fd_out = fds[0];
137         n = os_read_file(fds[0], &c, sizeof(c));
138         if(n != sizeof(c)){
139                 printk("winch_tramp : failed to read synchronization byte\n");
140                 printk("read failed, err = %d\n", -n);
141                 printk("fd %d will not support SIGWINCH\n", fd);
142                 *fd_out = -1;
143         }
144         return(pid);
145 }
146
147 void register_winch(int fd, void *device_data)
148 {
149         int pid, thread, thread_fd;
150         int count;
151         char c = 1;
152
153         if(!isatty(fd))
154                 return;
155
156         pid = tcgetpgrp(fd);
157         if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
158                              device_data) && (pid == -1)){
159                 thread = winch_tramp(fd, device_data, &thread_fd);
160                 if(fd != -1){
161                         register_winch_irq(thread_fd, fd, thread, device_data);
162
163                         count = os_write_file(thread_fd, &c, sizeof(c));
164                         if(count != sizeof(c))
165                                 printk("register_winch : failed to write "
166                                        "synchronization byte, err = %d\n",
167                                         -count);
168                 }
169         }
170 }
171
172 /*
173  * Overrides for Emacs so that we follow Linus's tabbing style.
174  * Emacs will notice this stuff at the end of the file and automatically
175  * adjust the settings for this buffer only.  This must remain at the end
176  * of the file.
177  * ---------------------------------------------------------------------------
178  * Local variables:
179  * c-file-style: "linux"
180  * End:
181  */