1 /* Test duplicating file descriptors.
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,
18 and Bruno Haible <bruno@clisp.org>, 2009. */
24 #include "signature.h"
25 SIGNATURE_CHECK (dup3
, int, (int, int, int));
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"
39 #include "binary-io.h"
42 /* Return true if FD is open. */
46 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
47 /* On native Windows, the initial state of unassigned standard file
48 descriptors is that they are open but point to an
49 INVALID_HANDLE_VALUE, and there is no fcntl. */
50 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
53 # error Please port fcntl to your platform
55 return 0 <= fcntl (fd
, F_GETFL
);
59 /* Return true if FD is not inherited to child processes. */
63 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
64 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
66 ASSERT (GetHandleInformation (h
, &flags
));
67 return (flags
& HANDLE_FLAG_INHERIT
) == 0;
70 ASSERT ((flags
= fcntl (fd
, F_GETFD
)) >= 0);
71 return (flags
& FD_CLOEXEC
) != 0;
79 int bad_fd
= getdtablesize ();
82 for (use_cloexec
= 0; use_cloexec
<= 1; use_cloexec
++)
87 const char *file
= "test-dup3.tmp";
88 int fd
= open (file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
98 /* Assume std descriptors were provided by invoker. */
99 ASSERT (STDERR_FILENO
< fd
);
100 ASSERT (is_open (fd
));
101 /* Ignore any other fd's leaked into this process. */
104 ASSERT (!is_open (fd
+ 1));
105 ASSERT (!is_open (fd
+ 2));
107 /* Assigning to self is invalid. */
109 ASSERT (dup3 (fd
, fd
, o_flags
) == -1);
110 ASSERT (errno
== EINVAL
);
111 ASSERT (is_open (fd
));
113 /* If the source is not open, then the destination is unaffected. */
115 ASSERT (dup3 (fd
+ 1, fd
+ 2, o_flags
) == -1);
116 ASSERT (errno
== EBADF
);
117 ASSERT (!is_open (fd
+ 2));
119 ASSERT (dup3 (fd
+ 1, fd
, o_flags
) == -1);
120 ASSERT (errno
== EBADF
);
121 ASSERT (is_open (fd
));
123 /* The destination must be valid. */
125 ASSERT (dup3 (fd
, -2, o_flags
) == -1);
126 ASSERT (errno
== EBADF
);
129 ASSERT (dup3 (fd
, 255, 0) == 255);
130 ASSERT (dup3 (fd
, 256, 0) == 256);
131 ASSERT (close (255) == 0);
132 ASSERT (close (256) == 0);
134 ASSERT (dup3 (fd
, bad_fd
- 1, 0) == bad_fd
- 1);
135 ASSERT (close (bad_fd
- 1) == 0);
137 ASSERT (dup3 (fd
, bad_fd
, o_flags
) == -1);
138 ASSERT (errno
== EBADF
);
140 /* Using dup3 can skip fds. */
141 ASSERT (dup3 (fd
, fd
+ 2, o_flags
) == fd
+ 2);
142 ASSERT (is_open (fd
));
143 ASSERT (!is_open (fd
+ 1));
144 ASSERT (is_open (fd
+ 2));
146 ASSERT (is_cloexec (fd
+ 2));
148 ASSERT (!is_cloexec (fd
+ 2));
150 /* Verify that dup3 closes the previous occupant of a fd. */
151 ASSERT (open ("/dev/null", O_WRONLY
, 0600) == fd
+ 1);
152 ASSERT (dup3 (fd
+ 1, fd
, o_flags
) == fd
);
153 ASSERT (close (fd
+ 1) == 0);
154 ASSERT (write (fd
, "1", 1) == 1);
155 ASSERT (dup3 (fd
+ 2, fd
, o_flags
) == fd
);
156 ASSERT (lseek (fd
, 0, SEEK_END
) == 0);
157 ASSERT (write (fd
+ 2, "2", 1) == 1);
158 ASSERT (lseek (fd
, 0, SEEK_SET
) == 0);
159 ASSERT (read (fd
, buffer
, 1) == 1);
160 ASSERT (*buffer
== '2');
163 ASSERT (close (fd
+ 2) == 0);
164 ASSERT (close (fd
) == 0);
165 ASSERT (unlink (file
) == 0);