1 /* Test that dup_safer leaves standard fds alone.
2 Copyright (C) 2009-2012 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. */
28 #include "binary-io.h"
31 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
32 /* Get declarations of the native Windows API functions. */
33 # define WIN32_LEAN_AND_MEAN
35 /* Get _get_osfhandle. */
36 # include "msvc-nothrow.h"
40 # define setmode(f,m) zero ()
41 static int zero (void) { return 0; }
44 /* This test intentionally closes stderr. So, we arrange to have fd 10
45 (outside the range of interesting fd's during the test) set up to
46 duplicate the original stderr. */
48 #define BACKUP_STDERR_FILENO 10
49 #define ASSERT_STREAM myerr
54 /* Return true if FD is open. */
58 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
59 /* On native Windows, the initial state of unassigned standard file
60 descriptors is that they are open but point to an
61 INVALID_HANDLE_VALUE, and there is no fcntl. */
62 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
65 # error Please port fcntl to your platform
67 return 0 <= fcntl (fd, F_GETFL);
71 /* Return true if FD is open and inheritable across exec/spawn. */
73 is_inheritable (int fd)
75 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
76 /* On native Windows, the initial state of unassigned standard file
77 descriptors is that they are open but point to an
78 INVALID_HANDLE_VALUE, and there is no fcntl. */
79 HANDLE h = (HANDLE) _get_osfhandle (fd);
81 if (h == INVALID_HANDLE_VALUE || GetHandleInformation (h, &flags) == 0)
83 return (flags & HANDLE_FLAG_INHERIT) != 0;
86 # error Please port fcntl to your platform
88 int i = fcntl (fd, F_GETFD);
89 return 0 <= i && (i & FD_CLOEXEC) == 0;
93 /* Return true if FD is open in the given MODE, which is either
94 O_TEXT or O_BINARY. */
96 is_mode (int fd, int mode)
98 int value = setmode (fd, O_BINARY);
100 return mode == value;
103 #define witness "test-dup-safer.txt"
111 /* We close fd 2 later, so save it in fd 10. */
112 if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
113 || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
116 /* Create file for later checks. */
117 fd = creat (witness, 0600);
118 ASSERT (STDERR_FILENO < fd);
120 /* Four iterations, with progressively more standard descriptors
122 for (i = -1; i <= STDERR_FILENO; i++)
125 ASSERT (close (i) == 0);
129 ASSERT (dup (-1) == -1);
130 ASSERT (errno == EBADF);
132 ASSERT (dup (10000000) == -1);
133 ASSERT (errno == EBADF);
136 ASSERT (dup (fd + 1) == -1);
137 ASSERT (errno == EBADF);
139 /* Preserve text vs. binary. */
140 setmode (fd, O_BINARY);
141 ASSERT (dup (fd) == fd + 1);
142 ASSERT (is_open (fd + 1));
143 ASSERT (is_inheritable (fd + 1));
144 ASSERT (is_mode (fd + 1, O_BINARY));
146 ASSERT (close (fd + 1) == 0);
147 setmode (fd, O_TEXT);
148 ASSERT (dup (fd) == fd + 1);
149 ASSERT (is_open (fd + 1));
150 ASSERT (is_inheritable (fd + 1));
151 ASSERT (is_mode (fd + 1, O_TEXT));
153 /* Create cloexec copy. */
154 ASSERT (close (fd + 1) == 0);
155 ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
156 ASSERT (set_cloexec_flag (fd + 1, true) == 0);
157 ASSERT (is_open (fd + 1));
158 ASSERT (!is_inheritable (fd + 1));
159 ASSERT (close (fd) == 0);
161 /* dup always creates inheritable copies. Also, check that
162 earliest slot past std fds is used. */
163 ASSERT (dup (fd + 1) == fd);
164 ASSERT (is_open (fd));
165 ASSERT (is_inheritable (fd));
166 ASSERT (close (fd + 1) == 0);
170 ASSERT (close (fd) == 0);
171 ASSERT (unlink (witness) == 0);