e6d3c62926358d5d739360278da26921f8459626
[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) 0
90 #endif
91
92 /* Return non-zero if FD is open in the given MODE, which is either
93    O_TEXT or O_BINARY.  */
94 static int
95 is_mode (int fd, int mode)
96 {
97   int value = setmode (fd, O_BINARY);
98   setmode (fd, value);
99   return mode == value;
100 }
101
102 int
103 main (void)
104 {
105   const char *file = "test-dup2.tmp";
106   char buffer[1];
107   int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
108
109   /* Assume std descriptors were provided by invoker.  */
110   ASSERT (STDERR_FILENO < fd);
111   ASSERT (is_open (fd));
112   /* Ignore any other fd's leaked into this process.  */
113   close (fd + 1);
114   close (fd + 2);
115   ASSERT (!is_open (fd + 1));
116   ASSERT (!is_open (fd + 2));
117
118   /* Assigning to self must be a no-op.  */
119   ASSERT (dup2 (fd, fd) == fd);
120   ASSERT (is_open (fd));
121
122   /* The source must be valid.  */
123   errno = 0;
124   ASSERT (dup2 (-1, fd) == -1);
125   ASSERT (errno == EBADF);
126   errno = 0;
127   ASSERT (dup2 (AT_FDCWD, fd) == -1);
128   ASSERT (errno == EBADF);
129   ASSERT (is_open (fd));
130
131   /* If the source is not open, then the destination is unaffected.  */
132   errno = 0;
133   ASSERT (dup2 (fd + 1, fd + 1) == -1);
134   ASSERT (errno == EBADF);
135   ASSERT (!is_open (fd + 1));
136   errno = 0;
137   ASSERT (dup2 (fd + 1, fd) == -1);
138   ASSERT (errno == EBADF);
139   ASSERT (is_open (fd));
140
141   /* The destination must be valid.  */
142   errno = 0;
143   ASSERT (dup2 (fd, -2) == -1);
144   ASSERT (errno == EBADF);
145   errno = 0;
146   ASSERT (dup2 (fd, 10000000) == -1);
147   ASSERT (errno == EBADF);
148
149   /* Using dup2 can skip fds.  */
150   ASSERT (dup2 (fd, fd + 2) == fd + 2);
151   ASSERT (is_open (fd));
152   ASSERT (!is_open (fd + 1));
153   ASSERT (is_open (fd + 2));
154
155   /* Verify that dup2 closes the previous occupant of a fd.  */
156   ASSERT (open ("/dev/null", O_WRONLY, 0600) == fd + 1);
157   ASSERT (dup2 (fd + 1, fd) == fd);
158   ASSERT (close (fd + 1) == 0);
159   ASSERT (write (fd, "1", 1) == 1);
160   ASSERT (dup2 (fd + 2, fd) == fd);
161   ASSERT (lseek (fd, 0, SEEK_END) == 0);
162   ASSERT (write (fd + 2, "2", 1) == 1);
163   ASSERT (lseek (fd, 0, SEEK_SET) == 0);
164   ASSERT (read (fd, buffer, 1) == 1);
165   ASSERT (*buffer == '2');
166
167   /* Any new fd created by dup2 must not be cloexec.  */
168   ASSERT (close (fd + 2) == 0);
169   ASSERT (dup_cloexec (fd) == fd + 1);
170   ASSERT (!is_inheritable (fd + 1));
171   ASSERT (dup2 (fd + 1, fd + 1) == fd + 1);
172   ASSERT (!is_inheritable (fd + 1));
173   ASSERT (dup2 (fd + 1, fd + 2) == fd + 2);
174   ASSERT (is_inheritable (fd + 2));
175
176   /* On systems that distinguish between text and binary mode, dup2
177      reuses the mode of the source.  */
178   setmode (fd, O_BINARY);
179   ASSERT (is_mode (fd, O_BINARY));
180   ASSERT (dup2 (fd, fd + 1) == fd + 1);
181   ASSERT (is_mode (fd + 1, O_BINARY));
182   setmode (fd, O_TEXT);
183   ASSERT (is_mode (fd, O_TEXT));
184   ASSERT (dup2 (fd, fd + 1) == fd + 1);
185   ASSERT (is_mode (fd + 1, O_TEXT));
186
187   /* Clean up.  */
188   ASSERT (close (fd + 2) == 0);
189   ASSERT (close (fd + 1) == 0);
190   ASSERT (close (fd) == 0);
191   ASSERT (unlink (file) == 0);
192
193   return 0;
194 }