2019-08-25 17:49:16 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2007-10-16 16:27:34 +08:00
|
|
|
/*
|
2007-07-16 14:38:53 +08:00
|
|
|
* Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2007-05-07 05:51:09 +08:00
|
|
|
#include <stdlib.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2007-10-16 16:27:34 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <termios.h>
|
2007-07-16 14:38:53 +08:00
|
|
|
#include <sys/stat.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include "chan_user.h"
|
2012-10-08 10:27:32 +08:00
|
|
|
#include <os.h>
|
|
|
|
#include <um_malloc.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
struct pty_chan {
|
|
|
|
void (*announce)(char *dev_name, int dev);
|
|
|
|
int dev;
|
|
|
|
int raw;
|
|
|
|
struct termios tt;
|
|
|
|
char dev_name[sizeof("/dev/pts/0123456\0")];
|
|
|
|
};
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
static void *pty_chan_init(char *str, int device, const struct chan_opts *opts)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct pty_chan *data;
|
|
|
|
|
2008-05-13 05:01:52 +08:00
|
|
|
data = uml_kmalloc(sizeof(*data), UM_GFP_KERNEL);
|
2007-07-16 14:38:53 +08:00
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
*data = ((struct pty_chan) { .announce = opts->announce,
|
2005-04-17 06:20:36 +08:00
|
|
|
.dev = device,
|
|
|
|
.raw = opts->raw });
|
2007-07-16 14:38:53 +08:00
|
|
|
return data;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pts_open(int input, int output, int primary, void *d,
|
|
|
|
char **dev_out)
|
|
|
|
{
|
|
|
|
struct pty_chan *data = d;
|
|
|
|
char *dev;
|
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
fd = get_pty();
|
2007-07-16 14:38:53 +08: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 10:27:49 +08:00
|
|
|
err = -errno;
|
2007-07-16 14:38:53 +08:00
|
|
|
printk(UM_KERN_ERR "open_pts : Failed to open pts\n");
|
[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 10:27:49 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-07-16 14:38:53 +08:00
|
|
|
|
|
|
|
if (data->raw) {
|
2005-04-17 06:20:36 +08:00
|
|
|
CATCH_EINTR(err = tcgetattr(fd, &data->tt));
|
2007-07-16 14:38:53 +08:00
|
|
|
if (err)
|
2007-10-16 16:26:41 +08:00
|
|
|
goto out_close;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
err = raw(fd);
|
2007-07-16 14:38:53 +08:00
|
|
|
if (err)
|
2007-10-16 16:26:41 +08:00
|
|
|
goto out_close;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dev = ptsname(fd);
|
|
|
|
sprintf(data->dev_name, "%s", dev);
|
|
|
|
*dev_out = data->dev_name;
|
2007-07-16 14:38:53 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (data->announce)
|
|
|
|
(*data->announce)(dev, data->dev);
|
2007-07-16 14:38:53 +08:00
|
|
|
|
|
|
|
return fd;
|
2007-10-16 16:26:41 +08:00
|
|
|
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int getmaster(char *line)
|
|
|
|
{
|
2007-07-16 14:38:53 +08:00
|
|
|
struct stat buf;
|
2005-04-17 06:20:36 +08:00
|
|
|
char *pty, *bank, *cp;
|
|
|
|
int master, err;
|
|
|
|
|
|
|
|
pty = &line[strlen("/dev/ptyp")];
|
|
|
|
for (bank = "pqrs"; *bank; bank++) {
|
|
|
|
line[strlen("/dev/pty")] = *bank;
|
|
|
|
*pty = '0';
|
2007-07-16 14:38:53 +08:00
|
|
|
/* Did we hit the end ? */
|
|
|
|
if ((stat(line, &buf) < 0) && (errno == ENOENT))
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
2007-07-16 14:38:53 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
for (cp = "0123456789abcdef"; *cp; cp++) {
|
|
|
|
*pty = *cp;
|
2007-07-16 14:38:53 +08:00
|
|
|
master = open(line, O_RDWR);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (master >= 0) {
|
|
|
|
char *tp = &line[strlen("/dev/")];
|
|
|
|
|
|
|
|
/* verify slave side is usable */
|
|
|
|
*tp = 't';
|
2007-07-16 14:38:53 +08:00
|
|
|
err = access(line, R_OK | W_OK);
|
2005-04-17 06:20:36 +08:00
|
|
|
*tp = 'p';
|
2007-10-16 16:27:34 +08:00
|
|
|
if (!err)
|
2007-07-16 14:38:53 +08:00
|
|
|
return master;
|
|
|
|
close(master);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-16 14:38:53 +08:00
|
|
|
|
|
|
|
printk(UM_KERN_ERR "getmaster - no usable host pty devices\n");
|
|
|
|
return -ENOENT;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pty_open(int input, int output, int primary, void *d,
|
|
|
|
char **dev_out)
|
|
|
|
{
|
|
|
|
struct pty_chan *data = d;
|
|
|
|
int fd, err;
|
|
|
|
char dev[sizeof("/dev/ptyxx\0")] = "/dev/ptyxx";
|
|
|
|
|
|
|
|
fd = getmaster(dev);
|
2007-07-16 14:38:53 +08:00
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
if (data->raw) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = raw(fd);
|
2007-10-16 16:26:41 +08:00
|
|
|
if (err) {
|
|
|
|
close(fd);
|
2007-07-16 14:38:53 +08:00
|
|
|
return err;
|
2007-10-16 16:26:41 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-10-16 16:27:34 +08:00
|
|
|
|
2007-07-16 14:38:53 +08:00
|
|
|
if (data->announce)
|
|
|
|
(*data->announce)(dev, data->dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
sprintf(data->dev_name, "%s", dev);
|
|
|
|
*dev_out = data->dev_name;
|
2007-07-16 14:38:53 +08:00
|
|
|
|
|
|
|
return fd;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
const struct chan_ops pty_ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.type = "pty",
|
|
|
|
.init = pty_chan_init,
|
|
|
|
.open = pty_open,
|
|
|
|
.close = generic_close,
|
|
|
|
.read = generic_read,
|
|
|
|
.write = generic_write,
|
2005-11-14 08:07:10 +08:00
|
|
|
.console_write = generic_console_write,
|
2005-04-17 06:20:36 +08:00
|
|
|
.window_size = generic_window_size,
|
|
|
|
.free = generic_free,
|
|
|
|
.winch = 0,
|
|
|
|
};
|
|
|
|
|
2006-09-27 16:50:33 +08:00
|
|
|
const struct chan_ops pts_ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.type = "pts",
|
|
|
|
.init = pty_chan_init,
|
|
|
|
.open = pts_open,
|
|
|
|
.close = generic_close,
|
|
|
|
.read = generic_read,
|
|
|
|
.write = generic_write,
|
2005-11-14 08:07:10 +08:00
|
|
|
.console_write = generic_console_write,
|
2005-04-17 06:20:36 +08:00
|
|
|
.window_size = generic_window_size,
|
|
|
|
.free = generic_free,
|
|
|
|
.winch = 0,
|
|
|
|
};
|