From ed8d8e847ab8c26165eaf8b446aee78c493dfce3 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 24 May 2020 20:27:39 +0200 Subject: [PATCH] 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. --- ChangeLog | 12 ++++++++++++ lib/open.c | 2 +- lib/openat.c | 2 +- modules/fcntl-safer-tests | 1 + modules/open-tests | 1 + modules/openat-tests | 1 + tests/test-open.h | 20 ++++++++++++++++++++ 7 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4dd90f83a1..ae9fae472f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2020-05-24 Bruno Haible + + 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 fopen: Fix the trailing slash workaround. diff --git a/lib/open.c b/lib/open.c index bb180fde29..751b42d7dc 100644 --- a/lib/open.c +++ b/lib/open.c @@ -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) { diff --git a/lib/openat.c b/lib/openat.c index 974f1a8b3b..fbe1d2e6e6 100644 --- a/lib/openat.c +++ b/lib/openat.c @@ -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) { diff --git a/modules/fcntl-safer-tests b/modules/fcntl-safer-tests index cb35aed252..b967c8aa78 100644 --- a/modules/fcntl-safer-tests +++ b/modules/fcntl-safer-tests @@ -5,6 +5,7 @@ tests/macros.h Depends-on: stdbool +fcntl symlink configure.ac: diff --git a/modules/open-tests b/modules/open-tests index 5bc4e0f885..b2b6710e5c 100644 --- a/modules/open-tests +++ b/modules/open-tests @@ -6,6 +6,7 @@ tests/macros.h Depends-on: stdbool +fcntl symlink configure.ac: diff --git a/modules/openat-tests b/modules/openat-tests index 0b7370d6f2..c4a72f5fb8 100644 --- a/modules/openat-tests +++ b/modules/openat-tests @@ -5,6 +5,7 @@ tests/signature.h tests/macros.h Depends-on: +fcntl symlink configure.ac: diff --git a/tests/test-open.h b/tests/test-open.h index c57054f747..862c8dfa53 100644 --- a/tests/test-open.h +++ b/tests/test-open.h @@ -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) { -- 2.39.5