vserver 1.9.5.x5
[linux-2.6.git] / arch / um / drivers / xterm.c
1 /* 
2  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <termios.h>
12 #include <signal.h>
13 #include <sched.h>
14 #include <sys/socket.h>
15 #include "kern_util.h"
16 #include "chan_user.h"
17 #include "helper.h"
18 #include "user_util.h"
19 #include "user.h"
20 #include "os.h"
21 #include "xterm.h"
22
23 struct xterm_chan {
24         int pid;
25         int helper_pid;
26         char *title;
27         int device;
28         int raw;
29         struct termios tt;
30         unsigned long stack;
31         int direct_rcv;
32 };
33
34 void *xterm_init(char *str, int device, struct chan_opts *opts)
35 {
36         struct xterm_chan *data;
37
38         data = malloc(sizeof(*data));
39         if(data == NULL) return(NULL);
40         *data = ((struct xterm_chan) { .pid             = -1, 
41                                        .helper_pid      = -1,
42                                        .device          = device, 
43                                        .title           = opts->xterm_title,
44                                        .raw             = opts->raw,
45                                        .stack           = opts->tramp_stack,
46                                        .direct_rcv      = !opts->in_kernel } );
47         return(data);
48 }
49
50 /* Only changed by xterm_setup, which is a setup */
51 static char *terminal_emulator = "xterm";
52 static char *title_switch = "-T";
53 static char *exec_switch = "-e";
54
55 static int __init xterm_setup(char *line, int *add)
56 {
57         *add = 0;
58         terminal_emulator = line;
59
60         line = strchr(line, ',');
61         if(line == NULL) return(0);
62         *line++ = '\0';
63         if(*line) title_switch = line;
64
65         line = strchr(line, ',');
66         if(line == NULL) return(0);
67         *line++ = '\0';
68         if(*line) exec_switch = line;
69
70         return(0);
71 }
72
73 __uml_setup("xterm=", xterm_setup,
74 "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
75 "    Specifies an alternate terminal emulator to use for the debugger,\n"
76 "    consoles, and serial lines when they are attached to the xterm channel.\n"
77 "    The values are the terminal emulator binary, the switch it uses to set\n"
78 "    its title, and the switch it uses to execute a subprocess,\n"
79 "    respectively.  The title switch must have the form '<switch> title',\n"
80 "    not '<switch>=title'.  Similarly, the exec switch must have the form\n"
81 "    '<switch> command arg1 arg2 ...'.\n"
82 "    The default values are 'xterm=xterm,-T,-e'.  Values for gnome-terminal\n"
83 "    are 'xterm=gnome-terminal,-t,-x'.\n\n"
84 );
85
86 /* XXX This badly needs some cleaning up in the error paths */
87 int xterm_open(int input, int output, int primary, void *d, char **dev_out)
88 {
89         struct xterm_chan *data = d;
90         unsigned long stack;
91         int pid, fd, new, err;
92         char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
93         char *argv[] = { terminal_emulator, title_switch, title, exec_switch, 
94                          "/usr/lib/uml/port-helper", "-uml-socket",
95                          file, NULL };
96
97         if(os_access(argv[4], OS_ACC_X_OK) < 0)
98                 argv[4] = "port-helper";
99
100         /* Check that DISPLAY is set, this doesn't guarantee the xterm
101          * will work but w/o it we can be pretty sure it won't. */
102         if (!getenv("DISPLAY")) {
103                 printk("xterm_open: $DISPLAY not set.\n");
104                 return -ENODEV;
105         }
106
107         fd = mkstemp(file);
108         if(fd < 0){
109                 printk("xterm_open : mkstemp failed, errno = %d\n", errno);
110                 return(-errno);
111         }
112
113         if(unlink(file)){
114                 printk("xterm_open : unlink failed, errno = %d\n", errno);
115                 return(-errno);
116         }
117         os_close_file(fd);
118
119         fd = os_create_unix_socket(file, sizeof(file), 1);
120         if(fd < 0){
121                 printk("xterm_open : create_unix_socket failed, errno = %d\n", 
122                        -fd);
123                 return(fd);
124         }
125
126         sprintf(title, data->title, data->device);
127         stack = data->stack;
128         pid = run_helper(NULL, NULL, argv, &stack);
129         if(pid < 0){
130                 printk("xterm_open : run_helper failed, errno = %d\n", -pid);
131                 return(pid);
132         }
133
134         if(data->stack == 0) free_stack(stack, 0);
135
136         if (data->direct_rcv) {
137                 new = os_rcv_fd(fd, &data->helper_pid);
138         } else {
139                 err = os_set_fd_block(fd, 0);
140                 if(err < 0){
141                         printk("xterm_open : failed to set descriptor "
142                                "non-blocking, err = %d\n", -err);
143                         return(err);
144                 }
145                 new = xterm_fd(fd, &data->helper_pid);
146         }
147         if(new < 0){
148                 printk("xterm_open : os_rcv_fd failed, err = %d\n", -new);
149                 goto out;
150         }
151
152         CATCH_EINTR(err = tcgetattr(new, &data->tt));
153         if(err){
154                 new = err;
155                 goto out;
156         }
157
158         if(data->raw){
159                 err = raw(new);
160                 if(err){
161                         new = err;
162                         goto out;
163                 }
164         }
165
166         data->pid = pid;
167         *dev_out = NULL;
168  out:
169         unlink(file);
170         return(new);
171 }
172
173 void xterm_close(int fd, void *d)
174 {
175         struct xterm_chan *data = d;
176         
177         if(data->pid != -1) 
178                 os_kill_process(data->pid, 1);
179         data->pid = -1;
180         if(data->helper_pid != -1) 
181                 os_kill_process(data->helper_pid, 0);
182         data->helper_pid = -1;
183         os_close_file(fd);
184 }
185
186 void xterm_free(void *d)
187 {
188         free(d);
189 }
190
191 int xterm_console_write(int fd, const char *buf, int n, void *d)
192 {
193         struct xterm_chan *data = d;
194
195         return(generic_console_write(fd, buf, n, &data->tt));
196 }
197
198 struct chan_ops xterm_ops = {
199         .type           = "xterm",
200         .init           = xterm_init,
201         .open           = xterm_open,
202         .close          = xterm_close,
203         .read           = generic_read,
204         .write          = generic_write,
205         .console_write  = xterm_console_write,
206         .window_size    = generic_window_size,
207         .free           = xterm_free,
208         .winch          = 1,
209 };
210
211 /*
212  * Overrides for Emacs so that we follow Linus's tabbing style.
213  * Emacs will notice this stuff at the end of the file and automatically
214  * adjust the settings for this buffer only.  This must remain at the end
215  * of the file.
216  * ---------------------------------------------------------------------------
217  * Local variables:
218  * c-file-style: "linux"
219  * End:
220  */