1 /* Test duplicating non-inheritable file descriptors.
2 Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 /* Get declarations of the Win32 API functions. */
31 # define WIN32_LEAN_AND_MEAN
35 #include "binary-io.h"
37 #define ASSERT(expr) \
42 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
49 /* Return non-zero if FD is open and inheritable across exec/spawn. */
51 is_inheritable (int fd)
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 HANDLE h = (HANDLE) _get_osfhandle (fd);
59 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
61 return (flags & HANDLE_FLAG_INHERIT) != 0;
64 # error Please port fcntl to your platform
66 int i = fcntl (fd, F_GETFD);
67 return 0 <= i && (i & FD_CLOEXEC) == 0;
72 # define setmode(f,m) zero ()
73 static int zero (void) { return 0; }
76 /* Return non-zero if FD is open in the given MODE, which is either
77 O_TEXT or O_BINARY. */
79 is_mode (int fd, int mode)
81 int value = setmode (fd, O_BINARY);
89 const char *file = "test-cloexec.tmp";
90 int fd = creat (file, 0600);
93 /* Assume std descriptors were provided by invoker. */
94 ASSERT (STDERR_FILENO < fd);
95 ASSERT (is_inheritable (fd));
97 /* Normal use of set_cloexec_flag. */
98 ASSERT (set_cloexec_flag (fd, true) == 0);
99 #if !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
100 ASSERT (!is_inheritable (fd));
102 ASSERT (set_cloexec_flag (fd, false) == 0);
103 ASSERT (is_inheritable (fd));
105 /* Normal use of dup_cloexec. */
106 fd2 = dup_cloexec (fd);
108 ASSERT (!is_inheritable (fd2));
109 ASSERT (close (fd) == 0);
110 ASSERT (dup_cloexec (fd2) == fd);
111 ASSERT (!is_inheritable (fd));
112 ASSERT (close (fd2) == 0);
114 /* On systems that distinguish between text and binary mode,
115 dup_cloexec reuses the mode of the source. */
116 setmode (fd, O_BINARY);
117 ASSERT (is_mode (fd, O_BINARY));
118 fd2 = dup_cloexec (fd);
120 ASSERT (is_mode (fd2, O_BINARY));
121 ASSERT (close (fd2) == 0);
122 setmode (fd, O_TEXT);
123 ASSERT (is_mode (fd, O_TEXT));
124 fd2 = dup_cloexec (fd);
126 ASSERT (is_mode (fd2, O_TEXT));
127 ASSERT (close (fd2) == 0);
129 /* Test error handling. */
131 ASSERT (set_cloexec_flag (-1, false) == -1);
132 ASSERT (errno == EBADF);
134 ASSERT (set_cloexec_flag (10000000, false) == -1);
135 ASSERT (errno == EBADF);
137 ASSERT (set_cloexec_flag (fd2, false) == -1);
138 ASSERT (errno == EBADF);
140 ASSERT (dup_cloexec (-1) == -1);
141 ASSERT (errno == EBADF);
143 ASSERT (dup_cloexec (10000000) == -1);
144 ASSERT (errno == EBADF);
146 ASSERT (dup_cloexec (fd2) == -1);
147 ASSERT (errno == EBADF);
150 ASSERT (close (fd) == 0);
151 ASSERT (unlink (file) == 0);