1
/* Test that dup_safer leaves standard fds alone.
2
Copyright (C) 2009, 2010 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 Win32 API functions. */
33
# define WIN32_LEAN_AND_MEAN
38
# define setmode(f,m) zero ()
39
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 Win32, 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 Win32, 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"
112
/* We close fd 2 later, so save it in fd 10. */
113
if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
114
|| (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
117
/* Create file for later checks. */
118
fd = creat (witness, 0600);
119
ASSERT (STDERR_FILENO < fd);
121
/* Four iterations, with progressively more standard descriptors
123
for (i = -1; i <= STDERR_FILENO; i++)
126
ASSERT (close (i) == 0);
130
ASSERT (dup (-1) == -1);
131
ASSERT (errno == EBADF);
133
ASSERT (dup (10000000) == -1);
134
ASSERT (errno == EBADF);
137
ASSERT (dup (fd + 1) == -1);
138
ASSERT (errno == EBADF);
140
/* Preserve text vs. binary. */
141
setmode (fd, O_BINARY);
142
ASSERT (dup (fd) == fd + 1);
143
ASSERT (is_open (fd + 1));
144
ASSERT (is_inheritable (fd + 1));
145
ASSERT (is_mode (fd + 1, O_BINARY));
147
ASSERT (close (fd + 1) == 0);
148
setmode (fd, O_TEXT);
149
ASSERT (dup (fd) == fd + 1);
150
ASSERT (is_open (fd + 1));
151
ASSERT (is_inheritable (fd + 1));
152
ASSERT (is_mode (fd + 1, O_TEXT));
154
/* Create cloexec copy. */
155
ASSERT (close (fd + 1) == 0);
156
ASSERT (fd_safer_flag (dup_cloexec (fd), O_CLOEXEC) == fd + 1);
157
ASSERT (set_cloexec_flag (fd + 1, true) == 0);
158
ASSERT (is_open (fd + 1));
159
ASSERT (!is_inheritable (fd + 1));
160
ASSERT (close (fd) == 0);
162
/* dup always creates inheritable copies. Also, check that
163
earliest slot past std fds is used. */
164
ASSERT (dup (fd + 1) == fd);
165
ASSERT (is_open (fd));
166
ASSERT (is_inheritable (fd));
167
ASSERT (close (fd + 1) == 0);
171
ASSERT (close (fd) == 0);
172
ASSERT (unlink (witness) == 0);