11da177e4SLinus Torvalds /* 263920f47SJeff Dike * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 31da177e4SLinus Torvalds * Licensed under the GPL 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 6e99525f9SJeff Dike #include <stddef.h> 763920f47SJeff Dike #include <stdio.h> 8cb8fa61cSJeff Dike #include <stdlib.h> 91da177e4SLinus Torvalds #include <unistd.h> 101da177e4SLinus Torvalds #include <errno.h> 11e99525f9SJeff Dike #include <string.h> 121da177e4SLinus Torvalds #include <termios.h> 131da177e4SLinus Torvalds #include "chan_user.h" 14*37185b33SAl Viro #include <os.h> 15*37185b33SAl Viro #include <um_malloc.h> 161da177e4SLinus Torvalds #include "xterm.h" 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds struct xterm_chan { 191da177e4SLinus Torvalds int pid; 201da177e4SLinus Torvalds int helper_pid; 211da177e4SLinus Torvalds char *title; 221da177e4SLinus Torvalds int device; 231da177e4SLinus Torvalds int raw; 241da177e4SLinus Torvalds struct termios tt; 251da177e4SLinus Torvalds }; 261da177e4SLinus Torvalds 2763920f47SJeff Dike static void *xterm_init(char *str, int device, const struct chan_opts *opts) 281da177e4SLinus Torvalds { 291da177e4SLinus Torvalds struct xterm_chan *data; 301da177e4SLinus Torvalds 3143f5b308SJeff Dike data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL); 3263920f47SJeff Dike if (data == NULL) 3363920f47SJeff Dike return NULL; 341da177e4SLinus Torvalds *data = ((struct xterm_chan) { .pid = -1, 351da177e4SLinus Torvalds .helper_pid = -1, 361da177e4SLinus Torvalds .device = device, 371da177e4SLinus Torvalds .title = opts->xterm_title, 3863920f47SJeff Dike .raw = opts->raw } ); 3963920f47SJeff Dike return data; 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* Only changed by xterm_setup, which is a setup */ 431da177e4SLinus Torvalds static char *terminal_emulator = "xterm"; 441da177e4SLinus Torvalds static char *title_switch = "-T"; 451da177e4SLinus Torvalds static char *exec_switch = "-e"; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds static int __init xterm_setup(char *line, int *add) 481da177e4SLinus Torvalds { 491da177e4SLinus Torvalds *add = 0; 501da177e4SLinus Torvalds terminal_emulator = line; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds line = strchr(line, ','); 5363920f47SJeff Dike if (line == NULL) 5463920f47SJeff Dike return 0; 5563920f47SJeff Dike 561da177e4SLinus Torvalds *line++ = '\0'; 5763920f47SJeff Dike if (*line) 5863920f47SJeff Dike title_switch = line; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds line = strchr(line, ','); 6163920f47SJeff Dike if (line == NULL) 6263920f47SJeff Dike return 0; 631da177e4SLinus Torvalds 6463920f47SJeff Dike *line++ = '\0'; 6563920f47SJeff Dike if (*line) 6663920f47SJeff Dike exec_switch = line; 6763920f47SJeff Dike 6863920f47SJeff Dike return 0; 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds __uml_setup("xterm=", xterm_setup, 721da177e4SLinus Torvalds "xterm=<terminal emulator>,<title switch>,<exec switch>\n" 731da177e4SLinus Torvalds " Specifies an alternate terminal emulator to use for the debugger,\n" 741da177e4SLinus Torvalds " consoles, and serial lines when they are attached to the xterm channel.\n" 751da177e4SLinus Torvalds " The values are the terminal emulator binary, the switch it uses to set\n" 761da177e4SLinus Torvalds " its title, and the switch it uses to execute a subprocess,\n" 771da177e4SLinus Torvalds " respectively. The title switch must have the form '<switch> title',\n" 781da177e4SLinus Torvalds " not '<switch>=title'. Similarly, the exec switch must have the form\n" 791da177e4SLinus Torvalds " '<switch> command arg1 arg2 ...'.\n" 801da177e4SLinus Torvalds " The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n" 811da177e4SLinus Torvalds " are 'xterm=gnome-terminal,-t,-x'.\n\n" 821da177e4SLinus Torvalds ); 831da177e4SLinus Torvalds 8463920f47SJeff Dike static int xterm_open(int input, int output, int primary, void *d, 851da177e4SLinus Torvalds char **dev_out) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds struct xterm_chan *data = d; 881da177e4SLinus Torvalds int pid, fd, new, err; 891da177e4SLinus Torvalds char title[256], file[] = "/tmp/xterm-pipeXXXXXX"; 901da177e4SLinus Torvalds char *argv[] = { terminal_emulator, title_switch, title, exec_switch, 910ce451acSRichard Weinberger OS_LIB_PATH "/uml/port-helper", "-uml-socket", 921da177e4SLinus Torvalds file, NULL }; 931da177e4SLinus Torvalds 9463920f47SJeff Dike if (access(argv[4], X_OK) < 0) 951da177e4SLinus Torvalds argv[4] = "port-helper"; 961da177e4SLinus Torvalds 97cb8fa61cSJeff Dike /* 98cb8fa61cSJeff Dike * Check that DISPLAY is set, this doesn't guarantee the xterm 99cb8fa61cSJeff Dike * will work but w/o it we can be pretty sure it won't. 100cb8fa61cSJeff Dike */ 10163920f47SJeff Dike if (getenv("DISPLAY") == NULL) { 10263920f47SJeff Dike printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n"); 1031da177e4SLinus Torvalds return -ENODEV; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 10663920f47SJeff Dike /* 10763920f47SJeff Dike * This business of getting a descriptor to a temp file, 10863920f47SJeff Dike * deleting the file and closing the descriptor is just to get 10963920f47SJeff Dike * a known-unused name for the Unix socket that we really 11063920f47SJeff Dike * want. 11163920f47SJeff Dike */ 1121da177e4SLinus Torvalds fd = mkstemp(file); 1131da177e4SLinus Torvalds if (fd < 0) { 114b4fd310eSJeff Dike err = -errno; 11563920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n", 11663920f47SJeff Dike errno); 117b4fd310eSJeff Dike return err; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds if (unlink(file)) { 121b4fd310eSJeff Dike err = -errno; 12263920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n", 12363920f47SJeff Dike errno); 124b40997b8SJonathan Neuschäfer close(fd); 125b4fd310eSJeff Dike return err; 1261da177e4SLinus Torvalds } 12763920f47SJeff Dike close(fd); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds fd = os_create_unix_socket(file, sizeof(file), 1); 1301da177e4SLinus Torvalds if (fd < 0) { 13163920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, " 13263920f47SJeff Dike "errno = %d\n", -fd); 13363920f47SJeff Dike return fd; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds sprintf(title, data->title, data->device); 137c4399016SJeff Dike pid = run_helper(NULL, NULL, argv); 1381da177e4SLinus Torvalds if (pid < 0) { 13963920f47SJeff Dike err = pid; 14063920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : run_helper failed, " 14163920f47SJeff Dike "errno = %d\n", -err); 14263920f47SJeff Dike goto out_close1; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds err = os_set_fd_block(fd, 0); 1461da177e4SLinus Torvalds if (err < 0) { 14763920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : failed to set descriptor " 1481da177e4SLinus Torvalds "non-blocking, err = %d\n", -err); 14963920f47SJeff Dike goto out_kill; 1501da177e4SLinus Torvalds } 15163920f47SJeff Dike 1521da177e4SLinus Torvalds new = xterm_fd(fd, &data->helper_pid); 1531da177e4SLinus Torvalds if (new < 0) { 15463920f47SJeff Dike err = new; 15563920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n", 15663920f47SJeff Dike -err); 15763920f47SJeff Dike goto out_kill; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 16089df6bfcSEduard-Gabriel Munteanu err = os_set_fd_block(new, 0); 16189df6bfcSEduard-Gabriel Munteanu if (err) { 16263920f47SJeff Dike printk(UM_KERN_ERR "xterm_open : failed to set xterm " 16363920f47SJeff Dike "descriptor non-blocking, err = %d\n", -err); 16463920f47SJeff Dike goto out_close2; 16589df6bfcSEduard-Gabriel Munteanu } 16689df6bfcSEduard-Gabriel Munteanu 1671da177e4SLinus Torvalds CATCH_EINTR(err = tcgetattr(new, &data->tt)); 1681da177e4SLinus Torvalds if (err) { 1691da177e4SLinus Torvalds new = err; 17063920f47SJeff Dike goto out_close2; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds if (data->raw) { 1741da177e4SLinus Torvalds err = raw(new); 1751da177e4SLinus Torvalds if (err) { 1761da177e4SLinus Torvalds new = err; 17763920f47SJeff Dike goto out_close2; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 18163920f47SJeff Dike unlink(file); 1821da177e4SLinus Torvalds data->pid = pid; 1831da177e4SLinus Torvalds *dev_out = NULL; 18463920f47SJeff Dike 18563920f47SJeff Dike return new; 18663920f47SJeff Dike 18763920f47SJeff Dike out_close2: 18863920f47SJeff Dike close(new); 18963920f47SJeff Dike out_kill: 19063920f47SJeff Dike os_kill_process(pid, 1); 19163920f47SJeff Dike out_close1: 19263920f47SJeff Dike close(fd); 19363920f47SJeff Dike 19463920f47SJeff Dike return err; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 19763920f47SJeff Dike static void xterm_close(int fd, void *d) 1981da177e4SLinus Torvalds { 1991da177e4SLinus Torvalds struct xterm_chan *data = d; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds if (data->pid != -1) 2021da177e4SLinus Torvalds os_kill_process(data->pid, 1); 2031da177e4SLinus Torvalds data->pid = -1; 20463920f47SJeff Dike 2051da177e4SLinus Torvalds if (data->helper_pid != -1) 2061da177e4SLinus Torvalds os_kill_process(data->helper_pid, 0); 2071da177e4SLinus Torvalds data->helper_pid = -1; 20863920f47SJeff Dike 2091da177e4SLinus Torvalds os_close_file(fd); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2125e7672ecSJeff Dike const struct chan_ops xterm_ops = { 2131da177e4SLinus Torvalds .type = "xterm", 2141da177e4SLinus Torvalds .init = xterm_init, 2151da177e4SLinus Torvalds .open = xterm_open, 2161da177e4SLinus Torvalds .close = xterm_close, 2171da177e4SLinus Torvalds .read = generic_read, 2181da177e4SLinus Torvalds .write = generic_write, 219fd9bc53bSPaolo 'Blaisorblade' Giarrusso .console_write = generic_console_write, 2201da177e4SLinus Torvalds .window_size = generic_window_size, 221e99525f9SJeff Dike .free = generic_free, 2221da177e4SLinus Torvalds .winch = 1, 2231da177e4SLinus Torvalds }; 224