• Ulrich Drepper's avatar
    F_DUPFD_CLOEXEC implementation · 22d2b35b
    Ulrich Drepper authored
    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: default avatarUlrich 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: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    22d2b35b
Name
Last commit
Last update
..
acpi Loading commit data...
asm-alpha Loading commit data...
asm-arm Loading commit data...
asm-arm26 Loading commit data...
asm-avr32 Loading commit data...
asm-blackfin Loading commit data...
asm-cris Loading commit data...
asm-frv Loading commit data...
asm-generic Loading commit data...
asm-h8300 Loading commit data...
asm-ia64 Loading commit data...
asm-m32r Loading commit data...
asm-m68k Loading commit data...
asm-m68knommu Loading commit data...
asm-mips Loading commit data...
asm-parisc Loading commit data...
asm-powerpc Loading commit data...
asm-ppc Loading commit data...
asm-s390 Loading commit data...
asm-sh Loading commit data...
asm-sh64 Loading commit data...
asm-sparc Loading commit data...
asm-sparc64 Loading commit data...
asm-um Loading commit data...
asm-v850 Loading commit data...
asm-x86 Loading commit data...
asm-xtensa Loading commit data...
crypto Loading commit data...
keys Loading commit data...
linux Loading commit data...
math-emu Loading commit data...
media Loading commit data...
mtd Loading commit data...
net Loading commit data...
pcmcia Loading commit data...
rdma Loading commit data...
rxrpc Loading commit data...
scsi Loading commit data...
sound Loading commit data...
video Loading commit data...
xen Loading commit data...
Kbuild Loading commit data...