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