Branch data Line data Source code
1 : : /* Test duplicating file descriptors.
2 : : Copyright (C) 2009-2012 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 <http://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 : : #include "binary-io.h"
30 : :
31 : : #if GNULIB_TEST_CLOEXEC
32 : : # include "cloexec.h"
33 : : #endif
34 : :
35 : : #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
36 : : /* Get declarations of the native Windows API functions. */
37 : : # define WIN32_LEAN_AND_MEAN
38 : : # include <windows.h>
39 : : /* Get _get_osfhandle. */
40 : : # include "msvc-nothrow.h"
41 : : #endif
42 : :
43 : : #include "macros.h"
44 : :
45 : : /* Return non-zero if FD is open. */
46 : : static int
47 : 10 : is_open (int fd)
48 : : {
49 : : #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
50 : : /* On native Windows, the initial state of unassigned standard file
51 : : descriptors is that they are open but point to an
52 : : INVALID_HANDLE_VALUE, and there is no fcntl. */
53 : : return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
54 : : #else
55 : : # ifndef F_GETFL
56 : : # error Please port fcntl to your platform
57 : : # endif
58 : 10 : return 0 <= fcntl (fd, F_GETFL);
59 : : #endif
60 : : }
61 : :
62 : : #if GNULIB_TEST_CLOEXEC
63 : : /* Return non-zero if FD is open and inheritable across exec/spawn. */
64 : : static int
65 : : is_inheritable (int fd)
66 : : {
67 : : # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
68 : : /* On native Windows, the initial state of unassigned standard file
69 : : descriptors is that they are open but point to an
70 : : INVALID_HANDLE_VALUE, and there is no fcntl. */
71 : : HANDLE h = (HANDLE) _get_osfhandle (fd);
72 : : DWORD flags;
73 : : if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
74 : : return 0;
75 : : return (flags & HANDLE_FLAG_INHERIT) != 0;
76 : : # else
77 : : # ifndef F_GETFD
78 : : # error Please port fcntl to your platform
79 : : # endif
80 : : int i = fcntl (fd, F_GETFD);
81 : : return 0 <= i && (i & FD_CLOEXEC) == 0;
82 : : # endif
83 : : }
84 : : #endif /* GNULIB_TEST_CLOEXEC */
85 : :
86 : : #if !O_BINARY
87 : : # define setmode(f,m) zero ()
88 : 10 : static int zero (void) { return 0; }
89 : : #endif
90 : :
91 : : /* Return non-zero if FD is open in the given MODE, which is either
92 : : O_TEXT or O_BINARY. */
93 : : static int
94 : 4 : is_mode (int fd, int mode)
95 : : {
96 : 4 : int value = setmode (fd, O_BINARY);
97 : 4 : setmode (fd, value);
98 : 4 : return mode == value;
99 : : }
100 : :
101 : : int
102 : 1 : main (void)
103 : : {
104 : 1 : const char *file = "test-dup2.tmp";
105 : : char buffer[1];
106 : 1 : int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
107 : :
108 : : /* Assume std descriptors were provided by invoker. */
109 [ - + ]: 1 : ASSERT (STDERR_FILENO < fd);
110 [ - + ]: 1 : ASSERT (is_open (fd));
111 : : /* Ignore any other fd's leaked into this process. */
112 : 1 : close (fd + 1);
113 : 1 : close (fd + 2);
114 [ - + ]: 1 : ASSERT (!is_open (fd + 1));
115 [ - + ]: 1 : ASSERT (!is_open (fd + 2));
116 : :
117 : : /* Assigning to self must be a no-op. */
118 [ - + ]: 1 : ASSERT (dup2 (fd, fd) == fd);
119 [ - + ]: 1 : ASSERT (is_open (fd));
120 : :
121 : : /* The source must be valid. */
122 : 1 : errno = 0;
123 [ - + ]: 1 : ASSERT (dup2 (-1, fd) == -1);
124 [ - + ]: 1 : ASSERT (errno == EBADF);
125 : 1 : errno = 0;
126 [ - + ]: 1 : ASSERT (dup2 (99, fd) == -1);
127 [ - + ]: 1 : ASSERT (errno == EBADF);
128 : 1 : errno = 0;
129 [ - + ]: 1 : ASSERT (dup2 (AT_FDCWD, fd) == -1);
130 [ - + ]: 1 : ASSERT (errno == EBADF);
131 [ - + ]: 1 : ASSERT (is_open (fd));
132 : :
133 : : /* If the source is not open, then the destination is unaffected. */
134 : 1 : errno = 0;
135 [ - + ]: 1 : ASSERT (dup2 (fd + 1, fd + 1) == -1);
136 [ - + ]: 1 : ASSERT (errno == EBADF);
137 [ - + ]: 1 : ASSERT (!is_open (fd + 1));
138 : 1 : errno = 0;
139 [ - + ]: 1 : ASSERT (dup2 (fd + 1, fd) == -1);
140 [ - + ]: 1 : ASSERT (errno == EBADF);
141 [ - + ]: 1 : ASSERT (is_open (fd));
142 : :
143 : : /* The destination must be valid. */
144 : 1 : errno = 0;
145 [ - + ]: 1 : ASSERT (dup2 (fd, -2) == -1);
146 [ - + ]: 1 : ASSERT (errno == EBADF);
147 : 1 : errno = 0;
148 [ - + ]: 1 : ASSERT (dup2 (fd, 10000000) == -1);
149 [ - + ]: 1 : ASSERT (errno == EBADF);
150 : :
151 : : /* Using dup2 can skip fds. */
152 [ - + ]: 1 : ASSERT (dup2 (fd, fd + 2) == fd + 2);
153 [ - + ]: 1 : ASSERT (is_open (fd));
154 [ - + ]: 1 : ASSERT (!is_open (fd + 1));
155 [ - + ]: 1 : ASSERT (is_open (fd + 2));
156 : :
157 : : /* Verify that dup2 closes the previous occupant of a fd. */
158 [ - + ]: 1 : ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
159 [ - + ]: 1 : ASSERT (dup2 (fd + 1, fd) == fd);
160 [ - + ]: 1 : ASSERT (close (fd + 1) == 0);
161 [ - + ]: 1 : ASSERT (write (fd, "1", 1) == 1);
162 [ - + ]: 1 : ASSERT (dup2 (fd + 2, fd) == fd);
163 [ - + ]: 1 : ASSERT (lseek (fd, 0, SEEK_END) == 0);
164 [ - + ]: 1 : ASSERT (write (fd + 2, "2", 1) == 1);
165 [ - + ]: 1 : ASSERT (lseek (fd, 0, SEEK_SET) == 0);
166 [ - + ]: 1 : ASSERT (read (fd, buffer, 1) == 1);
167 [ - + ]: 1 : ASSERT (*buffer == '2');
168 : :
169 : : #if GNULIB_TEST_CLOEXEC
170 : : /* Any new fd created by dup2 must not be cloexec. */
171 : : ASSERT (close (fd + 2) == 0);
172 : : ASSERT (dup_cloexec (fd) == fd + 1);
173 : : ASSERT (!is_inheritable (fd + 1));
174 : : ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
175 : : ASSERT (!is_inheritable (fd + 1));
176 : : ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
177 : : ASSERT (!is_inheritable (fd + 1));
178 : : ASSERT (is_inheritable (fd + 2));
179 : : errno = 0;
180 : : ASSERT (dup2 (fd + 1, -1) == -1);
181 : : ASSERT (errno == EBADF);
182 : : ASSERT (!is_inheritable (fd + 1));
183 : : #endif
184 : :
185 : : /* On systems that distinguish between text and binary mode, dup2
186 : : reuses the mode of the source. */
187 : 1 : setmode (fd, O_BINARY);
188 [ - + ]: 1 : ASSERT (is_mode (fd, O_BINARY));
189 [ - + ]: 1 : ASSERT (dup2 (fd, fd + 1) == fd + 1);
190 [ - + ]: 1 : ASSERT (is_mode (fd + 1, O_BINARY));
191 : 1 : setmode (fd, O_TEXT);
192 [ - + ]: 1 : ASSERT (is_mode (fd, O_TEXT));
193 [ - + ]: 1 : ASSERT (dup2 (fd, fd + 1) == fd + 1);
194 [ - + ]: 1 : ASSERT (is_mode (fd + 1, O_TEXT));
195 : :
196 : : /* Clean up. */
197 [ - + ]: 1 : ASSERT (close (fd + 2) == 0);
198 [ - + ]: 1 : ASSERT (close (fd + 1) == 0);
199 [ - + ]: 1 : ASSERT (close (fd) == 0);
200 [ - + ]: 1 : ASSERT (unlink (file) == 0);
201 : :
202 : 1 : return 0;
203 : : }
|