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