diff options
author | Eric Blake <ebb9@byu.net> | 2009-12-05 06:19:01 -0700 |
---|---|---|
committer | Eric Blake <ebb9@byu.net> | 2009-12-05 14:32:23 -0700 |
commit | 173359dbe40ac4381088060b0f26e66529fa0f29 (patch) | |
tree | 66cfe76dc1be6e35bf33ae60d2e48f48d687ffd5 /tests | |
parent | d8ccc13ba3ba05654f9be09bba5492cc8da30c3b (diff) | |
download | gnulib-173359dbe40ac4381088060b0f26e66529fa0f29.tar.gz |
test-dup2: enhance test
Ensure that dup2(cloexec_fd, target) returns an inheritable fd.
* modules/dup2-tests (Depends-on): Add cloexec.
* tests/test-dup2.c (main): Enhance test.
Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-dup2.c | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/tests/test-dup2.c b/tests/test-dup2.c index 32c354d0b0..005160fc6c 100644 --- a/tests/test-dup2.c +++ b/tests/test-dup2.c @@ -25,6 +25,8 @@ #include <stdio.h> #include <stdlib.h> +#include "cloexec.h" + #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ /* Get declarations of the Win32 API functions. */ # define WIN32_LEAN_AND_MEAN @@ -32,15 +34,15 @@ #endif #define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ + do \ + { \ + if (!(expr)) \ + { \ fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - fflush (stderr); \ - abort (); \ - } \ - } \ + fflush (stderr); \ + abort (); \ + } \ + } \ while (0) /* Return non-zero if FD is open. */ @@ -60,6 +62,28 @@ is_open (int fd) #endif } +/* Return non-zero if FD is open and inheritable across exec/spawn. */ +static int +is_inheritable (int fd) +{ +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + /* On Win32, the initial state of unassigned standard file + descriptors is that they are open but point to an + INVALID_HANDLE_VALUE, and there is no fcntl. */ + HANDLE h = (HANDLE) _get_osfhandle (fd); + DWORD flags; + if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0) + return 0; + return (flags & HANDLE_FLAG_INHERIT) != 0; +#else +# ifndef F_GETFD +# error Please port fcntl to your platform +# endif + int i = fcntl (fd, F_GETFD); + return 0 <= i && (i & FD_CLOEXEC) == 0; +#endif +} + int main (void) { @@ -125,8 +149,18 @@ main (void) ASSERT (read (fd, buffer, 1) == 1); ASSERT (*buffer == '2'); + /* Any new fd created by dup2 must not be cloexec. */ + ASSERT (close (fd + 2) == 0); + ASSERT (dup_cloexec (fd) == fd + 1); + ASSERT (!is_inheritable (fd + 1)); + ASSERT (dup2 (fd + 1, fd + 1) == fd + 1); + ASSERT (!is_inheritable (fd + 1)); + ASSERT (dup2 (fd + 1, fd + 2) == fd + 2); + ASSERT (is_inheritable (fd + 2)); + /* Clean up. */ ASSERT (close (fd + 2) == 0); + ASSERT (close (fd + 1) == 0); ASSERT (close (fd) == 0); ASSERT (unlink (file) == 0); |