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. */
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
31 /* Get _get_osfhandle. */
32 # if GNULIB_MSVC_NOTHROW
33 # include "msvc-nothrow.h"
39 #include "binary-io.h"
42 /* Return non-zero if FD is open and inheritable across exec/spawn. */
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
);
52 if (h
== INVALID_HANDLE_VALUE
|| GetHandleInformation (h
, &flags
) == 0)
54 return (flags
& HANDLE_FLAG_INHERIT
) != 0;
57 # error Please port fcntl to your platform
59 int i
= fcntl (fd
, F_GETFD
);
60 return 0 <= i
&& (i
& FD_CLOEXEC
) == 0;
65 # define set_binary_mode my_set_binary_mode
67 set_binary_mode (_GL_UNUSED
int fd
, _GL_UNUSED
int mode
)
73 /* Return non-zero if FD is open in the given MODE, which is either
74 O_TEXT or O_BINARY. */
76 is_mode (int fd
, int mode
)
78 int value
= set_binary_mode (fd
, O_BINARY
);
79 set_binary_mode (fd
, value
);
86 const char *file
= "test-cloexec.tmp";
87 int fd
= creat (file
, 0600);
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
));
100 ASSERT (set_cloexec_flag (fd
, false) == 0);
101 ASSERT (is_inheritable (fd
));
103 /* Normal use of dup_cloexec. */
104 fd2
= dup_cloexec (fd
);
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
);
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
);
124 ASSERT (is_mode (fd2
, O_TEXT
));
125 ASSERT (close (fd2
) == 0);
127 /* Test error handling. */
129 ASSERT (set_cloexec_flag (-1, false) == -1);
130 ASSERT (errno
== EBADF
);
132 ASSERT (set_cloexec_flag (bad_fd
, false) == -1);
133 ASSERT (errno
== EBADF
);
135 ASSERT (set_cloexec_flag (fd2
, false) == -1);
136 ASSERT (errno
== EBADF
);
138 ASSERT (dup_cloexec (-1) == -1);
139 ASSERT (errno
== EBADF
);
141 ASSERT (dup_cloexec (bad_fd
) == -1);
142 ASSERT (errno
== EBADF
);
144 ASSERT (dup_cloexec (fd2
) == -1);
145 ASSERT (errno
== EBADF
);
148 ASSERT (close (fd
) == 0);
149 ASSERT (unlink (file
) == 0);
151 return test_exit_status
;