ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <fcntl.h>
12 #include <termios.h>
13 #include <signal.h>
14 #include <sched.h>
15 #include <sys/socket.h>
16 #include "kern_util.h"
17 #include "chan_user.h"
18 #include "helper.h"
19 #include "user_util.h"
20 #include "user.h"
21 #include "os.h"
22 #include "xterm.h"
23
24 struct xterm_chan {
25         int pid;
26         int helper_pid;
27         char *title;
28         int device;
29         int raw;
30         struct termios tt;
31         unsigned long stack;
32         int direct_rcv;
33 };
34
35 void *xterm_init(char *str, int device, struct chan_opts *opts)
36 {
37         struct xterm_chan *data;
38
39         if((data = malloc(sizeof(*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 int xterm_open(int input, int output, int primary, void *d, char **dev_out)
87 {
88         struct xterm_chan *data = d;
89         unsigned long stack;
90         int pid, fd, new, err;
91         char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
92         char *argv[] = { terminal_emulator, title_switch, title, exec_switch, 
93                          "/usr/lib/uml/port-helper", "-uml-socket",
94                          file, NULL };
95
96         if(access(argv[4], X_OK))
97                 argv[4] = "port-helper";
98
99         fd = mkstemp(file);
100         if(fd < 0){
101                 printk("xterm_open : mkstemp failed, errno = %d\n", errno);
102                 return(-errno);
103         }
104
105         if(unlink(file)){
106                 printk("xterm_open : unlink failed, errno = %d\n", errno);
107                 return(-errno);
108         }
109         close(fd);
110
111         fd = create_unix_socket(file, sizeof(file));
112         if(fd < 0){
113                 printk("xterm_open : create_unix_socket failed, errno = %d\n", 
114                        -fd);
115                 return(-fd);
116         }
117
118         sprintf(title, data->title, data->device);
119         stack = data->stack;
120         pid = run_helper(NULL, NULL, argv, &stack);
121         if(pid < 0){
122                 printk("xterm_open : run_helper failed, errno = %d\n", -pid);
123                 return(pid);
124         }
125
126         if(data->stack == 0) free_stack(stack, 0);
127
128         if(data->direct_rcv)
129                 new = os_rcv_fd(fd, &data->helper_pid);
130         else {
131                 if((err = os_set_fd_block(fd, 0)) != 0){
132                         printk("xterm_open : failed to set descriptor "
133                                "non-blocking, errno = %d\n", err);
134                         return(err);
135                 }
136                 new = xterm_fd(fd, &data->helper_pid);
137         }
138         if(new < 0){
139                 printk("xterm_open : os_rcv_fd failed, errno = %d\n", -new);
140                 goto out;
141         }
142
143         tcgetattr(new, &data->tt);
144         if(data->raw) raw(new, 0);
145
146         data->pid = pid;
147         *dev_out = NULL;
148  out:
149         unlink(file);
150         return(new);
151 }
152
153 void xterm_close(int fd, void *d)
154 {
155         struct xterm_chan *data = d;
156         
157         if(data->pid != -1) 
158                 os_kill_process(data->pid, 1);
159         data->pid = -1;
160         if(data->helper_pid != -1) 
161                 os_kill_process(data->helper_pid, 0);
162         data->helper_pid = -1;
163         close(fd);
164 }
165
166 void xterm_free(void *d)
167 {
168         free(d);
169 }
170
171 int xterm_console_write(int fd, const char *buf, int n, void *d)
172 {
173         struct xterm_chan *data = d;
174
175         return(generic_console_write(fd, buf, n, &data->tt));
176 }
177
178 struct chan_ops xterm_ops = {
179         .type           = "xterm",
180         .init           = xterm_init,
181         .open           = xterm_open,
182         .close          = xterm_close,
183         .read           = generic_read,
184         .write          = generic_write,
185         .console_write  = xterm_console_write,
186         .window_size    = generic_window_size,
187         .free           = xterm_free,
188         .winch          = 1,
189 };
190
191 /*
192  * Overrides for Emacs so that we follow Linus's tabbing style.
193  * Emacs will notice this stuff at the end of the file and automatically
194  * adjust the settings for this buffer only.  This must remain at the end
195  * of the file.
196  * ---------------------------------------------------------------------------
197  * Local variables:
198  * c-file-style: "linux"
199  * End:
200  */