openat: don’t close (-1)
[gnulib.git] / tests / test-dup-safer.c
blobec2cb908ba7e7ac3e438606ba5cc43f94701ce28
1 /* Test that dup_safer leaves standard fds alone.
2 Copyright (C) 2009-2024 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. */
19 #include <config.h>
21 #include "unistd--.h"
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <unistd.h>
28 #include "binary-io.h"
29 #include "cloexec.h"
31 #if defined _WIN32 && ! defined __CYGWIN__
32 /* Get declarations of the native Windows API functions. */
33 # define WIN32_LEAN_AND_MEAN
34 # include <windows.h>
35 /* Get _get_osfhandle. */
36 # if GNULIB_MSVC_NOTHROW
37 # include "msvc-nothrow.h"
38 # else
39 # include <io.h>
40 # endif
41 #endif
43 #if !O_BINARY
44 # define set_binary_mode my_set_binary_mode
45 static int
46 set_binary_mode (_GL_UNUSED int fd, _GL_UNUSED int mode)
48 return 0;
50 #endif
52 /* This test intentionally closes stderr. So, we arrange to have fd 10
53 (outside the range of interesting fd's during the test) set up to
54 duplicate the original stderr. */
56 #define BACKUP_STDERR_FILENO 10
57 #define ASSERT_STREAM myerr
58 #include "macros.h"
60 static FILE *myerr;
62 /* Return true if FD is open. */
63 static bool
64 is_open (int fd)
66 #if defined _WIN32 && ! defined __CYGWIN__
67 /* On native Windows, the initial state of unassigned standard file
68 descriptors is that they are open but point to an
69 INVALID_HANDLE_VALUE, and there is no fcntl. */
70 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
71 #else
72 # ifndef F_GETFL
73 # error Please port fcntl to your platform
74 # endif
75 return 0 <= fcntl (fd, F_GETFL);
76 #endif
79 /* Return true if FD is open and inheritable across exec/spawn. */
80 static bool
81 is_inheritable (int fd)
83 #if defined _WIN32 && ! defined __CYGWIN__
84 /* On native Windows, the initial state of unassigned standard file
85 descriptors is that they are open but point to an
86 INVALID_HANDLE_VALUE, and there is no fcntl. */
87 HANDLE h = (HANDLE) _get_osfhandle (fd);
88 DWORD flags;
89 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
90 return 0;
91 return (flags & HANDLE_FLAG_INHERIT) != 0;
92 #else
93 # ifndef F_GETFD
94 # error Please port fcntl to your platform
95 # endif
96 int i = fcntl (fd, F_GETFD);
97 return 0 <= i && (i & FD_CLOEXEC) == 0;
98 #endif
101 /* Return true if FD is open in the given MODE, which is either
102 O_TEXT or O_BINARY. */
103 static bool
104 is_mode (int fd, int mode)
106 int value = set_binary_mode (fd, O_BINARY);
107 set_binary_mode (fd, value);
108 return mode == value;
111 #define witness "test-dup-safer.txt"
114 main (void)
116 int i;
117 int fd;
118 int bad_fd = getdtablesize ();
120 /* We close fd 2 later, so save it in fd 10. */
121 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
122 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
123 return 2;
125 /* Create file for later checks. */
126 fd = creat (witness, 0600);
127 ASSERT (STDERR_FILENO < fd);
129 /* Four iterations, with progressively more standard descriptors
130 closed. */
131 for (i = -1; i <= STDERR_FILENO; i++)
133 if (0 <= i)
134 ASSERT (close (i) == 0);
136 /* Detect errors. */
137 errno = 0;
138 ASSERT (dup (-1) == -1);
139 ASSERT (errno == EBADF);
140 errno = 0;
141 ASSERT (dup (bad_fd) == -1);
142 ASSERT (errno == EBADF);
143 close (fd + 1);
144 errno = 0;
145 ASSERT (dup (fd + 1) == -1);
146 ASSERT (errno == EBADF);
148 /* Preserve text vs. binary. */
149 set_binary_mode (fd, O_BINARY);
150 ASSERT (dup (fd) == fd + 1);
151 ASSERT (is_open (fd + 1));
152 ASSERT (is_inheritable (fd + 1));
153 ASSERT (is_mode (fd + 1, O_BINARY));
155 ASSERT (close (fd + 1) == 0);
156 set_binary_mode (fd, O_TEXT);
157 ASSERT (dup (fd) == fd + 1);
158 ASSERT (is_open (fd + 1));
159 ASSERT (is_inheritable (fd + 1));
160 ASSERT (is_mode (fd + 1, O_TEXT));
162 /* Create cloexec copy. */
163 ASSERT (close (fd + 1) == 0);
164 ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
165 ASSERT (set_cloexec_flag (fd + 1, true) == 0);
166 ASSERT (is_open (fd + 1));
167 ASSERT (!is_inheritable (fd + 1));
168 ASSERT (close (fd) == 0);
170 /* dup always creates inheritable copies. Also, check that
171 earliest slot past std fds is used. */
172 ASSERT (dup (fd + 1) == fd);
173 ASSERT (is_open (fd));
174 ASSERT (is_inheritable (fd));
175 ASSERT (close (fd + 1) == 0);
178 /* Cleanup. */
179 ASSERT (close (fd) == 0);
180 ASSERT (unlink (witness) == 0);
182 return test_exit_status;