about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2007-07-22 19:02:23 +0000
committerUlrich Drepper <drepper@redhat.com>2007-07-22 19:02:23 +0000
commitc942388dad906c5384837a09477ac743fffed157 (patch)
treeaad75651505b9f4d18eda8d494486c8bcbdb588e
parentcc7837639debe7c5fecb1bec7e0109db8336a90b (diff)
downloadglibc-c942388dad906c5384837a09477ac743fffed157.tar.gz
glibc-c942388dad906c5384837a09477ac743fffed157.tar.xz
glibc-c942388dad906c5384837a09477ac743fffed157.zip
* login/utmp_file.c (setutent_file): Use O_CLOEXEC if possible.
-rw-r--r--ChangeLog2
-rw-r--r--login/utmp_file.c42
2 files changed, 35 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index b7ee456696..1dbfa13ef1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2007-07-22  Ulrich Drepper  <drepper@redhat.com>
 
+	* login/utmp_file.c (setutent_file): Use O_CLOEXEC if possible.
+
 	* libio/fileops.c (_IO_new_file_fopen): Recognize 'e' flag and set
 	O_CLOEXEC is needed.
 	* nis/nss_compat/compat-grp.c: Use 'e' flag when opening file.
diff --git a/login/utmp_file.c b/login/utmp_file.c
index 871c856071..4a9e409454 100644
--- a/login/utmp_file.c
+++ b/login/utmp_file.c
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <utmp.h>
 #include <not-cancel.h>
+#include <kernel-features.h>
 
 #include "utmp-private.h"
 #include "utmp-equal.h"
@@ -140,24 +141,47 @@ setutent_file (void)
 
       file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
 
-      file_fd = open_not_cancel_2 (file_name, O_RDWR | O_LARGEFILE);
+#ifdef O_CLOEXEC
+# define O_flags O_LARGEFILE | O_CLOEXEC
+#else
+# define O_flags O_LARGEFILE
+#endif
+      file_fd = open_not_cancel_2 (file_name, O_RDWR | O_flags);
       if (file_fd == -1)
 	{
 	  /* Hhm, read-write access did not work.  Try read-only.  */
-	  file_fd = open_not_cancel_2 (file_name, O_RDONLY | O_LARGEFILE);
+	  file_fd = open_not_cancel_2 (file_name, O_RDONLY | O_flags);
 	  if (file_fd == -1)
 	    return 0;
 	}
 
-      /* We have to make sure the file is `closed on exec'.  */
-      result = fcntl_not_cancel (file_fd, F_GETFD, 0);
-      if (result >= 0)
-	result = fcntl_not_cancel (file_fd, F_SETFD, result | FD_CLOEXEC);
-      if (result == -1)
+#ifndef __ASSUME_O_CLOEXEC
+# ifdef O_CLOEXEC
+      static int have_o_cloexec;
+
+      if (have_o_cloexec <= 0)
+# endif
 	{
-	  close_not_cancel_no_status (file_fd);
-	  return 0;
+	  /* We have to make sure the file is `closed on exec'.  */
+	  result = fcntl_not_cancel (file_fd, F_GETFD, 0);
+	  if (result >= 0)
+	    {
+# ifdef O_CLOEXEC
+	      if (have_o_cloexec == 0)
+		have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;
+# endif
+
+	      result = fcntl_not_cancel (file_fd, F_SETFD,
+					 result | FD_CLOEXEC);
+	    }
+
+	  if (result == -1)
+	    {
+	      close_not_cancel_no_status (file_fd);
+	      return 0;
+	    }
 	}
+#endif
     }
 
   __lseek64 (file_fd, 0, SEEK_SET);