• Ulrich Drepper's avatar
    Introduce O_CLOEXEC · f23513e8
    Ulrich Drepper authored
    The problem is as follows: in multi-threaded code (or more correctly: all
    code using clone() with CLONE_FILES) we have a race when exec'ing.
    
       thread #1                       thread #2
    
       fd=open()
    
                                       fork + exec
    
      fcntl(fd,F_SETFD,FD_CLOEXEC)
    
    In some applications this can happen frequently.  Take a web browser.  One
    thread opens a file and another thread starts, say, an external PDF viewer.
     The result can even be a security issue if that open file descriptor
    refers to a sensitive file and the external program can somehow be tricked
    into using that descriptor.
    
    Just adding O_CLOEXEC support to open() doesn't solve the whole set of
    problems.  There are other ways to create file descriptors (socket,
    epoll_create, Unix domain socket transfer, etc).  These can and should be
    addressed separately though.  open() is such an easy case that it makes not
    much sense putting the fix off.
    
    The test program:
    
    #include <errno.h>
    #include <fcntl.h>
    #include <stdi...
    f23513e8
fcntl.h 3.29 KB