tests: avoid compiler warnings
[gnulib.git] / tests / test-fcntl.c
1 /* Test of fcntl(2).
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 /* Specification.  */
22 #include <fcntl.h>
23
24 /* Helpers.  */
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.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 "binary-io.h"
39
40 /* Use O_CLOEXEC if available, but test works without it.  */
41 #ifndef O_CLOEXEC
42 # define O_CLOEXEC 0
43 #endif
44
45 #if !O_BINARY
46 # define setmode(f,m) zero ()
47 static int zero (void) { return 0; }
48 #endif
49
50 #define ASSERT(expr) \
51   do                                                                         \
52     {                                                                        \
53       if (!(expr))                                                           \
54         {                                                                    \
55           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
56           fflush (stderr);                                                   \
57           abort ();                                                          \
58         }                                                                    \
59     }                                                                        \
60   while (0)
61
62 /* Return true if FD is open.  */
63 static bool
64 is_open (int fd)
65 {
66 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
67   /* On Win32, the initial state of unassigned standard file
68      descriptors is that they are open but point to an
69      INVALID_HANDLE_VALUE, and there is no fcntl.  */
70   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
71 #else
72 # ifndef F_GETFL
73 #  error Please port fcntl to your platform
74 # endif
75   return 0 <= fcntl (fd, F_GETFL);
76 #endif
77 }
78
79 /* Return true if FD is open and inheritable across exec/spawn.  */
80 static bool
81 is_inheritable (int fd)
82 {
83 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
84   /* On Win32, the initial state of unassigned standard file
85      descriptors is that they are open but point to an
86      INVALID_HANDLE_VALUE, and there is no fcntl.  */
87   HANDLE h = (HANDLE) _get_osfhandle (fd);
88   DWORD flags;
89   if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
90     return false;
91   return (flags & HANDLE_FLAG_INHERIT) != 0;
92 #else
93 # ifndef F_GETFD
94 #  error Please port fcntl to your platform
95 # endif
96   int i = fcntl (fd, F_GETFD);
97   return 0 <= i && (i & FD_CLOEXEC) == 0;
98 #endif
99 }
100
101 /* Return non-zero if FD is open in the given MODE, which is either
102    O_TEXT or O_BINARY.  */
103 static bool
104 is_mode (int fd, int mode)
105 {
106   int value = setmode (fd, O_BINARY);
107   setmode (fd, value);
108   return mode == value;
109 }
110
111 /* Since native fcntl can have more supported operations than our
112    replacement is aware of, and since various operations assign
113    different types to the vararg argument, a wrapper around fcntl must
114    be able to pass a vararg of unknown type on through to the original
115    fcntl.  Make sure that this works properly: func1 behaves like the
116    original fcntl interpreting the vararg as an int or a pointer to a
117    struct, and func2 behaves like rpl_fcntl that doesn't know what
118    type to forward.  */
119 struct dummy_struct
120 {
121   long filler;
122   int value;
123 };
124 static int
125 func1 (int a, ...)
126 {
127   va_list arg;
128   int i;
129   va_start (arg, a);
130   if (a < 4)
131     i = va_arg (arg, int);
132   else
133     {
134       struct dummy_struct *s = va_arg (arg, struct dummy_struct *);
135       i = s->value;
136     }
137   va_end (arg);
138   return i;
139 }
140 static int
141 func2 (int a, ...)
142 {
143   va_list arg;
144   void *p;
145   va_start (arg, a);
146   p = va_arg (arg, void *);
147   va_end (arg);
148   return func1 (a, p);
149 }
150
151 /* Ensure that all supported fcntl actions are distinct, and
152    usable in preprocessor expressions.  */
153 static void
154 check_flags (void)
155 {
156   switch (0)
157     {
158     case F_DUPFD:
159 #if F_DUPFD
160 #endif
161
162     case F_DUPFD_CLOEXEC:
163 #if F_DUPFD_CLOEXEC
164 #endif
165
166     case F_GETFD:
167 #if F_GETFD
168 #endif
169
170 #ifdef F_SETFD
171     case F_SETFD:
172 # if F_SETFD
173 # endif
174 #endif
175
176 #ifdef F_GETFL
177     case F_GETFL:
178 # if F_GETFL
179 # endif
180 #endif
181
182 #ifdef F_SETFL
183     case F_SETFL:
184 # if F_SETFL
185 # endif
186 #endif
187
188 #ifdef F_GETOWN
189     case F_GETOWN:
190 # if F_GETOWN
191 # endif
192 #endif
193
194 #ifdef F_SETOWN
195     case F_SETOWN:
196 # if F_SETOWN
197 # endif
198 #endif
199
200 #ifdef F_GETLK
201     case F_GETLK:
202 # if F_GETLK
203 # endif
204 #endif
205
206 #ifdef F_SETLK
207     case F_SETLK:
208 # if F_SETLK
209 # endif
210 #endif
211
212 #ifdef F_SETLKW
213     case F_SETLKW:
214 # if F_SETLKW
215 # endif
216 #endif
217
218       ;
219     }
220 }
221
222 int
223 main (void)
224 {
225   const char *file = "test-fcntl.tmp";
226   int fd;
227
228   /* Sanity check that rpl_fcntl is likely to work.  */
229   ASSERT (func2 (1, 2) == 2);
230   ASSERT (func2 (2, -2) == -2);
231   ASSERT (func2 (3, 0x80000000) == 0x80000000);
232   {
233     struct dummy_struct s = { 0L, 4 };
234     ASSERT (func2 (4, &s) == 4);
235   }
236   check_flags ();
237
238   /* Assume std descriptors were provided by invoker, and ignore fds
239      that might have been inherited.  */
240   fd = creat (file, 0600);
241   ASSERT (STDERR_FILENO < fd);
242   close (fd + 1);
243   close (fd + 2);
244
245   /* For F_DUPFD*, the source must be valid.  */
246   errno = 0;
247   ASSERT (fcntl (-1, F_DUPFD, 0) == -1);
248   ASSERT (errno == EBADF);
249   errno = 0;
250   ASSERT (fcntl (fd + 1, F_DUPFD, 0) == -1);
251   ASSERT (errno == EBADF);
252   errno = 0;
253   ASSERT (fcntl (10000000, F_DUPFD, 0) == -1);
254   ASSERT (errno == EBADF);
255   errno = 0;
256   ASSERT (fcntl (-1, F_DUPFD_CLOEXEC, 0) == -1);
257   ASSERT (errno == EBADF);
258   errno = 0;
259   ASSERT (fcntl (fd + 1, F_DUPFD_CLOEXEC, 0) == -1);
260   ASSERT (errno == EBADF);
261   errno = 0;
262   ASSERT (fcntl (10000000, F_DUPFD_CLOEXEC, 0) == -1);
263   ASSERT (errno == EBADF);
264
265   /* For F_DUPFD*, the destination must be valid.  */
266   ASSERT (getdtablesize () < 10000000);
267   errno = 0;
268   ASSERT (fcntl (fd, F_DUPFD, -1) == -1);
269   ASSERT (errno == EINVAL);
270   errno = 0;
271   ASSERT (fcntl (fd, F_DUPFD, 10000000) == -1);
272   ASSERT (errno == EINVAL);
273   ASSERT (getdtablesize () < 10000000);
274   errno = 0;
275   ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, -1) == -1);
276   ASSERT (errno == EINVAL);
277   errno = 0;
278   ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, 10000000) == -1);
279   ASSERT (errno == EINVAL);
280
281   /* For F_DUPFD*, check for correct inheritance, as well as
282      preservation of text vs. binary.  */
283   setmode (fd, O_BINARY);
284   ASSERT (is_open (fd));
285   ASSERT (!is_open (fd + 1));
286   ASSERT (!is_open (fd + 2));
287   ASSERT (is_inheritable (fd));
288   ASSERT (is_mode (fd, O_BINARY));
289
290   ASSERT (fcntl (fd, F_DUPFD, fd) == fd + 1);
291   ASSERT (is_open (fd));
292   ASSERT (is_open (fd + 1));
293   ASSERT (!is_open (fd + 2));
294   ASSERT (is_inheritable (fd + 1));
295   ASSERT (is_mode (fd, O_BINARY));
296   ASSERT (is_mode (fd + 1, O_BINARY));
297   ASSERT (close (fd + 1) == 0);
298
299   ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, fd + 2) == fd + 2);
300   ASSERT (is_open (fd));
301   ASSERT (!is_open (fd + 1));
302   ASSERT (is_open (fd + 2));
303   ASSERT (is_inheritable (fd));
304   ASSERT (!is_inheritable (fd + 2));
305   ASSERT (is_mode (fd, O_BINARY));
306   ASSERT (is_mode (fd + 2, O_BINARY));
307   ASSERT (close (fd) == 0);
308
309   setmode (fd + 2, O_TEXT);
310   ASSERT (fcntl (fd + 2, F_DUPFD, fd + 1) == fd + 1);
311   ASSERT (!is_open (fd));
312   ASSERT (is_open (fd + 1));
313   ASSERT (is_open (fd + 2));
314   ASSERT (is_inheritable (fd + 1));
315   ASSERT (!is_inheritable (fd + 2));
316   ASSERT (is_mode (fd + 1, O_TEXT));
317   ASSERT (is_mode (fd + 2, O_TEXT));
318   ASSERT (close (fd + 1) == 0);
319
320   ASSERT (fcntl (fd + 2, F_DUPFD_CLOEXEC, 0) == fd);
321   ASSERT (is_open (fd));
322   ASSERT (!is_open (fd + 1));
323   ASSERT (is_open (fd + 2));
324   ASSERT (!is_inheritable (fd));
325   ASSERT (!is_inheritable (fd + 2));
326   ASSERT (is_mode (fd, O_TEXT));
327   ASSERT (is_mode (fd + 2, O_TEXT));
328   ASSERT (close (fd + 2) == 0);
329
330   /* Test F_GETFD.  */
331   errno = 0;
332   ASSERT (fcntl (-1, F_GETFD) == -1);
333   ASSERT (errno == EBADF);
334   errno = 0;
335   ASSERT (fcntl (fd + 1, F_GETFD) == -1);
336   ASSERT (errno == EBADF);
337   errno = 0;
338   ASSERT (fcntl (10000000, F_GETFD) == -1);
339   ASSERT (errno == EBADF);
340   {
341     int result = fcntl (fd, F_GETFD);
342     ASSERT (0 <= result);
343     ASSERT ((result & FD_CLOEXEC) == FD_CLOEXEC);
344     ASSERT (dup (fd) == fd + 1);
345     result = fcntl (fd + 1, F_GETFD);
346     ASSERT (0 <= result);
347     ASSERT ((result & FD_CLOEXEC) == 0);
348     ASSERT (close (fd + 1) == 0);
349   }
350
351   /* Cleanup.  */
352   ASSERT (close (fd) == 0);
353   ASSERT (unlink (file) == 0);
354
355   return 0;
356 }