Mercurial > hg > octave-nkf > gnulib-hg
annotate tests/test-fcntl.c @ 16817:7747cb9b54b9
unistd_h: make it easier to avoid sys_types_h
This is useful for Emacs, which has its own method of porting to
Windows, and which therefore does not need the sys_types_h module.
* m4/off_t.m4: New file, defining gl_TYPE_OFF_T, which contains
code moved here from gl_SYS_TYPES_H.
* m4/sys_types_h.m4 (gl_SYS_TYPES_H): Require it instead of
using the code directly.
* m4/unistd_h.m4 (gl_UNISTD_H): Require gl_TYPE_OFF_T, not
gl_SYS_TYPES_H.
* modules/sys_types (Files):
* modules/unistd (Files): Add m4/off_t.m4.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 04 May 2012 13:05:31 -0700 |
parents | ec738d6aeef5 |
children | e542fd46ad6f |
rev | line source |
---|---|
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
1 /* Test of fcntl(2). |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
15752
diff
changeset
|
2 Copyright (C) 2009-2012 Free Software Foundation, Inc. |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
3 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
7 (at your option) any later version. |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
8 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
13 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
16 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
18 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
19 #include <config.h> |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
20 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
21 /* Specification. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
22 #include <fcntl.h> |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
23 |
12489 | 24 #include "signature.h" |
25 SIGNATURE_CHECK (fcntl, int, (int, int, ...)); | |
26 | |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
27 /* Helpers. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
28 #include <errno.h> |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
29 #include <stdarg.h> |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
30 #include <stdbool.h> |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
31 #include <unistd.h> |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
32 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
33 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
34 /* Get declarations of the native Windows API functions. */ |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
35 # define WIN32_LEAN_AND_MEAN |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
36 # include <windows.h> |
15752
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
15709
diff
changeset
|
37 /* Get _get_osfhandle. */ |
b86e9061a6d0
New module 'msvc-nothrow'. Makes _get_osfhandle safe on MSVC 9.
Bruno Haible <bruno@clisp.org>
parents:
15709
diff
changeset
|
38 # include "msvc-nothrow.h" |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
39 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
40 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
41 #include "binary-io.h" |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
42 #include "macros.h" |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
43 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
44 #if !O_BINARY |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
45 # define setmode(f,m) zero () |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
46 static int zero (void) { return 0; } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
47 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
48 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
49 /* Return true if FD is open. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
50 static bool |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
51 is_open (int fd) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
52 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
53 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
54 /* On native Windows, the initial state of unassigned standard file |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
55 descriptors is that they are open but point to an |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
56 INVALID_HANDLE_VALUE, and there is no fcntl. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
57 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
58 #else |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
59 # ifndef F_GETFL |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
60 # error Please port fcntl to your platform |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
61 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
62 return 0 <= fcntl (fd, F_GETFL); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
63 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
64 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
65 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
66 /* Return true if FD is open and inheritable across exec/spawn. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
67 static bool |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
68 is_inheritable (int fd) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
69 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
70 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ |
16214
ec738d6aeef5
Talk about "native Windows API", not "Win32".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
71 /* On native Windows, the initial state of unassigned standard file |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
72 descriptors is that they are open but point to an |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
73 INVALID_HANDLE_VALUE, and there is no fcntl. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
74 HANDLE h = (HANDLE) _get_osfhandle (fd); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
75 DWORD flags; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
76 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
77 return false; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
78 return (flags & HANDLE_FLAG_INHERIT) != 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
79 #else |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
80 # ifndef F_GETFD |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
81 # error Please port fcntl to your platform |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
82 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
83 int i = fcntl (fd, F_GETFD); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
84 return 0 <= i && (i & FD_CLOEXEC) == 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
85 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
86 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
87 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
88 /* Return non-zero if FD is open in the given MODE, which is either |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
89 O_TEXT or O_BINARY. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
90 static bool |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
91 is_mode (int fd, int mode) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
92 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
93 int value = setmode (fd, O_BINARY); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
94 setmode (fd, value); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
95 return mode == value; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
96 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
97 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
98 /* Since native fcntl can have more supported operations than our |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
99 replacement is aware of, and since various operations assign |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
100 different types to the vararg argument, a wrapper around fcntl must |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
101 be able to pass a vararg of unknown type on through to the original |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
102 fcntl. Make sure that this works properly: func1 behaves like the |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
103 original fcntl interpreting the vararg as an int or a pointer to a |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
104 struct, and func2 behaves like rpl_fcntl that doesn't know what |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
105 type to forward. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
106 struct dummy_struct |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
107 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
108 long filler; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
109 int value; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
110 }; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
111 static int |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
112 func1 (int a, ...) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
113 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
114 va_list arg; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
115 int i; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
116 va_start (arg, a); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
117 if (a < 4) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
118 i = va_arg (arg, int); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
119 else |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
120 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
121 struct dummy_struct *s = va_arg (arg, struct dummy_struct *); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
122 i = s->value; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
123 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
124 va_end (arg); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
125 return i; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
126 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
127 static int |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
128 func2 (int a, ...) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
129 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
130 va_list arg; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
131 void *p; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
132 va_start (arg, a); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
133 p = va_arg (arg, void *); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
134 va_end (arg); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
135 return func1 (a, p); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
136 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
137 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
138 /* Ensure that all supported fcntl actions are distinct, and |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
139 usable in preprocessor expressions. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
140 static void |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
141 check_flags (void) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
142 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
143 switch (0) |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
144 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
145 case F_DUPFD: |
12456
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
146 #if F_DUPFD |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
147 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
148 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
149 case F_DUPFD_CLOEXEC: |
12456
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
150 #if F_DUPFD_CLOEXEC |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
151 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
152 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
153 case F_GETFD: |
12456
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
154 #if F_GETFD |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
155 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
156 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
157 #ifdef F_SETFD |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
158 case F_SETFD: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
159 # if F_SETFD |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
160 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
161 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
162 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
163 #ifdef F_GETFL |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
164 case F_GETFL: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
165 # if F_GETFL |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
166 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
167 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
168 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
169 #ifdef F_SETFL |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
170 case F_SETFL: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
171 # if F_SETFL |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
172 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
173 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
174 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
175 #ifdef F_GETOWN |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
176 case F_GETOWN: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
177 # if F_GETOWN |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
178 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
179 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
180 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
181 #ifdef F_SETOWN |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
182 case F_SETOWN: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
183 # if F_SETOWN |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
184 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
185 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
186 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
187 #ifdef F_GETLK |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
188 case F_GETLK: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
189 # if F_GETLK |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
190 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
191 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
192 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
193 #ifdef F_SETLK |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
194 case F_SETLK: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
195 # if F_SETLK |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
196 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
197 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
198 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
199 #ifdef F_SETLKW |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
200 case F_SETLKW: |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
201 # if F_SETLKW |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
202 # endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
203 #endif |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
204 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
205 ; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
206 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
207 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
208 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
209 int |
12481 | 210 main (void) |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
211 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
212 const char *file = "test-fcntl.tmp"; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
213 int fd; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
214 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
215 /* Sanity check that rpl_fcntl is likely to work. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
216 ASSERT (func2 (1, 2) == 2); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
217 ASSERT (func2 (2, -2) == -2); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
218 ASSERT (func2 (3, 0x80000000) == 0x80000000); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
219 { |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
220 struct dummy_struct s = { 0L, 4 }; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
221 ASSERT (func2 (4, &s) == 4); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
222 } |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
223 check_flags (); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
224 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
225 /* Assume std descriptors were provided by invoker, and ignore fds |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
226 that might have been inherited. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
227 fd = creat (file, 0600); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
228 ASSERT (STDERR_FILENO < fd); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
229 close (fd + 1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
230 close (fd + 2); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
231 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
232 /* For F_DUPFD*, the source must be valid. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
233 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
234 ASSERT (fcntl (-1, F_DUPFD, 0) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
235 ASSERT (errno == EBADF); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
236 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
237 ASSERT (fcntl (fd + 1, F_DUPFD, 0) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
238 ASSERT (errno == EBADF); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
239 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
240 ASSERT (fcntl (10000000, F_DUPFD, 0) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
241 ASSERT (errno == EBADF); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
242 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
243 ASSERT (fcntl (-1, F_DUPFD_CLOEXEC, 0) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
244 ASSERT (errno == EBADF); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
245 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
246 ASSERT (fcntl (fd + 1, F_DUPFD_CLOEXEC, 0) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
247 ASSERT (errno == EBADF); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
248 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
249 ASSERT (fcntl (10000000, F_DUPFD_CLOEXEC, 0) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
250 ASSERT (errno == EBADF); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
251 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
252 /* For F_DUPFD*, the destination must be valid. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
253 ASSERT (getdtablesize () < 10000000); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
254 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
255 ASSERT (fcntl (fd, F_DUPFD, -1) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
256 ASSERT (errno == EINVAL); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
257 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
258 ASSERT (fcntl (fd, F_DUPFD, 10000000) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
259 ASSERT (errno == EINVAL); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
260 ASSERT (getdtablesize () < 10000000); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
261 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
262 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, -1) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
263 ASSERT (errno == EINVAL); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
264 errno = 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
265 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, 10000000) == -1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
266 ASSERT (errno == EINVAL); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
267 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
268 /* For F_DUPFD*, check for correct inheritance, as well as |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
269 preservation of text vs. binary. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
270 setmode (fd, O_BINARY); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
271 ASSERT (is_open (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
272 ASSERT (!is_open (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
273 ASSERT (!is_open (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
274 ASSERT (is_inheritable (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
275 ASSERT (is_mode (fd, O_BINARY)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
276 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
277 ASSERT (fcntl (fd, F_DUPFD, fd) == fd + 1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
278 ASSERT (is_open (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
279 ASSERT (is_open (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
280 ASSERT (!is_open (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
281 ASSERT (is_inheritable (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
282 ASSERT (is_mode (fd, O_BINARY)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
283 ASSERT (is_mode (fd + 1, O_BINARY)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
284 ASSERT (close (fd + 1) == 0); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
285 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
286 ASSERT (fcntl (fd, F_DUPFD_CLOEXEC, fd + 2) == fd + 2); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
287 ASSERT (is_open (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
288 ASSERT (!is_open (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
289 ASSERT (is_open (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
290 ASSERT (is_inheritable (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
291 ASSERT (!is_inheritable (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
292 ASSERT (is_mode (fd, O_BINARY)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
293 ASSERT (is_mode (fd + 2, O_BINARY)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
294 ASSERT (close (fd) == 0); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
295 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
296 setmode (fd + 2, O_TEXT); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
297 ASSERT (fcntl (fd + 2, F_DUPFD, fd + 1) == fd + 1); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
298 ASSERT (!is_open (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
299 ASSERT (is_open (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
300 ASSERT (is_open (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
301 ASSERT (is_inheritable (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
302 ASSERT (!is_inheritable (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
303 ASSERT (is_mode (fd + 1, O_TEXT)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
304 ASSERT (is_mode (fd + 2, O_TEXT)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
305 ASSERT (close (fd + 1) == 0); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
306 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
307 ASSERT (fcntl (fd + 2, F_DUPFD_CLOEXEC, 0) == fd); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
308 ASSERT (is_open (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
309 ASSERT (!is_open (fd + 1)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
310 ASSERT (is_open (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
311 ASSERT (!is_inheritable (fd)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
312 ASSERT (!is_inheritable (fd + 2)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
313 ASSERT (is_mode (fd, O_TEXT)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
314 ASSERT (is_mode (fd + 2, O_TEXT)); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
315 ASSERT (close (fd + 2) == 0); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
316 |
15709 | 317 /* Test F_GETFD on invalid file descriptors. */ |
12456
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
318 errno = 0; |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
319 ASSERT (fcntl (-1, F_GETFD) == -1); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
320 ASSERT (errno == EBADF); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
321 errno = 0; |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
322 ASSERT (fcntl (fd + 1, F_GETFD) == -1); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
323 ASSERT (errno == EBADF); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
324 errno = 0; |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
325 ASSERT (fcntl (10000000, F_GETFD) == -1); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
326 ASSERT (errno == EBADF); |
15709 | 327 |
328 /* Test F_GETFD, the FD_CLOEXEC bit. */ | |
12456
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
329 { |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
330 int result = fcntl (fd, F_GETFD); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
331 ASSERT (0 <= result); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
332 ASSERT ((result & FD_CLOEXEC) == FD_CLOEXEC); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
333 ASSERT (dup (fd) == fd + 1); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
334 result = fcntl (fd + 1, F_GETFD); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
335 ASSERT (0 <= result); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
336 ASSERT ((result & FD_CLOEXEC) == 0); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
337 ASSERT (close (fd + 1) == 0); |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
338 } |
f3aceada3c52
fcntl: port portions of fcntl to mingw
Eric Blake <ebb9@byu.net>
parents:
12454
diff
changeset
|
339 |
15709 | 340 #ifdef F_SETFD |
341 /* Test F_SETFD on invalid file descriptors. */ | |
342 errno = 0; | |
343 ASSERT (fcntl (-1, F_SETFD, 0) == -1); | |
344 ASSERT (errno == EBADF); | |
345 errno = 0; | |
346 ASSERT (fcntl (fd + 1, F_SETFD, 0) == -1); | |
347 ASSERT (errno == EBADF); | |
348 errno = 0; | |
349 ASSERT (fcntl (10000000, F_SETFD, 0) == -1); | |
350 ASSERT (errno == EBADF); | |
351 #endif | |
352 | |
353 #ifdef F_GETFL | |
354 /* Test F_GETFL on invalid file descriptors. */ | |
355 errno = 0; | |
356 ASSERT (fcntl (-1, F_GETFL) == -1); | |
357 ASSERT (errno == EBADF); | |
358 errno = 0; | |
359 ASSERT (fcntl (fd + 1, F_GETFL) == -1); | |
360 ASSERT (errno == EBADF); | |
361 errno = 0; | |
362 ASSERT (fcntl (10000000, F_GETFL) == -1); | |
363 ASSERT (errno == EBADF); | |
364 #endif | |
365 | |
366 #ifdef F_SETFL | |
367 /* Test F_SETFL on invalid file descriptors. */ | |
368 errno = 0; | |
369 ASSERT (fcntl (-1, F_SETFL, 0) == -1); | |
370 ASSERT (errno == EBADF); | |
371 errno = 0; | |
372 ASSERT (fcntl (fd + 1, F_SETFL, 0) == -1); | |
373 ASSERT (errno == EBADF); | |
374 errno = 0; | |
375 ASSERT (fcntl (10000000, F_SETFL, 0) == -1); | |
376 ASSERT (errno == EBADF); | |
377 #endif | |
378 | |
379 #ifdef F_GETOWN | |
380 /* Test F_GETOWN on invalid file descriptors. */ | |
381 errno = 0; | |
382 ASSERT (fcntl (-1, F_GETOWN) == -1); | |
383 ASSERT (errno == EBADF); | |
384 errno = 0; | |
385 ASSERT (fcntl (fd + 1, F_GETOWN) == -1); | |
386 ASSERT (errno == EBADF); | |
387 errno = 0; | |
388 ASSERT (fcntl (10000000, F_GETOWN) == -1); | |
389 ASSERT (errno == EBADF); | |
390 #endif | |
391 | |
392 #ifdef F_SETOWN | |
393 /* Test F_SETFL on invalid file descriptors. */ | |
394 errno = 0; | |
395 ASSERT (fcntl (-1, F_SETOWN, 0) == -1); | |
396 ASSERT (errno == EBADF); | |
397 errno = 0; | |
398 ASSERT (fcntl (fd + 1, F_SETOWN, 0) == -1); | |
399 ASSERT (errno == EBADF); | |
400 errno = 0; | |
401 ASSERT (fcntl (10000000, F_SETOWN, 0) == -1); | |
402 ASSERT (errno == EBADF); | |
403 #endif | |
404 | |
12454
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
405 /* Cleanup. */ |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
406 ASSERT (close (fd) == 0); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
407 ASSERT (unlink (file) == 0); |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
408 |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
409 return 0; |
f7624052e60d
fcntl: support F_DUPFD_CLOEXEC on systems with fcntl
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
410 } |