This source file includes following definitions.
- is_inheritable
- zero
- is_mode
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21 #include "cloexec.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26
27 #if defined _WIN32 && ! defined __CYGWIN__
28
29 # define WIN32_LEAN_AND_MEAN
30 # include <windows.h>
31
32 # if GNULIB_MSVC_NOTHROW
33 # include "msvc-nothrow.h"
34 # else
35 # include <io.h>
36 # endif
37 #endif
38
39 #include "binary-io.h"
40 #include "macros.h"
41
42
43 static int
44 is_inheritable (int fd)
45 {
46 #if defined _WIN32 && ! defined __CYGWIN__
47
48
49
50 HANDLE h = (HANDLE) _get_osfhandle (fd);
51 DWORD flags;
52 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
53 return 0;
54 return (flags & HANDLE_FLAG_INHERIT) != 0;
55 #else
56 # ifndef F_GETFD
57 # error Please port fcntl to your platform
58 # endif
59 int i = fcntl (fd, F_GETFD);
60 return 0 <= i && (i & FD_CLOEXEC) == 0;
61 #endif
62 }
63
64 #if !O_BINARY
65 # define set_binary_mode(f,m) zero ()
66 static int zero (void) { return 0; }
67 #endif
68
69
70
71 static int
72 is_mode (int fd, int mode)
73 {
74 int value = set_binary_mode (fd, O_BINARY);
75 set_binary_mode (fd, value);
76 return mode == value;
77 }
78
79 int
80 main (void)
81 {
82 const char *file = "test-cloexec.tmp";
83 int fd = creat (file, 0600);
84 int fd2;
85 int bad_fd = getdtablesize ();
86
87
88 ASSERT (STDERR_FILENO < fd);
89 ASSERT (is_inheritable (fd));
90
91
92 ASSERT (set_cloexec_flag (fd, true) == 0);
93 #if !(defined _WIN32 && ! defined __CYGWIN__)
94 ASSERT (!is_inheritable (fd));
95 #endif
96 ASSERT (set_cloexec_flag (fd, false) == 0);
97 ASSERT (is_inheritable (fd));
98
99
100 fd2 = dup_cloexec (fd);
101 ASSERT (fd < fd2);
102 ASSERT (!is_inheritable (fd2));
103 ASSERT (close (fd) == 0);
104 ASSERT (dup_cloexec (fd2) == fd);
105 ASSERT (!is_inheritable (fd));
106 ASSERT (close (fd2) == 0);
107
108
109
110 set_binary_mode (fd, O_BINARY);
111 ASSERT (is_mode (fd, O_BINARY));
112 fd2 = dup_cloexec (fd);
113 ASSERT (fd < fd2);
114 ASSERT (is_mode (fd2, O_BINARY));
115 ASSERT (close (fd2) == 0);
116 set_binary_mode (fd, O_TEXT);
117 ASSERT (is_mode (fd, O_TEXT));
118 fd2 = dup_cloexec (fd);
119 ASSERT (fd < fd2);
120 ASSERT (is_mode (fd2, O_TEXT));
121 ASSERT (close (fd2) == 0);
122
123
124 errno = 0;
125 ASSERT (set_cloexec_flag (-1, false) == -1);
126 ASSERT (errno == EBADF);
127 errno = 0;
128 ASSERT (set_cloexec_flag (bad_fd, false) == -1);
129 ASSERT (errno == EBADF);
130 errno = 0;
131 ASSERT (set_cloexec_flag (fd2, false) == -1);
132 ASSERT (errno == EBADF);
133 errno = 0;
134 ASSERT (dup_cloexec (-1) == -1);
135 ASSERT (errno == EBADF);
136 errno = 0;
137 ASSERT (dup_cloexec (bad_fd) == -1);
138 ASSERT (errno == EBADF);
139 errno = 0;
140 ASSERT (dup_cloexec (fd2) == -1);
141 ASSERT (errno == EBADF);
142
143
144 ASSERT (close (fd) == 0);
145 ASSERT (unlink (file) == 0);
146
147 return 0;
148 }