Skip to content

Commit 22d2b35

Browse files
Ulrich DrepperLinus Torvalds
authored andcommitted
F_DUPFD_CLOEXEC implementation
One more small change to extend the availability of creation of file descriptors with FD_CLOEXEC set. Adding a new command to fcntl() requires no new system call and the overall impact on code size if minimal. If this patch gets accepted we will also add this change to the next revision of the POSIX spec. To test the patch, use the following little program. Adjust the value of F_DUPFD_CLOEXEC appropriately. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #ifndef F_DUPFD_CLOEXEC # define F_DUPFD_CLOEXEC 12 #endif int main (int argc, char *argv[]) { if (argc > 1) { if (fcntl (3, F_GETFD) == 0) { puts ("descriptor not closed"); exit (1); } if (errno != EBADF) { puts ("error not EBADF"); exit (1); } exit (0); } int fd = fcntl (STDOUT_FILENO, F_DUPFD_CLOEXEC, 0); if (fd == -1 && errno == EINVAL) { puts ("F_DUPFD_CLOEXEC not supported"); return 0; } if (fd != 3) { puts ("program called with descriptors other than 0,1,2"); return 1; } execl ("/proc/self/exe", "/proc/self/exe", "1", NULL); puts ("execl failed"); return 1; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Ulrich Drepper <[email protected]> Cc: Al Viro <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: <[email protected]> Cc: Kyle McMartin <[email protected]> Cc: Stephen Rothwell <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 18796aa commit 22d2b35

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

fs/fcntl.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static int locate_fd(struct files_struct *files,
110110
return error;
111111
}
112112

113-
static int dupfd(struct file *file, unsigned int start)
113+
static int dupfd(struct file *file, unsigned int start, int cloexec)
114114
{
115115
struct files_struct * files = current->files;
116116
struct fdtable *fdt;
@@ -122,7 +122,10 @@ static int dupfd(struct file *file, unsigned int start)
122122
/* locate_fd() may have expanded fdtable, load the ptr */
123123
fdt = files_fdtable(files);
124124
FD_SET(fd, fdt->open_fds);
125-
FD_CLR(fd, fdt->close_on_exec);
125+
if (cloexec)
126+
FD_SET(fd, fdt->close_on_exec);
127+
else
128+
FD_CLR(fd, fdt->close_on_exec);
126129
spin_unlock(&files->file_lock);
127130
fd_install(fd, file);
128131
} else {
@@ -195,7 +198,7 @@ asmlinkage long sys_dup(unsigned int fildes)
195198
struct file * file = fget(fildes);
196199

197200
if (file)
198-
ret = dupfd(file, 0);
201+
ret = dupfd(file, 0, 0);
199202
return ret;
200203
}
201204

@@ -319,8 +322,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
319322

320323
switch (cmd) {
321324
case F_DUPFD:
325+
case F_DUPFD_CLOEXEC:
322326
get_file(filp);
323-
err = dupfd(filp, arg);
327+
err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
324328
break;
325329
case F_GETFD:
326330
err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;

include/linux/fcntl.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
#include <asm/fcntl.h>
55

6-
/* Cancel a blocking posix lock; internal use only until we expose an
7-
* asynchronous lock api to userspace: */
8-
#define F_CANCELLK (F_LINUX_SPECIFIC_BASE+5)
6+
#define F_SETLEASE (F_LINUX_SPECIFIC_BASE + 0)
7+
#define F_GETLEASE (F_LINUX_SPECIFIC_BASE + 1)
98

10-
#define F_SETLEASE (F_LINUX_SPECIFIC_BASE+0)
11-
#define F_GETLEASE (F_LINUX_SPECIFIC_BASE+1)
9+
/*
10+
* Cancel a blocking posix lock; internal use only until we expose an
11+
* asynchronous lock api to userspace:
12+
*/
13+
#define F_CANCELLK (F_LINUX_SPECIFIC_BASE + 5)
14+
15+
/* Create a file descriptor with FD_CLOEXEC set. */
16+
#define F_DUPFD_CLOEXEC (F_LINUX_SPECIFIC_BASE + 6)
1217

1318
/*
1419
* Request nofications on a directory.

0 commit comments

Comments
 (0)