2 Copyright (C) 2009-2017 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. */
24 #include "signature.h"
25 SIGNATURE_CHECK (fcntl
, int, (int, int, ...));
33 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
34 /* Get declarations of the native Windows API functions. */
35 # define WIN32_LEAN_AND_MEAN
37 /* Get _get_osfhandle. */
38 # if GNULIB_MSVC_NOTHROW
39 # include "msvc-nothrow.h"
45 #include "binary-io.h"
49 # define setmode(f,m) zero ()
50 static int zero (void) { return 0; }
53 /* Return true if FD is open. */
57 #if (defined _WIN32 || 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
;
64 # error Please port fcntl to your platform
66 return 0 <= fcntl (fd
, F_GETFL
);
70 /* Return true if FD is open and inheritable across exec/spawn. */
72 is_inheritable (int fd
)
74 #if (defined _WIN32 || 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
);
80 if (h
== INVALID_HANDLE_VALUE
|| GetHandleInformation (h
, &flags
) == 0)
82 return (flags
& HANDLE_FLAG_INHERIT
) != 0;
85 # error Please port fcntl to your platform
87 int i
= fcntl (fd
, F_GETFD
);
88 return 0 <= i
&& (i
& FD_CLOEXEC
) == 0;
92 /* Return non-zero if FD is open in the given MODE, which is either
93 O_TEXT or O_BINARY. */
95 is_mode (int fd
, int mode
)
97 int value
= setmode (fd
, O_BINARY
);
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
122 i
= va_arg (arg
, int);
125 struct dummy_struct
*s
= va_arg (arg
, struct dummy_struct
*);
137 p
= va_arg (arg
, void *);
142 /* Ensure that all supported fcntl actions are distinct, and
143 usable in preprocessor expressions. */
153 case F_DUPFD_CLOEXEC
:
216 const char *file
= "test-fcntl.tmp";
218 int bad_fd
= getdtablesize ();
220 /* Sanity check that rpl_fcntl is likely to work. */
221 ASSERT (func2 (1, 2) == 2);
222 ASSERT (func2 (2, -2) == -2);
223 ASSERT (func2 (3, 0x80000000) == 0x80000000);
225 struct dummy_struct s
= { 0L, 4 };
226 ASSERT (func2 (4, &s
) == 4);
230 /* Assume std descriptors were provided by invoker, and ignore fds
231 that might have been inherited. */
232 fd
= creat (file
, 0600);
233 ASSERT (STDERR_FILENO
< fd
);
237 /* For F_DUPFD*, the source must be valid. */
239 ASSERT (fcntl (-1, F_DUPFD
, 0) == -1);
240 ASSERT (errno
== EBADF
);
242 ASSERT (fcntl (fd
+ 1, F_DUPFD
, 0) == -1);
243 ASSERT (errno
== EBADF
);
245 ASSERT (fcntl (bad_fd
, F_DUPFD
, 0) == -1);
246 ASSERT (errno
== EBADF
);
248 ASSERT (fcntl (-1, F_DUPFD_CLOEXEC
, 0) == -1);
249 ASSERT (errno
== EBADF
);
251 ASSERT (fcntl (fd
+ 1, F_DUPFD_CLOEXEC
, 0) == -1);
252 ASSERT (errno
== EBADF
);
254 ASSERT (fcntl (bad_fd
, F_DUPFD_CLOEXEC
, 0) == -1);
255 ASSERT (errno
== EBADF
);
257 /* For F_DUPFD*, the destination must be valid. */
259 ASSERT (fcntl (fd
, F_DUPFD
, -1) == -1);
260 ASSERT (errno
== EINVAL
);
262 ASSERT (fcntl (fd
, F_DUPFD
, bad_fd
) == -1);
263 ASSERT (errno
== EINVAL
);
265 ASSERT (fcntl (fd
, F_DUPFD_CLOEXEC
, -1) == -1);
266 ASSERT (errno
== EINVAL
);
268 ASSERT (fcntl (fd
, F_DUPFD_CLOEXEC
, bad_fd
) == -1);
269 ASSERT (errno
== EINVAL
);
271 /* For F_DUPFD*, check for correct inheritance, as well as
272 preservation of text vs. binary. */
273 setmode (fd
, O_BINARY
);
274 ASSERT (is_open (fd
));
275 ASSERT (!is_open (fd
+ 1));
276 ASSERT (!is_open (fd
+ 2));
277 ASSERT (is_inheritable (fd
));
278 ASSERT (is_mode (fd
, O_BINARY
));
280 ASSERT (fcntl (fd
, F_DUPFD
, fd
) == fd
+ 1);
281 ASSERT (is_open (fd
));
282 ASSERT (is_open (fd
+ 1));
283 ASSERT (!is_open (fd
+ 2));
284 ASSERT (is_inheritable (fd
+ 1));
285 ASSERT (is_mode (fd
, O_BINARY
));
286 ASSERT (is_mode (fd
+ 1, O_BINARY
));
287 ASSERT (close (fd
+ 1) == 0);
289 ASSERT (fcntl (fd
, F_DUPFD_CLOEXEC
, fd
+ 2) == fd
+ 2);
290 ASSERT (is_open (fd
));
291 ASSERT (!is_open (fd
+ 1));
292 ASSERT (is_open (fd
+ 2));
293 ASSERT (is_inheritable (fd
));
294 ASSERT (!is_inheritable (fd
+ 2));
295 ASSERT (is_mode (fd
, O_BINARY
));
296 ASSERT (is_mode (fd
+ 2, O_BINARY
));
297 ASSERT (close (fd
) == 0);
299 setmode (fd
+ 2, O_TEXT
);
300 ASSERT (fcntl (fd
+ 2, F_DUPFD
, fd
+ 1) == fd
+ 1);
301 ASSERT (!is_open (fd
));
302 ASSERT (is_open (fd
+ 1));
303 ASSERT (is_open (fd
+ 2));
304 ASSERT (is_inheritable (fd
+ 1));
305 ASSERT (!is_inheritable (fd
+ 2));
306 ASSERT (is_mode (fd
+ 1, O_TEXT
));
307 ASSERT (is_mode (fd
+ 2, O_TEXT
));
308 ASSERT (close (fd
+ 1) == 0);
310 ASSERT (fcntl (fd
+ 2, F_DUPFD_CLOEXEC
, 0) == fd
);
311 ASSERT (is_open (fd
));
312 ASSERT (!is_open (fd
+ 1));
313 ASSERT (is_open (fd
+ 2));
314 ASSERT (!is_inheritable (fd
));
315 ASSERT (!is_inheritable (fd
+ 2));
316 ASSERT (is_mode (fd
, O_TEXT
));
317 ASSERT (is_mode (fd
+ 2, O_TEXT
));
318 ASSERT (close (fd
+ 2) == 0);
320 /* Test F_GETFD on invalid file descriptors. */
322 ASSERT (fcntl (-1, F_GETFD
) == -1);
323 ASSERT (errno
== EBADF
);
325 ASSERT (fcntl (fd
+ 1, F_GETFD
) == -1);
326 ASSERT (errno
== EBADF
);
328 ASSERT (fcntl (bad_fd
, F_GETFD
) == -1);
329 ASSERT (errno
== EBADF
);
331 /* Test F_GETFD, the FD_CLOEXEC bit. */
333 int result
= fcntl (fd
, F_GETFD
);
334 ASSERT (0 <= result
);
335 ASSERT ((result
& FD_CLOEXEC
) == FD_CLOEXEC
);
336 ASSERT (dup (fd
) == fd
+ 1);
337 result
= fcntl (fd
+ 1, F_GETFD
);
338 ASSERT (0 <= result
);
339 ASSERT ((result
& FD_CLOEXEC
) == 0);
340 ASSERT (close (fd
+ 1) == 0);
344 /* Test F_SETFD on invalid file descriptors. */
346 ASSERT (fcntl (-1, F_SETFD
, 0) == -1);
347 ASSERT (errno
== EBADF
);
349 ASSERT (fcntl (fd
+ 1, F_SETFD
, 0) == -1);
350 ASSERT (errno
== EBADF
);
352 ASSERT (fcntl (bad_fd
, F_SETFD
, 0) == -1);
353 ASSERT (errno
== EBADF
);
357 /* Test F_GETFL on invalid file descriptors. */
359 ASSERT (fcntl (-1, F_GETFL
) == -1);
360 ASSERT (errno
== EBADF
);
362 ASSERT (fcntl (fd
+ 1, F_GETFL
) == -1);
363 ASSERT (errno
== EBADF
);
365 ASSERT (fcntl (bad_fd
, F_GETFL
) == -1);
366 ASSERT (errno
== EBADF
);
370 /* Test F_SETFL on invalid file descriptors. */
372 ASSERT (fcntl (-1, F_SETFL
, 0) == -1);
373 ASSERT (errno
== EBADF
);
375 ASSERT (fcntl (fd
+ 1, F_SETFL
, 0) == -1);
376 ASSERT (errno
== EBADF
);
378 ASSERT (fcntl (bad_fd
, F_SETFL
, 0) == -1);
379 ASSERT (errno
== EBADF
);
383 /* Test F_GETOWN on invalid file descriptors. */
385 ASSERT (fcntl (-1, F_GETOWN
) == -1);
386 ASSERT (errno
== EBADF
);
388 ASSERT (fcntl (fd
+ 1, F_GETOWN
) == -1);
389 ASSERT (errno
== EBADF
);
391 ASSERT (fcntl (bad_fd
, F_GETOWN
) == -1);
392 ASSERT (errno
== EBADF
);
396 /* Test F_SETFL on invalid file descriptors. */
398 ASSERT (fcntl (-1, F_SETOWN
, 0) == -1);
399 ASSERT (errno
== EBADF
);
401 ASSERT (fcntl (fd
+ 1, F_SETOWN
, 0) == -1);
402 ASSERT (errno
== EBADF
);
404 ASSERT (fcntl (bad_fd
, F_SETOWN
, 0) == -1);
405 ASSERT (errno
== EBADF
);
409 ASSERT (close (fd
) == 0);
410 ASSERT (unlink (file
) == 0);