1 /* Test duplicating file descriptors.
2 Copyright (C) 2009-2023 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (dup2, int, (int, int));
25
26 #include <errno.h>
27 #include <fcntl.h>
28
29 #if HAVE_SYS_RESOURCE_H
30 # include <sys/resource.h>
31 #endif
32
33 #include "binary-io.h"
34
35 #if GNULIB_TEST_CLOEXEC
36 # include "cloexec.h"
37 #endif
38
39 #if defined _WIN32 && ! defined __CYGWIN__
40 /* Get declarations of the native Windows API functions. */
41 # define WIN32_LEAN_AND_MEAN
42 # include <windows.h>
43 /* Get _get_osfhandle. */
44 # if GNULIB_MSVC_NOTHROW
45 # include "msvc-nothrow.h"
46 # else
47 # include <io.h>
48 # endif
49 #endif
50
51 #include "macros.h"
52
53 /* Tell GCC not to warn about the specific edge cases tested here. */
54 #if __GNUC__ >= 13
55 # pragma GCC diagnostic ignored "-Wanalyzer-fd-leak"
56 # pragma GCC diagnostic ignored "-Wanalyzer-fd-use-without-check"
57 #endif
58
59 /* Return non-zero if FD is open. */
60 static int
61 is_open (int fd)
62 {
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;
68 #else
69 # ifndef F_GETFL
70 # error Please port fcntl to your platform
71 # endif
72 return 0 <= fcntl (fd, F_GETFL);
73 #endif
74 }
75
76 #if GNULIB_TEST_CLOEXEC
77 /* Return non-zero if FD is open and inheritable across exec/spawn. */
78 static int
79 is_inheritable (int fd)
80 {
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);
86 DWORD flags;
87 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
88 return 0;
89 return (flags & HANDLE_FLAG_INHERIT) != 0;
90 # else
91 # ifndef F_GETFD
92 # error Please port fcntl to your platform
93 # endif
94 int i = fcntl (fd, F_GETFD);
95 return 0 <= i && (i & FD_CLOEXEC) == 0;
96 # endif
97 }
98 #endif /* GNULIB_TEST_CLOEXEC */
99
100 #if !O_BINARY
101 # define set_binary_mode(f,m) zero ()
102 static int zero (void) { return 0; }
103 #endif
104
105 /* Return non-zero if FD is open in the given MODE, which is either
106 O_TEXT or O_BINARY. */
107 static int
108 is_mode (int fd, int mode)
109 {
110 int value = set_binary_mode (fd, O_BINARY);
111 set_binary_mode (fd, value);
112 return mode == value;
113 }
114
115 int
116 main (void)
117 {
118 const char *file = "test-dup2.tmp";
119 char buffer[1];
120 int bad_fd = getdtablesize ();
121 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
122
123 /* Assume std descriptors were provided by invoker. */
124 ASSERT (STDERR_FILENO < fd);
125 ASSERT (is_open (fd));
126 /* Ignore any other fd's leaked into this process. */
127 close (fd + 1);
128 close (fd + 2);
129 ASSERT (!is_open (fd + 1));
130 ASSERT (!is_open (fd + 2));
131
132 /* Assigning to self must be a no-op. */
133 ASSERT (dup2 (fd, fd) == fd);
134 ASSERT (is_open (fd));
135
136 /* The source must be valid. */
137 errno = 0;
138 ASSERT (dup2 (-1, fd) == -1);
139 ASSERT (errno == EBADF);
140 close (99);
141 errno = 0;
142 ASSERT (dup2 (99, fd) == -1);
143 ASSERT (errno == EBADF);
144 errno = 0;
145 ASSERT (dup2 (AT_FDCWD, fd) == -1);
146 ASSERT (errno == EBADF);
147 ASSERT (is_open (fd));
148
149 /* If the source is not open, then the destination is unaffected. */
150 errno = 0;
151 ASSERT (dup2 (fd + 1, fd + 1) == -1);
152 ASSERT (errno == EBADF);
153 ASSERT (!is_open (fd + 1));
154 errno = 0;
155 ASSERT (dup2 (fd + 1, fd) == -1);
156 ASSERT (errno == EBADF);
157 ASSERT (is_open (fd));
158
159 /* The destination must be valid. */
160 errno = 0;
161 ASSERT (dup2 (fd, -2) == -1);
162 ASSERT (errno == EBADF);
163 if (bad_fd > 256)
164 {
165 ASSERT (dup2 (fd, 255) == 255);
166 ASSERT (dup2 (fd, 256) == 256);
167 ASSERT (close (255) == 0);
168 ASSERT (close (256) == 0);
169 }
170 ASSERT (dup2 (fd, bad_fd - 1) == bad_fd - 1);
171 ASSERT (close (bad_fd - 1) == 0);
172 errno = 0;
173 ASSERT (dup2 (fd, bad_fd) == -1);
174 ASSERT (errno == EBADF);
175
176 /* Using dup2 can skip fds. */
177 ASSERT (dup2 (fd, fd + 2) == fd + 2);
178 ASSERT (is_open (fd));
179 ASSERT (!is_open (fd + 1));
180 ASSERT (is_open (fd + 2));
181
182 /* Verify that dup2 closes the previous occupant of a fd. */
183 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
184 ASSERT (dup2 (fd + 1, fd) == fd);
185 ASSERT (close (fd + 1) == 0);
186 ASSERT (write (fd, "1", 1) == 1);
187 ASSERT (dup2 (fd + 2, fd) == fd);
188 ASSERT (lseek (fd, 0, SEEK_END) == 0);
189 ASSERT (write (fd + 2, "2", 1) == 1);
190 ASSERT (lseek (fd, 0, SEEK_SET) == 0);
191 ASSERT (read (fd, buffer, 1) == 1);
192 ASSERT (*buffer == '2');
193
194 #if GNULIB_TEST_CLOEXEC
195 /* Any new fd created by dup2 must not be cloexec. */
196 ASSERT (close (fd + 2) == 0);
197 ASSERT (dup_cloexec (fd) == fd + 1);
198 ASSERT (!is_inheritable (fd + 1));
199 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
200 ASSERT (!is_inheritable (fd + 1));
201 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
202 ASSERT (!is_inheritable (fd + 1));
203 ASSERT (is_inheritable (fd + 2));
204 errno = 0;
205 ASSERT (dup2 (fd + 1, -1) == -1);
206 ASSERT (errno == EBADF);
207 ASSERT (!is_inheritable (fd + 1));
208 #endif
209
210 /* On systems that distinguish between text and binary mode, dup2
211 reuses the mode of the source. */
212 set_binary_mode (fd, O_BINARY);
213 ASSERT (is_mode (fd, O_BINARY));
214 ASSERT (dup2 (fd, fd + 1) == fd + 1);
215 ASSERT (is_mode (fd + 1, O_BINARY));
216 set_binary_mode (fd, O_TEXT);
217 ASSERT (is_mode (fd, O_TEXT));
218 ASSERT (dup2 (fd, fd + 1) == fd + 1);
219 ASSERT (is_mode (fd + 1, O_TEXT));
220
221 /* Clean up. */
222 ASSERT (close (fd + 2) == 0);
223 ASSERT (close (fd + 1) == 0);
224 ASSERT (close (fd) == 0);
225 ASSERT (unlink (file) == 0);
226
227 return 0;
228 }