unistr/u{8,16,32}-uctomb: Avoid possible trouble with huge strings.
[gnulib.git] / tests / test-fcntl.c
blob9ef354c03d8e49957c5a5d9a5f0a6bf0a639639c
1 /* Test of fcntl(2).
2 Copyright (C) 2009-2020 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 /* Specification. */
22 #include <fcntl.h>
24 #include "signature.h"
25 SIGNATURE_CHECK (fcntl, int, (int, int, ...));
27 /* Helpers. */
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <unistd.h>
33 #if defined _WIN32 && ! defined __CYGWIN__
34 /* Get declarations of the native Windows API functions. */
35 # define WIN32_LEAN_AND_MEAN
36 # include <windows.h>
37 /* Get _get_osfhandle. */
38 # if GNULIB_MSVC_NOTHROW
39 # include "msvc-nothrow.h"
40 # else
41 # include <io.h>
42 # endif
43 #endif
45 #include "binary-io.h"
46 #include "macros.h"
48 #if !O_BINARY
49 # define set_binary_mode(f,m) zero ()
50 static int zero (void) { return 0; }
51 #endif
53 /* Return true if FD is open. */
54 static bool
55 is_open (int fd)
57 #if defined _WIN32 && ! defined __CYGWIN__
58 /* On native Windows, the initial state of unassigned standard file
59 descriptors is that they are open but point to an
60 INVALID_HANDLE_VALUE, and there is no fcntl. */
61 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
62 #else
63 # ifndef F_GETFL
64 # error Please port fcntl to your platform
65 # endif
66 return 0 <= fcntl (fd, F_GETFL);
67 #endif
70 /* Return true if FD is open and inheritable across exec/spawn. */
71 static bool
72 is_inheritable (int fd)
74 #if defined _WIN32 && ! defined __CYGWIN__
75 /* On native Windows, the initial state of unassigned standard file
76 descriptors is that they are open but point to an
77 INVALID_HANDLE_VALUE, and there is no fcntl. */
78 HANDLE h = (HANDLE) _get_osfhandle (fd);
79 DWORD flags;
80 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
81 return false;
82 return (flags & HANDLE_FLAG_INHERIT) != 0;
83 #else
84 # ifndef F_GETFD
85 # error Please port fcntl to your platform
86 # endif
87 int i = fcntl (fd, F_GETFD);
88 return 0 <= i && (i & FD_CLOEXEC) == 0;
89 #endif
92 /* Return non-zero if FD is open in the given MODE, which is either
93 O_TEXT or O_BINARY. */
94 static bool
95 is_mode (int fd, int mode)
97 int value = set_binary_mode (fd, O_BINARY);
98 set_binary_mode (fd, value);
99 return mode == value;
102 /* Since native fcntl can have more supported operations than our
103 replacement is aware of, and since various operations assign
104 different types to the vararg argument, a wrapper around fcntl must
105 be able to pass a vararg of unknown type on through to the original
106 fcntl. Make sure that this works properly: func1 behaves like the
107 original fcntl interpreting the vararg as an int or a pointer to a
108 struct, and func2 behaves like rpl_fcntl that doesn't know what
109 type to forward. */
110 struct dummy_struct
112 long filler;
113 int value;
115 static int
116 func1 (int a, ...)
118 va_list arg;
119 int i;
120 va_start (arg, a);
121 if (a < 4)
122 i = va_arg (arg, int);
123 else
125 struct dummy_struct *s = va_arg (arg, struct dummy_struct *);
126 i = s->value;
128 va_end (arg);
129 return i;
131 static int
132 func2 (int a, ...)
134 va_list arg;
135 void *p;
136 va_start (arg, a);
137 p = va_arg (arg, void *);
138 va_end (arg);
139 return func1 (a, p);
142 /* Ensure that all supported fcntl actions are distinct, and
143 usable in preprocessor expressions. */
144 static void
145 check_flags (void)
147 switch (0)
149 case F_DUPFD:
150 #if F_DUPFD
151 #endif
153 case F_DUPFD_CLOEXEC:
154 #if F_DUPFD_CLOEXEC
155 #endif
157 case F_GETFD:
158 #if F_GETFD
159 #endif
161 #ifdef F_SETFD
162 case F_SETFD:
163 # if F_SETFD
164 # endif
165 #endif
167 #ifdef F_GETFL
168 case F_GETFL:
169 # if F_GETFL
170 # endif
171 #endif
173 #ifdef F_SETFL
174 case F_SETFL:
175 # if F_SETFL
176 # endif
177 #endif
179 #ifdef F_GETOWN
180 case F_GETOWN:
181 # if F_GETOWN
182 # endif
183 #endif
185 #ifdef F_SETOWN
186 case F_SETOWN:
187 # if F_SETOWN
188 # endif
189 #endif
191 #ifdef F_GETLK
192 case F_GETLK:
193 # if F_GETLK
194 # endif
195 #endif
197 #ifdef F_SETLK
198 case F_SETLK:
199 # if F_SETLK
200 # endif
201 #endif
203 #ifdef F_SETLKW
204 case F_SETLKW:
205 # if F_SETLKW
206 # endif
207 #endif
209 default:
215 main (void)
217 const char *file = "test-fcntl.tmp";
218 int fd;
219 int bad_fd = getdtablesize ();
221 /* Sanity check that rpl_fcntl is likely to work. */
222 ASSERT (func2 (1, 2) == 2);
223 ASSERT (func2 (2, -2) == -2);
224 ASSERT (func2 (3, 0x80000000) == 0x80000000);
226 struct dummy_struct s = { 0L, 4 };
227 ASSERT (func2 (4, &s) == 4);
229 check_flags ();
231 /* Assume std descriptors were provided by invoker, and ignore fds
232 that might have been inherited. */
233 fd = creat (file, 0600);
234 ASSERT (STDERR_FILENO < fd);
235 close (fd + 1);
236 close (fd + 2);
238 /* For F_DUPFD*, the source must be valid. */
239 errno = 0;
240 ASSERT (fcntl (-1, F_DUPFD, 0) == -1);
241 ASSERT (errno == EBADF);
242 errno = 0;
243 ASSERT (fcntl (fd + 1, F_DUPFD, 0) == -1);
244 ASSERT (errno == EBADF);
245 errno = 0;
246 ASSERT (fcntl (bad_fd, F_DUPFD, 0) == -1);
247 ASSERT (errno == EBADF);
248 errno = 0;
249 ASSERT (fcntl (-1, F_DUPFD_CLOEXEC, 0) == -1);
250 ASSERT (errno == EBADF);
251 errno = 0;
252 ASSERT (fcntl (fd + 1, F_DUPFD_CLOEXEC, 0) == -1);
253 ASSERT (errno == EBADF);
254 errno = 0;
255 ASSERT (fcntl (bad_fd, F_DUPFD_CLOEXEC, 0) == -1);
256 ASSERT (errno == EBADF);
258 /* For F_DUPFD*, the destination must be valid. */
259 errno = 0;
260 ASSERT (fcntl (fd, F_DUPFD, -1) == -1);
261 ASSERT (errno == EINVAL);
262 errno = 0;
263 ASSERT (fcntl (fd, F_DUPFD, bad_fd) == -1);
264 ASSERT (errno == EINVAL);
265 errno = 0;
266 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, -1) == -1);
267 ASSERT (errno == EINVAL);
268 errno = 0;
269 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, bad_fd) == -1);
270 ASSERT (errno == EINVAL
271 || errno == EMFILE /* WSL */);
273 /* For F_DUPFD*, check for correct inheritance, as well as
274 preservation of text vs. binary. */
275 set_binary_mode (fd, O_BINARY);
276 ASSERT (is_open (fd));
277 ASSERT (!is_open (fd + 1));
278 ASSERT (!is_open (fd + 2));
279 ASSERT (is_inheritable (fd));
280 ASSERT (is_mode (fd, O_BINARY));
282 ASSERT (fcntl (fd, F_DUPFD, fd) == fd + 1);
283 ASSERT (is_open (fd));
284 ASSERT (is_open (fd + 1));
285 ASSERT (!is_open (fd + 2));
286 ASSERT (is_inheritable (fd + 1));
287 ASSERT (is_mode (fd, O_BINARY));
288 ASSERT (is_mode (fd + 1, O_BINARY));
289 ASSERT (close (fd + 1) == 0);
291 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, fd + 2) == fd + 2);
292 ASSERT (is_open (fd));
293 ASSERT (!is_open (fd + 1));
294 ASSERT (is_open (fd + 2));
295 ASSERT (is_inheritable (fd));
296 ASSERT (!is_inheritable (fd + 2));
297 ASSERT (is_mode (fd, O_BINARY));
298 ASSERT (is_mode (fd + 2, O_BINARY));
299 ASSERT (close (fd) == 0);
301 set_binary_mode (fd + 2, O_TEXT);
302 ASSERT (fcntl (fd + 2, F_DUPFD, fd + 1) == fd + 1);
303 ASSERT (!is_open (fd));
304 ASSERT (is_open (fd + 1));
305 ASSERT (is_open (fd + 2));
306 ASSERT (is_inheritable (fd + 1));
307 ASSERT (!is_inheritable (fd + 2));
308 ASSERT (is_mode (fd + 1, O_TEXT));
309 ASSERT (is_mode (fd + 2, O_TEXT));
310 ASSERT (close (fd + 1) == 0);
312 ASSERT (fcntl (fd + 2, F_DUPFD_CLOEXEC, 0) == fd);
313 ASSERT (is_open (fd));
314 ASSERT (!is_open (fd + 1));
315 ASSERT (is_open (fd + 2));
316 ASSERT (!is_inheritable (fd));
317 ASSERT (!is_inheritable (fd + 2));
318 ASSERT (is_mode (fd, O_TEXT));
319 ASSERT (is_mode (fd + 2, O_TEXT));
320 ASSERT (close (fd + 2) == 0);
322 /* Test F_GETFD on invalid file descriptors. */
323 errno = 0;
324 ASSERT (fcntl (-1, F_GETFD) == -1);
325 ASSERT (errno == EBADF);
326 errno = 0;
327 ASSERT (fcntl (fd + 1, F_GETFD) == -1);
328 ASSERT (errno == EBADF);
329 errno = 0;
330 ASSERT (fcntl (bad_fd, F_GETFD) == -1);
331 ASSERT (errno == EBADF);
333 /* Test F_GETFD, the FD_CLOEXEC bit. */
335 int result = fcntl (fd, F_GETFD);
336 ASSERT (0 <= result);
337 ASSERT ((result & FD_CLOEXEC) == FD_CLOEXEC);
338 ASSERT (dup (fd) == fd + 1);
339 result = fcntl (fd + 1, F_GETFD);
340 ASSERT (0 <= result);
341 ASSERT ((result & FD_CLOEXEC) == 0);
342 ASSERT (close (fd + 1) == 0);
345 #ifdef F_SETFD
346 /* Test F_SETFD on invalid file descriptors. */
347 errno = 0;
348 ASSERT (fcntl (-1, F_SETFD, 0) == -1);
349 ASSERT (errno == EBADF);
350 errno = 0;
351 ASSERT (fcntl (fd + 1, F_SETFD, 0) == -1);
352 ASSERT (errno == EBADF);
353 errno = 0;
354 ASSERT (fcntl (bad_fd, F_SETFD, 0) == -1);
355 ASSERT (errno == EBADF);
356 #endif
358 #ifdef F_GETFL
359 /* Test F_GETFL on invalid file descriptors. */
360 errno = 0;
361 ASSERT (fcntl (-1, F_GETFL) == -1);
362 ASSERT (errno == EBADF);
363 errno = 0;
364 ASSERT (fcntl (fd + 1, F_GETFL) == -1);
365 ASSERT (errno == EBADF);
366 errno = 0;
367 ASSERT (fcntl (bad_fd, F_GETFL) == -1);
368 ASSERT (errno == EBADF);
369 #endif
371 #ifdef F_SETFL
372 /* Test F_SETFL on invalid file descriptors. */
373 errno = 0;
374 ASSERT (fcntl (-1, F_SETFL, 0) == -1);
375 ASSERT (errno == EBADF);
376 errno = 0;
377 ASSERT (fcntl (fd + 1, F_SETFL, 0) == -1);
378 ASSERT (errno == EBADF);
379 errno = 0;
380 ASSERT (fcntl (bad_fd, F_SETFL, 0) == -1);
381 ASSERT (errno == EBADF);
382 #endif
384 #ifdef F_GETOWN
385 /* Test F_GETOWN on invalid file descriptors. */
386 errno = 0;
387 ASSERT (fcntl (-1, F_GETOWN) == -1);
388 ASSERT (errno == EBADF);
389 errno = 0;
390 ASSERT (fcntl (fd + 1, F_GETOWN) == -1);
391 ASSERT (errno == EBADF);
392 errno = 0;
393 ASSERT (fcntl (bad_fd, F_GETOWN) == -1);
394 ASSERT (errno == EBADF);
395 #endif
397 #ifdef F_SETOWN
398 /* Test F_SETFL on invalid file descriptors. */
399 errno = 0;
400 ASSERT (fcntl (-1, F_SETOWN, 0) == -1);
401 ASSERT (errno == EBADF);
402 errno = 0;
403 ASSERT (fcntl (fd + 1, F_SETOWN, 0) == -1);
404 ASSERT (errno == EBADF);
405 errno = 0;
406 ASSERT (fcntl (bad_fd, F_SETOWN, 0) == -1);
407 ASSERT (errno == EBADF);
408 #endif
410 /* Cleanup. */
411 ASSERT (close (fd) == 0);
412 ASSERT (unlink (file) == 0);
414 return 0;