1 /* Test duplicating file descriptors.
2 Copyright (C) 2009-2025 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 <https://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));
30 #if defined _WIN32 && ! defined __CYGWIN__
31 /* Get declarations of the native Windows API functions. */
32 # define WIN32_LEAN_AND_MEAN
34 /* Get _get_osfhandle. */
35 # if GNULIB_MSVC_NOTHROW
36 # include "msvc-nothrow.h"
42 #include "binary-io.h"
45 /* Return true if FD is open. */
49 #if defined _WIN32 && ! defined __CYGWIN__
50 /* On native Windows, the initial state of unassigned standard file
51 descriptors is that they are open but point to an
52 INVALID_HANDLE_VALUE, and there is no fcntl. */
53 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
56 # error Please port fcntl to your platform
58 return 0 <= fcntl (fd
, F_GETFL
);
62 /* Return true if FD is not inherited to child processes. */
66 #if defined _WIN32 && ! defined __CYGWIN__
67 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
69 ASSERT (GetHandleInformation (h
, &flags
));
70 return (flags
& HANDLE_FLAG_INHERIT
) == 0;
73 ASSERT ((flags
= fcntl (fd
, F_GETFD
)) >= 0);
74 return (flags
& FD_CLOEXEC
) != 0;
82 int bad_fd
= getdtablesize ();
85 for (use_cloexec
= 0; use_cloexec
<= 1; use_cloexec
++)
90 const char *file
= "test-dup3.tmp";
91 int fd
= open (file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
101 /* Assume std descriptors were provided by invoker. */
102 ASSERT (STDERR_FILENO
< fd
);
103 ASSERT (is_open (fd
));
104 /* Ignore any other fd's leaked into this process. */
107 ASSERT (!is_open (fd
+ 1));
108 ASSERT (!is_open (fd
+ 2));
110 /* Assigning to self is invalid. */
112 ASSERT (dup3 (fd
, fd
, o_flags
) == -1);
113 ASSERT (errno
== EINVAL
);
114 ASSERT (is_open (fd
));
116 /* If the source is not open, then the destination is unaffected. */
118 ASSERT (dup3 (fd
+ 1, fd
+ 2, o_flags
) == -1);
119 ASSERT (errno
== EBADF
);
120 ASSERT (!is_open (fd
+ 2));
122 ASSERT (dup3 (fd
+ 1, fd
, o_flags
) == -1);
123 ASSERT (errno
== EBADF
);
124 ASSERT (is_open (fd
));
126 /* The destination must be valid. */
128 ASSERT (dup3 (fd
, -2, o_flags
) == -1);
129 ASSERT (errno
== EBADF
);
132 ASSERT (dup3 (fd
, 255, 0) == 255);
133 ASSERT (dup3 (fd
, 256, 0) == 256);
134 ASSERT (close (255) == 0);
135 ASSERT (close (256) == 0);
137 ASSERT (dup3 (fd
, bad_fd
- 1, 0) == bad_fd
- 1);
138 ASSERT (close (bad_fd
- 1) == 0);
140 ASSERT (dup3 (fd
, bad_fd
, o_flags
) == -1);
141 ASSERT (errno
== EBADF
);
143 /* Using dup3 can skip fds. */
144 ASSERT (dup3 (fd
, fd
+ 2, o_flags
) == fd
+ 2);
145 ASSERT (is_open (fd
));
146 ASSERT (!is_open (fd
+ 1));
147 ASSERT (is_open (fd
+ 2));
149 ASSERT (is_cloexec (fd
+ 2));
151 ASSERT (!is_cloexec (fd
+ 2));
153 /* Verify that dup3 closes the previous occupant of a fd. */
154 ASSERT (open ("/dev/null", O_WRONLY
, 0600) == fd
+ 1);
155 ASSERT (dup3 (fd
+ 1, fd
, o_flags
) == fd
);
156 ASSERT (close (fd
+ 1) == 0);
157 ASSERT (write (fd
, "1", 1) == 1);
158 ASSERT (dup3 (fd
+ 2, fd
, o_flags
) == fd
);
159 ASSERT (lseek (fd
, 0, SEEK_END
) == 0);
160 ASSERT (write (fd
+ 2, "2", 1) == 1);
161 ASSERT (lseek (fd
, 0, SEEK_SET
) == 0);
162 ASSERT (read (fd
, buffer
, 1) == 1);
163 ASSERT (*buffer
== '2');
166 ASSERT (close (fd
+ 2) == 0);
167 ASSERT (close (fd
) == 0);
168 ASSERT (unlink (file
) == 0);
171 return test_exit_status
;