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>
27 static void *xterm_init(char *str
, int device
, const struct chan_opts
*opts
)
29 struct xterm_chan
*data
;
31 data
= uml_kmalloc(sizeof(*data
), UM_GFP_KERNEL
);
34 *data
= ((struct xterm_chan
) { .pid
= -1,
37 .title
= opts
->xterm_title
,
42 /* Only changed by xterm_setup, which is a setup */
43 static char *terminal_emulator
= "xterm";
44 static char *title_switch
= "-T";
45 static char *exec_switch
= "-e";
47 static int __init
xterm_setup(char *line
, int *add
)
50 terminal_emulator
= line
;
52 line
= strchr(line
, ',');
60 line
= strchr(line
, ',');
71 __uml_setup("xterm=", xterm_setup
,
72 "xterm=<terminal emulator>,<title switch>,<exec switch>\n"
73 " Specifies an alternate terminal emulator to use for the debugger,\n"
74 " consoles, and serial lines when they are attached to the xterm channel.\n"
75 " The values are the terminal emulator binary, the switch it uses to set\n"
76 " its title, and the switch it uses to execute a subprocess,\n"
77 " respectively. The title switch must have the form '<switch> title',\n"
78 " not '<switch>=title'. Similarly, the exec switch must have the form\n"
79 " '<switch> command arg1 arg2 ...'.\n"
80 " The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n"
81 " are 'xterm=gnome-terminal,-t,-x'.\n\n"
84 static int xterm_open(int input
, int output
, int primary
, void *d
,
87 struct xterm_chan
*data
= d
;
88 int pid
, fd
, new, err
;
89 char title
[256], file
[] = "/tmp/xterm-pipeXXXXXX";
90 char *argv
[] = { terminal_emulator
, title_switch
, title
, exec_switch
,
91 OS_LIB_PATH
"/uml/port-helper", "-uml-socket",
94 if (access(argv
[4], X_OK
) < 0)
95 argv
[4] = "port-helper";
98 * Check that DISPLAY is set, this doesn't guarantee the xterm
99 * will work but w/o it we can be pretty sure it won't.
101 if (getenv("DISPLAY") == NULL
) {
102 printk(UM_KERN_ERR
"xterm_open: $DISPLAY not set.\n");
107 * This business of getting a descriptor to a temp file,
108 * deleting the file and closing the descriptor is just to get
109 * a known-unused name for the Unix socket that we really
115 printk(UM_KERN_ERR
"xterm_open : mkstemp failed, errno = %d\n",
122 printk(UM_KERN_ERR
"xterm_open : unlink failed, errno = %d\n",
129 fd
= os_create_unix_socket(file
, sizeof(file
), 1);
131 printk(UM_KERN_ERR
"xterm_open : create_unix_socket failed, "
132 "errno = %d\n", -fd
);
136 sprintf(title
, data
->title
, data
->device
);
137 pid
= run_helper(NULL
, NULL
, argv
);
140 printk(UM_KERN_ERR
"xterm_open : run_helper failed, "
141 "errno = %d\n", -err
);
145 err
= os_set_fd_block(fd
, 0);
147 printk(UM_KERN_ERR
"xterm_open : failed to set descriptor "
148 "non-blocking, err = %d\n", -err
);
152 new = xterm_fd(fd
, &data
->helper_pid
);
155 printk(UM_KERN_ERR
"xterm_open : os_rcv_fd failed, err = %d\n",
160 err
= os_set_fd_block(new, 0);
162 printk(UM_KERN_ERR
"xterm_open : failed to set xterm "
163 "descriptor non-blocking, err = %d\n", -err
);
167 CATCH_EINTR(err
= tcgetattr(new, &data
->tt
));
190 os_kill_process(pid
, 1);
197 static void xterm_close(int fd
, void *d
)
199 struct xterm_chan
*data
= d
;
202 os_kill_process(data
->pid
, 1);
205 if (data
->helper_pid
!= -1)
206 os_kill_process(data
->helper_pid
, 0);
207 data
->helper_pid
= -1;
212 const struct chan_ops xterm_ops
= {
216 .close
= xterm_close
,
217 .read
= generic_read
,
218 .write
= generic_write
,
219 .console_write
= generic_console_write
,
220 .window_size
= generic_window_size
,
221 .free
= generic_free
,