18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <stddef.h>
78c2ecf20Sopenharmony_ci#include <stdio.h>
88c2ecf20Sopenharmony_ci#include <stdlib.h>
98c2ecf20Sopenharmony_ci#include <unistd.h>
108c2ecf20Sopenharmony_ci#include <errno.h>
118c2ecf20Sopenharmony_ci#include <string.h>
128c2ecf20Sopenharmony_ci#include <termios.h>
138c2ecf20Sopenharmony_ci#include "chan_user.h"
148c2ecf20Sopenharmony_ci#include <os.h>
158c2ecf20Sopenharmony_ci#include <um_malloc.h>
168c2ecf20Sopenharmony_ci#include "xterm.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct xterm_chan {
198c2ecf20Sopenharmony_ci	int pid;
208c2ecf20Sopenharmony_ci	int helper_pid;
218c2ecf20Sopenharmony_ci	int chan_fd;
228c2ecf20Sopenharmony_ci	char *title;
238c2ecf20Sopenharmony_ci	int device;
248c2ecf20Sopenharmony_ci	int raw;
258c2ecf20Sopenharmony_ci	struct termios tt;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic void *xterm_init(char *str, int device, const struct chan_opts *opts)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	struct xterm_chan *data;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
338c2ecf20Sopenharmony_ci	if (data == NULL)
348c2ecf20Sopenharmony_ci		return NULL;
358c2ecf20Sopenharmony_ci	*data = ((struct xterm_chan) { .pid 		= -1,
368c2ecf20Sopenharmony_ci				       .helper_pid 	= -1,
378c2ecf20Sopenharmony_ci				       .chan_fd		= -1,
388c2ecf20Sopenharmony_ci				       .device 		= device,
398c2ecf20Sopenharmony_ci				       .title 		= opts->xterm_title,
408c2ecf20Sopenharmony_ci				       .raw  		= opts->raw } );
418c2ecf20Sopenharmony_ci	return data;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Only changed by xterm_setup, which is a setup */
458c2ecf20Sopenharmony_cistatic char *terminal_emulator = "xterm";
468c2ecf20Sopenharmony_cistatic char *title_switch = "-T";
478c2ecf20Sopenharmony_cistatic char *exec_switch = "-e";
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int __init xterm_setup(char *line, int *add)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	*add = 0;
528c2ecf20Sopenharmony_ci	terminal_emulator = line;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	line = strchr(line, ',');
558c2ecf20Sopenharmony_ci	if (line == NULL)
568c2ecf20Sopenharmony_ci		return 0;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	*line++ = '\0';
598c2ecf20Sopenharmony_ci	if (*line)
608c2ecf20Sopenharmony_ci		title_switch = line;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	line = strchr(line, ',');
638c2ecf20Sopenharmony_ci	if (line == NULL)
648c2ecf20Sopenharmony_ci		return 0;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	*line++ = '\0';
678c2ecf20Sopenharmony_ci	if (*line)
688c2ecf20Sopenharmony_ci		exec_switch = line;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return 0;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci__uml_setup("xterm=", xterm_setup,
748c2ecf20Sopenharmony_ci"xterm=<terminal emulator>,<title switch>,<exec switch>\n"
758c2ecf20Sopenharmony_ci"    Specifies an alternate terminal emulator to use for the debugger,\n"
768c2ecf20Sopenharmony_ci"    consoles, and serial lines when they are attached to the xterm channel.\n"
778c2ecf20Sopenharmony_ci"    The values are the terminal emulator binary, the switch it uses to set\n"
788c2ecf20Sopenharmony_ci"    its title, and the switch it uses to execute a subprocess,\n"
798c2ecf20Sopenharmony_ci"    respectively.  The title switch must have the form '<switch> title',\n"
808c2ecf20Sopenharmony_ci"    not '<switch>=title'.  Similarly, the exec switch must have the form\n"
818c2ecf20Sopenharmony_ci"    '<switch> command arg1 arg2 ...'.\n"
828c2ecf20Sopenharmony_ci"    The default values are 'xterm=xterm,-T,-e'.  Values for gnome-terminal\n"
838c2ecf20Sopenharmony_ci"    are 'xterm=gnome-terminal,-t,-x'.\n\n"
848c2ecf20Sopenharmony_ci);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int xterm_open(int input, int output, int primary, void *d,
878c2ecf20Sopenharmony_ci		      char **dev_out)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct xterm_chan *data = d;
908c2ecf20Sopenharmony_ci	int pid, fd, new, err;
918c2ecf20Sopenharmony_ci	char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
928c2ecf20Sopenharmony_ci	char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
938c2ecf20Sopenharmony_ci			 OS_LIB_PATH "/uml/port-helper", "-uml-socket",
948c2ecf20Sopenharmony_ci			 file, NULL };
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (access(argv[4], X_OK) < 0)
978c2ecf20Sopenharmony_ci		argv[4] = "port-helper";
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/*
1008c2ecf20Sopenharmony_ci	 * Check that DISPLAY is set, this doesn't guarantee the xterm
1018c2ecf20Sopenharmony_ci	 * will work but w/o it we can be pretty sure it won't.
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci	if (getenv("DISPLAY") == NULL) {
1048c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n");
1058c2ecf20Sopenharmony_ci		return -ENODEV;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/*
1098c2ecf20Sopenharmony_ci	 * This business of getting a descriptor to a temp file,
1108c2ecf20Sopenharmony_ci	 * deleting the file and closing the descriptor is just to get
1118c2ecf20Sopenharmony_ci	 * a known-unused name for the Unix socket that we really
1128c2ecf20Sopenharmony_ci	 * want.
1138c2ecf20Sopenharmony_ci	 */
1148c2ecf20Sopenharmony_ci	fd = mkstemp(file);
1158c2ecf20Sopenharmony_ci	if (fd < 0) {
1168c2ecf20Sopenharmony_ci		err = -errno;
1178c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n",
1188c2ecf20Sopenharmony_ci		       errno);
1198c2ecf20Sopenharmony_ci		return err;
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (unlink(file)) {
1238c2ecf20Sopenharmony_ci		err = -errno;
1248c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n",
1258c2ecf20Sopenharmony_ci		       errno);
1268c2ecf20Sopenharmony_ci		close(fd);
1278c2ecf20Sopenharmony_ci		return err;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci	close(fd);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	fd = os_create_unix_socket(file, sizeof(file), 1);
1328c2ecf20Sopenharmony_ci	if (fd < 0) {
1338c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
1348c2ecf20Sopenharmony_ci		       "errno = %d\n", -fd);
1358c2ecf20Sopenharmony_ci		return fd;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	sprintf(title, data->title, data->device);
1398c2ecf20Sopenharmony_ci	pid = run_helper(NULL, NULL, argv);
1408c2ecf20Sopenharmony_ci	if (pid < 0) {
1418c2ecf20Sopenharmony_ci		err = pid;
1428c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : run_helper failed, "
1438c2ecf20Sopenharmony_ci		       "errno = %d\n", -err);
1448c2ecf20Sopenharmony_ci		goto out_close1;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	err = os_set_fd_block(fd, 0);
1488c2ecf20Sopenharmony_ci	if (err < 0) {
1498c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
1508c2ecf20Sopenharmony_ci		       "non-blocking, err = %d\n", -err);
1518c2ecf20Sopenharmony_ci		goto out_kill;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	data->chan_fd = fd;
1558c2ecf20Sopenharmony_ci	new = xterm_fd(fd, &data->helper_pid);
1568c2ecf20Sopenharmony_ci	if (new < 0) {
1578c2ecf20Sopenharmony_ci		err = new;
1588c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n",
1598c2ecf20Sopenharmony_ci		       -err);
1608c2ecf20Sopenharmony_ci		goto out_kill;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	err = os_set_fd_block(new, 0);
1648c2ecf20Sopenharmony_ci	if (err) {
1658c2ecf20Sopenharmony_ci		printk(UM_KERN_ERR "xterm_open : failed to set xterm "
1668c2ecf20Sopenharmony_ci		       "descriptor non-blocking, err = %d\n", -err);
1678c2ecf20Sopenharmony_ci		goto out_close2;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	CATCH_EINTR(err = tcgetattr(new, &data->tt));
1718c2ecf20Sopenharmony_ci	if (err) {
1728c2ecf20Sopenharmony_ci		new = err;
1738c2ecf20Sopenharmony_ci		goto out_close2;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (data->raw) {
1778c2ecf20Sopenharmony_ci		err = raw(new);
1788c2ecf20Sopenharmony_ci		if (err) {
1798c2ecf20Sopenharmony_ci			new = err;
1808c2ecf20Sopenharmony_ci			goto out_close2;
1818c2ecf20Sopenharmony_ci		}
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	unlink(file);
1858c2ecf20Sopenharmony_ci	data->pid = pid;
1868c2ecf20Sopenharmony_ci	*dev_out = NULL;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return new;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci out_close2:
1918c2ecf20Sopenharmony_ci	close(new);
1928c2ecf20Sopenharmony_ci out_kill:
1938c2ecf20Sopenharmony_ci	os_kill_process(pid, 1);
1948c2ecf20Sopenharmony_ci out_close1:
1958c2ecf20Sopenharmony_ci	close(fd);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return err;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic void xterm_close(int fd, void *d)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct xterm_chan *data = d;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (data->pid != -1)
2058c2ecf20Sopenharmony_ci		os_kill_process(data->pid, 1);
2068c2ecf20Sopenharmony_ci	data->pid = -1;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (data->helper_pid != -1)
2098c2ecf20Sopenharmony_ci		os_kill_process(data->helper_pid, 0);
2108c2ecf20Sopenharmony_ci	data->helper_pid = -1;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	if (data->chan_fd != -1)
2138c2ecf20Sopenharmony_ci		os_close_file(data->chan_fd);
2148c2ecf20Sopenharmony_ci	os_close_file(fd);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ciconst struct chan_ops xterm_ops = {
2188c2ecf20Sopenharmony_ci	.type		= "xterm",
2198c2ecf20Sopenharmony_ci	.init		= xterm_init,
2208c2ecf20Sopenharmony_ci	.open		= xterm_open,
2218c2ecf20Sopenharmony_ci	.close		= xterm_close,
2228c2ecf20Sopenharmony_ci	.read		= generic_read,
2238c2ecf20Sopenharmony_ci	.write		= generic_write,
2248c2ecf20Sopenharmony_ci	.console_write	= generic_console_write,
2258c2ecf20Sopenharmony_ci	.window_size	= generic_window_size,
2268c2ecf20Sopenharmony_ci	.free		= generic_free,
2278c2ecf20Sopenharmony_ci	.winch		= 1,
2288c2ecf20Sopenharmony_ci};
229