Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 6 | #include <stddef.h> |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 8 | #include <stdlib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 11 | #include <string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <termios.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "chan_user.h" |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 14 | #include "kern_constants.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "os.h" |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 16 | #include "um_malloc.h" |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 17 | #include "user.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 29 | static void *xterm_init(char *str, int device, const struct chan_opts *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | struct xterm_chan *data; |
| 32 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 33 | data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 34 | if (data == NULL) |
| 35 | return NULL; |
| 36 | *data = ((struct xterm_chan) { .pid = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | .helper_pid = -1, |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 38 | .device = device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | .title = opts->xterm_title, |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 40 | .raw = opts->raw } ); |
| 41 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, ','); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 55 | if (line == NULL) |
| 56 | return 0; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | *line++ = '\0'; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 59 | if (*line) |
| 60 | title_switch = line; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | line = strchr(line, ','); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 63 | if (line == NULL) |
| 64 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 66 | *line++ = '\0'; |
| 67 | if (*line) |
| 68 | exec_switch = line; |
| 69 | |
| 70 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 86 | static int xterm_open(int input, int output, int primary, void *d, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | char **dev_out) |
| 88 | { |
| 89 | struct xterm_chan *data = d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | int pid, fd, new, err; |
| 91 | char title[256], file[] = "/tmp/xterm-pipeXXXXXX"; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 92 | char *argv[] = { terminal_emulator, title_switch, title, exec_switch, |
Richard Weinberger | 0ce451a | 2011-05-24 17:13:00 -0700 | [diff] [blame] | 93 | OS_LIB_PATH "/uml/port-helper", "-uml-socket", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | file, NULL }; |
| 95 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 96 | if (access(argv[4], X_OK) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | argv[4] = "port-helper"; |
| 98 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 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 | */ |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 103 | if (getenv("DISPLAY") == NULL) { |
| 104 | printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return -ENODEV; |
| 106 | } |
| 107 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 108 | /* |
| 109 | * This business of getting a descriptor to a temp file, |
| 110 | * deleting the file and closing the descriptor is just to get |
| 111 | * a known-unused name for the Unix socket that we really |
| 112 | * want. |
| 113 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | fd = mkstemp(file); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 115 | if (fd < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 116 | err = -errno; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 117 | printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n", |
| 118 | errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 119 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 122 | if (unlink(file)) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 123 | err = -errno; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 124 | printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n", |
| 125 | errno); |
Jonathan Neuschäfer | b40997b | 2011-09-14 16:21:20 -0700 | [diff] [blame^] | 126 | close(fd); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 127 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 129 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | fd = os_create_unix_socket(file, sizeof(file), 1); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 132 | if (fd < 0) { |
| 133 | printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, " |
| 134 | "errno = %d\n", -fd); |
| 135 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | sprintf(title, data->title, data->device); |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 139 | pid = run_helper(NULL, NULL, argv); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 140 | if (pid < 0) { |
| 141 | err = pid; |
| 142 | printk(UM_KERN_ERR "xterm_open : run_helper failed, " |
| 143 | "errno = %d\n", -err); |
| 144 | goto out_close1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 147 | err = os_set_fd_block(fd, 0); |
| 148 | if (err < 0) { |
| 149 | printk(UM_KERN_ERR "xterm_open : failed to set descriptor " |
| 150 | "non-blocking, err = %d\n", -err); |
| 151 | goto out_kill; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 153 | |
| 154 | new = xterm_fd(fd, &data->helper_pid); |
| 155 | if (new < 0) { |
| 156 | err = new; |
| 157 | printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n", |
| 158 | -err); |
| 159 | goto out_kill; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Eduard-Gabriel Munteanu | 89df6bf | 2007-07-15 23:38:51 -0700 | [diff] [blame] | 162 | err = os_set_fd_block(new, 0); |
| 163 | if (err) { |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 164 | printk(UM_KERN_ERR "xterm_open : failed to set xterm " |
| 165 | "descriptor non-blocking, err = %d\n", -err); |
| 166 | goto out_close2; |
Eduard-Gabriel Munteanu | 89df6bf | 2007-07-15 23:38:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | CATCH_EINTR(err = tcgetattr(new, &data->tt)); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 170 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | new = err; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 172 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 175 | if (data->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | err = raw(new); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 177 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | new = err; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 179 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 183 | unlink(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | data->pid = pid; |
| 185 | *dev_out = NULL; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 186 | |
| 187 | return new; |
| 188 | |
| 189 | out_close2: |
| 190 | close(new); |
| 191 | out_kill: |
| 192 | os_kill_process(pid, 1); |
| 193 | out_close1: |
| 194 | close(fd); |
| 195 | |
| 196 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 199 | static void xterm_close(int fd, void *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
| 201 | struct xterm_chan *data = d; |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 202 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 203 | if (data->pid != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | os_kill_process(data->pid, 1); |
| 205 | data->pid = -1; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 206 | |
| 207 | if (data->helper_pid != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | os_kill_process(data->helper_pid, 0); |
| 209 | data->helper_pid = -1; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | os_close_file(fd); |
| 212 | } |
| 213 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 214 | const struct chan_ops xterm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | .type = "xterm", |
| 216 | .init = xterm_init, |
| 217 | .open = xterm_open, |
| 218 | .close = xterm_close, |
| 219 | .read = generic_read, |
| 220 | .write = generic_write, |
Paolo 'Blaisorblade' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 221 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | .window_size = generic_window_size, |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 223 | .free = generic_free, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | .winch = 1, |
| 225 | }; |