vserver 1.9.3
[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                 tcgetattr(fd, &save);
31                 new = save;
32                 new.c_oflag |= OPOST;
33                 tcsetattr(fd, TCSAFLUSH, &new);
34         }
35         err = generic_write(fd, buf, n, NULL);
36         if(isatty(fd)) tcsetattr(fd, TCSAFLUSH, &save);
37         return(err);
38 }
39
40 static void winch_handler(int sig)
41 {
42 }
43
44 struct winch_data {
45         int pty_fd;
46         int pipe_fd;
47         int close_me;
48 };
49
50 static int winch_thread(void *arg)
51 {
52         struct winch_data *data = arg;
53         sigset_t sigs;
54         int pty_fd, pipe_fd;
55         int count, err;
56         char c = 1;
57
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);
65
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);
73         }
74
75         if(setsid() < 0){
76                 printk("winch_thread : setsid failed, errno = %d\n", errno);
77                 exit(1);
78         }
79
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);
84         }
85
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);
90
91         while(1){
92                 pause();
93
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);
98         }
99 }
100
101 static int winch_tramp(int fd, void *device_data, int *fd_out)
102 {
103         struct winch_data data;
104         unsigned long stack;
105         int fds[2], pid, n, err;
106         char c;
107
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);
112         }
113
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);
121         }
122
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;
131         }
132         return(pid);
133 }
134
135 void register_winch(int fd, void *device_data)
136 {
137         int pid, thread, thread_fd;
138         int count;
139         char c = 1;
140
141         if(!isatty(fd))
142                 return;
143
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);
150
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);
156                 }
157         }
158 }
159
160 /*
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:
169  */