1 /* Test duplicating file descriptors.
2 Copyright (C) 2009 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. */
28 #include "binary-io.h"
31 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
32 /* Get declarations of the Win32 API functions. */
33 # define WIN32_LEAN_AND_MEAN
37 #define ASSERT(expr) \
42 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
49 /* Return non-zero if FD is open. */
53 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
54 /* On Win32, 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 /* Return non-zero if FD is open and inheritable across exec/spawn. */
68 is_inheritable (int fd)
70 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
71 /* On Win32, the initial state of unassigned standard file
72 descriptors is that they are open but point to an
73 INVALID_HANDLE_VALUE, and there is no fcntl. */
74 HANDLE h = (HANDLE) _get_osfhandle (fd);
76 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
78 return (flags & HANDLE_FLAG_INHERIT) != 0;
81 # error Please port fcntl to your platform
83 int i = fcntl (fd, F_GETFD);
84 return 0 <= i && (i & FD_CLOEXEC) == 0;
89 # define setmode(f,m) zero ()
90 static int zero (void) { return 0; }
93 /* Return non-zero if FD is open in the given MODE, which is either
94 O_TEXT or O_BINARY. */
96 is_mode (int fd, int mode)
98 int value = setmode (fd, O_BINARY);
100 return mode == value;
106 const char *file = "test-dup2.tmp";
108 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
110 /* Assume std descriptors were provided by invoker. */
111 ASSERT (STDERR_FILENO < fd);
112 ASSERT (is_open (fd));
113 /* Ignore any other fd's leaked into this process. */
116 ASSERT (!is_open (fd + 1));
117 ASSERT (!is_open (fd + 2));
119 /* Assigning to self must be a no-op. */
120 ASSERT (dup2 (fd, fd) == fd);
121 ASSERT (is_open (fd));
123 /* The source must be valid. */
125 ASSERT (dup2 (-1, fd) == -1);
126 ASSERT (errno == EBADF);
128 ASSERT (dup2 (AT_FDCWD, fd) == -1);
129 ASSERT (errno == EBADF);
130 ASSERT (is_open (fd));
132 /* If the source is not open, then the destination is unaffected. */
134 ASSERT (dup2 (fd + 1, fd + 1) == -1);
135 ASSERT (errno == EBADF);
136 ASSERT (!is_open (fd + 1));
138 ASSERT (dup2 (fd + 1, fd) == -1);
139 ASSERT (errno == EBADF);
140 ASSERT (is_open (fd));
142 /* The destination must be valid. */
144 ASSERT (dup2 (fd, -2) == -1);
145 ASSERT (errno == EBADF);
147 ASSERT (dup2 (fd, 10000000) == -1);
148 ASSERT (errno == EBADF);
150 /* Using dup2 can skip fds. */
151 ASSERT (dup2 (fd, fd + 2) == fd + 2);
152 ASSERT (is_open (fd));
153 ASSERT (!is_open (fd + 1));
154 ASSERT (is_open (fd + 2));
156 /* Verify that dup2 closes the previous occupant of a fd. */
157 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
158 ASSERT (dup2 (fd + 1, fd) == fd);
159 ASSERT (close (fd + 1) == 0);
160 ASSERT (write (fd, "1", 1) == 1);
161 ASSERT (dup2 (fd + 2, fd) == fd);
162 ASSERT (lseek (fd, 0, SEEK_END) == 0);
163 ASSERT (write (fd + 2, "2", 1) == 1);
164 ASSERT (lseek (fd, 0, SEEK_SET) == 0);
165 ASSERT (read (fd, buffer, 1) == 1);
166 ASSERT (*buffer == '2');
168 /* Any new fd created by dup2 must not be cloexec. */
169 ASSERT (close (fd + 2) == 0);
170 ASSERT (dup_cloexec (fd) == fd + 1);
171 ASSERT (!is_inheritable (fd + 1));
172 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
173 ASSERT (!is_inheritable (fd + 1));
174 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
175 ASSERT (is_inheritable (fd + 2));
177 /* On systems that distinguish between text and binary mode, dup2
178 reuses the mode of the source. */
179 setmode (fd, O_BINARY);
180 ASSERT (is_mode (fd, O_BINARY));
181 ASSERT (dup2 (fd, fd + 1) == fd + 1);
182 ASSERT (is_mode (fd + 1, O_BINARY));
183 setmode (fd, O_TEXT);
184 ASSERT (is_mode (fd, O_TEXT));
185 ASSERT (dup2 (fd, fd + 1) == fd + 1);
186 ASSERT (is_mode (fd + 1, O_TEXT));
189 ASSERT (close (fd + 2) == 0);
190 ASSERT (close (fd + 1) == 0);
191 ASSERT (close (fd) == 0);
192 ASSERT (unlink (file) == 0);