source: diffutils/vendor/current/gnulib-tests/test-fcntl.c@ 530

Last change on this file since 530 was 530, checked in by Yuri Dario, 12 years ago

diffutils: initial vendor import of diffutils 3.2.0.

File size: 8.2 KB
Line 
1/* -*- buffer-read-only: t -*- vi: set ro: */
2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3/* Test of fcntl(2).
4 Copyright (C) 2009-2011 Free Software Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19/* Written by Eric Blake <ebb9@byu.net>, 2009. */
20
21#include <config.h>
22
23/* Specification. */
24#include <fcntl.h>
25
26#include "signature.h"
27SIGNATURE_CHECK (fcntl, int, (int, int, ...));
28
29/* Helpers. */
30#include <errno.h>
31#include <stdarg.h>
32#include <stdbool.h>
33#include <unistd.h>
34
35#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
36/* Get declarations of the Win32 API functions. */
37# define WIN32_LEAN_AND_MEAN
38# include <windows.h>
39#endif
40
41#include "binary-io.h"
42#include "macros.h"
43
44#if !O_BINARY
45# define setmode(f,m) zero ()
46static int zero (void) { return 0; }
47#endif
48
49/* Return true if FD is open. */
50static bool
51is_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 true if FD is open and inheritable across exec/spawn. */
67static bool
68is_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 false;
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/* Return non-zero if FD is open in the given MODE, which is either
89 O_TEXT or O_BINARY. */
90static bool
91is_mode (int fd, int mode)
92{
93 int value = setmode (fd, O_BINARY);
94 setmode (fd, value);
95 return mode == value;
96}
97
98/* Since native fcntl can have more supported operations than our
99 replacement is aware of, and since various operations assign
100 different types to the vararg argument, a wrapper around fcntl must
101 be able to pass a vararg of unknown type on through to the original
102 fcntl. Make sure that this works properly: func1 behaves like the
103 original fcntl interpreting the vararg as an int or a pointer to a
104 struct, and func2 behaves like rpl_fcntl that doesn't know what
105 type to forward. */
106struct dummy_struct
107{
108 long filler;
109 int value;
110};
111static int
112func1 (int a, ...)
113{
114 va_list arg;
115 int i;
116 va_start (arg, a);
117 if (a < 4)
118 i = va_arg (arg, int);
119 else
120 {
121 struct dummy_struct *s = va_arg (arg, struct dummy_struct *);
122 i = s->value;
123 }
124 va_end (arg);
125 return i;
126}
127static int
128func2 (int a, ...)
129{
130 va_list arg;
131 void *p;
132 va_start (arg, a);
133 p = va_arg (arg, void *);
134 va_end (arg);
135 return func1 (a, p);
136}
137
138/* Ensure that all supported fcntl actions are distinct, and
139 usable in preprocessor expressions. */
140static void
141check_flags (void)
142{
143 switch (0)
144 {
145 case F_DUPFD:
146#if F_DUPFD
147#endif
148
149 case F_DUPFD_CLOEXEC:
150#if F_DUPFD_CLOEXEC
151#endif
152
153 case F_GETFD:
154#if F_GETFD
155#endif
156
157#ifdef F_SETFD
158 case F_SETFD:
159# if F_SETFD
160# endif
161#endif
162
163#ifdef F_GETFL
164 case F_GETFL:
165# if F_GETFL
166# endif
167#endif
168
169#ifdef F_SETFL
170 case F_SETFL:
171# if F_SETFL
172# endif
173#endif
174
175#ifdef F_GETOWN
176 case F_GETOWN:
177# if F_GETOWN
178# endif
179#endif
180
181#ifdef F_SETOWN
182 case F_SETOWN:
183# if F_SETOWN
184# endif
185#endif
186
187#ifdef F_GETLK
188 case F_GETLK:
189# if F_GETLK
190# endif
191#endif
192
193#ifdef F_SETLK
194 case F_SETLK:
195# if F_SETLK
196# endif
197#endif
198
199#ifdef F_SETLKW
200 case F_SETLKW:
201# if F_SETLKW
202# endif
203#endif
204
205 ;
206 }
207}
208
209int
210main (void)
211{
212 const char *file = "test-fcntl.tmp";
213 int fd;
214
215 /* Sanity check that rpl_fcntl is likely to work. */
216 ASSERT (func2 (1, 2) == 2);
217 ASSERT (func2 (2, -2) == -2);
218 ASSERT (func2 (3, 0x80000000) == 0x80000000);
219 {
220 struct dummy_struct s = { 0L, 4 };
221 ASSERT (func2 (4, &s) == 4);
222 }
223 check_flags ();
224
225 /* Assume std descriptors were provided by invoker, and ignore fds
226 that might have been inherited. */
227 fd = creat (file, 0600);
228 ASSERT (STDERR_FILENO < fd);
229 close (fd + 1);
230 close (fd + 2);
231
232 /* For F_DUPFD*, the source must be valid. */
233 errno = 0;
234 ASSERT (fcntl (-1, F_DUPFD, 0) == -1);
235 ASSERT (errno == EBADF);
236 errno = 0;
237 ASSERT (fcntl (fd + 1, F_DUPFD, 0) == -1);
238 ASSERT (errno == EBADF);
239 errno = 0;
240 ASSERT (fcntl (10000000, F_DUPFD, 0) == -1);
241 ASSERT (errno == EBADF);
242 errno = 0;
243 ASSERT (fcntl (-1, F_DUPFD_CLOEXEC, 0) == -1);
244 ASSERT (errno == EBADF);
245 errno = 0;
246 ASSERT (fcntl (fd + 1, F_DUPFD_CLOEXEC, 0) == -1);
247 ASSERT (errno == EBADF);
248 errno = 0;
249 ASSERT (fcntl (10000000, F_DUPFD_CLOEXEC, 0) == -1);
250 ASSERT (errno == EBADF);
251
252 /* For F_DUPFD*, the destination must be valid. */
253 ASSERT (getdtablesize () < 10000000);
254 errno = 0;
255 ASSERT (fcntl (fd, F_DUPFD, -1) == -1);
256 ASSERT (errno == EINVAL);
257 errno = 0;
258 ASSERT (fcntl (fd, F_DUPFD, 10000000) == -1);
259 ASSERT (errno == EINVAL);
260 ASSERT (getdtablesize () < 10000000);
261 errno = 0;
262 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, -1) == -1);
263 ASSERT (errno == EINVAL);
264 errno = 0;
265 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, 10000000) == -1);
266 ASSERT (errno == EINVAL);
267
268 /* For F_DUPFD*, check for correct inheritance, as well as
269 preservation of text vs. binary. */
270 setmode (fd, O_BINARY);
271 ASSERT (is_open (fd));
272 ASSERT (!is_open (fd + 1));
273 ASSERT (!is_open (fd + 2));
274 ASSERT (is_inheritable (fd));
275 ASSERT (is_mode (fd, O_BINARY));
276
277 ASSERT (fcntl (fd, F_DUPFD, fd) == fd + 1);
278 ASSERT (is_open (fd));
279 ASSERT (is_open (fd + 1));
280 ASSERT (!is_open (fd + 2));
281 ASSERT (is_inheritable (fd + 1));
282 ASSERT (is_mode (fd, O_BINARY));
283 ASSERT (is_mode (fd + 1, O_BINARY));
284 ASSERT (close (fd + 1) == 0);
285
286 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, fd + 2) == fd + 2);
287 ASSERT (is_open (fd));
288 ASSERT (!is_open (fd + 1));
289 ASSERT (is_open (fd + 2));
290 ASSERT (is_inheritable (fd));
291 ASSERT (!is_inheritable (fd + 2));
292 ASSERT (is_mode (fd, O_BINARY));
293 ASSERT (is_mode (fd + 2, O_BINARY));
294 ASSERT (close (fd) == 0);
295
296 setmode (fd + 2, O_TEXT);
297 ASSERT (fcntl (fd + 2, F_DUPFD, fd + 1) == fd + 1);
298 ASSERT (!is_open (fd));
299 ASSERT (is_open (fd + 1));
300 ASSERT (is_open (fd + 2));
301 ASSERT (is_inheritable (fd + 1));
302 ASSERT (!is_inheritable (fd + 2));
303 ASSERT (is_mode (fd + 1, O_TEXT));
304 ASSERT (is_mode (fd + 2, O_TEXT));
305 ASSERT (close (fd + 1) == 0);
306
307 ASSERT (fcntl (fd + 2, F_DUPFD_CLOEXEC, 0) == fd);
308 ASSERT (is_open (fd));
309 ASSERT (!is_open (fd + 1));
310 ASSERT (is_open (fd + 2));
311 ASSERT (!is_inheritable (fd));
312 ASSERT (!is_inheritable (fd + 2));
313 ASSERT (is_mode (fd, O_TEXT));
314 ASSERT (is_mode (fd + 2, O_TEXT));
315 ASSERT (close (fd + 2) == 0);
316
317 /* Test F_GETFD. */
318 errno = 0;
319 ASSERT (fcntl (-1, F_GETFD) == -1);
320 ASSERT (errno == EBADF);
321 errno = 0;
322 ASSERT (fcntl (fd + 1, F_GETFD) == -1);
323 ASSERT (errno == EBADF);
324 errno = 0;
325 ASSERT (fcntl (10000000, F_GETFD) == -1);
326 ASSERT (errno == EBADF);
327 {
328 int result = fcntl (fd, F_GETFD);
329 ASSERT (0 <= result);
330 ASSERT ((result & FD_CLOEXEC) == FD_CLOEXEC);
331 ASSERT (dup (fd) == fd + 1);
332 result = fcntl (fd + 1, F_GETFD);
333 ASSERT (0 <= result);
334 ASSERT ((result & FD_CLOEXEC) == 0);
335 ASSERT (close (fd + 1) == 0);
336 }
337
338 /* Cleanup. */
339 ASSERT (close (fd) == 0);
340 ASSERT (unlink (file) == 0);
341
342 return 0;
343}
Note: See TracBrowser for help on using the repository browser.