1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
13 #include "chan_user.h"
15 #include <um_malloc.h>
28 static void *xterm_init(char *str, int device, const struct chan_opts *opts)
30 struct xterm_chan *data;
32 data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
35 *data = ((struct xterm_chan) { .pid = -1,
39 .title = opts->xterm_title,
44 /* Only changed by xterm_setup, which is a setup */
45 static char *terminal_emulator = CONFIG_XTERM_CHAN_DEFAULT_EMULATOR;
46 static char *title_switch = "-T";
47 static char *exec_switch = "-e";
49 static int __init xterm_setup(char *line, int *add)
52 terminal_emulator = line;
54 line = strchr(line, ',');
62 line = strchr(line, ',');
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=" CONFIG_XTERM_CHAN_DEFAULT_EMULATOR
84 " Values for gnome-terminal are 'xterm=gnome-terminal,-t,-x'.\n\n"
87 static int xterm_open(int input, int output, int primary, void *d,
90 struct xterm_chan *data = d;
91 int pid, fd, new, err;
92 char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
93 char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
94 OS_LIB_PATH "/uml/port-helper", "-uml-socket",
97 if (access(argv[4], X_OK) < 0)
98 argv[4] = "port-helper";
101 * Check that DISPLAY is set, this doesn't guarantee the xterm
102 * will work but w/o it we can be pretty sure it won't.
104 if (getenv("DISPLAY") == NULL) {
105 printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n");
110 * This business of getting a descriptor to a temp file,
111 * deleting the file and closing the descriptor is just to get
112 * a known-unused name for the Unix socket that we really
118 printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n",
125 printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n",
132 fd = os_create_unix_socket(file, sizeof(file), 1);
134 printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
135 "errno = %d\n", -fd);
139 sprintf(title, data->title, data->device);
140 pid = run_helper(NULL, NULL, argv);
143 printk(UM_KERN_ERR "xterm_open : run_helper failed, "
144 "errno = %d\n", -err);
148 err = os_set_fd_block(fd, 0);
150 printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
151 "non-blocking, err = %d\n", -err);
156 new = xterm_fd(fd, &data->helper_pid);
159 printk(UM_KERN_ERR "xterm_open : xterm_fd failed, err = %d\n",
164 err = os_set_fd_block(new, 0);
166 printk(UM_KERN_ERR "xterm_open : failed to set xterm "
167 "descriptor non-blocking, err = %d\n", -err);
171 CATCH_EINTR(err = tcgetattr(new, &data->tt));
194 os_kill_process(pid, 1);
201 static void xterm_close(int fd, void *d)
203 struct xterm_chan *data = d;
206 os_kill_process(data->pid, 1);
209 if (data->helper_pid != -1)
210 os_kill_process(data->helper_pid, 0);
211 data->helper_pid = -1;
213 if (data->chan_fd != -1)
214 os_close_file(data->chan_fd);
218 const struct chan_ops xterm_ops = {
222 .close = xterm_close,
223 .read = generic_read,
224 .write = generic_write,
225 .console_write = generic_console_write,
226 .window_size = generic_window_size,
227 .free = generic_free,