open, openat: Really support O_CLOEXEC.
authorBruno Haible <bruno@clisp.org>
Sun, 24 May 2020 18:27:39 +0000 (20:27 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 24 May 2020 18:27:39 +0000 (20:27 +0200)
* lib/open.c (open): When have_cloexec is still undecided, do pass a
O_CLOEXEC flag to orig_open.
* lib/openat.c (rpl_openat): When have_cloexec is still undecided, do
pass a O_CLOEXEC flag to orig_openat.
* tests/test-open.h (test_open): Verify that O_CLOEXEC is honoured.
* modules/open-tests (Depends-on): Add fcntl.
* modules/openat-tests (Depends-on): Likewise.
* modules/fcntl-safer-tests (Depends-on): Likewise.

ChangeLog
lib/open.c
lib/openat.c
modules/fcntl-safer-tests
modules/open-tests
modules/openat-tests
tests/test-open.h

index 4dd90f83a138420dfcd72326b305ea18f7e32ec7..ae9fae472f93a5f534e5c0612fd85962c85167ea 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2020-05-24  Bruno Haible  <bruno@clisp.org>
+
+       open, openat: Really support O_CLOEXEC.
+       * lib/open.c (open): When have_cloexec is still undecided, do pass a
+       O_CLOEXEC flag to orig_open.
+       * lib/openat.c (rpl_openat): When have_cloexec is still undecided, do
+       pass a O_CLOEXEC flag to orig_openat.
+       * tests/test-open.h (test_open): Verify that O_CLOEXEC is honoured.
+       * modules/open-tests (Depends-on): Add fcntl.
+       * modules/openat-tests (Depends-on): Likewise.
+       * modules/fcntl-safer-tests (Depends-on): Likewise.
+
 2020-05-24  Bruno Haible  <bruno@clisp.org>
 
        fopen: Fix the trailing slash workaround.
index bb180fde2920b19ce5277f3322a9d123e0a91f85..751b42d7dcf6489653ef2a0a892f5e42d965e6f0 100644 (file)
@@ -124,7 +124,7 @@ open (const char *filename, int flags, ...)
 #endif
 
   fd = orig_open (filename,
-                  flags & ~(have_cloexec <= 0 ? O_CLOEXEC : 0), mode);
+                  flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
 
   if (flags & O_CLOEXEC)
     {
index 974f1a8b3b460199afed67f72112e6d504bd68fb..fbe1d2e6e605e48473b32d9d36050e1055981854 100644 (file)
@@ -114,7 +114,7 @@ rpl_openat (int dfd, char const *filename, int flags, ...)
 # endif
 
   fd = orig_openat (dfd, filename,
-                    flags & ~(have_cloexec <= 0 ? O_CLOEXEC : 0), mode);
+                    flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
 
   if (flags & O_CLOEXEC)
     {
index cb35aed252f31310c1d2266db5f19fb282aeb66c..b967c8aa786e8d0318169767dfbcbb8e3dfe603f 100644 (file)
@@ -5,6 +5,7 @@ tests/macros.h
 
 Depends-on:
 stdbool
+fcntl
 symlink
 
 configure.ac:
index 5bc4e0f88523209fc2a1ecd3de69a1fb26b9f2c0..b2b6710e5c37e5131a3d236a1d5d5cf5056468c0 100644 (file)
@@ -6,6 +6,7 @@ tests/macros.h
 
 Depends-on:
 stdbool
+fcntl
 symlink
 
 configure.ac:
index 0b7370d6f22e601145102db229aea68ab953291d..c4a72f5fb8c6e1084fe74ff74b5327f800b4681c 100644 (file)
@@ -5,6 +5,7 @@ tests/signature.h
 tests/macros.h
 
 Depends-on:
+fcntl
 symlink
 
 configure.ac:
index c57054f747ac3891b987a37bd72c77cc4fabbf30..862c8dfa5376541ade498faf90394936f7f83f55 100644 (file)
@@ -88,6 +88,26 @@ test_open (int (*func) (char const *, int, ...), bool print)
   ASSERT (0 <= fd);
   ASSERT (close (fd) == 0);
 
+  /* O_CLOEXEC must be honoured.  */
+  if (O_CLOEXEC)
+    {
+      /* Since the O_CLOEXEC handling goes through a special code path at its
+         first invocation, test it twice.  */
+      int i;
+
+      for (i = 0; i < 2; i++)
+        {
+          int flags;
+
+          fd = func (BASE "file", O_CLOEXEC | O_RDONLY);
+          ASSERT (0 <= fd);
+          flags = fcntl (fd, F_GETFD);
+          ASSERT (flags >= 0);
+          ASSERT ((flags & FD_CLOEXEC) != 0);
+          ASSERT (close (fd) == 0);
+        }
+    }
+
   /* Symlink handling, where supported.  */
   if (symlink (BASE "file", BASE "link") != 0)
     {