1 /* Test duplicating file descriptors.
2 Copyright (C) 2009-2020 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));
31 #if defined _WIN32 && ! defined __CYGWIN__
32 /* Get declarations of the native Windows API functions. */
33 # define WIN32_LEAN_AND_MEAN
35 /* Get _get_osfhandle. */
36 # if GNULIB_MSVC_NOTHROW
37 # include "msvc-nothrow.h"
43 #include "binary-io.h"
46 /* Return true if FD is open. */
50 #if defined _WIN32 && ! defined __CYGWIN__
51 /* On native Windows, the initial state of unassigned standard file
52 descriptors is that they are open but point to an
53 INVALID_HANDLE_VALUE, and there is no fcntl. */
54 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
57 # error Please port fcntl to your platform
59 return 0 <= fcntl (fd
, F_GETFL
);
63 /* Return true if FD is not inherited to child processes. */
67 #if defined _WIN32 && ! defined __CYGWIN__
68 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
70 ASSERT (GetHandleInformation (h
, &flags
));
71 return (flags
& HANDLE_FLAG_INHERIT
) == 0;
74 ASSERT ((flags
= fcntl (fd
, F_GETFD
)) >= 0);
75 return (flags
& FD_CLOEXEC
) != 0;
83 int bad_fd
= getdtablesize ();
86 for (use_cloexec
= 0; use_cloexec
<= 1; use_cloexec
++)
91 const char *file
= "test-dup3.tmp";
92 int fd
= open (file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
102 /* Assume std descriptors were provided by invoker. */
103 ASSERT (STDERR_FILENO
< fd
);
104 ASSERT (is_open (fd
));
105 /* Ignore any other fd's leaked into this process. */
108 ASSERT (!is_open (fd
+ 1));
109 ASSERT (!is_open (fd
+ 2));
111 /* Assigning to self is invalid. */
113 ASSERT (dup3 (fd
, fd
, o_flags
) == -1);
114 ASSERT (errno
== EINVAL
);
115 ASSERT (is_open (fd
));
117 /* If the source is not open, then the destination is unaffected. */
119 ASSERT (dup3 (fd
+ 1, fd
+ 2, o_flags
) == -1);
120 ASSERT (errno
== EBADF
);
121 ASSERT (!is_open (fd
+ 2));
123 ASSERT (dup3 (fd
+ 1, fd
, o_flags
) == -1);
124 ASSERT (errno
== EBADF
);
125 ASSERT (is_open (fd
));
127 /* The destination must be valid. */
129 ASSERT (dup3 (fd
, -2, o_flags
) == -1);
130 ASSERT (errno
== EBADF
);
133 ASSERT (dup3 (fd
, 255, 0) == 255);
134 ASSERT (dup3 (fd
, 256, 0) == 256);
135 ASSERT (close (255) == 0);
136 ASSERT (close (256) == 0);
138 ASSERT (dup3 (fd
, bad_fd
- 1, 0) == bad_fd
- 1);
139 ASSERT (close (bad_fd
- 1) == 0);
141 ASSERT (dup3 (fd
, bad_fd
, o_flags
) == -1);
142 ASSERT (errno
== EBADF
);
144 /* Using dup3 can skip fds. */
145 ASSERT (dup3 (fd
, fd
+ 2, o_flags
) == fd
+ 2);
146 ASSERT (is_open (fd
));
147 ASSERT (!is_open (fd
+ 1));
148 ASSERT (is_open (fd
+ 2));
150 ASSERT (is_cloexec (fd
+ 2));
152 ASSERT (!is_cloexec (fd
+ 2));
154 /* Verify that dup3 closes the previous occupant of a fd. */
155 ASSERT (open ("/dev/null", O_WRONLY
, 0600) == fd
+ 1);
156 ASSERT (dup3 (fd
+ 1, fd
, o_flags
) == fd
);
157 ASSERT (close (fd
+ 1) == 0);
158 ASSERT (write (fd
, "1", 1) == 1);
159 ASSERT (dup3 (fd
+ 2, fd
, o_flags
) == fd
);
160 ASSERT (lseek (fd
, 0, SEEK_END
) == 0);
161 ASSERT (write (fd
+ 2, "2", 1) == 1);
162 ASSERT (lseek (fd
, 0, SEEK_SET
) == 0);
163 ASSERT (read (fd
, buffer
, 1) == 1);
164 ASSERT (*buffer
== '2');
167 ASSERT (close (fd
+ 2) == 0);
168 ASSERT (close (fd
) == 0);
169 ASSERT (unlink (file
) == 0);