1 /* Test that dup_safer leaves standard fds alone.
2 Copyright (C) 2009-2017 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 # if GNULIB_MSVC_NOTHROW
38 # include "msvc-nothrow.h"
45 # define setmode(f,m) zero ()
46 static int zero (void) { return 0; }
49 /* This test intentionally closes stderr. So, we arrange to have fd 10
50 (outside the range of interesting fd's during the test) set up to
51 duplicate the original stderr. */
53 #define BACKUP_STDERR_FILENO 10
54 #define ASSERT_STREAM myerr
59 /* Return true if FD is open. */
63 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
64 /* On native Windows, the initial state of unassigned standard file
65 descriptors is that they are open but point to an
66 INVALID_HANDLE_VALUE, and there is no fcntl. */
67 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
70 # error Please port fcntl to your platform
72 return 0 <= fcntl (fd
, F_GETFL
);
76 /* Return true if FD is open and inheritable across exec/spawn. */
78 is_inheritable (int fd
)
80 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
81 /* On native Windows, the initial state of unassigned standard file
82 descriptors is that they are open but point to an
83 INVALID_HANDLE_VALUE, and there is no fcntl. */
84 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
86 if (h
== INVALID_HANDLE_VALUE
|| GetHandleInformation (h
, &flags
) == 0)
88 return (flags
& HANDLE_FLAG_INHERIT
) != 0;
91 # error Please port fcntl to your platform
93 int i
= fcntl (fd
, F_GETFD
);
94 return 0 <= i
&& (i
& FD_CLOEXEC
) == 0;
98 /* Return true if FD is open in the given MODE, which is either
99 O_TEXT or O_BINARY. */
101 is_mode (int fd
, int mode
)
103 int value
= setmode (fd
, O_BINARY
);
105 return mode
== value
;
108 #define witness "test-dup-safer.txt"
115 int bad_fd
= getdtablesize ();
117 /* We close fd 2 later, so save it in fd 10. */
118 if (dup2 (STDERR_FILENO
, BACKUP_STDERR_FILENO
) != BACKUP_STDERR_FILENO
119 || (myerr
= fdopen (BACKUP_STDERR_FILENO
, "w")) == NULL
)
122 /* Create file for later checks. */
123 fd
= creat (witness
, 0600);
124 ASSERT (STDERR_FILENO
< fd
);
126 /* Four iterations, with progressively more standard descriptors
128 for (i
= -1; i
<= STDERR_FILENO
; i
++)
131 ASSERT (close (i
) == 0);
135 ASSERT (dup (-1) == -1);
136 ASSERT (errno
== EBADF
);
138 ASSERT (dup (bad_fd
) == -1);
139 ASSERT (errno
== EBADF
);
142 ASSERT (dup (fd
+ 1) == -1);
143 ASSERT (errno
== EBADF
);
145 /* Preserve text vs. binary. */
146 setmode (fd
, O_BINARY
);
147 ASSERT (dup (fd
) == fd
+ 1);
148 ASSERT (is_open (fd
+ 1));
149 ASSERT (is_inheritable (fd
+ 1));
150 ASSERT (is_mode (fd
+ 1, O_BINARY
));
152 ASSERT (close (fd
+ 1) == 0);
153 setmode (fd
, O_TEXT
);
154 ASSERT (dup (fd
) == fd
+ 1);
155 ASSERT (is_open (fd
+ 1));
156 ASSERT (is_inheritable (fd
+ 1));
157 ASSERT (is_mode (fd
+ 1, O_TEXT
));
159 /* Create cloexec copy. */
160 ASSERT (close (fd
+ 1) == 0);
161 ASSERT (fd_safer_flag (dup_cloexec (fd
), O_CLOEXEC
) == fd
+ 1);
162 ASSERT (set_cloexec_flag (fd
+ 1, true) == 0);
163 ASSERT (is_open (fd
+ 1));
164 ASSERT (!is_inheritable (fd
+ 1));
165 ASSERT (close (fd
) == 0);
167 /* dup always creates inheritable copies. Also, check that
168 earliest slot past std fds is used. */
169 ASSERT (dup (fd
+ 1) == fd
);
170 ASSERT (is_open (fd
));
171 ASSERT (is_inheritable (fd
));
172 ASSERT (close (fd
+ 1) == 0);
176 ASSERT (close (fd
) == 0);
177 ASSERT (unlink (witness
) == 0);