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 <drepper@redhat.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Cc: <linux-arch@vger.kernel.org>
Cc: Kyle McMartin <kyle@mcmartin.ca>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Ulrich Drepper 2007-10-16 23:30:26 -07:00 committed by Linus Torvalds
parent 18796aa002
commit 22d2b35b20
2 changed files with 18 additions and 9 deletions

View file

@ -110,7 +110,7 @@ out:
return error; return error;
} }
static int dupfd(struct file *file, unsigned int start) static int dupfd(struct file *file, unsigned int start, int cloexec)
{ {
struct files_struct * files = current->files; struct files_struct * files = current->files;
struct fdtable *fdt; struct fdtable *fdt;
@ -122,6 +122,9 @@ static int dupfd(struct file *file, unsigned int start)
/* locate_fd() may have expanded fdtable, load the ptr */ /* locate_fd() may have expanded fdtable, load the ptr */
fdt = files_fdtable(files); fdt = files_fdtable(files);
FD_SET(fd, fdt->open_fds); FD_SET(fd, fdt->open_fds);
if (cloexec)
FD_SET(fd, fdt->close_on_exec);
else
FD_CLR(fd, fdt->close_on_exec); FD_CLR(fd, fdt->close_on_exec);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
fd_install(fd, file); fd_install(fd, file);
@ -195,7 +198,7 @@ asmlinkage long sys_dup(unsigned int fildes)
struct file * file = fget(fildes); struct file * file = fget(fildes);
if (file) if (file)
ret = dupfd(file, 0); ret = dupfd(file, 0, 0);
return ret; return ret;
} }
@ -319,8 +322,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
switch (cmd) { switch (cmd) {
case F_DUPFD: case F_DUPFD:
case F_DUPFD_CLOEXEC:
get_file(filp); get_file(filp);
err = dupfd(filp, arg); err = dupfd(filp, arg, cmd == F_DUPFD_CLOEXEC);
break; break;
case F_GETFD: case F_GETFD:
err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; err = get_close_on_exec(fd) ? FD_CLOEXEC : 0;

View file

@ -3,13 +3,18 @@
#include <asm/fcntl.h> #include <asm/fcntl.h>
/* Cancel a blocking posix lock; internal use only until we expose an
* asynchronous lock api to userspace: */
#define F_CANCELLK (F_LINUX_SPECIFIC_BASE+5)
#define F_SETLEASE (F_LINUX_SPECIFIC_BASE + 0) #define F_SETLEASE (F_LINUX_SPECIFIC_BASE + 0)
#define F_GETLEASE (F_LINUX_SPECIFIC_BASE + 1) #define F_GETLEASE (F_LINUX_SPECIFIC_BASE + 1)
/*
* Cancel a blocking posix lock; internal use only until we expose an
* asynchronous lock api to userspace:
*/
#define F_CANCELLK (F_LINUX_SPECIFIC_BASE + 5)
/* Create a file descriptor with FD_CLOEXEC set. */
#define F_DUPFD_CLOEXEC (F_LINUX_SPECIFIC_BASE + 6)
/* /*
* Request nofications on a directory. * Request nofications on a directory.
* See below for events that may be notified. * See below for events that may be notified.