1 /* Test duplicating non-inheritable file descriptors.
2 Copyright (C) 2009-2020 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Get declarations of the native Windows API functions. */
29 # define WIN32_LEAN_AND_MEAN
31 /* Get _get_osfhandle. */
32 # if GNULIB_MSVC_NOTHROW
33 # include "msvc-nothrow.h"
39 #include "binary-io.h"
42 /* Return non-zero if FD is open and inheritable across exec/spawn. */
44 is_inheritable (int fd
)
46 #if defined _WIN32 && ! defined __CYGWIN__
47 /* On native Windows, the initial state of unassigned standard file
48 descriptors is that they are open but point to an
49 INVALID_HANDLE_VALUE, and there is no fcntl. */
50 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
52 if (h
== INVALID_HANDLE_VALUE
|| GetHandleInformation (h
, &flags
) == 0)
54 return (flags
& HANDLE_FLAG_INHERIT
) != 0;
57 # error Please port fcntl to your platform
59 int i
= fcntl (fd
, F_GETFD
);
60 return 0 <= i
&& (i
& FD_CLOEXEC
) == 0;
65 # define set_binary_mode(f,m) zero ()
66 static int zero (void) { return 0; }
69 /* Return non-zero if FD is open in the given MODE, which is either
70 O_TEXT or O_BINARY. */
72 is_mode (int fd
, int mode
)
74 int value
= set_binary_mode (fd
, O_BINARY
);
75 set_binary_mode (fd
, value
);
82 const char *file
= "test-cloexec.tmp";
83 int fd
= creat (file
, 0600);
85 int bad_fd
= getdtablesize ();
87 /* Assume std descriptors were provided by invoker. */
88 ASSERT (STDERR_FILENO
< fd
);
89 ASSERT (is_inheritable (fd
));
91 /* Normal use of set_cloexec_flag. */
92 ASSERT (set_cloexec_flag (fd
, true) == 0);
93 #if !(defined _WIN32 && ! defined __CYGWIN__)
94 ASSERT (!is_inheritable (fd
));
96 ASSERT (set_cloexec_flag (fd
, false) == 0);
97 ASSERT (is_inheritable (fd
));
99 /* Normal use of dup_cloexec. */
100 fd2
= dup_cloexec (fd
);
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);
108 /* On systems that distinguish between text and binary mode,
109 dup_cloexec reuses the mode of the source. */
110 set_binary_mode (fd
, O_BINARY
);
111 ASSERT (is_mode (fd
, O_BINARY
));
112 fd2
= dup_cloexec (fd
);
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
);
120 ASSERT (is_mode (fd2
, O_TEXT
));
121 ASSERT (close (fd2
) == 0);
123 /* Test error handling. */
125 ASSERT (set_cloexec_flag (-1, false) == -1);
126 ASSERT (errno
== EBADF
);
128 ASSERT (set_cloexec_flag (bad_fd
, false) == -1);
129 ASSERT (errno
== EBADF
);
131 ASSERT (set_cloexec_flag (fd2
, false) == -1);
132 ASSERT (errno
== EBADF
);
134 ASSERT (dup_cloexec (-1) == -1);
135 ASSERT (errno
== EBADF
);
137 ASSERT (dup_cloexec (bad_fd
) == -1);
138 ASSERT (errno
== EBADF
);
140 ASSERT (dup_cloexec (fd2
) == -1);
141 ASSERT (errno
== EBADF
);
144 ASSERT (close (fd
) == 0);
145 ASSERT (unlink (file
) == 0);