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