Browse Source

clean up O_CLOEXEC handling

Drop the "#ifndef O_CLOEXEC" cruft, enable O_CLOEXEC in most fcntl.h
headers, and import __ASSUME_O_CLOEXEC from glibc.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Mike Frysinger 15 years ago
parent
commit
b8f1f91cc6

+ 3 - 5
libc/misc/dirent/opendir.c

@@ -80,9 +80,6 @@ DIR *opendir(const char *name)
 		return NULL;
 		return NULL;
 	}
 	}
 # define O_DIRECTORY 0
 # define O_DIRECTORY 0
-#endif
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
 #endif
 #endif
 	fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
 	fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
 	if (fd < 0)
 	if (fd < 0)
@@ -104,8 +101,9 @@ DIR *opendir(const char *name)
 	/* According to POSIX, directory streams should be closed when
 	/* According to POSIX, directory streams should be closed when
 	 * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
 	 * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
 	 */
 	 */
-	if (O_CLOEXEC == 0)
-		fcntl(fd, F_SETFD, FD_CLOEXEC);
+#ifndef __ASSUME_O_CLOEXEC
+	fcntl(fd, F_SETFD, FD_CLOEXEC);
+#endif
 
 
 	ptr = fd_to_DIR(fd, statbuf.st_blksize);
 	ptr = fd_to_DIR(fd, statbuf.st_blksize);
 	if (!ptr) {
 	if (!ptr) {

+ 4 - 17
libc/misc/utmp/utent.c

@@ -34,9 +34,6 @@ static const char *static_ut_name = default_file_name;
 static void __setutent(void)
 static void __setutent(void)
 {
 {
     if (static_fd < 0) {
     if (static_fd < 0) {
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
 	static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC);
 	static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC);
 	if (static_fd < 0) {
 	if (static_fd < 0) {
 	    static_fd = open(static_ut_name, O_RDONLY | O_CLOEXEC);
 	    static_fd = open(static_ut_name, O_RDONLY | O_CLOEXEC);
@@ -44,20 +41,10 @@ static void __setutent(void)
 		return; /* static_fd remains < 0 */
 		return; /* static_fd remains < 0 */
 	    }
 	    }
 	}
 	}
-	if (O_CLOEXEC == 0) {
-	    /* Make sure the file will be closed on exec()  */
-	    fcntl(static_fd, F_SETFD, FD_CLOEXEC);
-	    /* thus far, {G,S}ETFD only has this single flag,
-	     * and setting it never fails.
-	     *int ret = fcntl(static_fd, F_GETFD, 0);
-	     *if (ret >= 0) {
-	     *    ret = fcntl(static_fd, F_SETFD, ret | FD_CLOEXEC);
-	     *}
-	     *if (ret < 0) {
-	     *    static_fd = -1;
-	     *}
-             */
-	}
+#ifndef __ASSUME_O_CLOEXEC
+	/* Make sure the file will be closed on exec()  */
+	fcntl(static_fd, F_SETFD, FD_CLOEXEC);
+#endif
 	return;
 	return;
     }
     }
     lseek(static_fd, 0, SEEK_SET);
     lseek(static_fd, 0, SEEK_SET);

+ 3 - 5
libc/pwd_grp/lckpwdf.c

@@ -59,16 +59,14 @@ lckpwdf (void)
   /* Prevent problems caused by multiple threads.  */
   /* Prevent problems caused by multiple threads.  */
   __UCLIBC_MUTEX_LOCK(mylock);
   __UCLIBC_MUTEX_LOCK(mylock);
 
 
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
   lock_fd = open (_PATH_PASSWD, O_WRONLY | O_CLOEXEC);
   lock_fd = open (_PATH_PASSWD, O_WRONLY | O_CLOEXEC);
   if (lock_fd == -1) {
   if (lock_fd == -1) {
     goto DONE;
     goto DONE;
   }
   }
+#ifndef __ASSUME_O_CLOEXEC
   /* Make sure file gets correctly closed when process finished.  */
   /* Make sure file gets correctly closed when process finished.  */
-  if (O_CLOEXEC == 0)
-    fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
+  fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
+#endif
 
 
   /* Now we have to get exclusive write access.  Since multiple
   /* Now we have to get exclusive write access.  Since multiple
      process could try this we won't stop when it first fails.
      process could try this we won't stop when it first fails.

+ 0 - 2
libc/sysdeps/linux/alpha/bits/fcntl.h

@@ -50,9 +50,7 @@
 # define O_NOFOLLOW	0200000	/* Do not follow links.  */
 # define O_NOFOLLOW	0200000	/* Do not follow links.  */
 # define O_DIRECT	02000000 /* Direct disk access.  */
 # define O_DIRECT	02000000 /* Direct disk access.  */
 # define O_NOATIME	04000000 /* Do not set atime.  */
 # define O_NOATIME	04000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC      010000000 /* Set close_on_exec.  */
 # define O_CLOEXEC      010000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 #ifdef __USE_LARGEFILE64
 #ifdef __USE_LARGEFILE64

+ 0 - 2
libc/sysdeps/linux/arm/bits/fcntl.h

@@ -50,9 +50,7 @@
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_DIRECT	0200000	/* Direct disk access.	*/
 # define O_DIRECT	0200000	/* Direct disk access.	*/
 # define O_NOATIME     01000000 /* Do not set atime.  */
 # define O_NOATIME     01000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 1 - 0
libc/sysdeps/linux/avr32/bits/fcntl.h

@@ -30,6 +30,7 @@
 # define O_DIRECTORY	00200000	/* direct disk access */
 # define O_DIRECTORY	00200000	/* direct disk access */
 # define O_NOFOLLOW	00400000	/* don't follow links */
 # define O_NOFOLLOW	00400000	/* don't follow links */
 # define O_NOATIME	01000000	/* don't set atime */
 # define O_NOATIME	01000000	/* don't set atime */
+# define O_CLOEXEC	02000000	/* set close_on_exec */
 #endif
 #endif
 
 
 #ifdef __USE_LARGEFILE64
 #ifdef __USE_LARGEFILE64

+ 2 - 0
libc/sysdeps/linux/bfin/bits/fcntl.h

@@ -48,6 +48,8 @@
 # define O_DIRECTORY	 040000	/* Must be a directory.	 */
 # define O_DIRECTORY	 040000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_DIRECT	0200000	/* Direct disk access.	*/
 # define O_DIRECT	0200000	/* Direct disk access.	*/
+# define O_NOATIME	01000000	/* don't set atime */
+# define O_CLOEXEC	02000000	/* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 8 - 0
libc/sysdeps/linux/common/bits/kernel-features.h

@@ -70,6 +70,14 @@
 # define __ASSUME_GETDENTS32_D_TYPE 1
 # define __ASSUME_GETDENTS32_D_TYPE 1
 #endif
 #endif
 
 
+/* Support for various CLOEXEC and NONBLOCK flags was added for x86,
+   x86-64, PPC, IA-64, SPARC< and S390 in 2.6.23.  */
+#if __LINUX_KERNEL_VERSION >= 0x020617 \
+    && (defined __i386__ || defined __x86_64__ || defined __powerpc__ \
+	|| defined __ia64__ || defined __sparc__ || defined __s390__)
+# define __ASSUME_O_CLOEXEC 1
+#endif
+
 /* These features were surely available with 2.4.12.  */
 /* These features were surely available with 2.4.12.  */
 #if __LINUX_KERNEL_VERSION >= 132108 && defined __mc68000__
 #if __LINUX_KERNEL_VERSION >= 132108 && defined __mc68000__
 # define __ASSUME_MMAP2_SYSCALL		1
 # define __ASSUME_MMAP2_SYSCALL		1

+ 1 - 0
libc/sysdeps/linux/cris/bits/fcntl.h

@@ -50,6 +50,7 @@
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOATIME	01000000 /* Do not set atime.  */
 # define O_NOATIME	01000000 /* Do not set atime.  */
+# define O_CLOEXEC	02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 2 - 0
libc/sysdeps/linux/frv/bits/fcntl.h

@@ -45,6 +45,8 @@
 # define O_DIRECT	 040000	/* Direct disk access.  */
 # define O_DIRECT	 040000	/* Direct disk access.  */
 # define O_DIRECTORY	0200000	/* Must be a directory.  */
 # define O_DIRECTORY	0200000	/* Must be a directory.  */
 # define O_NOFOLLOW	0400000	/* Do not follow links.  */
 # define O_NOFOLLOW	0400000	/* Do not follow links.  */
+# define O_NOATIME	01000000 /* don't set atime */
+# define O_CLOEXEC	02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 1 - 0
libc/sysdeps/linux/hppa/bits/fcntl.h

@@ -50,6 +50,7 @@
 # define O_DIRECTORY	00010000 /* Must be a directory. */
 # define O_DIRECTORY	00010000 /* Must be a directory. */
 # define O_NOFOLLOW	00000200 /* Do not follow links. */
 # define O_NOFOLLOW	00000200 /* Do not follow links. */
 # define O_NOATIME	04000000 /* Do not set atime. */
 # define O_NOATIME	04000000 /* Do not set atime. */
+# define O_CLOEXEC	010000000 /* set close_on_exec */
 #endif
 #endif
 
 
 #ifdef __USE_LARGEFILE64
 #ifdef __USE_LARGEFILE64

+ 0 - 2
libc/sysdeps/linux/i386/bits/fcntl.h

@@ -50,9 +50,7 @@
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOATIME     01000000 /* Do not set atime.  */
 # define O_NOATIME     01000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 0 - 2
libc/sysdeps/linux/ia64/bits/fcntl.h

@@ -49,9 +49,7 @@
 # define O_DIRECTORY	0200000	/* must be a directory */
 # define O_DIRECTORY	0200000	/* must be a directory */
 # define O_NOFOLLOW	0400000 /* don't follow links */
 # define O_NOFOLLOW	0400000 /* don't follow links */
 # define O_NOATIME	01000000 /* Do not set atime.  */
 # define O_NOATIME	01000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC	02000000 /* Set close_on_exec.  */
 # define O_CLOEXEC	02000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 #ifdef __USE_LARGEFILE64
 #ifdef __USE_LARGEFILE64

+ 1 - 0
libc/sysdeps/linux/m68k/bits/fcntl.h

@@ -49,6 +49,7 @@
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_DIRECT	0200000	/* Direct disk access.	*/
 # define O_DIRECT	0200000	/* Direct disk access.	*/
 # define O_NOATIME	01000000 /* Do not set atime.  */
 # define O_NOATIME	01000000 /* Do not set atime.  */
+# define O_CLOEXEC	02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 2 - 0
libc/sysdeps/linux/microblaze/bits/fcntl.h

@@ -45,6 +45,8 @@
 # define O_DIRECTORY	 040000	/* Must be a directory.	 */
 # define O_DIRECTORY	 040000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_DIRECT	0200000	/* Direct disk access.	*/
 # define O_DIRECT	0200000	/* Direct disk access.	*/
+# define O_NOATIME	01000000 /* Do not set atime.  */
+# define O_CLOEXEC	02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 1 - 0
libc/sysdeps/linux/mips/bits/fcntl.h

@@ -51,6 +51,7 @@
 # define O_DIRECT	0x8000	/* Direct disk access hint.  */
 # define O_DIRECT	0x8000	/* Direct disk access hint.  */
 # define O_DIRECTORY	0x10000	/* Must be a directory.	 */
 # define O_DIRECTORY	0x10000	/* Must be a directory.	 */
 # define O_NOATIME	0x40000	/* Do not set atime.  */
 # define O_NOATIME	0x40000	/* Do not set atime.  */
+# define O_CLOEXEC	02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has no synchronisity options for data and read operations.
 /* For now Linux has no synchronisity options for data and read operations.

+ 0 - 2
libc/sysdeps/linux/powerpc/bits/fcntl.h

@@ -50,9 +50,7 @@
 # define O_DIRECTORY	 040000	/* Must be a directory.	 */
 # define O_DIRECTORY	 040000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0100000	/* Do not follow links.	 */
 # define O_NOATIME	01000000 /* Do not set atime.  */
 # define O_NOATIME	01000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC	02000000 /* Set close_on_exec.  */
 # define O_CLOEXEC	02000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 #ifdef __USE_LARGEFILE64
 #ifdef __USE_LARGEFILE64

+ 0 - 2
libc/sysdeps/linux/sh/bits/fcntl.h

@@ -50,9 +50,7 @@
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOATIME     01000000 /* Do not set atime.  */
 # define O_NOATIME     01000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 2 - 0
libc/sysdeps/linux/sh64/bits/fcntl.h

@@ -45,6 +45,8 @@
 # define O_DIRECT	 040000	/* Direct disk access.  */
 # define O_DIRECT	 040000	/* Direct disk access.  */
 # define O_DIRECTORY	0200000	/* Must be a directory.  */
 # define O_DIRECTORY	0200000	/* Must be a directory.  */
 # define O_NOFOLLOW	0400000	/* Do not follow links.  */
 # define O_NOFOLLOW	0400000	/* Do not follow links.  */
+# define O_NOATIME	01000000 /* Do not set atime.  */
+# define O_CLOEXEC	02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 0 - 2
libc/sysdeps/linux/sparc/bits/fcntl.h

@@ -49,9 +49,7 @@
 # define O_NOFOLLOW	0x20000 /* don't follow links */
 # define O_NOFOLLOW	0x20000 /* don't follow links */
 # define O_DIRECT	0x100000 /* direct disk access hint */
 # define O_DIRECT	0x100000 /* direct disk access hint */
 # define O_NOATIME	0x200000 /* Do not set atime.  */
 # define O_NOATIME	0x200000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC	0x400000 /* Set close_on_exit.  */
 # define O_CLOEXEC	0x400000 /* Set close_on_exit.  */
-# endif
 #endif
 #endif
 
 
 #ifdef __USE_LARGEFILE64
 #ifdef __USE_LARGEFILE64

+ 0 - 2
libc/sysdeps/linux/x86_64/bits/fcntl.h

@@ -50,9 +50,7 @@
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOATIME     01000000 /* Do not set atime.  */
 # define O_NOATIME     01000000 /* Do not set atime.  */
-# if 0
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
 # define O_CLOEXEC     02000000 /* Set close_on_exec.  */
-# endif
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.

+ 1 - 0
libc/sysdeps/linux/xtensa/bits/fcntl.h

@@ -50,6 +50,7 @@
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_DIRECTORY	0200000	/* Must be a directory.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOFOLLOW	0400000	/* Do not follow links.	 */
 # define O_NOATIME     01000000 /* Do not set atime.  */
 # define O_NOATIME     01000000 /* Do not set atime.  */
+# define O_CLOEXEC     02000000 /* set close_on_exec */
 #endif
 #endif
 
 
 /* For now Linux has synchronisity options for data and read operations.
 /* For now Linux has synchronisity options for data and read operations.