1 /* Test duplicating file descriptors.
2 Copyright (C) 2009-2025 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. */
23 #include "signature.h"
24 SIGNATURE_CHECK (dup2
, int, (int, int));
29 #if HAVE_SYS_RESOURCE_H
30 # include <sys/resource.h>
33 #include "binary-io.h"
35 #if GNULIB_TEST_CLOEXEC
39 #if defined _WIN32 && ! defined __CYGWIN__
40 /* Get declarations of the native Windows API functions. */
41 # define WIN32_LEAN_AND_MEAN
43 /* Get _get_osfhandle. */
44 # if GNULIB_MSVC_NOTHROW
45 # include "msvc-nothrow.h"
53 /* Tell GCC not to warn about the specific edge cases tested here. */
54 #if _GL_GNUC_PREREQ (13, 0)
55 # pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
56 # pragma GCC diagnostic ignored "-Wanalyzer-fd-use-without-check"
59 /* Return non-zero if FD is open. */
63 #if defined _WIN32 && ! defined __CYGWIN__
64 /* On native Windows, the initial state of unassigned standard file
65 descriptors is that they are open but point to an
66 INVALID_HANDLE_VALUE, and there is no fcntl. */
67 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
70 # error Please port fcntl to your platform
72 return 0 <= fcntl (fd
, F_GETFL
);
76 #if GNULIB_TEST_CLOEXEC
77 /* Return non-zero if FD is open and inheritable across exec/spawn. */
79 is_inheritable (int fd
)
81 # if defined _WIN32 && ! defined __CYGWIN__
82 /* On native Windows, the initial state of unassigned standard file
83 descriptors is that they are open but point to an
84 INVALID_HANDLE_VALUE, and there is no fcntl. */
85 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
87 if (h
== INVALID_HANDLE_VALUE
|| GetHandleInformation (h
, &flags
) == 0)
89 return (flags
& HANDLE_FLAG_INHERIT
) != 0;
92 # error Please port fcntl to your platform
94 int i
= fcntl (fd
, F_GETFD
);
95 return 0 <= i
&& (i
& FD_CLOEXEC
) == 0;
98 #endif /* GNULIB_TEST_CLOEXEC */
101 # define set_binary_mode my_set_binary_mode
103 set_binary_mode (_GL_UNUSED
int fd
, _GL_UNUSED
int mode
)
109 /* Return non-zero if FD is open in the given MODE, which is either
110 O_TEXT or O_BINARY. */
112 is_mode (int fd
, int mode
)
114 int value
= set_binary_mode (fd
, O_BINARY
);
115 set_binary_mode (fd
, value
);
116 return mode
== value
;
122 const char *file
= "test-dup2.tmp";
124 int bad_fd
= getdtablesize ();
125 int fd
= open (file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
127 /* Assume std descriptors were provided by invoker. */
128 ASSERT (STDERR_FILENO
< fd
);
129 ASSERT (is_open (fd
));
130 /* Ignore any other fd's leaked into this process. */
133 ASSERT (!is_open (fd
+ 1));
134 ASSERT (!is_open (fd
+ 2));
136 /* Assigning to self must be a no-op. */
137 ASSERT (dup2 (fd
, fd
) == fd
);
138 ASSERT (is_open (fd
));
140 /* The source must be valid. */
142 ASSERT (dup2 (-1, fd
) == -1);
143 ASSERT (errno
== EBADF
);
146 ASSERT (dup2 (99, fd
) == -1);
147 ASSERT (errno
== EBADF
);
149 ASSERT (dup2 (AT_FDCWD
, fd
) == -1);
150 ASSERT (errno
== EBADF
);
151 ASSERT (is_open (fd
));
153 /* If the source is not open, then the destination is unaffected. */
155 ASSERT (dup2 (fd
+ 1, fd
+ 1) == -1);
156 ASSERT (errno
== EBADF
);
157 ASSERT (!is_open (fd
+ 1));
159 ASSERT (dup2 (fd
+ 1, fd
) == -1);
160 ASSERT (errno
== EBADF
);
161 ASSERT (is_open (fd
));
163 /* The destination must be valid. */
165 ASSERT (dup2 (fd
, -2) == -1);
166 ASSERT (errno
== EBADF
);
169 ASSERT (dup2 (fd
, 255) == 255);
170 ASSERT (dup2 (fd
, 256) == 256);
171 ASSERT (close (255) == 0);
172 ASSERT (close (256) == 0);
174 ASSERT (dup2 (fd
, bad_fd
- 1) == bad_fd
- 1);
175 ASSERT (close (bad_fd
- 1) == 0);
177 ASSERT (dup2 (fd
, bad_fd
) == -1);
178 ASSERT (errno
== EBADF
);
180 /* Using dup2 can skip fds. */
181 ASSERT (dup2 (fd
, fd
+ 2) == fd
+ 2);
182 ASSERT (is_open (fd
));
183 ASSERT (!is_open (fd
+ 1));
184 ASSERT (is_open (fd
+ 2));
186 /* Verify that dup2 closes the previous occupant of a fd. */
187 ASSERT (open ("/dev/null", O_WRONLY
, 0600) == fd
+ 1);
188 ASSERT (dup2 (fd
+ 1, fd
) == fd
);
189 ASSERT (close (fd
+ 1) == 0);
190 ASSERT (write (fd
, "1", 1) == 1);
191 ASSERT (dup2 (fd
+ 2, fd
) == fd
);
192 ASSERT (lseek (fd
, 0, SEEK_END
) == 0);
193 ASSERT (write (fd
+ 2, "2", 1) == 1);
194 ASSERT (lseek (fd
, 0, SEEK_SET
) == 0);
195 ASSERT (read (fd
, buffer
, 1) == 1);
196 ASSERT (*buffer
== '2');
198 #if GNULIB_TEST_CLOEXEC
199 /* Any new fd created by dup2 must not be cloexec. */
200 ASSERT (close (fd
+ 2) == 0);
201 ASSERT (dup_cloexec (fd
) == fd
+ 1);
202 ASSERT (!is_inheritable (fd
+ 1));
203 ASSERT (dup2 (fd
+ 1, fd
+ 1) == fd
+ 1);
204 ASSERT (!is_inheritable (fd
+ 1));
205 ASSERT (dup2 (fd
+ 1, fd
+ 2) == fd
+ 2);
206 ASSERT (!is_inheritable (fd
+ 1));
207 ASSERT (is_inheritable (fd
+ 2));
209 ASSERT (dup2 (fd
+ 1, -1) == -1);
210 ASSERT (errno
== EBADF
);
211 ASSERT (!is_inheritable (fd
+ 1));
214 /* On systems that distinguish between text and binary mode, dup2
215 reuses the mode of the source. */
216 set_binary_mode (fd
, O_BINARY
);
217 ASSERT (is_mode (fd
, O_BINARY
));
218 ASSERT (dup2 (fd
, fd
+ 1) == fd
+ 1);
219 ASSERT (is_mode (fd
+ 1, O_BINARY
));
220 set_binary_mode (fd
, O_TEXT
);
221 ASSERT (is_mode (fd
, O_TEXT
));
222 ASSERT (dup2 (fd
, fd
+ 1) == fd
+ 1);
223 ASSERT (is_mode (fd
+ 1, O_TEXT
));
226 ASSERT (close (fd
+ 2) == 0);
227 ASSERT (close (fd
+ 1) == 0);
228 ASSERT (close (fd
) == 0);
229 ASSERT (unlink (file
) == 0);
231 return test_exit_status
;