[git commit nptl] CLOEXEC: use open(CLOEXEC) if exist; do not check fcntl(FD_CLOEXEC) failure

Denys Vlasenko vda.linux at googlemail.com
Fri Sep 18 14:07:31 UTC 2009


commit: http://git.uclibc.org/uClibc/commit/?id=12b4c6b14c197397b47122c1a6c60e179d83376f
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/nptl

    text           data     bss     dec     hex filename
-    370              0       0     370     172 libc/misc/dirent/opendir.o
+    366              0       0     366     16e libc/misc/dirent/opendir.o
-    375              4       0     379     17b libc/pwd_grp/lckpwdf.o
+    356              4       0     360     168 libc/pwd_grp/lckpwdf.o
-    248              0       0     248      f8 librt/shm.o
+    209              0       0     209      d1 librt/shm.o

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
Signed-off-by: Austin Foxley <austinf at cetoncorp.com>
---
 libc/misc/dirent/opendir.c                         |   25 ++++++++-----
 libc/misc/utmp/utent.c                             |   39 ++++++++++++--------
 libc/pwd_grp/lckpwdf.c                             |   34 ++++++-----------
 .../sysdeps/unix/sysv/linux/mq_notify.c            |    4 +--
 librt/shm.c                                        |   17 +++++----
 5 files changed, 61 insertions(+), 58 deletions(-)

diff --git a/libc/misc/dirent/opendir.c b/libc/misc/dirent/opendir.c
index 5f9d26e..29e28b1 100644
--- a/libc/misc/dirent/opendir.c
+++ b/libc/misc/dirent/opendir.c
@@ -84,26 +84,25 @@ DIR *opendir(const char *name)
 	}
 # define O_DIRECTORY 0
 #endif
+
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
-	if ((fd = open_not_cancel_2(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0)
+	fd = open_not_cancel_2(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
 #else
-	if ((fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY)) < 0)
+	fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
 #endif
+	if (fd < 0)
 		return NULL;
 
 	/* Note: we should check to make sure that between the stat() and open()
 	 * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
 	 * defined and since Linux has supported it for like ever, i'm not going
 	 * to worry about it right now (if ever). */
-	if (fstat(fd, &statbuf) < 0)
-		goto close_and_ret;
-
-	/* According to POSIX, directory streams should be closed when
-	 * exec. From "Anna Pluzhnikov" <besp at midway.uchicago.edu>.
-	 */
-	if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
+	if (fstat(fd, &statbuf) < 0) {
 		int saved_errno;
-close_and_ret:
 		saved_errno = errno;
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
 		close_not_cancel_no_status(fd);
@@ -114,6 +113,12 @@ close_and_ret:
 		return NULL;
 	}
 
+	/* According to POSIX, directory streams should be closed when
+	 * exec. From "Anna Pluzhnikov" <besp at midway.uchicago.edu>.
+	 */
+	if (O_CLOEXEC == 0)
+		fcntl(fd, F_SETFD, FD_CLOEXEC);
+
 	ptr = fd_to_DIR(fd, statbuf.st_blksize);
 
 	if (!ptr) {
diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c
index dc3110f..eca2ba1 100644
--- a/libc/misc/utmp/utent.c
+++ b/libc/misc/utmp/utent.c
@@ -38,36 +38,43 @@ static const char *static_ut_name = default_file_name;
 static void __setutent(void)
 {
     if (static_fd < 0) {
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
-	static_fd = open(static_ut_name, O_RDWR);
+	static_fd = open_not_cancel_2(static_ut_name, O_RDWR | O_CLOEXEC);
 #else
-	static_fd = open_not_cancel_2(static_ut_name, O_RDWR);
+	static_fd = open(static_ut_name, O_RDWR | O_CLOEXEC);
 #endif
 	if (static_fd < 0) {
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
-	    static_fd = open(static_ut_name, O_RDONLY);
+	    static_fd = open_not_cancel_2(static_ut_name, O_RDONLY | O_CLOEXEC);
 #else
-	    static_fd = open(static_ut_name, O_RDONLY);
+	    static_fd = open(static_ut_name, O_RDONLY | O_CLOEXEC);
 #endif
 	    if (static_fd < 0) {
 		return; /* static_fd remains < 0 */
 	    }
 	}
-	/* Make sure the file will be closed on exec()  */
+
+	if (O_CLOEXEC == 0) {
+	    /* Make sure the file will be closed on exec()  */
 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
-	fcntl_not_cancel(static_fd, F_SETFD, FD_CLOEXEC);
+	    fcntl_not_cancel(static_fd, F_SETFD, FD_CLOEXEC);
 #else
-	fcntl(static_fd, F_SETFD, FD_CLOEXEC);
+	    fcntl(static_fd, F_SETFD, FD_CLOEXEC);
 #endif
-	// 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;
-	//}
+	    // 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;
+	    //}
+	}
 	return;
     }
     lseek(static_fd, 0, SEEK_SET);
diff --git a/libc/pwd_grp/lckpwdf.c b/libc/pwd_grp/lckpwdf.c
index c0c8f0d..68c46e0 100644
--- a/libc/pwd_grp/lckpwdf.c
+++ b/libc/pwd_grp/lckpwdf.c
@@ -28,17 +28,6 @@
 #include <paths.h>
 #include <shadow.h>
 
-/* Experimentally off - libc_hidden_proto(memset) */
-/* libc_hidden_proto(open) */
-/* libc_hidden_proto(fcntl) */
-/* libc_hidden_proto(close) */
-/* libc_hidden_proto(sigfillset) */
-/* libc_hidden_proto(sigaction) */
-/* libc_hidden_proto(sigprocmask) */
-/* libc_hidden_proto(sigaddset) */
-/* libc_hidden_proto(sigemptyset) */
-/* libc_hidden_proto(alarm) */
-
 /* How long to wait for getting the lock before returning with an error.  */
 #define TIMEOUT 15 /* sec */
 
@@ -56,7 +45,6 @@ static void noop_handler (int __sig);
 int
 lckpwdf (void)
 {
-  int flags;
   sigset_t saved_set;			/* Saved set of caught signals.  */
   struct sigaction saved_act;		/* Saved signal action.  */
   sigset_t new_set;			/* New set of caught signals.  */
@@ -72,16 +60,18 @@ lckpwdf (void)
   /* Prevent problems caused by multiple threads.  */
   __UCLIBC_MUTEX_LOCK(mylock);
 
-  lock_fd = open (_PATH_PASSWD, O_WRONLY);
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+  lock_fd = open (_PATH_PASSWD, O_WRONLY | O_CLOEXEC);
   if (lock_fd == -1) {
     /* Cannot create lock file.  */
-	goto DONE;
+    goto DONE;
   }
-
   /* Make sure file gets correctly closed when process finished.  */
-  flags = fcntl (lock_fd, F_GETFD);
-  flags |= FD_CLOEXEC;
-  fcntl (lock_fd, F_SETFD, flags);
+  if (O_CLOEXEC == 0)
+    fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
+
   /* Now we have to get exclusive write access.  Since multiple
      process could try this we won't stop when it first fails.
      Instead we set a timeout for the system call.  Once the timer
@@ -91,7 +81,7 @@ lckpwdf (void)
 
      It is important that we don't change the signal state.  We must
      restore the old signal behaviour.  */
-  memset (&new_act, '\0', sizeof (struct sigaction));
+  memset (&new_act, '\0', sizeof (new_act));
   new_act.sa_handler = noop_handler;
   __sigfillset (&new_act.sa_mask);
 
@@ -125,7 +115,7 @@ lckpwdf (void)
   if (result < 0) {
     close(lock_fd);
     lock_fd = -1;
-	goto DONE;
+    goto DONE;
   }
   rv = 0;
 
@@ -146,7 +136,7 @@ ulckpwdf (void)
   else
     {
       /* Prevent problems caused by multiple threads.  */
-	  __UCLIBC_MUTEX_LOCK(mylock);
+      __UCLIBC_MUTEX_LOCK(mylock);
 
       result = close (lock_fd);
 
@@ -154,7 +144,7 @@ ulckpwdf (void)
       lock_fd = -1;
 
       /* Clear mutex.  */
-	  __UCLIBC_MUTEX_UNLOCK(mylock);
+      __UCLIBC_MUTEX_UNLOCK(mylock);
     }
 
   return result;
diff --git a/libpthread/linuxthreads/sysdeps/unix/sysv/linux/mq_notify.c b/libpthread/linuxthreads/sysdeps/unix/sysv/linux/mq_notify.c
index 31d614b..a44d051 100644
--- a/libpthread/linuxthreads/sysdeps/unix/sysv/linux/mq_notify.c
+++ b/libpthread/linuxthreads/sysdeps/unix/sysv/linux/mq_notify.c
@@ -163,8 +163,7 @@ init_mq_netlink (void)
 	return;
 
       /* Make sure the descriptor is closed on exec.  */
-      if (fcntl (netlink_socket, F_SETFD, FD_CLOEXEC) != 0)
-	goto errout;
+      fcntl (netlink_socket, F_SETFD, FD_CLOEXEC);
     }
 
   int err = 1;
@@ -213,7 +212,6 @@ init_mq_netlink (void)
 
   if (err != 0)
     {
-    errout:
       close_not_cancel_no_status (netlink_socket);
       netlink_socket = -1;
     }
diff --git a/librt/shm.c b/librt/shm.c
index f1ef0e7..f0a9740 100644
--- a/librt/shm.c
+++ b/librt/shm.c
@@ -71,13 +71,16 @@ int shm_open(const char *name, int oflag, mode_t mode)
 #else
 	fd = open(shm_name, oflag, mode);
 	if (fd >= 0) {
-		int fdflags = fcntl(fd, F_GETFD, 0);
-		if (fdflags >= 0)
-			fdflags = fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
-		if (fdflags < 0) {
-			close(fd);
-			fd = -1;
-		}
+		fcntl(fd, F_SETFD, FD_CLOEXEC);
+		// thus far, {G,S}ETFD only has this single flag,
+		// and setting it never fails.
+		//int fdflags = fcntl(fd, F_GETFD);
+		//if (fdflags >= 0)
+		//	fdflags = fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
+		//if (fdflags < 0) {
+		//	close(fd);
+		//	fd = -1;
+		//}
 	}
 #endif
 	old_errno = errno;
-- 
1.6.3.3



More information about the uClibc-cvs mailing list