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) 0
92 /* Return non-zero if FD is open in the given MODE, which is either
93 O_TEXT or O_BINARY. */
95 is_mode (int fd, int mode)
97 int value = setmode (fd, O_BINARY);
105 const char *file = "test-dup2.tmp";
107 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
109 /* Assume std descriptors were provided by invoker. */
110 ASSERT (STDERR_FILENO < fd);
111 ASSERT (is_open (fd));
112 /* Ignore any other fd's leaked into this process. */
115 ASSERT (!is_open (fd + 1));
116 ASSERT (!is_open (fd + 2));
118 /* Assigning to self must be a no-op. */
119 ASSERT (dup2 (fd, fd) == fd);
120 ASSERT (is_open (fd));
122 /* The source must be valid. */
124 ASSERT (dup2 (-1, fd) == -1);
125 ASSERT (errno == EBADF);
127 ASSERT (dup2 (AT_FDCWD, fd) == -1);
128 ASSERT (errno == EBADF);
129 ASSERT (is_open (fd));
131 /* If the source is not open, then the destination is unaffected. */
133 ASSERT (dup2 (fd + 1, fd + 1) == -1);
134 ASSERT (errno == EBADF);
135 ASSERT (!is_open (fd + 1));
137 ASSERT (dup2 (fd + 1, fd) == -1);
138 ASSERT (errno == EBADF);
139 ASSERT (is_open (fd));
141 /* The destination must be valid. */
143 ASSERT (dup2 (fd, -2) == -1);
144 ASSERT (errno == EBADF);
146 ASSERT (dup2 (fd, 10000000) == -1);
147 ASSERT (errno == EBADF);
149 /* Using dup2 can skip fds. */
150 ASSERT (dup2 (fd, fd + 2) == fd + 2);
151 ASSERT (is_open (fd));
152 ASSERT (!is_open (fd + 1));
153 ASSERT (is_open (fd + 2));
155 /* Verify that dup2 closes the previous occupant of a fd. */
156 ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
157 ASSERT (dup2 (fd + 1, fd) == fd);
158 ASSERT (close (fd + 1) == 0);
159 ASSERT (write (fd, "1", 1) == 1);
160 ASSERT (dup2 (fd + 2, fd) == fd);
161 ASSERT (lseek (fd, 0, SEEK_END) == 0);
162 ASSERT (write (fd + 2, "2", 1) == 1);
163 ASSERT (lseek (fd, 0, SEEK_SET) == 0);
164 ASSERT (read (fd, buffer, 1) == 1);
165 ASSERT (*buffer == '2');
167 /* Any new fd created by dup2 must not be cloexec. */
168 ASSERT (close (fd + 2) == 0);
169 ASSERT (dup_cloexec (fd) == fd + 1);
170 ASSERT (!is_inheritable (fd + 1));
171 ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
172 ASSERT (!is_inheritable (fd + 1));
173 ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
174 ASSERT (is_inheritable (fd + 2));
176 /* On systems that distinguish between text and binary mode, dup2
177 reuses the mode of the source. */
178 setmode (fd, O_BINARY);
179 ASSERT (is_mode (fd, O_BINARY));
180 ASSERT (dup2 (fd, fd + 1) == fd + 1);
181 ASSERT (is_mode (fd + 1, O_BINARY));
182 setmode (fd, O_TEXT);
183 ASSERT (is_mode (fd, O_TEXT));
184 ASSERT (dup2 (fd, fd + 1) == fd + 1);
185 ASSERT (is_mode (fd + 1, O_TEXT));
188 ASSERT (close (fd + 2) == 0);
189 ASSERT (close (fd + 1) == 0);
190 ASSERT (close (fd) == 0);
191 ASSERT (unlink (file) == 0);