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