2006-07-10 11:45:14 +00:00
|
|
|
/*
|
2008-02-05 06:31:05 +00:00
|
|
|
* Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 22:20:36 +00:00
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
2008-02-05 06:31:05 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
2012-10-08 02:27:32 +00:00
|
|
|
#include <os.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
static void copy_stat(struct uml_stat *dst, const struct stat64 *src)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
*dst = ((struct uml_stat) {
|
|
|
|
.ust_dev = src->st_dev, /* device */
|
|
|
|
.ust_ino = src->st_ino, /* inode */
|
|
|
|
.ust_mode = src->st_mode, /* protection */
|
|
|
|
.ust_nlink = src->st_nlink, /* number of hard links */
|
|
|
|
.ust_uid = src->st_uid, /* user ID of owner */
|
|
|
|
.ust_gid = src->st_gid, /* group ID of owner */
|
|
|
|
.ust_size = src->st_size, /* total size, in bytes */
|
|
|
|
.ust_blksize = src->st_blksize, /* blocksize for filesys I/O */
|
|
|
|
.ust_blocks = src->st_blocks, /* number of blocks allocated */
|
|
|
|
.ust_atime = src->st_atime, /* time of last access */
|
|
|
|
.ust_mtime = src->st_mtime, /* time of last modification */
|
|
|
|
.ust_ctime = src->st_ctime, /* time of last change */
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_stat_fd(const int fd, struct uml_stat *ubuf)
|
|
|
|
{
|
|
|
|
struct stat64 sbuf;
|
|
|
|
int err;
|
|
|
|
|
2006-07-10 11:45:15 +00:00
|
|
|
CATCH_EINTR(err = fstat64(fd, &sbuf));
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (ubuf != NULL)
|
2005-04-16 22:20:36 +00:00
|
|
|
copy_stat(ubuf, &sbuf);
|
2006-07-10 11:45:14 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_stat_file(const char *file_name, struct uml_stat *ubuf)
|
|
|
|
{
|
|
|
|
struct stat64 sbuf;
|
|
|
|
int err;
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
CATCH_EINTR(err = stat64(file_name, &sbuf));
|
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (ubuf != NULL)
|
2005-04-16 22:20:36 +00:00
|
|
|
copy_stat(ubuf, &sbuf);
|
2006-07-10 11:45:14 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
int os_access(const char *file, int mode)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int amode, err;
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
amode = (mode & OS_ACC_R_OK ? R_OK : 0) |
|
|
|
|
(mode & OS_ACC_W_OK ? W_OK : 0) |
|
|
|
|
(mode & OS_ACC_X_OK ? X_OK : 0) |
|
|
|
|
(mode & OS_ACC_F_OK ? F_OK : 0);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err = access(file, amode);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME? required only by hostaudio (because it passes ioctls verbatim) */
|
|
|
|
int os_ioctl_generic(int fd, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = ioctl(fd, cmd, arg);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: ensure namebuf in os_get_if_name is big enough */
|
|
|
|
int os_get_ifname(int fd, char* namebuf)
|
|
|
|
{
|
2008-02-05 06:31:05 +00:00
|
|
|
if (ioctl(fd, SIOCGIFNAME, namebuf) < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_set_slip(int fd)
|
|
|
|
{
|
|
|
|
int disc, sencap;
|
|
|
|
|
|
|
|
disc = N_SLIP;
|
2008-02-05 06:31:05 +00:00
|
|
|
if (ioctl(fd, TIOCSETD, &disc) < 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
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
sencap = 0;
|
2008-02-05 06:31:05 +00:00
|
|
|
if (ioctl(fd, SIOCSIFENCAP, &sencap) < 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
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_mode_fd(int fd, int mode)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
CATCH_EINTR(err = fchmod(fd, mode));
|
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_file_type(char *file)
|
|
|
|
{
|
|
|
|
struct uml_stat buf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = os_stat_file(file, &buf);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (S_ISDIR(buf.ust_mode))
|
2006-07-10 11:45:14 +00:00
|
|
|
return OS_TYPE_DIR;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (S_ISLNK(buf.ust_mode))
|
2006-07-10 11:45:14 +00:00
|
|
|
return OS_TYPE_SYMLINK;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (S_ISCHR(buf.ust_mode))
|
2006-07-10 11:45:14 +00:00
|
|
|
return OS_TYPE_CHARDEV;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (S_ISBLK(buf.ust_mode))
|
2006-07-10 11:45:14 +00:00
|
|
|
return OS_TYPE_BLOCKDEV;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (S_ISFIFO(buf.ust_mode))
|
2006-07-10 11:45:14 +00:00
|
|
|
return OS_TYPE_FIFO;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (S_ISSOCK(buf.ust_mode))
|
2006-07-10 11:45:14 +00:00
|
|
|
return OS_TYPE_SOCK;
|
|
|
|
else return OS_TYPE_FILE;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
int os_file_mode(const char *file, struct openflags *mode_out)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
*mode_out = OPENFLAGS();
|
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
err = access(file, W_OK);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err && (errno != EACCES))
|
2007-10-16 08:27:11 +00:00
|
|
|
return -errno;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (!err)
|
2007-10-16 08:27:11 +00:00
|
|
|
*mode_out = of_write(*mode_out);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
err = access(file, R_OK);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err && (errno != EACCES))
|
2007-10-16 08:27:11 +00:00
|
|
|
return -errno;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (!err)
|
2007-10-16 08:27:11 +00:00
|
|
|
*mode_out = of_read(*mode_out);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
int os_open_file(const char *file, struct openflags flags, int mode)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
[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
|
|
|
int fd, err, f = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (flags.r && flags.w)
|
|
|
|
f = O_RDWR;
|
|
|
|
else if (flags.r)
|
|
|
|
f = O_RDONLY;
|
|
|
|
else if (flags.w)
|
|
|
|
f = O_WRONLY;
|
2005-04-16 22:20:36 +00:00
|
|
|
else f = 0;
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (flags.s)
|
|
|
|
f |= O_SYNC;
|
|
|
|
if (flags.c)
|
|
|
|
f |= O_CREAT;
|
|
|
|
if (flags.t)
|
|
|
|
f |= O_TRUNC;
|
|
|
|
if (flags.e)
|
|
|
|
f |= O_EXCL;
|
2008-02-05 06:31:18 +00:00
|
|
|
if (flags.a)
|
|
|
|
f |= O_APPEND;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
fd = open64(file, f, mode);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (fd < 0)
|
2007-10-16 08:27:11 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (flags.cl && fcntl(fd, F_SETFD, 1)) {
|
[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-10-16 08:27:11 +00:00
|
|
|
close(fd);
|
[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-10-16 08:27:11 +00:00
|
|
|
return fd;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
int os_connect_socket(const char *name)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un sock;
|
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
sock.sun_family = AF_UNIX;
|
|
|
|
snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (fd < 0) {
|
2006-02-24 21:03:56 +00:00
|
|
|
err = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err = connect(fd, (struct sockaddr *) &sock, sizeof(sock));
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err) {
|
2006-02-24 21:03:56 +00:00
|
|
|
err = -errno;
|
|
|
|
goto out_close;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-02-24 21:03:56 +00:00
|
|
|
return fd;
|
|
|
|
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
out:
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_close_file(int fd)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2007-10-16 08:27:00 +00:00
|
|
|
int os_seek_file(int fd, unsigned long long offset)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-10-16 08:27:00 +00:00
|
|
|
unsigned long long actual;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
actual = lseek64(fd, offset, SEEK_SET);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (actual != offset)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_read_file(int fd, void *buf, int len)
|
uml: start fixing os_read_file and os_write_file
This patch starts the removal of a very old, very broken piece of code. This
stems from the problem of passing a userspace buffer into read() or write() on
the host. If that buffer had not yet been faulted in, read and write will
return -EFAULT.
To avoid this problem, the solution was to fault the buffer in before the
system call by touching the pages that hold the buffer by doing a copy-user of
a byte to each page. This is obviously bogus, but it does usually work, in tt
mode, since the kernel and process are in the same address space and userspace
addresses can be accessed directly in the kernel.
In skas mode, where the kernel and process are in separate address spaces, it
is completely bogus because the userspace address, which is invalid in the
kernel, is passed into the system call instead of the corresponding physical
address, which would be valid. Here, it appears that this code, on every host
read() or write(), tries to fault in a random process page. This doesn't seem
to cause any correctness problems, but there is a performance impact. This
patch, and the ones following, result in a 10-15% performance gain on a kernel
build.
This code can't be immediately tossed out because when it is, you can't log
in. Apparently, there is some code in the console driver which depends on
this somehow.
However, we can start removing it by switching the code which does I/O using
kernel addresses to using plain read() and write(). This patch introduces
os_read_file_k and os_write_file_k for use with kernel buffers and converts
all call locations which use obvious kernel buffers to use them. These
include I/O using buffers which are local variables which are on the stack or
kmalloc-ed. Later patches will handle the less obvious cases, followed by a
mass conversion back to the original interface.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 21:51:32 +00:00
|
|
|
{
|
|
|
|
int n = read(fd, buf, len);
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (n < 0)
|
uml: start fixing os_read_file and os_write_file
This patch starts the removal of a very old, very broken piece of code. This
stems from the problem of passing a userspace buffer into read() or write() on
the host. If that buffer had not yet been faulted in, read and write will
return -EFAULT.
To avoid this problem, the solution was to fault the buffer in before the
system call by touching the pages that hold the buffer by doing a copy-user of
a byte to each page. This is obviously bogus, but it does usually work, in tt
mode, since the kernel and process are in the same address space and userspace
addresses can be accessed directly in the kernel.
In skas mode, where the kernel and process are in separate address spaces, it
is completely bogus because the userspace address, which is invalid in the
kernel, is passed into the system call instead of the corresponding physical
address, which would be valid. Here, it appears that this code, on every host
read() or write(), tries to fault in a random process page. This doesn't seem
to cause any correctness problems, but there is a performance impact. This
patch, and the ones following, result in a 10-15% performance gain on a kernel
build.
This code can't be immediately tossed out because when it is, you can't log
in. Apparently, there is some code in the console driver which depends on
this somehow.
However, we can start removing it by switching the code which does I/O using
kernel addresses to using plain read() and write(). This patch introduces
os_read_file_k and os_write_file_k for use with kernel buffers and converts
all call locations which use obvious kernel buffers to use them. These
include I/O using buffers which are local variables which are on the stack or
kmalloc-ed. Later patches will handle the less obvious cases, followed by a
mass conversion back to the original interface.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 21:51:32 +00:00
|
|
|
return -errno;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
int os_write_file(int fd, const void *buf, int len)
|
uml: start fixing os_read_file and os_write_file
This patch starts the removal of a very old, very broken piece of code. This
stems from the problem of passing a userspace buffer into read() or write() on
the host. If that buffer had not yet been faulted in, read and write will
return -EFAULT.
To avoid this problem, the solution was to fault the buffer in before the
system call by touching the pages that hold the buffer by doing a copy-user of
a byte to each page. This is obviously bogus, but it does usually work, in tt
mode, since the kernel and process are in the same address space and userspace
addresses can be accessed directly in the kernel.
In skas mode, where the kernel and process are in separate address spaces, it
is completely bogus because the userspace address, which is invalid in the
kernel, is passed into the system call instead of the corresponding physical
address, which would be valid. Here, it appears that this code, on every host
read() or write(), tries to fault in a random process page. This doesn't seem
to cause any correctness problems, but there is a performance impact. This
patch, and the ones following, result in a 10-15% performance gain on a kernel
build.
This code can't be immediately tossed out because when it is, you can't log
in. Apparently, there is some code in the console driver which depends on
this somehow.
However, we can start removing it by switching the code which does I/O using
kernel addresses to using plain read() and write(). This patch introduces
os_read_file_k and os_write_file_k for use with kernel buffers and converts
all call locations which use obvious kernel buffers to use them. These
include I/O using buffers which are local variables which are on the stack or
kmalloc-ed. Later patches will handle the less obvious cases, followed by a
mass conversion back to the original interface.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 21:51:32 +00:00
|
|
|
{
|
|
|
|
int n = write(fd, (void *) buf, len);
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (n < 0)
|
uml: start fixing os_read_file and os_write_file
This patch starts the removal of a very old, very broken piece of code. This
stems from the problem of passing a userspace buffer into read() or write() on
the host. If that buffer had not yet been faulted in, read and write will
return -EFAULT.
To avoid this problem, the solution was to fault the buffer in before the
system call by touching the pages that hold the buffer by doing a copy-user of
a byte to each page. This is obviously bogus, but it does usually work, in tt
mode, since the kernel and process are in the same address space and userspace
addresses can be accessed directly in the kernel.
In skas mode, where the kernel and process are in separate address spaces, it
is completely bogus because the userspace address, which is invalid in the
kernel, is passed into the system call instead of the corresponding physical
address, which would be valid. Here, it appears that this code, on every host
read() or write(), tries to fault in a random process page. This doesn't seem
to cause any correctness problems, but there is a performance impact. This
patch, and the ones following, result in a 10-15% performance gain on a kernel
build.
This code can't be immediately tossed out because when it is, you can't log
in. Apparently, there is some code in the console driver which depends on
this somehow.
However, we can start removing it by switching the code which does I/O using
kernel addresses to using plain read() and write(). This patch introduces
os_read_file_k and os_write_file_k for use with kernel buffers and converts
all call locations which use obvious kernel buffers to use them. These
include I/O using buffers which are local variables which are on the stack or
kmalloc-ed. Later patches will handle the less obvious cases, followed by a
mass conversion back to the original interface.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-06 21:51:32 +00:00
|
|
|
return -errno;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
int os_file_size(const char *file, unsigned long long *size_out)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct uml_stat buf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = os_stat_file(file, &buf);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
|
|
|
|
-err);
|
2007-10-16 08:27:11 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (S_ISBLK(buf.ust_mode)) {
|
2007-09-19 05:46:21 +00:00
|
|
|
int fd;
|
|
|
|
long blocks;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
fd = open(file, O_RDONLY, 0);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (fd < 0) {
|
2007-10-16 08:27:11 +00:00
|
|
|
err = -errno;
|
2008-02-05 06:31:05 +00:00
|
|
|
printk(UM_KERN_ERR "Couldn't open \"%s\", "
|
|
|
|
"errno = %d\n", file, errno);
|
2007-10-16 08:27:11 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-02-05 06:31:05 +00:00
|
|
|
if (ioctl(fd, BLKGETSIZE, &blocks) < 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;
|
2008-02-05 06:31:05 +00:00
|
|
|
printk(UM_KERN_ERR "Couldn't get the block size of "
|
|
|
|
"\"%s\", errno = %d\n", file, errno);
|
2007-10-16 08:27:11 +00:00
|
|
|
close(fd);
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
*size_out = ((long long) blocks) * 512;
|
2007-10-16 08:27:11 +00:00
|
|
|
close(fd);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-10-16 08:27:11 +00:00
|
|
|
else *size_out = buf.ust_size;
|
|
|
|
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
int os_file_modtime(const char *file, unsigned long *modtime)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct uml_stat buf;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = os_stat_file(file, &buf);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "Couldn't stat \"%s\" : err = %d\n", file,
|
|
|
|
-err);
|
2006-07-10 11:45:14 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*modtime = buf.ust_mtime;
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
int os_set_exec_close(int fd)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-10-16 08:27:11 +00:00
|
|
|
int err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
CATCH_EINTR(err = fcntl(fd, F_SETFD, FD_CLOEXEC));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2007-10-16 08:27:11 +00:00
|
|
|
return -errno;
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_pipe(int *fds, int stream, int close_on_exec)
|
|
|
|
{
|
|
|
|
int err, type = stream ? SOCK_STREAM : SOCK_DGRAM;
|
|
|
|
|
|
|
|
err = socketpair(AF_UNIX, type, 0, fds);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2007-10-16 08:27:11 +00:00
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (!close_on_exec)
|
2007-10-16 08:27:11 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
err = os_set_exec_close(fds[0]);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto error;
|
|
|
|
|
2007-10-16 08:27:11 +00:00
|
|
|
err = os_set_exec_close(fds[1]);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto error;
|
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
error:
|
2008-02-05 06:31:05 +00:00
|
|
|
printk(UM_KERN_ERR "os_pipe : Setting FD_CLOEXEC failed, err = %d\n",
|
|
|
|
-err);
|
2007-10-16 08:27:11 +00:00
|
|
|
close(fds[1]);
|
|
|
|
close(fds[0]);
|
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:31:04 +00:00
|
|
|
int os_set_fd_async(int fd)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-02-05 06:31:04 +00:00
|
|
|
int err, flags;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags < 0)
|
|
|
|
return -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
|
|
|
|
2008-02-05 06:31:04 +00:00
|
|
|
flags |= O_ASYNC | O_NONBLOCK;
|
|
|
|
if (fcntl(fd, F_SETFL, flags) < 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;
|
2008-02-05 06:31:05 +00:00
|
|
|
printk(UM_KERN_ERR "os_set_fd_async : failed to set O_ASYNC "
|
|
|
|
"and O_NONBLOCK on fd # %d, errno = %d\n", fd, 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
|
|
|
}
|
|
|
|
|
2008-02-05 06:31:04 +00:00
|
|
|
if ((fcntl(fd, F_SETSIG, SIGIO) < 0) ||
|
|
|
|
(fcntl(fd, F_SETOWN, os_getpid()) < 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;
|
2008-02-05 06:31:05 +00:00
|
|
|
printk(UM_KERN_ERR "os_set_fd_async : Failed to fcntl F_SETOWN "
|
2008-02-05 06:31:04 +00:00
|
|
|
"(or F_SETSIG) fd %d, errno = %d\n", fd, 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
|
|
|
}
|
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_clear_fd_async(int fd)
|
|
|
|
{
|
2008-02-05 06:31:05 +00:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags < 0)
|
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
flags &= ~(O_ASYNC | O_NONBLOCK);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (fcntl(fd, F_SETFL, flags) < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_set_fd_block(int fd, int blocking)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (flags < 0)
|
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (blocking)
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
else
|
|
|
|
flags |= O_NONBLOCK;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (fcntl(fd, F_SETFL, flags) < 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
|
|
|
return -errno;
|
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_accept_connection(int fd)
|
|
|
|
{
|
|
|
|
int new;
|
|
|
|
|
|
|
|
new = accept(fd, NULL, 0);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (new < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
|
|
|
return new;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SHUT_RD
|
|
|
|
#define SHUT_RD 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SHUT_WR
|
|
|
|
#define SHUT_WR 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SHUT_RDWR
|
|
|
|
#define SHUT_RDWR 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int os_shutdown_socket(int fd, int r, int w)
|
|
|
|
{
|
|
|
|
int what, err;
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (r && w)
|
|
|
|
what = SHUT_RDWR;
|
|
|
|
else if (r)
|
|
|
|
what = SHUT_RD;
|
|
|
|
else if (w)
|
|
|
|
what = SHUT_WR;
|
|
|
|
else
|
2006-07-10 11:45:14 +00:00
|
|
|
return -EINVAL;
|
2008-02-05 06:31:05 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
err = shutdown(fd, what);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int os_rcv_fd(int fd, int *helper_pid_out)
|
|
|
|
{
|
|
|
|
int new, n;
|
|
|
|
char buf[CMSG_SPACE(sizeof(new))];
|
|
|
|
struct msghdr msg;
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
struct iovec iov;
|
|
|
|
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
iov = ((struct iovec) { .iov_base = helper_pid_out,
|
|
|
|
.iov_len = sizeof(*helper_pid_out) });
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = buf;
|
|
|
|
msg.msg_controllen = sizeof(buf);
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
|
|
|
|
n = recvmsg(fd, &msg, 0);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (n < 0)
|
2006-07-10 11:45:14 +00:00
|
|
|
return -errno;
|
2008-02-05 06:31:05 +00:00
|
|
|
else if (n != iov.iov_len)
|
2005-04-16 22:20:36 +00:00
|
|
|
*helper_pid_out = -1;
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (cmsg == NULL) {
|
|
|
|
printk(UM_KERN_ERR "rcv_fd didn't receive anything, "
|
|
|
|
"error = %d\n", errno);
|
2006-07-10 11:45:14 +00:00
|
|
|
return -1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-02-05 06:31:05 +00:00
|
|
|
if ((cmsg->cmsg_level != SOL_SOCKET) ||
|
|
|
|
(cmsg->cmsg_type != SCM_RIGHTS)) {
|
|
|
|
printk(UM_KERN_ERR "rcv_fd didn't receive a descriptor\n");
|
2006-07-10 11:45:14 +00:00
|
|
|
return -1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new = ((int *) CMSG_DATA(cmsg))[0];
|
2006-07-10 11:45:14 +00:00
|
|
|
return new;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-05 06:30:35 +00:00
|
|
|
int os_create_unix_socket(const char *file, int len, int close_on_exec)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int sock, err;
|
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_DGRAM, 0);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (sock < 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
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
if (close_on_exec) {
|
2007-10-16 08:27:11 +00:00
|
|
|
err = os_set_exec_close(sock);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 0)
|
|
|
|
printk(UM_KERN_ERR "create_unix_socket : "
|
|
|
|
"close_on_exec failed, err = %d", -err);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
snprintf(addr.sun_path, len, "%s", file);
|
|
|
|
|
|
|
|
err = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err < 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
|
|
|
return -errno;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-07-10 11:45:14 +00:00
|
|
|
return sock;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_flush_stdout(void)
|
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_lock_file(int fd, int excl)
|
|
|
|
{
|
|
|
|
int type = excl ? F_WRLCK : F_RDLCK;
|
|
|
|
struct flock lock = ((struct flock) { .l_type = type,
|
|
|
|
.l_whence = SEEK_SET,
|
|
|
|
.l_start = 0,
|
|
|
|
.l_len = 0 } );
|
|
|
|
int err, save;
|
|
|
|
|
|
|
|
err = fcntl(fd, F_SETLK, &lock);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (!err)
|
2005-04-16 22:20:36 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
save = -errno;
|
|
|
|
err = fcntl(fd, F_GETLK, &lock);
|
2008-02-05 06:31:05 +00:00
|
|
|
if (err) {
|
2005-04-16 22:20:36 +00:00
|
|
|
err = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-02-05 06:31:05 +00:00
|
|
|
printk(UM_KERN_ERR "F_SETLK failed, file already locked by pid %d\n",
|
|
|
|
lock.l_pid);
|
2005-04-16 22:20:36 +00:00
|
|
|
err = save;
|
|
|
|
out:
|
2006-07-10 11:45:14 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-04-21 05:27:08 +00:00
|
|
|
|
|
|
|
unsigned os_major(unsigned long long dev)
|
|
|
|
{
|
|
|
|
return major(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned os_minor(unsigned long long dev)
|
|
|
|
{
|
|
|
|
return minor(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long os_makedev(unsigned major, unsigned minor)
|
|
|
|
{
|
|
|
|
return makedev(major, minor);
|
|
|
|
}
|