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" |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 14 | #include <os.h> |
| 15 | #include <um_malloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "xterm.h" |
| 17 | |
| 18 | struct xterm_chan { |
| 19 | int pid; |
| 20 | int helper_pid; |
| 21 | char *title; |
| 22 | int device; |
| 23 | int raw; |
| 24 | struct termios tt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 27 | 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] | 28 | { |
| 29 | struct xterm_chan *data; |
| 30 | |
Jeff Dike | 43f5b30 | 2008-05-12 14:01:52 -0700 | [diff] [blame] | 31 | data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 32 | if (data == NULL) |
| 33 | return NULL; |
| 34 | *data = ((struct xterm_chan) { .pid = -1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .helper_pid = -1, |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 36 | .device = device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | .title = opts->xterm_title, |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 38 | .raw = opts->raw } ); |
| 39 | return data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* Only changed by xterm_setup, which is a setup */ |
| 43 | static char *terminal_emulator = "xterm"; |
| 44 | static char *title_switch = "-T"; |
| 45 | static char *exec_switch = "-e"; |
| 46 | |
| 47 | static int __init xterm_setup(char *line, int *add) |
| 48 | { |
| 49 | *add = 0; |
| 50 | terminal_emulator = line; |
| 51 | |
| 52 | line = strchr(line, ','); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 53 | if (line == NULL) |
| 54 | return 0; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | *line++ = '\0'; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 57 | if (*line) |
| 58 | title_switch = line; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | line = strchr(line, ','); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 61 | if (line == NULL) |
| 62 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 64 | *line++ = '\0'; |
| 65 | if (*line) |
| 66 | exec_switch = line; |
| 67 | |
| 68 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | __uml_setup("xterm=", xterm_setup, |
| 72 | "xterm=<terminal emulator>,<title switch>,<exec switch>\n" |
| 73 | " Specifies an alternate terminal emulator to use for the debugger,\n" |
| 74 | " consoles, and serial lines when they are attached to the xterm channel.\n" |
| 75 | " The values are the terminal emulator binary, the switch it uses to set\n" |
| 76 | " its title, and the switch it uses to execute a subprocess,\n" |
| 77 | " respectively. The title switch must have the form '<switch> title',\n" |
| 78 | " not '<switch>=title'. Similarly, the exec switch must have the form\n" |
| 79 | " '<switch> command arg1 arg2 ...'.\n" |
| 80 | " The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n" |
| 81 | " are 'xterm=gnome-terminal,-t,-x'.\n\n" |
| 82 | ); |
| 83 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 84 | static int xterm_open(int input, int output, int primary, void *d, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | char **dev_out) |
| 86 | { |
| 87 | struct xterm_chan *data = d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | int pid, fd, new, err; |
| 89 | char title[256], file[] = "/tmp/xterm-pipeXXXXXX"; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 90 | char *argv[] = { terminal_emulator, title_switch, title, exec_switch, |
Richard Weinberger | 0ce451a | 2011-05-24 17:13:00 -0700 | [diff] [blame] | 91 | OS_LIB_PATH "/uml/port-helper", "-uml-socket", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | file, NULL }; |
| 93 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 94 | if (access(argv[4], X_OK) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | argv[4] = "port-helper"; |
| 96 | |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Check that DISPLAY is set, this doesn't guarantee the xterm |
| 99 | * will work but w/o it we can be pretty sure it won't. |
| 100 | */ |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 101 | if (getenv("DISPLAY") == NULL) { |
| 102 | printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return -ENODEV; |
| 104 | } |
| 105 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | fd = mkstemp(file); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 113 | if (fd < 0) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 114 | err = -errno; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 115 | printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n", |
| 116 | errno); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 117 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 120 | if (unlink(file)) { |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 121 | err = -errno; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 122 | printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n", |
| 123 | errno); |
Jonathan Neuschäfer | b40997b | 2011-09-14 16:21:20 -0700 | [diff] [blame] | 124 | close(fd); |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 125 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 127 | close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | fd = os_create_unix_socket(file, sizeof(file), 1); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 130 | if (fd < 0) { |
| 131 | printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, " |
| 132 | "errno = %d\n", -fd); |
| 133 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | sprintf(title, data->title, data->device); |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 137 | pid = run_helper(NULL, NULL, argv); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 138 | if (pid < 0) { |
| 139 | err = pid; |
| 140 | printk(UM_KERN_ERR "xterm_open : run_helper failed, " |
| 141 | "errno = %d\n", -err); |
| 142 | goto out_close1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 145 | err = os_set_fd_block(fd, 0); |
| 146 | if (err < 0) { |
| 147 | printk(UM_KERN_ERR "xterm_open : failed to set descriptor " |
| 148 | "non-blocking, err = %d\n", -err); |
| 149 | goto out_kill; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 151 | |
| 152 | new = xterm_fd(fd, &data->helper_pid); |
| 153 | if (new < 0) { |
| 154 | err = new; |
| 155 | printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n", |
| 156 | -err); |
| 157 | goto out_kill; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Eduard-Gabriel Munteanu | 89df6bf | 2007-07-15 23:38:51 -0700 | [diff] [blame] | 160 | err = os_set_fd_block(new, 0); |
| 161 | if (err) { |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 162 | printk(UM_KERN_ERR "xterm_open : failed to set xterm " |
| 163 | "descriptor non-blocking, err = %d\n", -err); |
| 164 | goto out_close2; |
Eduard-Gabriel Munteanu | 89df6bf | 2007-07-15 23:38:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | CATCH_EINTR(err = tcgetattr(new, &data->tt)); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 168 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | new = err; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 170 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 173 | if (data->raw) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | err = raw(new); |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 175 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | new = err; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 177 | goto out_close2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 181 | unlink(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | data->pid = pid; |
| 183 | *dev_out = NULL; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 184 | |
| 185 | return new; |
| 186 | |
| 187 | out_close2: |
| 188 | close(new); |
| 189 | out_kill: |
| 190 | os_kill_process(pid, 1); |
| 191 | out_close1: |
| 192 | close(fd); |
| 193 | |
| 194 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 197 | static void xterm_close(int fd, void *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | struct xterm_chan *data = d; |
Jeff Dike | cb8fa61 | 2007-10-16 01:27:34 -0700 | [diff] [blame] | 200 | |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 201 | if (data->pid != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | os_kill_process(data->pid, 1); |
| 203 | data->pid = -1; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 204 | |
| 205 | if (data->helper_pid != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | os_kill_process(data->helper_pid, 0); |
| 207 | data->helper_pid = -1; |
Jeff Dike | 63920f4 | 2007-07-15 23:38:52 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | os_close_file(fd); |
| 210 | } |
| 211 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 212 | const struct chan_ops xterm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | .type = "xterm", |
| 214 | .init = xterm_init, |
| 215 | .open = xterm_open, |
| 216 | .close = xterm_close, |
| 217 | .read = generic_read, |
| 218 | .write = generic_write, |
Paolo 'Blaisorblade' Giarrusso | fd9bc53 | 2005-11-13 16:07:10 -0800 | [diff] [blame] | 219 | .console_write = generic_console_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | .window_size = generic_window_size, |
Jeff Dike | e99525f | 2007-10-16 01:26:41 -0700 | [diff] [blame] | 221 | .free = generic_free, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | .winch = 1, |
| 223 | }; |