2 Copyright (C) 2009 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 /* Get declarations of the Win32 API functions. */
34 # define WIN32_LEAN_AND_MEAN
38 #include "binary-io.h"
40 /* Use O_CLOEXEC if available, but test works without it. */
46 # define setmode(f,m) zero ()
47 static int zero (void) { return 0; }
50 #define ASSERT(expr) \
55 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
62 /* Return true if FD is open. */
66 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
67 /* On Win32, the initial state of unassigned standard file
68 descriptors is that they are open but point to an
69 INVALID_HANDLE_VALUE, and there is no fcntl. */
70 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
73 # error Please port fcntl to your platform
75 return 0 <= fcntl (fd, F_GETFL);
79 /* Return true if FD is open and inheritable across exec/spawn. */
81 is_inheritable (int fd)
83 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
84 /* On Win32, the initial state of unassigned standard file
85 descriptors is that they are open but point to an
86 INVALID_HANDLE_VALUE, and there is no fcntl. */
87 HANDLE h = (HANDLE) _get_osfhandle (fd);
89 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
91 return (flags & HANDLE_FLAG_INHERIT) != 0;
94 # error Please port fcntl to your platform
96 int i = fcntl (fd, F_GETFD);
97 return 0 <= i && (i & FD_CLOEXEC) == 0;
101 /* Return non-zero if FD is open in the given MODE, which is either
102 O_TEXT or O_BINARY. */
104 is_mode (int fd, int mode)
106 int value = setmode (fd, O_BINARY);
108 return mode == value;
111 /* Since native fcntl can have more supported operations than our
112 replacement is aware of, and since various operations assign
113 different types to the vararg argument, a wrapper around fcntl must
114 be able to pass a vararg of unknown type on through to the original
115 fcntl. Make sure that this works properly: func1 behaves like the
116 original fcntl interpreting the vararg as an int or a pointer to a
117 struct, and func2 behaves like rpl_fcntl that doesn't know what
131 i = va_arg (arg, int);
134 struct dummy_struct *s = va_arg (arg, struct dummy_struct *);
146 p = va_arg (arg, void *);
151 /* Ensure that all supported fcntl actions are distinct, and
152 usable in preprocessor expressions. */
164 #ifdef F_DUPFD_CLOEXEC
165 case F_DUPFD_CLOEXEC:
229 main (int argc, char **argv)
231 const char *file = "test-fcntl.tmp";
234 /* Sanity check that rpl_fcntl is likely to work. */
235 ASSERT (func2 (1, 2) == 2);
236 ASSERT (func2 (2, -2) == -2);
237 ASSERT (func2 (3, 0x80000000) == 0x80000000);
239 struct dummy_struct s = { 0L, 4 };
240 ASSERT (func2 (4, &s) == 4);
246 /* Assume std descriptors were provided by invoker, and ignore fds
247 that might have been inherited. */
248 fd = creat (file, 0600);
249 ASSERT (STDERR_FILENO < fd);
253 /* For F_DUPFD*, the source must be valid. */
255 ASSERT (fcntl (-1, F_DUPFD, 0) == -1);
256 ASSERT (errno == EBADF);
258 ASSERT (fcntl (fd + 1, F_DUPFD, 0) == -1);
259 ASSERT (errno == EBADF);
261 ASSERT (fcntl (10000000, F_DUPFD, 0) == -1);
262 ASSERT (errno == EBADF);
264 ASSERT (fcntl (-1, F_DUPFD_CLOEXEC, 0) == -1);
265 ASSERT (errno == EBADF);
267 ASSERT (fcntl (fd + 1, F_DUPFD_CLOEXEC, 0) == -1);
268 ASSERT (errno == EBADF);
270 ASSERT (fcntl (10000000, F_DUPFD_CLOEXEC, 0) == -1);
271 ASSERT (errno == EBADF);
273 /* For F_DUPFD*, the destination must be valid. */
274 ASSERT (getdtablesize () < 10000000);
276 ASSERT (fcntl (fd, F_DUPFD, -1) == -1);
277 ASSERT (errno == EINVAL);
279 ASSERT (fcntl (fd, F_DUPFD, 10000000) == -1);
280 ASSERT (errno == EINVAL);
281 ASSERT (getdtablesize () < 10000000);
283 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, -1) == -1);
284 ASSERT (errno == EINVAL);
286 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, 10000000) == -1);
287 ASSERT (errno == EINVAL);
289 /* For F_DUPFD*, check for correct inheritance, as well as
290 preservation of text vs. binary. */
291 setmode (fd, O_BINARY);
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_mode (fd, O_BINARY));
298 ASSERT (fcntl (fd, F_DUPFD, fd) == fd + 1);
299 ASSERT (is_open (fd));
300 ASSERT (is_open (fd + 1));
301 ASSERT (!is_open (fd + 2));
302 ASSERT (is_inheritable (fd + 1));
303 ASSERT (is_mode (fd, O_BINARY));
304 ASSERT (is_mode (fd + 1, O_BINARY));
305 ASSERT (close (fd + 1) == 0);
307 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, fd + 2) == fd + 2);
308 ASSERT (is_open (fd));
309 ASSERT (!is_open (fd + 1));
310 ASSERT (is_open (fd + 2));
311 ASSERT (is_inheritable (fd));
312 ASSERT (!is_inheritable (fd + 2));
313 ASSERT (is_mode (fd, O_BINARY));
314 ASSERT (is_mode (fd + 2, O_BINARY));
315 ASSERT (close (fd) == 0);
317 setmode (fd + 2, O_TEXT);
318 ASSERT (fcntl (fd + 2, F_DUPFD, fd + 1) == fd + 1);
319 ASSERT (!is_open (fd));
320 ASSERT (is_open (fd + 1));
321 ASSERT (is_open (fd + 2));
322 ASSERT (is_inheritable (fd + 1));
323 ASSERT (!is_inheritable (fd + 2));
324 ASSERT (is_mode (fd + 1, O_TEXT));
325 ASSERT (is_mode (fd + 2, O_TEXT));
326 ASSERT (close (fd + 1) == 0);
328 ASSERT (fcntl (fd + 2, F_DUPFD_CLOEXEC, 0) == fd);
329 ASSERT (is_open (fd));
330 ASSERT (!is_open (fd + 1));
331 ASSERT (is_open (fd + 2));
332 ASSERT (!is_inheritable (fd));
333 ASSERT (!is_inheritable (fd + 2));
334 ASSERT (is_mode (fd, O_TEXT));
335 ASSERT (is_mode (fd + 2, O_TEXT));
336 ASSERT (close (fd + 2) == 0);
339 ASSERT (close (fd) == 0);
340 ASSERT (unlink (file) == 0);
342 #endif /* HAVE_FCNTL */