Skip to content

Commit 336dd1f

Browse files
Ulrich Dreppertorvalds
authored andcommitted
flag parameters: dup2
This patch adds the new dup3 syscall. It extends the old dup2 syscall by one parameter which is meant to hold a flag value. Support for the O_CLOEXEC flag is added in this patch. The following test must be adjusted for architectures other than x86 and x86-64 and in case the syscall numbers changed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include <fcntl.h> #include <stdio.h> #include <time.h> #include <unistd.h> #include <sys/syscall.h> #ifndef __NR_dup3 # ifdef __x86_64__ # define __NR_dup3 292 # elif defined __i386__ # define __NR_dup3 330 # else # error "need __NR_dup3" # endif #endif int main (void) { int fd = syscall (__NR_dup3, 1, 4, 0); if (fd == -1) { puts ("dup3(0) failed"); return 1; } int coe = fcntl (fd, F_GETFD); if (coe == -1) { puts ("fcntl failed"); return 1; } if (coe & FD_CLOEXEC) { puts ("dup3(0) set close-on-exec flag"); return 1; } close (fd); fd = syscall (__NR_dup3, 1, 4, O_CLOEXEC); if (fd == -1) { puts ("dup3(O_CLOEXEC) failed"); return 1; } coe = fcntl (fd, F_GETFD); if (coe == -1) { puts ("fcntl failed"); return 1; } if ((coe & FD_CLOEXEC) == 0) { puts ("dup3(O_CLOEXEC) set close-on-exec flag"); return 1; } close (fd); puts ("OK"); return 0; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Ulrich Drepper <drepper@redhat.com> Acked-by: Davide Libenzi <davidel@xmailserver.org> Cc: Michael Kerrisk <mtk.manpages@googlemail.com> Cc: <linux-arch@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a0998b5 commit 336dd1f

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

arch/x86/ia32/ia32entry.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,4 +829,5 @@ ia32_sys_call_table:
829829
.quad compat_sys_signalfd4
830830
.quad sys_eventfd2
831831
.quad sys_epoll_create2
832+
.quad sys_dup3 /* 330 */
832833
ia32_syscall_end:

arch/x86/kernel/syscall_table_32.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,4 @@ ENTRY(sys_call_table)
329329
.long sys_signalfd4
330330
.long sys_eventfd2
331331
.long sys_epoll_create2
332+
.long sys_dup3 /* 330 */

fs/fcntl.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,16 @@ static int dupfd(struct file *file, unsigned int start, int cloexec)
125125
return fd;
126126
}
127127

128-
asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
128+
asmlinkage long sys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
129129
{
130130
int err = -EBADF;
131131
struct file * file, *tofree;
132132
struct files_struct * files = current->files;
133133
struct fdtable *fdt;
134134

135+
if ((flags & ~O_CLOEXEC) != 0)
136+
return -EINVAL;
137+
135138
spin_lock(&files->file_lock);
136139
if (!(file = fcheck(oldfd)))
137140
goto out_unlock;
@@ -163,7 +166,10 @@ asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
163166

164167
rcu_assign_pointer(fdt->fd[newfd], file);
165168
FD_SET(newfd, fdt->open_fds);
166-
FD_CLR(newfd, fdt->close_on_exec);
169+
if (flags & O_CLOEXEC)
170+
FD_SET(newfd, fdt->close_on_exec);
171+
else
172+
FD_CLR(newfd, fdt->close_on_exec);
167173
spin_unlock(&files->file_lock);
168174

169175
if (tofree)
@@ -181,6 +187,11 @@ asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
181187
goto out;
182188
}
183189

190+
asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
191+
{
192+
return sys_dup3(oldfd, newfd, 0);
193+
}
194+
184195
asmlinkage long sys_dup(unsigned int fildes)
185196
{
186197
int ret = -EBADF;

include/asm-x86/unistd_32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@
335335
#define __NR_signalfd4 327
336336
#define __NR_eventfd2 328
337337
#define __NR_epoll_create2 329
338+
#define __NR_dup3 330
338339

339340
#ifdef __KERNEL__
340341

include/asm-x86/unistd_64.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ __SYSCALL(__NR_signalfd4, sys_signalfd4)
647647
__SYSCALL(__NR_eventfd2, sys_eventfd2)
648648
#define __NR_epoll_create2 291
649649
__SYSCALL(__NR_epoll_create2, sys_epoll_create2)
650+
#define __NR_dup3 292
651+
__SYSCALL(__NR_dup3, sys_dup3)
650652

651653

652654
#ifndef __NO_STUBS

include/linux/syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ asmlinkage long sys_fcntl64(unsigned int fd,
305305
#endif
306306
asmlinkage long sys_dup(unsigned int fildes);
307307
asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd);
308+
asmlinkage long sys_dup3(unsigned int oldfd, unsigned int newfd, int flags);
308309
asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int on);
309310
asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd,
310311
unsigned long arg);

0 commit comments

Comments
 (0)