Skip to content

Commit

Permalink
[PATCH] uml: preserve errno in error paths
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jeff Dike authored and Linus Torvalds committed Sep 17, 2005
1 parent 64b7673 commit b4fd310
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 78 deletions.
11 changes: 8 additions & 3 deletions arch/um/drivers/mcast_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static int mcast_open(void *data)
struct mcast_data *pri = data;
struct sockaddr_in *sin = pri->mcast_addr;
struct ip_mreq mreq;
int fd = -EINVAL, yes = 1, err = -EINVAL;;
int fd, yes = 1, err = 0;


if ((sin->sin_addr.s_addr == 0) || (sin->sin_port == 0))
Expand All @@ -65,13 +65,14 @@ static int mcast_open(void *data)
if (fd < 0){
printk("mcast_open : data socket failed, errno = %d\n",
errno);
fd = -errno;
err = -errno;
goto out;
}

if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
printk("mcast_open: SO_REUSEADDR failed, errno = %d\n",
errno);
err = -errno;
goto out_close;
}

Expand All @@ -80,19 +81,22 @@ static int mcast_open(void *data)
sizeof(pri->ttl)) < 0) {
printk("mcast_open: IP_MULTICAST_TTL failed, error = %d\n",
errno);
err = -errno;
goto out_close;
}

/* set LOOP, so data does get fed back to local sockets */
if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
printk("mcast_open: IP_MULTICAST_LOOP failed, error = %d\n",
errno);
err = -errno;
goto out_close;
}

/* bind socket to mcast address */
if (bind(fd, (struct sockaddr *) sin, sizeof(*sin)) < 0) {
printk("mcast_open : data bind failed, errno = %d\n", errno);
err = -errno;
goto out_close;
}

Expand All @@ -107,14 +111,15 @@ static int mcast_open(void *data)
"interface on the host.\n");
printk("eth0 should be configured in order to use the "
"multicast transport.\n");
err = -errno;
goto out_close;
}

out:
return fd;

out_close:
os_close_file(fd);
out:
return err;
}

Expand Down
6 changes: 3 additions & 3 deletions arch/um/drivers/mconsole_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ int mconsole_notify(char *sock_name, int type, const void *data, int len)
if(notify_sock < 0){
notify_sock = socket(PF_UNIX, SOCK_DGRAM, 0);
if(notify_sock < 0){
printk("mconsole_notify - socket failed, errno = %d\n",
errno);
err = -errno;
printk("mconsole_notify - socket failed, errno = %d\n",
err);
}
}
unlock_notify();
Expand All @@ -198,8 +198,8 @@ int mconsole_notify(char *sock_name, int type, const void *data, int len)
n = sendto(notify_sock, &packet, len, 0, (struct sockaddr *) &target,
sizeof(target));
if(n < 0){
printk("mconsole_notify - sendto failed, errno = %d\n", errno);
err = -errno;
printk("mconsole_notify - sendto failed, errno = %d\n", errno);
}
return(err);
}
Expand Down
3 changes: 2 additions & 1 deletion arch/um/drivers/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ static int pts_open(int input, int output, int primary, void *d,

fd = get_pty();
if(fd < 0){
err = -errno;
printk("open_pts : Failed to open pts\n");
return(-errno);
return err;
}
if(data->raw){
CATCH_EINTR(err = tcgetattr(fd, &data->tt));
Expand Down
6 changes: 4 additions & 2 deletions arch/um/drivers/xterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ int xterm_open(int input, int output, int primary, void *d,

fd = mkstemp(file);
if(fd < 0){
err = -errno;
printk("xterm_open : mkstemp failed, errno = %d\n", errno);
return(-errno);
return err;
}

if(unlink(file)){
err = -errno;
printk("xterm_open : unlink failed, errno = %d\n", errno);
return(-errno);
return err;
}
os_close_file(fd);

Expand Down
12 changes: 7 additions & 5 deletions arch/um/kernel/helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ int run_helper(void (*pre_exec)(void *), void *pre_data, char **argv,
data.fd = fds[1];
pid = clone(helper_child, (void *) sp, CLONE_VM | SIGCHLD, &data);
if(pid < 0){
printk("run_helper : clone failed, errno = %d\n", errno);
ret = -errno;
printk("run_helper : clone failed, errno = %d\n", errno);
goto out_close;
}

Expand Down Expand Up @@ -122,24 +122,26 @@ int run_helper_thread(int (*proc)(void *), void *arg, unsigned int flags,
unsigned long *stack_out, int stack_order)
{
unsigned long stack, sp;
int pid, status;
int pid, status, err;

stack = alloc_stack(stack_order, um_in_interrupt());
if(stack == 0) return(-ENOMEM);

sp = stack + (page_size() << stack_order) - sizeof(void *);
pid = clone(proc, (void *) sp, flags | SIGCHLD, arg);
if(pid < 0){
err = -errno;
printk("run_helper_thread : clone failed, errno = %d\n",
errno);
return(-errno);
return err;
}
if(stack_out == NULL){
CATCH_EINTR(pid = waitpid(pid, &status, 0));
if(pid < 0){
err = -errno;
printk("run_helper_thread - wait failed, errno = %d\n",
errno);
pid = -errno;
pid = err;
}
if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
printk("run_helper_thread - thread returned status "
Expand All @@ -156,8 +158,8 @@ int helper_wait(int pid)

CATCH_EINTR(ret = waitpid(pid, NULL, WNOHANG));
if(ret < 0){
ret = -errno;
printk("helper_wait : waitpid failed, errno = %d\n", errno);
return(-errno);
}
return(ret);
}
12 changes: 4 additions & 8 deletions arch/um/kernel/user_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,14 @@ int raw(int fd)
int err;

CATCH_EINTR(err = tcgetattr(fd, &tt));
if (err < 0) {
printk("tcgetattr failed, errno = %d\n", errno);
return(-errno);
}
if(err < 0)
return -errno;

cfmakeraw(&tt);

CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
if (err < 0) {
printk("tcsetattr failed, errno = %d\n", errno);
return(-errno);
}
if(err < 0)
return -errno;

/* XXX tcsetattr could have applied only some changes
* (and cfmakeraw() is a set of changes) */
Expand Down
5 changes: 3 additions & 2 deletions arch/um/os-Linux/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,16 @@ static int init_aio_26(void)
int err;

if(io_setup(256, &ctx)){
err = -errno;
printk("aio_thread failed to initialize context, err = %d\n",
errno);
return -errno;
return err;
}

err = run_helper_thread(aio_thread, NULL,
CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
if(err < 0)
return -errno;
return err;

aio_pid = err;

Expand Down
8 changes: 5 additions & 3 deletions arch/um/os-Linux/drivers/tuntap_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iov;
int pid, n;
int pid, n, err;

sprintf(version_buf, "%d", UML_NET_VERSION);

Expand Down Expand Up @@ -105,9 +105,10 @@ static int tuntap_open_tramp(char *gate, int *fd_out, int me, int remote,
n = recvmsg(me, &msg, 0);
*used_out = n;
if(n < 0){
err = -errno;
printk("tuntap_open_tramp : recvmsg failed - errno = %d\n",
errno);
return(-errno);
return err;
}
CATCH_EINTR(waitpid(pid, NULL, 0));

Expand Down Expand Up @@ -147,9 +148,10 @@ static int tuntap_open(void *data)
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
strlcpy(ifr.ifr_name, pri->dev_name, sizeof(ifr.ifr_name));
if(ioctl(pri->fd, TUNSETIFF, (void *) &ifr) < 0){
err = -errno;
printk("TUNSETIFF failed, errno = %d\n", errno);
os_close_file(pri->fd);
return(-errno);
return err;
}
}
else {
Expand Down
Loading

0 comments on commit b4fd310

Please sign in to comment.