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