1 /* Test duplicating non-inheritable file descriptors.
2 Copyright (C) 2009, 2010 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. */
27 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
28 /* Get declarations of the Win32 API functions. */
29 # define WIN32_LEAN_AND_MEAN
33 #include "binary-io.h"
36 /* Return non-zero if FD is open and inheritable across exec/spawn. */
38 is_inheritable (int fd)
40 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
41 /* On Win32, the initial state of unassigned standard file
42 descriptors is that they are open but point to an
43 INVALID_HANDLE_VALUE, and there is no fcntl. */
44 HANDLE h = (HANDLE) _get_osfhandle (fd);
46 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
48 return (flags & HANDLE_FLAG_INHERIT) != 0;
51 # error Please port fcntl to your platform
53 int i = fcntl (fd, F_GETFD);
54 return 0 <= i && (i & FD_CLOEXEC) == 0;
59 # define setmode(f,m) zero ()
60 static int zero (void) { return 0; }
63 /* Return non-zero if FD is open in the given MODE, which is either
64 O_TEXT or O_BINARY. */
66 is_mode (int fd, int mode)
68 int value = setmode (fd, O_BINARY);
76 const char *file = "test-cloexec.tmp";
77 int fd = creat (file, 0600);
80 /* Assume std descriptors were provided by invoker. */
81 ASSERT (STDERR_FILENO < fd);
82 ASSERT (is_inheritable (fd));
84 /* Normal use of set_cloexec_flag. */
85 ASSERT (set_cloexec_flag (fd, true) == 0);
86 #if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
87 ASSERT (!is_inheritable (fd));
89 ASSERT (set_cloexec_flag (fd, false) == 0);
90 ASSERT (is_inheritable (fd));
92 /* Normal use of dup_cloexec. */
93 fd2 = dup_cloexec (fd);
95 ASSERT (!is_inheritable (fd2));
96 ASSERT (close (fd) == 0);
97 ASSERT (dup_cloexec (fd2) == fd);
98 ASSERT (!is_inheritable (fd));
99 ASSERT (close (fd2) == 0);
101 /* On systems that distinguish between text and binary mode,
102 dup_cloexec reuses the mode of the source. */
103 setmode (fd, O_BINARY);
104 ASSERT (is_mode (fd, O_BINARY));
105 fd2 = dup_cloexec (fd);
107 ASSERT (is_mode (fd2, O_BINARY));
108 ASSERT (close (fd2) == 0);
109 setmode (fd, O_TEXT);
110 ASSERT (is_mode (fd, O_TEXT));
111 fd2 = dup_cloexec (fd);
113 ASSERT (is_mode (fd2, O_TEXT));
114 ASSERT (close (fd2) == 0);
116 /* Test error handling. */
118 ASSERT (set_cloexec_flag (-1, false) == -1);
119 ASSERT (errno == EBADF);
121 ASSERT (set_cloexec_flag (10000000, false) == -1);
122 ASSERT (errno == EBADF);
124 ASSERT (set_cloexec_flag (fd2, false) == -1);
125 ASSERT (errno == EBADF);
127 ASSERT (dup_cloexec (-1) == -1);
128 ASSERT (errno == EBADF);
130 ASSERT (dup_cloexec (10000000) == -1);
131 ASSERT (errno == EBADF);
133 ASSERT (dup_cloexec (fd2) == -1);
134 ASSERT (errno == EBADF);
137 ASSERT (close (fd) == 0);
138 ASSERT (unlink (file) == 0);