xref: /openbmc/linux/arch/um/drivers/xterm.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1dbddf429SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
363920f47SJeff Dike  * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
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"
1437185b33SAl Viro #include <os.h>
1537185b33SAl 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;
219431f7c1SAnton Ivanov 	int chan_fd;
221da177e4SLinus Torvalds 	char *title;
231da177e4SLinus Torvalds 	int device;
241da177e4SLinus Torvalds 	int raw;
251da177e4SLinus Torvalds 	struct termios tt;
261da177e4SLinus Torvalds };
271da177e4SLinus Torvalds 
xterm_init(char * str,int device,const struct chan_opts * opts)2863920f47SJeff Dike static void *xterm_init(char *str, int device, const struct chan_opts *opts)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	struct xterm_chan *data;
311da177e4SLinus Torvalds 
3243f5b308SJeff Dike 	data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
3363920f47SJeff Dike 	if (data == NULL)
3463920f47SJeff Dike 		return NULL;
351da177e4SLinus Torvalds 	*data = ((struct xterm_chan) { .pid 		= -1,
361da177e4SLinus Torvalds 				       .helper_pid 	= -1,
379431f7c1SAnton Ivanov 				       .chan_fd		= -1,
381da177e4SLinus Torvalds 				       .device 		= device,
391da177e4SLinus Torvalds 				       .title 		= opts->xterm_title,
4063920f47SJeff Dike 				       .raw  		= opts->raw } );
4163920f47SJeff Dike 	return data;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* Only changed by xterm_setup, which is a setup */
45*16aa835aSJohannes Berg static char *terminal_emulator = CONFIG_XTERM_CHAN_DEFAULT_EMULATOR;
461da177e4SLinus Torvalds static char *title_switch = "-T";
471da177e4SLinus Torvalds static char *exec_switch = "-e";
481da177e4SLinus Torvalds 
xterm_setup(char * line,int * add)491da177e4SLinus Torvalds static int __init xterm_setup(char *line, int *add)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	*add = 0;
521da177e4SLinus Torvalds 	terminal_emulator = line;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	line = strchr(line, ',');
5563920f47SJeff Dike 	if (line == NULL)
5663920f47SJeff Dike 		return 0;
5763920f47SJeff Dike 
581da177e4SLinus Torvalds 	*line++ = '\0';
5963920f47SJeff Dike 	if (*line)
6063920f47SJeff Dike 		title_switch = line;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	line = strchr(line, ',');
6363920f47SJeff Dike 	if (line == NULL)
6463920f47SJeff Dike 		return 0;
651da177e4SLinus Torvalds 
6663920f47SJeff Dike 	*line++ = '\0';
6763920f47SJeff Dike 	if (*line)
6863920f47SJeff Dike 		exec_switch = line;
6963920f47SJeff Dike 
7063920f47SJeff Dike 	return 0;
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds __uml_setup("xterm=", xterm_setup,
741da177e4SLinus Torvalds "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
751da177e4SLinus Torvalds "    Specifies an alternate terminal emulator to use for the debugger,\n"
761da177e4SLinus Torvalds "    consoles, and serial lines when they are attached to the xterm channel.\n"
771da177e4SLinus Torvalds "    The values are the terminal emulator binary, the switch it uses to set\n"
781da177e4SLinus Torvalds "    its title, and the switch it uses to execute a subprocess,\n"
791da177e4SLinus Torvalds "    respectively.  The title switch must have the form '<switch> title',\n"
801da177e4SLinus Torvalds "    not '<switch>=title'.  Similarly, the exec switch must have the form\n"
811da177e4SLinus Torvalds "    '<switch> command arg1 arg2 ...'.\n"
82*16aa835aSJohannes Berg "    The default values are 'xterm=" CONFIG_XTERM_CHAN_DEFAULT_EMULATOR
83*16aa835aSJohannes Berg      ",-T,-e'.\n"
84*16aa835aSJohannes Berg "    Values for gnome-terminal are 'xterm=gnome-terminal,-t,-x'.\n\n"
851da177e4SLinus Torvalds );
861da177e4SLinus Torvalds 
xterm_open(int input,int output,int primary,void * d,char ** dev_out)8763920f47SJeff Dike static int xterm_open(int input, int output, int primary, void *d,
881da177e4SLinus Torvalds 		      char **dev_out)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	struct xterm_chan *data = d;
911da177e4SLinus Torvalds 	int pid, fd, new, err;
921da177e4SLinus Torvalds 	char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
931da177e4SLinus Torvalds 	char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
940ce451acSRichard Weinberger 			 OS_LIB_PATH "/uml/port-helper", "-uml-socket",
951da177e4SLinus Torvalds 			 file, NULL };
961da177e4SLinus Torvalds 
9763920f47SJeff Dike 	if (access(argv[4], X_OK) < 0)
981da177e4SLinus Torvalds 		argv[4] = "port-helper";
991da177e4SLinus Torvalds 
100cb8fa61cSJeff Dike 	/*
101cb8fa61cSJeff Dike 	 * Check that DISPLAY is set, this doesn't guarantee the xterm
102cb8fa61cSJeff Dike 	 * will work but w/o it we can be pretty sure it won't.
103cb8fa61cSJeff Dike 	 */
10463920f47SJeff Dike 	if (getenv("DISPLAY") == NULL) {
10563920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n");
1061da177e4SLinus Torvalds 		return -ENODEV;
1071da177e4SLinus Torvalds 	}
1081da177e4SLinus Torvalds 
10963920f47SJeff Dike 	/*
11063920f47SJeff Dike 	 * This business of getting a descriptor to a temp file,
11163920f47SJeff Dike 	 * deleting the file and closing the descriptor is just to get
11263920f47SJeff Dike 	 * a known-unused name for the Unix socket that we really
11363920f47SJeff Dike 	 * want.
11463920f47SJeff Dike 	 */
1151da177e4SLinus Torvalds 	fd = mkstemp(file);
1161da177e4SLinus Torvalds 	if (fd < 0) {
117b4fd310eSJeff Dike 		err = -errno;
11863920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n",
11963920f47SJeff Dike 		       errno);
120b4fd310eSJeff Dike 		return err;
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (unlink(file)) {
124b4fd310eSJeff Dike 		err = -errno;
12563920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n",
12663920f47SJeff Dike 		       errno);
127b40997b8SJonathan Neuschäfer 		close(fd);
128b4fd310eSJeff Dike 		return err;
1291da177e4SLinus Torvalds 	}
13063920f47SJeff Dike 	close(fd);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	fd = os_create_unix_socket(file, sizeof(file), 1);
1331da177e4SLinus Torvalds 	if (fd < 0) {
13463920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
13563920f47SJeff Dike 		       "errno = %d\n", -fd);
13663920f47SJeff Dike 		return fd;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	sprintf(title, data->title, data->device);
140c4399016SJeff Dike 	pid = run_helper(NULL, NULL, argv);
1411da177e4SLinus Torvalds 	if (pid < 0) {
14263920f47SJeff Dike 		err = pid;
14363920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : run_helper failed, "
14463920f47SJeff Dike 		       "errno = %d\n", -err);
14563920f47SJeff Dike 		goto out_close1;
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	err = os_set_fd_block(fd, 0);
1491da177e4SLinus Torvalds 	if (err < 0) {
15063920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
1511da177e4SLinus Torvalds 		       "non-blocking, err = %d\n", -err);
15263920f47SJeff Dike 		goto out_kill;
1531da177e4SLinus Torvalds 	}
15463920f47SJeff Dike 
1559431f7c1SAnton Ivanov 	data->chan_fd = fd;
1561da177e4SLinus Torvalds 	new = xterm_fd(fd, &data->helper_pid);
1571da177e4SLinus Torvalds 	if (new < 0) {
15863920f47SJeff Dike 		err = new;
15963920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n",
16063920f47SJeff Dike 		       -err);
16163920f47SJeff Dike 		goto out_kill;
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds 
16489df6bfcSEduard-Gabriel Munteanu 	err = os_set_fd_block(new, 0);
16589df6bfcSEduard-Gabriel Munteanu 	if (err) {
16663920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : failed to set xterm "
16763920f47SJeff Dike 		       "descriptor non-blocking, err = %d\n", -err);
16863920f47SJeff Dike 		goto out_close2;
16989df6bfcSEduard-Gabriel Munteanu 	}
17089df6bfcSEduard-Gabriel Munteanu 
1711da177e4SLinus Torvalds 	CATCH_EINTR(err = tcgetattr(new, &data->tt));
1721da177e4SLinus Torvalds 	if (err) {
1731da177e4SLinus Torvalds 		new = err;
17463920f47SJeff Dike 		goto out_close2;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (data->raw) {
1781da177e4SLinus Torvalds 		err = raw(new);
1791da177e4SLinus Torvalds 		if (err) {
1801da177e4SLinus Torvalds 			new = err;
18163920f47SJeff Dike 			goto out_close2;
1821da177e4SLinus Torvalds 		}
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 
18563920f47SJeff Dike 	unlink(file);
1861da177e4SLinus Torvalds 	data->pid = pid;
1871da177e4SLinus Torvalds 	*dev_out = NULL;
18863920f47SJeff Dike 
18963920f47SJeff Dike 	return new;
19063920f47SJeff Dike 
19163920f47SJeff Dike  out_close2:
19263920f47SJeff Dike 	close(new);
19363920f47SJeff Dike  out_kill:
19463920f47SJeff Dike 	os_kill_process(pid, 1);
19563920f47SJeff Dike  out_close1:
19663920f47SJeff Dike 	close(fd);
19763920f47SJeff Dike 
19863920f47SJeff Dike 	return err;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
xterm_close(int fd,void * d)20163920f47SJeff Dike static void xterm_close(int fd, void *d)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	struct xterm_chan *data = d;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	if (data->pid != -1)
2061da177e4SLinus Torvalds 		os_kill_process(data->pid, 1);
2071da177e4SLinus Torvalds 	data->pid = -1;
20863920f47SJeff Dike 
2091da177e4SLinus Torvalds 	if (data->helper_pid != -1)
2101da177e4SLinus Torvalds 		os_kill_process(data->helper_pid, 0);
2111da177e4SLinus Torvalds 	data->helper_pid = -1;
21263920f47SJeff Dike 
2139431f7c1SAnton Ivanov 	if (data->chan_fd != -1)
2149431f7c1SAnton Ivanov 		os_close_file(data->chan_fd);
2151da177e4SLinus Torvalds 	os_close_file(fd);
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2185e7672ecSJeff Dike const struct chan_ops xterm_ops = {
2191da177e4SLinus Torvalds 	.type		= "xterm",
2201da177e4SLinus Torvalds 	.init		= xterm_init,
2211da177e4SLinus Torvalds 	.open		= xterm_open,
2221da177e4SLinus Torvalds 	.close		= xterm_close,
2231da177e4SLinus Torvalds 	.read		= generic_read,
2241da177e4SLinus Torvalds 	.write		= generic_write,
225fd9bc53bSPaolo 'Blaisorblade' Giarrusso 	.console_write	= generic_console_write,
2261da177e4SLinus Torvalds 	.window_size	= generic_window_size,
227e99525f9SJeff Dike 	.free		= generic_free,
2281da177e4SLinus Torvalds 	.winch		= 1,
2291da177e4SLinus Torvalds };
230