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