1 /* Test that dup_safer leaves standard fds alone.
2 Copyright (C) 2009-2013 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 #include "binary-io.h"
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 /* Get declarations of the native Windows API functions. */
34 # define WIN32_LEAN_AND_MEAN
36 /* Get _get_osfhandle. */
37 # include "msvc-nothrow.h"
41 # define setmode(f,m) zero ()
42 static int zero (void) { return 0; }
45 /* This test intentionally closes stderr. So, we arrange to have fd 10
46 (outside the range of interesting fd's during the test) set up to
47 duplicate the original stderr. */
49 #define BACKUP_STDERR_FILENO 10
50 #define ASSERT_STREAM myerr
55 /* Return true if FD is open. */
59 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
60 /* On native Windows, the initial state of unassigned standard file
61 descriptors is that they are open but point to an
62 INVALID_HANDLE_VALUE, and there is no fcntl. */
63 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
66 # error Please port fcntl to your platform
68 return 0 <= fcntl (fd, F_GETFL);
72 /* Return true if FD is open and inheritable across exec/spawn. */
74 is_inheritable (int fd)
76 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
77 /* On native Windows, the initial state of unassigned standard file
78 descriptors is that they are open but point to an
79 INVALID_HANDLE_VALUE, and there is no fcntl. */
80 HANDLE h = (HANDLE) _get_osfhandle (fd);
82 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
84 return (flags & HANDLE_FLAG_INHERIT) != 0;
87 # error Please port fcntl to your platform
89 int i = fcntl (fd, F_GETFD);
90 return 0 <= i && (i & FD_CLOEXEC) == 0;
94 /* Return true if FD is open in the given MODE, which is either
95 O_TEXT or O_BINARY. */
97 is_mode (int fd, int mode)
99 int value = setmode (fd, O_BINARY);
101 return mode == value;
104 #define witness "test-dup-safer.txt"
111 int bad_fd = getdtablesize ();
113 /* We close fd 2 later, so save it in fd 10. */
114 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
115 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
118 /* Create file for later checks. */
119 fd = creat (witness, 0600);
120 ASSERT (STDERR_FILENO < fd);
122 /* Four iterations, with progressively more standard descriptors
124 for (i = -1; i <= STDERR_FILENO; i++)
127 ASSERT (close (i) == 0);
131 ASSERT (dup (-1) == -1);
132 ASSERT (errno == EBADF);
134 ASSERT (dup (bad_fd) == -1);
135 ASSERT (errno == EBADF);
138 ASSERT (dup (fd + 1) == -1);
139 ASSERT (errno == EBADF);
141 /* Preserve text vs. binary. */
142 setmode (fd, O_BINARY);
143 ASSERT (dup (fd) == fd + 1);
144 ASSERT (is_open (fd + 1));
145 ASSERT (is_inheritable (fd + 1));
146 ASSERT (is_mode (fd + 1, O_BINARY));
148 ASSERT (close (fd + 1) == 0);
149 setmode (fd, O_TEXT);
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_TEXT));
155 /* Create cloexec copy. */
156 ASSERT (close (fd + 1) == 0);
157 ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
158 ASSERT (set_cloexec_flag (fd + 1, true) == 0);
159 ASSERT (is_open (fd + 1));
160 ASSERT (!is_inheritable (fd + 1));
161 ASSERT (close (fd) == 0);
163 /* dup always creates inheritable copies. Also, check that
164 earliest slot past std fds is used. */
165 ASSERT (dup (fd + 1) == fd);
166 ASSERT (is_open (fd));
167 ASSERT (is_inheritable (fd));
168 ASSERT (close (fd + 1) == 0);
172 ASSERT (close (fd) == 0);
173 ASSERT (unlink (witness) == 0);