Skip to content

Commit

Permalink
[PATCH] dup3 fix
Browse files Browse the repository at this point in the history
Al Viro notice one cornercase that the new dup3() code.  The dup2()
function, as a special case, handles dup-ing to the same file
descriptor.  In this case the current dup3() code does nothing at
all.  I.e., it ingnores the flags parameter.  This shouldn't happen,
the close-on-exec flag should be set if requested.

In case the O_CLOEXEC bit in the flags parameter is not set the
dup3() function should behave in this respect identical to dup2().
This means dup3(fd, fd, 0) should not actively reset the c-o-e
flag.

The patch below implements this minor change.

[AV: credits to Artur Grabowski for bringing that up as potential subtle point
in dup2() behaviour]

Signed-off-by: Ulrich Drepper <[email protected]>
Signed-off-by: Al Viro <[email protected]>
  • Loading branch information
Ulrich Drepper authored and Al Viro committed Jul 27, 2008
1 parent 58ec42b commit 3c33393
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion fs/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,13 @@ asmlinkage long sys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
if (!(file = fcheck(oldfd)))
goto out_unlock;
err = newfd;
if (newfd == oldfd)
if (unlikely(newfd == oldfd)) {
if (flags & O_CLOEXEC) {
fdt = files_fdtable(files);
FD_SET(newfd, fdt->close_on_exec);
}
goto out_unlock;
}
err = -EBADF;
if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
goto out_unlock;
Expand Down

0 comments on commit 3c33393

Please sign in to comment.