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