xref: /openbmc/linux/arch/um/drivers/xterm.c (revision e99525f9706900417f37721e601d2b414d41bfee)
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 
6*e99525f9SJeff Dike #include <stddef.h>
71da177e4SLinus Torvalds #include <stdlib.h>
863920f47SJeff Dike #include <stdio.h>
91da177e4SLinus Torvalds #include <unistd.h>
101da177e4SLinus Torvalds #include <errno.h>
11*e99525f9SJeff Dike #include <string.h>
121da177e4SLinus Torvalds #include <termios.h>
131da177e4SLinus Torvalds #include "chan_user.h"
14*e99525f9SJeff Dike #include "kern_constants.h"
151da177e4SLinus Torvalds #include "os.h"
16*e99525f9SJeff Dike #include "um_malloc.h"
1763920f47SJeff Dike #include "user.h"
181da177e4SLinus Torvalds #include "xterm.h"
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds struct xterm_chan {
211da177e4SLinus Torvalds 	int pid;
221da177e4SLinus Torvalds 	int helper_pid;
231da177e4SLinus Torvalds 	char *title;
241da177e4SLinus Torvalds 	int device;
251da177e4SLinus Torvalds 	int raw;
261da177e4SLinus Torvalds 	struct termios tt;
271da177e4SLinus Torvalds };
281da177e4SLinus Torvalds 
2963920f47SJeff Dike static void *xterm_init(char *str, int device, const struct chan_opts *opts)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	struct xterm_chan *data;
321da177e4SLinus Torvalds 
33*e99525f9SJeff Dike 	data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
3463920f47SJeff Dike 	if (data == NULL)
3563920f47SJeff Dike 		return NULL;
361da177e4SLinus Torvalds 	*data = ((struct xterm_chan) { .pid 		= -1,
371da177e4SLinus Torvalds 				       .helper_pid 	= -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 */
451da177e4SLinus Torvalds static char *terminal_emulator = "xterm";
461da177e4SLinus Torvalds static char *title_switch = "-T";
471da177e4SLinus Torvalds static char *exec_switch = "-e";
481da177e4SLinus Torvalds 
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"
821da177e4SLinus Torvalds "    The default values are 'xterm=xterm,-T,-e'.  Values for gnome-terminal\n"
831da177e4SLinus Torvalds "    are 'xterm=gnome-terminal,-t,-x'.\n\n"
841da177e4SLinus Torvalds );
851da177e4SLinus Torvalds 
8663920f47SJeff Dike static int xterm_open(int input, int output, int primary, void *d,
871da177e4SLinus Torvalds 		      char **dev_out)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	struct xterm_chan *data = d;
901da177e4SLinus Torvalds 	int pid, fd, new, err;
911da177e4SLinus Torvalds 	char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
921da177e4SLinus Torvalds 	char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
931da177e4SLinus Torvalds 			 "/usr/lib/uml/port-helper", "-uml-socket",
941da177e4SLinus Torvalds 			 file, NULL };
951da177e4SLinus Torvalds 
9663920f47SJeff Dike 	if (access(argv[4], X_OK) < 0)
971da177e4SLinus Torvalds 		argv[4] = "port-helper";
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	/* Check that DISPLAY is set, this doesn't guarantee the xterm
1001da177e4SLinus Torvalds 	 * will work but w/o it we can be pretty sure it won't. */
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);
124b4fd310eSJeff Dike 		return err;
1251da177e4SLinus Torvalds 	}
12663920f47SJeff Dike 	close(fd);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	fd = os_create_unix_socket(file, sizeof(file), 1);
1291da177e4SLinus Torvalds 	if (fd < 0) {
13063920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
13163920f47SJeff Dike 		       "errno = %d\n", -fd);
13263920f47SJeff Dike 		return fd;
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	sprintf(title, data->title, data->device);
136c4399016SJeff Dike 	pid = run_helper(NULL, NULL, argv);
1371da177e4SLinus Torvalds 	if (pid < 0) {
13863920f47SJeff Dike 		err = pid;
13963920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : run_helper failed, "
14063920f47SJeff Dike 		       "errno = %d\n", -err);
14163920f47SJeff Dike 		goto out_close1;
1421da177e4SLinus Torvalds 	}
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	err = os_set_fd_block(fd, 0);
1451da177e4SLinus Torvalds 	if (err < 0) {
14663920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
1471da177e4SLinus Torvalds 		       "non-blocking, err = %d\n", -err);
14863920f47SJeff Dike 		goto out_kill;
1491da177e4SLinus Torvalds 	}
15063920f47SJeff Dike 
1511da177e4SLinus Torvalds 	new = xterm_fd(fd, &data->helper_pid);
1521da177e4SLinus Torvalds 	if (new < 0) {
15363920f47SJeff Dike 		err = new;
15463920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n",
15563920f47SJeff Dike 		       -err);
15663920f47SJeff Dike 		goto out_kill;
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 
15989df6bfcSEduard-Gabriel Munteanu 	err = os_set_fd_block(new, 0);
16089df6bfcSEduard-Gabriel Munteanu 	if (err) {
16163920f47SJeff Dike 		printk(UM_KERN_ERR "xterm_open : failed to set xterm "
16263920f47SJeff Dike 		       "descriptor non-blocking, err = %d\n", -err);
16363920f47SJeff Dike 		goto out_close2;
16489df6bfcSEduard-Gabriel Munteanu 	}
16589df6bfcSEduard-Gabriel Munteanu 
1661da177e4SLinus Torvalds 	CATCH_EINTR(err = tcgetattr(new, &data->tt));
1671da177e4SLinus Torvalds 	if (err) {
1681da177e4SLinus Torvalds 		new = err;
16963920f47SJeff Dike 		goto out_close2;
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	if (data->raw) {
1731da177e4SLinus Torvalds 		err = raw(new);
1741da177e4SLinus Torvalds 		if (err) {
1751da177e4SLinus Torvalds 			new = err;
17663920f47SJeff Dike 			goto out_close2;
1771da177e4SLinus Torvalds 		}
1781da177e4SLinus Torvalds 	}
1791da177e4SLinus Torvalds 
18063920f47SJeff Dike 	unlink(file);
1811da177e4SLinus Torvalds 	data->pid = pid;
1821da177e4SLinus Torvalds 	*dev_out = NULL;
18363920f47SJeff Dike 
18463920f47SJeff Dike 	return new;
18563920f47SJeff Dike 
18663920f47SJeff Dike  out_close2:
18763920f47SJeff Dike 	close(new);
18863920f47SJeff Dike  out_kill:
18963920f47SJeff Dike 	os_kill_process(pid, 1);
19063920f47SJeff Dike  out_close1:
19163920f47SJeff Dike 	close(fd);
19263920f47SJeff Dike 
19363920f47SJeff Dike 	return err;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
19663920f47SJeff Dike static void xterm_close(int fd, void *d)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct xterm_chan *data = d;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	if (data->pid != -1)
2011da177e4SLinus Torvalds 		os_kill_process(data->pid, 1);
2021da177e4SLinus Torvalds 	data->pid = -1;
20363920f47SJeff Dike 
2041da177e4SLinus Torvalds 	if (data->helper_pid != -1)
2051da177e4SLinus Torvalds 		os_kill_process(data->helper_pid, 0);
2061da177e4SLinus Torvalds 	data->helper_pid = -1;
20763920f47SJeff Dike 
2081da177e4SLinus Torvalds 	os_close_file(fd);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2115e7672ecSJeff Dike const struct chan_ops xterm_ops = {
2121da177e4SLinus Torvalds 	.type		= "xterm",
2131da177e4SLinus Torvalds 	.init		= xterm_init,
2141da177e4SLinus Torvalds 	.open		= xterm_open,
2151da177e4SLinus Torvalds 	.close		= xterm_close,
2161da177e4SLinus Torvalds 	.read		= generic_read,
2171da177e4SLinus Torvalds 	.write		= generic_write,
218fd9bc53bSPaolo 'Blaisorblade' Giarrusso 	.console_write	= generic_console_write,
2191da177e4SLinus Torvalds 	.window_size	= generic_window_size,
220*e99525f9SJeff Dike 	.free		= generic_free,
2211da177e4SLinus Torvalds 	.winch		= 1,
2221da177e4SLinus Torvalds };
223