1 /* 2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 3 * Licensed under the GPL 4 */ 5 6 #include <stddef.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <unistd.h> 10 #include <errno.h> 11 #include <string.h> 12 #include <termios.h> 13 #include "chan_user.h" 14 #include "kern_constants.h" 15 #include "os.h" 16 #include "um_malloc.h" 17 #include "user.h" 18 #include "xterm.h" 19 20 struct xterm_chan { 21 int pid; 22 int helper_pid; 23 char *title; 24 int device; 25 int raw; 26 struct termios tt; 27 }; 28 29 static void *xterm_init(char *str, int device, const struct chan_opts *opts) 30 { 31 struct xterm_chan *data; 32 33 data = kmalloc(sizeof(*data), UM_GFP_KERNEL); 34 if (data == NULL) 35 return NULL; 36 *data = ((struct xterm_chan) { .pid = -1, 37 .helper_pid = -1, 38 .device = device, 39 .title = opts->xterm_title, 40 .raw = opts->raw } ); 41 return data; 42 } 43 44 /* Only changed by xterm_setup, which is a setup */ 45 static char *terminal_emulator = "xterm"; 46 static char *title_switch = "-T"; 47 static char *exec_switch = "-e"; 48 49 static int __init xterm_setup(char *line, int *add) 50 { 51 *add = 0; 52 terminal_emulator = line; 53 54 line = strchr(line, ','); 55 if (line == NULL) 56 return 0; 57 58 *line++ = '\0'; 59 if (*line) 60 title_switch = line; 61 62 line = strchr(line, ','); 63 if (line == NULL) 64 return 0; 65 66 *line++ = '\0'; 67 if (*line) 68 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 static int xterm_open(int input, int output, int primary, void *d, 87 char **dev_out) 88 { 89 struct xterm_chan *data = d; 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) < 0) 97 argv[4] = "port-helper"; 98 99 /* Check that DISPLAY is set, this doesn't guarantee the xterm 100 * will work but w/o it we can be pretty sure it won't. */ 101 if (getenv("DISPLAY") == NULL) { 102 printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n"); 103 return -ENODEV; 104 } 105 106 /* 107 * This business of getting a descriptor to a temp file, 108 * deleting the file and closing the descriptor is just to get 109 * a known-unused name for the Unix socket that we really 110 * want. 111 */ 112 fd = mkstemp(file); 113 if (fd < 0) { 114 err = -errno; 115 printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n", 116 errno); 117 return err; 118 } 119 120 if (unlink(file)) { 121 err = -errno; 122 printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n", 123 errno); 124 return err; 125 } 126 close(fd); 127 128 fd = os_create_unix_socket(file, sizeof(file), 1); 129 if (fd < 0) { 130 printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, " 131 "errno = %d\n", -fd); 132 return fd; 133 } 134 135 sprintf(title, data->title, data->device); 136 pid = run_helper(NULL, NULL, argv); 137 if (pid < 0) { 138 err = pid; 139 printk(UM_KERN_ERR "xterm_open : run_helper failed, " 140 "errno = %d\n", -err); 141 goto out_close1; 142 } 143 144 err = os_set_fd_block(fd, 0); 145 if (err < 0) { 146 printk(UM_KERN_ERR "xterm_open : failed to set descriptor " 147 "non-blocking, err = %d\n", -err); 148 goto out_kill; 149 } 150 151 new = xterm_fd(fd, &data->helper_pid); 152 if (new < 0) { 153 err = new; 154 printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n", 155 -err); 156 goto out_kill; 157 } 158 159 err = os_set_fd_block(new, 0); 160 if (err) { 161 printk(UM_KERN_ERR "xterm_open : failed to set xterm " 162 "descriptor non-blocking, err = %d\n", -err); 163 goto out_close2; 164 } 165 166 CATCH_EINTR(err = tcgetattr(new, &data->tt)); 167 if (err) { 168 new = err; 169 goto out_close2; 170 } 171 172 if (data->raw) { 173 err = raw(new); 174 if (err) { 175 new = err; 176 goto out_close2; 177 } 178 } 179 180 unlink(file); 181 data->pid = pid; 182 *dev_out = NULL; 183 184 return new; 185 186 out_close2: 187 close(new); 188 out_kill: 189 os_kill_process(pid, 1); 190 out_close1: 191 close(fd); 192 193 return err; 194 } 195 196 static void xterm_close(int fd, void *d) 197 { 198 struct xterm_chan *data = d; 199 200 if (data->pid != -1) 201 os_kill_process(data->pid, 1); 202 data->pid = -1; 203 204 if (data->helper_pid != -1) 205 os_kill_process(data->helper_pid, 0); 206 data->helper_pid = -1; 207 208 os_close_file(fd); 209 } 210 211 const struct chan_ops xterm_ops = { 212 .type = "xterm", 213 .init = xterm_init, 214 .open = xterm_open, 215 .close = xterm_close, 216 .read = generic_read, 217 .write = generic_write, 218 .console_write = generic_console_write, 219 .window_size = generic_window_size, 220 .free = generic_free, 221 .winch = 1, 222 }; 223