1 /* Test duplicating file descriptors.
2 Copyright (C) 2009-2013 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. */
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 __WIN32__) && ! defined __CYGWIN__
40 /* Get declarations of the native Windows API functions. */
41 # define WIN32_LEAN_AND_MEAN
43 /* Get _get_osfhandle. */
44 # include "msvc-nothrow.h"
49 /* Return non-zero if FD is open. */
53 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
54 /* On native Windows, the initial state of unassigned standard file
55 descriptors is that they are open but point to an
56 INVALID_HANDLE_VALUE, and there is no fcntl. */
57 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
60 # error Please port fcntl to your platform
62 return 0 <= fcntl (fd, F_GETFL);
66 #if GNULIB_TEST_CLOEXEC
67 /* Return non-zero if FD is open and inheritable across exec/spawn. */
69 is_inheritable (int fd)
71 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
72 /* On native Windows, the initial state of unassigned standard file
73 descriptors is that they are open but point to an
74 INVALID_HANDLE_VALUE, and there is no fcntl. */
75 HANDLE h = (HANDLE) _get_osfhandle (fd);
77 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
79 return (flags & HANDLE_FLAG_INHERIT) != 0;
82 # error Please port fcntl to your platform
84 int i = fcntl (fd, F_GETFD);
85 return 0 <= i && (i & FD_CLOEXEC) == 0;
88 #endif /* GNULIB_TEST_CLOEXEC */
91 # define setmode(f,m) zero ()
92 static int zero (void) { return 0; }
95 /* Return non-zero if FD is open in the given MODE, which is either
96 O_TEXT or O_BINARY. */
98 is_mode (int fd, int mode)
100 int value = setmode (fd, O_BINARY);
102 return mode == value;
108 const char *file = "test-dup2.tmp";
110 int bad_fd = getdtablesize ();
111 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
113 /* Assume std descriptors were provided by invoker. */
114 ASSERT (STDERR_FILENO < fd);
115 ASSERT (is_open (fd));
116 /* Ignore any other fd's leaked into this process. */
119 ASSERT (!is_open (fd + 1));
120 ASSERT (!is_open (fd + 2));
122 /* Assigning to self must be a no-op. */
123 ASSERT (dup2 (fd, fd) == fd);
124 ASSERT (is_open (fd));
126 /* The source must be valid. */
128 ASSERT (dup2 (-1, fd) == -1);
129 ASSERT (errno == EBADF);
132 ASSERT (dup2 (99, fd) == -1);
133 ASSERT (errno == EBADF);
135 ASSERT (dup2 (AT_FDCWD, fd) == -1);
136 ASSERT (errno == EBADF);
137 ASSERT (is_open (fd));
139 /* If the source is not open, then the destination is unaffected. */
141 ASSERT (dup2 (fd + 1, fd + 1) == -1);
142 ASSERT (errno == EBADF);
143 ASSERT (!is_open (fd + 1));
145 ASSERT (dup2 (fd + 1, fd) == -1);
146 ASSERT (errno == EBADF);
147 ASSERT (is_open (fd));
149 /* The destination must be valid. */
151 ASSERT (dup2 (fd, -2) == -1);
152 ASSERT (errno == EBADF);
154 ASSERT (dup2 (fd, bad_fd) == -1);
155 ASSERT (errno == EBADF);
157 /* Using dup2 can skip fds. */
158 ASSERT (dup2 (fd, fd + 2) == fd + 2);
159 ASSERT (is_open (fd));
160 ASSERT (!is_open (fd + 1));
161 ASSERT (is_open (fd + 2));
163 /* Verify that dup2 closes the previous occupant of a fd. */
164 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
165 ASSERT (dup2 (fd + 1, fd) == fd);
166 ASSERT (close (fd + 1) == 0);
167 ASSERT (write (fd, "1", 1) == 1);
168 ASSERT (dup2 (fd + 2, fd) == fd);
169 ASSERT (lseek (fd, 0, SEEK_END) == 0);
170 ASSERT (write (fd + 2, "2", 1) == 1);
171 ASSERT (lseek (fd, 0, SEEK_SET) == 0);
172 ASSERT (read (fd, buffer, 1) == 1);
173 ASSERT (*buffer == '2');
175 #if GNULIB_TEST_CLOEXEC
176 /* Any new fd created by dup2 must not be cloexec. */
177 ASSERT (close (fd + 2) == 0);
178 ASSERT (dup_cloexec (fd) == fd + 1);
179 ASSERT (!is_inheritable (fd + 1));
180 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
181 ASSERT (!is_inheritable (fd + 1));
182 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
183 ASSERT (!is_inheritable (fd + 1));
184 ASSERT (is_inheritable (fd + 2));
186 ASSERT (dup2 (fd + 1, -1) == -1);
187 ASSERT (errno == EBADF);
188 ASSERT (!is_inheritable (fd + 1));
191 /* On systems that distinguish between text and binary mode, dup2
192 reuses the mode of the source. */
193 setmode (fd, O_BINARY);
194 ASSERT (is_mode (fd, O_BINARY));
195 ASSERT (dup2 (fd, fd + 1) == fd + 1);
196 ASSERT (is_mode (fd + 1, O_BINARY));
197 setmode (fd, O_TEXT);
198 ASSERT (is_mode (fd, O_TEXT));
199 ASSERT (dup2 (fd, fd + 1) == fd + 1);
200 ASSERT (is_mode (fd + 1, O_TEXT));
203 ASSERT (close (fd + 2) == 0);
204 ASSERT (close (fd + 1) == 0);
205 ASSERT (close (fd) == 0);
206 ASSERT (unlink (file) == 0);