doc: Mention the byteswap module in function documentation.
[gnulib.git] / tests / test-dup3.c
blob738046359b6fbdb2bc3ada204104384286336c0d
1 /* Test duplicating file descriptors.
2 Copyright (C) 2009-2024 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. */
20 #include <config.h>
22 #include <unistd.h>
24 #include "signature.h"
25 SIGNATURE_CHECK (dup3, int, (int, int, int));
27 #include <errno.h>
28 #include <fcntl.h>
30 #if defined _WIN32 && ! defined __CYGWIN__
31 /* Get declarations of the native Windows API functions. */
32 # define WIN32_LEAN_AND_MEAN
33 # include <windows.h>
34 /* Get _get_osfhandle. */
35 # if GNULIB_MSVC_NOTHROW
36 # include "msvc-nothrow.h"
37 # else
38 # include <io.h>
39 # endif
40 #endif
42 #include "binary-io.h"
43 #include "macros.h"
45 /* Return true if FD is open. */
46 static bool
47 is_open (int fd)
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;
54 #else
55 # ifndef F_GETFL
56 # error Please port fcntl to your platform
57 # endif
58 return 0 <= fcntl (fd, F_GETFL);
59 #endif
62 /* Return true if FD is not inherited to child processes. */
63 static bool
64 is_cloexec (int fd)
66 #if defined _WIN32 && ! defined __CYGWIN__
67 HANDLE h = (HANDLE) _get_osfhandle (fd);
68 DWORD flags;
69 ASSERT (GetHandleInformation (h, &flags));
70 return (flags & HANDLE_FLAG_INHERIT) == 0;
71 #else
72 int flags;
73 ASSERT ((flags = fcntl (fd, F_GETFD)) >= 0);
74 return (flags & FD_CLOEXEC) != 0;
75 #endif
78 int
79 main ()
81 int use_cloexec;
82 int bad_fd = getdtablesize ();
84 #if O_CLOEXEC
85 for (use_cloexec = 0; use_cloexec <= 1; use_cloexec++)
86 #else
87 use_cloexec = 0;
88 #endif
90 const char *file = "test-dup3.tmp";
91 int fd = open (file, O_CREAT | O_TRUNC | O_RDWR, 0600);
92 int o_flags;
93 char buffer[1];
95 o_flags = 0;
96 #if O_CLOEXEC
97 if (use_cloexec)
98 o_flags |= O_CLOEXEC;
99 #endif
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. */
105 close (fd + 1);
106 close (fd + 2);
107 ASSERT (!is_open (fd + 1));
108 ASSERT (!is_open (fd + 2));
110 /* Assigning to self is invalid. */
111 errno = 0;
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. */
117 errno = 0;
118 ASSERT (dup3 (fd + 1, fd + 2, o_flags) == -1);
119 ASSERT (errno == EBADF);
120 ASSERT (!is_open (fd + 2));
121 errno = 0;
122 ASSERT (dup3 (fd + 1, fd, o_flags) == -1);
123 ASSERT (errno == EBADF);
124 ASSERT (is_open (fd));
126 /* The destination must be valid. */
127 errno = 0;
128 ASSERT (dup3 (fd, -2, o_flags) == -1);
129 ASSERT (errno == EBADF);
130 if (bad_fd > 256)
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);
139 errno = 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));
148 if (use_cloexec)
149 ASSERT (is_cloexec (fd + 2));
150 else
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');
165 /* Clean up. */
166 ASSERT (close (fd + 2) == 0);
167 ASSERT (close (fd) == 0);
168 ASSERT (unlink (file) == 0);
171 return test_exit_status;