2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2007-07-16 06:38:52 +00:00
|
|
|
* Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 22:20:36 +00:00
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
2007-10-16 08:26:41 +00:00
|
|
|
#include <stddef.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <stdlib.h>
|
2007-07-16 06:38:52 +00:00
|
|
|
#include <stdio.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2007-10-16 08:26:41 +00:00
|
|
|
#include <string.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include "chan_user.h"
|
2007-10-16 08:26:41 +00:00
|
|
|
#include "kern_constants.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "os.h"
|
2007-10-16 08:26:41 +00:00
|
|
|
#include "um_malloc.h"
|
2007-07-16 06:38:52 +00:00
|
|
|
#include "user.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "xterm.h"
|
|
|
|
|
|
|
|
struct xterm_chan {
|
|
|
|
int pid;
|
|
|
|
int helper_pid;
|
|
|
|
char *title;
|
|
|
|
int device;
|
|
|
|
int raw;
|
|
|
|
struct termios tt;
|
|
|
|
};
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
static void *xterm_init(char *str, int device, const struct chan_opts *opts)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct xterm_chan *data;
|
|
|
|
|
2007-10-16 08:26:41 +00:00
|
|
|
data = kmalloc(sizeof(*data), UM_GFP_KERNEL);
|
2007-07-16 06:38:52 +00:00
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
*data = ((struct xterm_chan) { .pid = -1,
|
2005-04-16 22:20:36 +00:00
|
|
|
.helper_pid = -1,
|
2007-07-16 06:38:52 +00:00
|
|
|
.device = device,
|
2005-04-16 22:20:36 +00:00
|
|
|
.title = opts->xterm_title,
|
2007-07-16 06:38:52 +00:00
|
|
|
.raw = opts->raw } );
|
|
|
|
return data;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Only changed by xterm_setup, which is a setup */
|
|
|
|
static char *terminal_emulator = "xterm";
|
|
|
|
static char *title_switch = "-T";
|
|
|
|
static char *exec_switch = "-e";
|
|
|
|
|
|
|
|
static int __init xterm_setup(char *line, int *add)
|
|
|
|
{
|
|
|
|
*add = 0;
|
|
|
|
terminal_emulator = line;
|
|
|
|
|
|
|
|
line = strchr(line, ',');
|
2007-07-16 06:38:52 +00:00
|
|
|
if (line == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
*line++ = '\0';
|
2007-07-16 06:38:52 +00:00
|
|
|
if (*line)
|
|
|
|
title_switch = line;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
line = strchr(line, ',');
|
2007-07-16 06:38:52 +00:00
|
|
|
if (line == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
*line++ = '\0';
|
2007-07-16 06:38:52 +00:00
|
|
|
if (*line)
|
|
|
|
exec_switch = line;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__uml_setup("xterm=", xterm_setup,
|
|
|
|
"xterm=<terminal emulator>,<title switch>,<exec switch>\n"
|
|
|
|
" Specifies an alternate terminal emulator to use for the debugger,\n"
|
|
|
|
" consoles, and serial lines when they are attached to the xterm channel.\n"
|
|
|
|
" The values are the terminal emulator binary, the switch it uses to set\n"
|
|
|
|
" its title, and the switch it uses to execute a subprocess,\n"
|
|
|
|
" respectively. The title switch must have the form '<switch> title',\n"
|
|
|
|
" not '<switch>=title'. Similarly, the exec switch must have the form\n"
|
|
|
|
" '<switch> command arg1 arg2 ...'.\n"
|
|
|
|
" The default values are 'xterm=xterm,-T,-e'. Values for gnome-terminal\n"
|
|
|
|
" are 'xterm=gnome-terminal,-t,-x'.\n\n"
|
|
|
|
);
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
static int xterm_open(int input, int output, int primary, void *d,
|
2005-04-16 22:20:36 +00:00
|
|
|
char **dev_out)
|
|
|
|
{
|
|
|
|
struct xterm_chan *data = d;
|
|
|
|
int pid, fd, new, err;
|
|
|
|
char title[256], file[] = "/tmp/xterm-pipeXXXXXX";
|
2007-07-16 06:38:52 +00:00
|
|
|
char *argv[] = { terminal_emulator, title_switch, title, exec_switch,
|
2005-04-16 22:20:36 +00:00
|
|
|
"/usr/lib/uml/port-helper", "-uml-socket",
|
|
|
|
file, NULL };
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
if (access(argv[4], X_OK) < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
argv[4] = "port-helper";
|
|
|
|
|
|
|
|
/* Check that DISPLAY is set, this doesn't guarantee the xterm
|
|
|
|
* will work but w/o it we can be pretty sure it won't. */
|
2007-07-16 06:38:52 +00:00
|
|
|
if (getenv("DISPLAY") == NULL) {
|
|
|
|
printk(UM_KERN_ERR "xterm_open: $DISPLAY not set.\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
/*
|
|
|
|
* This business of getting a descriptor to a temp file,
|
|
|
|
* deleting the file and closing the descriptor is just to get
|
|
|
|
* a known-unused name for the Unix socket that we really
|
|
|
|
* want.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
fd = mkstemp(file);
|
2007-07-16 06:38:52 +00:00
|
|
|
if (fd < 0) {
|
[PATCH] uml: preserve errno in error paths
The poster child for this patch is the third tuntap_user hunk. When an ioctl
fails, it properly closes the opened file descriptor and returns. However,
the close resets errno to 0, and the 'return errno' that follows returns 0
rather than the value that ioctl set. This caused the caller to believe that
the device open succeeded and had opened file descriptor 0, which caused no
end of interesting behavior.
The rest of this patch is a pass through the UML sources looking for places
where errno could be reset before being passed back out. A common culprit is
printk, which could call write, being called before errno is returned.
In some cases, where the code ends up being much smaller, I just deleted the
printk.
There was another case where a caller of run_helper looked at errno after a
failure, rather than the return value of run_helper, which was the errno value
that it wanted.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-17 02:27:49 +00:00
|
|
|
err = -errno;
|
2007-07-16 06:38:52 +00:00
|
|
|
printk(UM_KERN_ERR "xterm_open : mkstemp failed, errno = %d\n",
|
|
|
|
errno);
|
[PATCH] uml: preserve errno in error paths
The poster child for this patch is the third tuntap_user hunk. When an ioctl
fails, it properly closes the opened file descriptor and returns. However,
the close resets errno to 0, and the 'return errno' that follows returns 0
rather than the value that ioctl set. This caused the caller to believe that
the device open succeeded and had opened file descriptor 0, which caused no
end of interesting behavior.
The rest of this patch is a pass through the UML sources looking for places
where errno could be reset before being passed back out. A common culprit is
printk, which could call write, being called before errno is returned.
In some cases, where the code ends up being much smaller, I just deleted the
printk.
There was another case where a caller of run_helper looked at errno after a
failure, rather than the return value of run_helper, which was the errno value
that it wanted.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-17 02:27:49 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
if (unlink(file)) {
|
[PATCH] uml: preserve errno in error paths
The poster child for this patch is the third tuntap_user hunk. When an ioctl
fails, it properly closes the opened file descriptor and returns. However,
the close resets errno to 0, and the 'return errno' that follows returns 0
rather than the value that ioctl set. This caused the caller to believe that
the device open succeeded and had opened file descriptor 0, which caused no
end of interesting behavior.
The rest of this patch is a pass through the UML sources looking for places
where errno could be reset before being passed back out. A common culprit is
printk, which could call write, being called before errno is returned.
In some cases, where the code ends up being much smaller, I just deleted the
printk.
There was another case where a caller of run_helper looked at errno after a
failure, rather than the return value of run_helper, which was the errno value
that it wanted.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-17 02:27:49 +00:00
|
|
|
err = -errno;
|
2007-07-16 06:38:52 +00:00
|
|
|
printk(UM_KERN_ERR "xterm_open : unlink failed, errno = %d\n",
|
|
|
|
errno);
|
[PATCH] uml: preserve errno in error paths
The poster child for this patch is the third tuntap_user hunk. When an ioctl
fails, it properly closes the opened file descriptor and returns. However,
the close resets errno to 0, and the 'return errno' that follows returns 0
rather than the value that ioctl set. This caused the caller to believe that
the device open succeeded and had opened file descriptor 0, which caused no
end of interesting behavior.
The rest of this patch is a pass through the UML sources looking for places
where errno could be reset before being passed back out. A common culprit is
printk, which could call write, being called before errno is returned.
In some cases, where the code ends up being much smaller, I just deleted the
printk.
There was another case where a caller of run_helper looked at errno after a
failure, rather than the return value of run_helper, which was the errno value
that it wanted.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-17 02:27:49 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-07-16 06:38:52 +00:00
|
|
|
close(fd);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
fd = os_create_unix_socket(file, sizeof(file), 1);
|
2007-07-16 06:38:52 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
printk(UM_KERN_ERR "xterm_open : create_unix_socket failed, "
|
|
|
|
"errno = %d\n", -fd);
|
|
|
|
return fd;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(title, data->title, data->device);
|
2007-07-16 06:38:56 +00:00
|
|
|
pid = run_helper(NULL, NULL, argv);
|
2007-07-16 06:38:52 +00:00
|
|
|
if (pid < 0) {
|
|
|
|
err = pid;
|
|
|
|
printk(UM_KERN_ERR "xterm_open : run_helper failed, "
|
|
|
|
"errno = %d\n", -err);
|
|
|
|
goto out_close1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
err = os_set_fd_block(fd, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "xterm_open : failed to set descriptor "
|
|
|
|
"non-blocking, err = %d\n", -err);
|
|
|
|
goto out_kill;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-07-16 06:38:52 +00:00
|
|
|
|
|
|
|
new = xterm_fd(fd, &data->helper_pid);
|
|
|
|
if (new < 0) {
|
|
|
|
err = new;
|
|
|
|
printk(UM_KERN_ERR "xterm_open : os_rcv_fd failed, err = %d\n",
|
|
|
|
-err);
|
|
|
|
goto out_kill;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:51 +00:00
|
|
|
err = os_set_fd_block(new, 0);
|
|
|
|
if (err) {
|
2007-07-16 06:38:52 +00:00
|
|
|
printk(UM_KERN_ERR "xterm_open : failed to set xterm "
|
|
|
|
"descriptor non-blocking, err = %d\n", -err);
|
|
|
|
goto out_close2;
|
2007-07-16 06:38:51 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
CATCH_EINTR(err = tcgetattr(new, &data->tt));
|
2007-07-16 06:38:52 +00:00
|
|
|
if (err) {
|
2005-04-16 22:20:36 +00:00
|
|
|
new = err;
|
2007-07-16 06:38:52 +00:00
|
|
|
goto out_close2;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
if (data->raw) {
|
2005-04-16 22:20:36 +00:00
|
|
|
err = raw(new);
|
2007-07-16 06:38:52 +00:00
|
|
|
if (err) {
|
2005-04-16 22:20:36 +00:00
|
|
|
new = err;
|
2007-07-16 06:38:52 +00:00
|
|
|
goto out_close2;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
unlink(file);
|
2005-04-16 22:20:36 +00:00
|
|
|
data->pid = pid;
|
|
|
|
*dev_out = NULL;
|
2007-07-16 06:38:52 +00:00
|
|
|
|
|
|
|
return new;
|
|
|
|
|
|
|
|
out_close2:
|
|
|
|
close(new);
|
|
|
|
out_kill:
|
|
|
|
os_kill_process(pid, 1);
|
|
|
|
out_close1:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
static void xterm_close(int fd, void *d)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct xterm_chan *data = d;
|
|
|
|
|
2007-07-16 06:38:52 +00:00
|
|
|
if (data->pid != -1)
|
2005-04-16 22:20:36 +00:00
|
|
|
os_kill_process(data->pid, 1);
|
|
|
|
data->pid = -1;
|
2007-07-16 06:38:52 +00:00
|
|
|
|
|
|
|
if (data->helper_pid != -1)
|
2005-04-16 22:20:36 +00:00
|
|
|
os_kill_process(data->helper_pid, 0);
|
|
|
|
data->helper_pid = -1;
|
2007-07-16 06:38:52 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
os_close_file(fd);
|
|
|
|
}
|
|
|
|
|
2006-09-27 08:50:33 +00:00
|
|
|
const struct chan_ops xterm_ops = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.type = "xterm",
|
|
|
|
.init = xterm_init,
|
|
|
|
.open = xterm_open,
|
|
|
|
.close = xterm_close,
|
|
|
|
.read = generic_read,
|
|
|
|
.write = generic_write,
|
2005-11-14 00:07:10 +00:00
|
|
|
.console_write = generic_console_write,
|
2005-04-16 22:20:36 +00:00
|
|
|
.window_size = generic_window_size,
|
2007-10-16 08:26:41 +00:00
|
|
|
.free = generic_free,
|
2005-04-16 22:20:36 +00:00
|
|
|
.winch = 1,
|
|
|
|
};
|