popen-safer: test O_CLOEXEC at run-time.
authorPaolo Bonzini <bonzini@gnu.org>
Fri, 21 Aug 2009 07:36:14 +0000 (09:36 +0200)
committerPaolo Bonzini <bonzini@gnu.org>
Fri, 21 Aug 2009 12:05:25 +0000 (14:05 +0200)
* lib/popen-safer.c: Test O_CLOEXEC at run-time.

ChangeLog
lib/popen-safer.c

index a1b015d03029b857c7b0ae7c405b5438c64de190..565d70afa080ba9c9e492008fd09fe76f108870a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2009-08-21  Paolo Bonzini  <bonzini@gnu.org>
+
+       popen-safer: test O_CLOEXEC at run-time.
+       * lib/popen-safer.c: Test O_CLOEXEC at run-time.
+
 2009-08-21  Paolo Bonzini  <bonzini@gnu.org>
 
        fcntl: move more flags to the header
index 1905be5ecbf898b079255f63d4629490a0d33b8e..3d87c28bb38fc103b797ca1c5a318004836bd686 100644 (file)
 
 #include "cloexec.h"
 
-#ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-#endif
-
 /* Like open (name, flags | O_CLOEXEC), although not necessarily
    atomic.  FLAGS must not include O_CREAT.  */
 
 static int
 open_noinherit (char const *name, int flags)
 {
-  int fd = open (name, flags | O_CLOEXEC);
-  if (0 <= fd && !O_CLOEXEC && set_cloexec_flag (fd, true) != 0)
+  int fd;
+#ifdef O_CLOEXEC
+  /* 0 = unknown, 1 = yes, -1 = no.  */
+  static int have_cloexec;
+  if (have_cloexec >= 0)
+    {
+      fd = open (name, flags | O_CLOEXEC);
+      if (have_cloexec == 0 && (0 <= fd || errno == EINVAL))
+       have_cloexec = (0 <= fd ? 1 : -1);
+      if (have_cloexec == 1)
+       return fd;
+    }
+#endif
+
+  fd = open (name, flags);
+  if (0 <= fd && set_cloexec_flag (fd, true) != 0)
     {
       int saved_errno = errno;
       close (fd);