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