usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-cloexec.c
blobdf2e11fad067419cdcc41204737589c23f6f6026
1 /* Test duplicating non-inheritable file descriptors.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
19 #include <config.h>
21 #include "cloexec.h"
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
30 # include <windows.h>
31 /* Get _get_osfhandle. */
32 # if GNULIB_MSVC_NOTHROW
33 # include "msvc-nothrow.h"
34 # else
35 # include <io.h>
36 # endif
37 #endif
39 #include "binary-io.h"
40 #include "macros.h"
42 /* Return non-zero if FD is open and inheritable across exec/spawn. */
43 static int
44 is_inheritable (int fd)
46 #if defined _WIN32 && ! defined __CYGWIN__
47 /* On native Windows, the initial state of unassigned standard file
48 descriptors is that they are open but point to an
49 INVALID_HANDLE_VALUE, and there is no fcntl. */
50 HANDLE h = (HANDLE) _get_osfhandle (fd);
51 DWORD flags;
52 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
53 return 0;
54 return (flags & HANDLE_FLAG_INHERIT) != 0;
55 #else
56 # ifndef F_GETFD
57 # error Please port fcntl to your platform
58 # endif
59 int i = fcntl (fd, F_GETFD);
60 return 0 <= i && (i & FD_CLOEXEC) == 0;
61 #endif
64 #if !O_BINARY
65 # define set_binary_mode my_set_binary_mode
66 static int
67 set_binary_mode (_GL_UNUSED int fd, _GL_UNUSED int mode)
69 return 0;
71 #endif
73 /* Return non-zero if FD is open in the given MODE, which is either
74 O_TEXT or O_BINARY. */
75 static int
76 is_mode (int fd, int mode)
78 int value = set_binary_mode (fd, O_BINARY);
79 set_binary_mode (fd, value);
80 return mode == value;
83 int
84 main (void)
86 const char *file = "test-cloexec.tmp";
87 int fd = creat (file, 0600);
88 int fd2;
89 int bad_fd = getdtablesize ();
91 /* Assume std descriptors were provided by invoker. */
92 ASSERT (STDERR_FILENO < fd);
93 ASSERT (is_inheritable (fd));
95 /* Normal use of set_cloexec_flag. */
96 ASSERT (set_cloexec_flag (fd, true) == 0);
97 #if !(defined _WIN32 && ! defined __CYGWIN__)
98 ASSERT (!is_inheritable (fd));
99 #endif
100 ASSERT (set_cloexec_flag (fd, false) == 0);
101 ASSERT (is_inheritable (fd));
103 /* Normal use of dup_cloexec. */
104 fd2 = dup_cloexec (fd);
105 ASSERT (fd < fd2);
106 ASSERT (!is_inheritable (fd2));
107 ASSERT (close (fd) == 0);
108 ASSERT (dup_cloexec (fd2) == fd);
109 ASSERT (!is_inheritable (fd));
110 ASSERT (close (fd2) == 0);
112 /* On systems that distinguish between text and binary mode,
113 dup_cloexec reuses the mode of the source. */
114 set_binary_mode (fd, O_BINARY);
115 ASSERT (is_mode (fd, O_BINARY));
116 fd2 = dup_cloexec (fd);
117 ASSERT (fd < fd2);
118 ASSERT (is_mode (fd2, O_BINARY));
119 ASSERT (close (fd2) == 0);
120 set_binary_mode (fd, O_TEXT);
121 ASSERT (is_mode (fd, O_TEXT));
122 fd2 = dup_cloexec (fd);
123 ASSERT (fd < fd2);
124 ASSERT (is_mode (fd2, O_TEXT));
125 ASSERT (close (fd2) == 0);
127 /* Test error handling. */
128 errno = 0;
129 ASSERT (set_cloexec_flag (-1, false) == -1);
130 ASSERT (errno == EBADF);
131 errno = 0;
132 ASSERT (set_cloexec_flag (bad_fd, false) == -1);
133 ASSERT (errno == EBADF);
134 errno = 0;
135 ASSERT (set_cloexec_flag (fd2, false) == -1);
136 ASSERT (errno == EBADF);
137 errno = 0;
138 ASSERT (dup_cloexec (-1) == -1);
139 ASSERT (errno == EBADF);
140 errno = 0;
141 ASSERT (dup_cloexec (bad_fd) == -1);
142 ASSERT (errno == EBADF);
143 errno = 0;
144 ASSERT (dup_cloexec (fd2) == -1);
145 ASSERT (errno == EBADF);
147 /* Clean up. */
148 ASSERT (close (fd) == 0);
149 ASSERT (unlink (file) == 0);
151 return test_exit_status;