about summary refs log tree commit diff
path: root/nss/nss_files/files-XXX.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2007-08-10 23:41:40 +0000
committerUlrich Drepper <drepper@redhat.com>2007-08-10 23:41:40 +0000
commitaa132749c85c62b1e28ae885b84c9fa8aed45dfb (patch)
tree16defacc2d854a94586cdc407a368a614efcb5dd /nss/nss_files/files-XXX.c
parent916ee8bcba20a92ce4c4633667b5b37a12f8cba3 (diff)
downloadglibc-aa132749c85c62b1e28ae885b84c9fa8aed45dfb.tar.gz
glibc-aa132749c85c62b1e28ae885b84c9fa8aed45dfb.tar.xz
glibc-aa132749c85c62b1e28ae885b84c9fa8aed45dfb.zip
* nss/nss_files/files-XXX.c (internal_setent): Use O_CLOEXEC if
	possible.
	* nss/nss_files/files-alias.c (internal_setent): Likewise.
	* nss/Makefile (libnss_files-routines): Add files-have_o_cloexec.
	* nss/nss_files/files-have_o_cloexec.c: New file.
Diffstat (limited to 'nss/nss_files/files-XXX.c')
-rw-r--r--nss/nss_files/files-XXX.c51
1 files changed, 34 insertions, 17 deletions
diff --git a/nss/nss_files/files-XXX.c b/nss/nss_files/files-XXX.c
index fb13fbe2b6..2149d1c401 100644
--- a/nss/nss_files/files-XXX.c
+++ b/nss/nss_files/files-XXX.c
@@ -1,5 +1,5 @@
 /* Common code for file-based databases in nss_files module.
-   Copyright (C) 1996-1999,2001,2002,2004 Free Software Foundation, Inc.
+   Copyright (C) 1996-1999,2001,2002,2004,2007 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -24,6 +24,8 @@
 #include <bits/libc-lock.h>
 #include "nsswitch.h"
 
+#include <kernel-features.h>
+
 /* These symbols are defined by the including source file:
 
    ENTNAME -- database name of the structure and functions (hostent, pwent).
@@ -74,29 +76,44 @@ internal_setent (int stayopen)
 
   if (stream == NULL)
     {
-      stream = fopen (DATAFILE, "r");
+      stream = fopen (DATAFILE, "re");
 
       if (stream == NULL)
 	status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
       else
 	{
-	  /* We have to make sure the file is  `closed on exec'.  */
-	  int result, flags;
-
-	  result = flags = fcntl (fileno (stream), F_GETFD, 0);
-	  if (result >= 0)
+#if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
+# ifdef O_CLOEXEC
+	  if (__have_o_cloexec <= 0)
+# endif
 	    {
-	      flags |= FD_CLOEXEC;
-	      result = fcntl (fileno (stream), F_SETFD, flags);
-	    }
-	  if (result < 0)
-	    {
-	      /* Something went wrong.  Close the stream and return a
-		 failure.  */
-	      fclose (stream);
-	      stream = NULL;
-	      status = NSS_STATUS_UNAVAIL;
+	      /* We have to make sure the file is  `closed on exec'.  */
+	      int result;
+	      int flags;
+
+	      result = flags = fcntl (fileno (stream), F_GETFD, 0);
+	      if (result >= 0)
+		{
+# ifdef O_CLOEXEC
+		  if (__have_o_cloexec == 0)
+		    __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
+		  if (__have_o_cloexec < 0)
+# endif
+		    {
+		      flags |= FD_CLOEXEC;
+		      result = fcntl (fileno (stream), F_SETFD, flags);
+		    }
+		}
+	      if (result < 0)
+		{
+		  /* Something went wrong.  Close the stream and return a
+		     failure.  */
+		  fclose (stream);
+		  stream = NULL;
+		  status = NSS_STATUS_UNAVAIL;
+		}
 	    }
+#endif
 	}
     }
   else