maint: update copyright
[gnulib.git] / tests / test-dup2.c
1 /* Test duplicating file descriptors.
2    Copyright (C) 2009-2014 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 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   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 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 is_mode (int fd, int mode)
99 {
100   int value = setmode (fd, O_BINARY);
101   setmode (fd, value);
102   return mode == value;
103 }
104
105 int
106 main (void)
107 {
108   const char *file = "test-dup2.tmp";
109   char buffer[1];
110   int bad_fd = getdtablesize ();
111   int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
112
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.  */
117   close (fd + 1);
118   close (fd + 2);
119   ASSERT (!is_open (fd + 1));
120   ASSERT (!is_open (fd + 2));
121
122   /* Assigning to self must be a no-op.  */
123   ASSERT (dup2 (fd, fd) == fd);
124   ASSERT (is_open (fd));
125
126   /* The source must be valid.  */
127   errno = 0;
128   ASSERT (dup2 (-1, fd) == -1);
129   ASSERT (errno == EBADF);
130   close (99);
131   errno = 0;
132   ASSERT (dup2 (99, fd) == -1);
133   ASSERT (errno == EBADF);
134   errno = 0;
135   ASSERT (dup2 (AT_FDCWD, fd) == -1);
136   ASSERT (errno == EBADF);
137   ASSERT (is_open (fd));
138
139   /* If the source is not open, then the destination is unaffected.  */
140   errno = 0;
141   ASSERT (dup2 (fd + 1, fd + 1) == -1);
142   ASSERT (errno == EBADF);
143   ASSERT (!is_open (fd + 1));
144   errno = 0;
145   ASSERT (dup2 (fd + 1, fd) == -1);
146   ASSERT (errno == EBADF);
147   ASSERT (is_open (fd));
148
149   /* The destination must be valid.  */
150   errno = 0;
151   ASSERT (dup2 (fd, -2) == -1);
152   ASSERT (errno == EBADF);
153   if (bad_fd > 256)
154     {
155       ASSERT (dup2 (fd, 255) == 255);
156       ASSERT (dup2 (fd, 256) == 256);
157       ASSERT (close (255) == 0);
158       ASSERT (close (256) == 0);
159     }
160   ASSERT (dup2 (fd, bad_fd - 1) == bad_fd - 1);
161   ASSERT (close (bad_fd - 1) == 0);
162   errno = 0;
163   ASSERT (dup2 (fd, bad_fd) == -1);
164   ASSERT (errno == EBADF);
165
166   /* Using dup2 can skip fds.  */
167   ASSERT (dup2 (fd, fd + 2) == fd + 2);
168   ASSERT (is_open (fd));
169   ASSERT (!is_open (fd + 1));
170   ASSERT (is_open (fd + 2));
171
172   /* Verify that dup2 closes the previous occupant of a fd.  */
173   ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
174   ASSERT (dup2 (fd + 1, fd) == fd);
175   ASSERT (close (fd + 1) == 0);
176   ASSERT (write (fd, "1", 1) == 1);
177   ASSERT (dup2 (fd + 2, fd) == fd);
178   ASSERT (lseek (fd, 0, SEEK_END) == 0);
179   ASSERT (write (fd + 2, "2", 1) == 1);
180   ASSERT (lseek (fd, 0, SEEK_SET) == 0);
181   ASSERT (read (fd, buffer, 1) == 1);
182   ASSERT (*buffer == '2');
183
184 #if GNULIB_TEST_CLOEXEC
185   /* Any new fd created by dup2 must not be cloexec.  */
186   ASSERT (close (fd + 2) == 0);
187   ASSERT (dup_cloexec (fd) == fd + 1);
188   ASSERT (!is_inheritable (fd + 1));
189   ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
190   ASSERT (!is_inheritable (fd + 1));
191   ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
192   ASSERT (!is_inheritable (fd + 1));
193   ASSERT (is_inheritable (fd + 2));
194   errno = 0;
195   ASSERT (dup2 (fd + 1, -1) == -1);
196   ASSERT (errno == EBADF);
197   ASSERT (!is_inheritable (fd + 1));
198 #endif
199
200   /* On systems that distinguish between text and binary mode, dup2
201      reuses the mode of the source.  */
202   setmode (fd, O_BINARY);
203   ASSERT (is_mode (fd, O_BINARY));
204   ASSERT (dup2 (fd, fd + 1) == fd + 1);
205   ASSERT (is_mode (fd + 1, O_BINARY));
206   setmode (fd, O_TEXT);
207   ASSERT (is_mode (fd, O_TEXT));
208   ASSERT (dup2 (fd, fd + 1) == fd + 1);
209   ASSERT (is_mode (fd + 1, O_TEXT));
210
211   /* Clean up.  */
212   ASSERT (close (fd + 2) == 0);
213   ASSERT (close (fd + 1) == 0);
214   ASSERT (close (fd) == 0);
215   ASSERT (unlink (file) == 0);
216
217   return 0;
218 }