Debian Codegraph @

lib/dup2.c

raw
1 /* Duplicate an open file descriptor to a specified file descriptor.
2
3 Copyright (C) 1999, 2004-2007, 2009-2026 Free Software Foundation, Inc.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* written by Paul Eggert */
19
20 #include <config.h>
21
22 /* Specification. */
23 #include <unistd.h>
24
25 #include <errno.h>
26 #include <fcntl.h>
27
28 #undef dup2
29
30 #if defined _WIN32 && ! defined __CYGWIN__
31
32 /* Get declarations of the native Windows API functions. */
33 # define WIN32_LEAN_AND_MEAN
34 # include <windows.h>
35
36 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
37 # include "msvc-inval.h"
38 # endif
39
40 /* Get _get_osfhandle. */
41 # if GNULIB_MSVC_NOTHROW
42 # include "msvc-nothrow.h"
43 # else
44 # include <io.h>
45 # endif
46
47 # if HAVE_MSVC_INVALID_PARAMETER_HANDLER
48 static int
49 dup2_nothrow (int fd, int desired_fd)
50 {
51 int result;
52
53 TRY_MSVC_INVAL
54 {
55 result = _dup2 (fd, desired_fd);
56 }
57 CATCH_MSVC_INVAL
58 {
59 errno = EBADF;
60 result = -1;
61 }
62 DONE_MSVC_INVAL;
63
64 return result;
65 }
66 # else
67 # define dup2_nothrow _dup2
68 # endif
69
70 static int
71 ms_windows_dup2 (int fd, int desired_fd)
72 {
73 /* If fd is closed, mingw hangs on dup2 (fd, fd). If fd is open,
74 dup2 (fd, fd) returns 0, but all further attempts to use fd in
75 future dup2 calls will hang. */
76 if (fd == desired_fd)
77 {
78 if ((HANDLE) _get_osfhandle (fd) == INVALID_HANDLE_VALUE)
79 {
80 errno = EBADF;
81 return -1;
82 }
83 return fd;
84 }
85
86 /* Wine 1.0.1 return 0 when desired_fd is negative but not -1:
87 https://bugs.winehq.org/show_bug.cgi?id=21289 */
88 if (desired_fd < 0)
89 {
90 errno = EBADF;
91 return -1;
92 }
93
94 int result = dup2_nothrow (fd, desired_fd);
95
96 if (result == 0)
97 result = desired_fd;
98
99 return result;
100 }
101
102 # define dup2 ms_windows_dup2
103
104 #elif defined __KLIBC__
105
106 # include <InnoTekLIBC/backend.h>
107
108 static int
109 klibc_dup2dirfd (int fd, int desired_fd)
110 {
111 int tempfd = open ("NUL", O_RDONLY);
112 if (tempfd < 0)
113 return tempfd;
114
115 if (tempfd >= desired_fd)
116 {
117 close (tempfd);
118
119 char path[_MAX_PATH];
120 if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
121 return -1;
122
123 for (;;)
124 {
125 close (desired_fd);
126
127 int dupfd = open (path, O_RDONLY);
128 if (dupfd < 0)
129 return dupfd;
130
131 if (dupfd == desired_fd)
132 return dupfd;
133
134 /* If lower FD was closed by other threads, fill again. */
135 if (dupfd < desired_fd)
136 {
137 tempfd = dupfd;
138 break;
139 }
140
141 /* desired_fd was opened by other threads. Try again. */
142 /* FIXME: Closing desired_fd opened by other threads may lead to
143 unexpected behavior. */
144 close (dupfd);
145 }
146 }
147
148 int dupfd = klibc_dup2dirfd (fd, desired_fd);
149
150 close (tempfd);
151
152 return dupfd;
153 }
154
155 static int
156 klibc_dup2 (int fd, int desired_fd)
157 {
158 int dupfd = dup2 (fd, desired_fd);
159 if (dupfd < 0 && errno == ENOTSUP)
160 {
161 struct stat sbuf;
162 if (!fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
163 {
164 close (desired_fd);
165
166 return klibc_dup2dirfd (fd, desired_fd);
167 }
168 }
169
170 return dupfd;
171 }
172
173 # define dup2 klibc_dup2
174 #endif
175
176 int
177 rpl_dup2 (int fd, int desired_fd)
178 {
179 #ifdef F_GETFL
180 /* On Linux kernels 2.6.26-2.6.29, dup2 (fd, fd) returns -EBADF.
181 On Cygwin 1.5.x, dup2 (1, 1) returns 0.
182 On Cygwin 1.7.17, dup2 (1, -1) dumps core.
183 On Cygwin 1.7.25, dup2 (1, 256) can dump core.
184 On Haiku, dup2 (fd, fd) mistakenly clears FD_CLOEXEC. */
185 # if HAVE_SETDTABLESIZE
186 setdtablesize (desired_fd + 1);
187 # endif
188 if (desired_fd < 0)
189 fd = desired_fd;
190 if (fd == desired_fd)
191 return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
192 #endif
193
194 int result = dup2 (fd, desired_fd);
195
196 /* Correct an errno value on FreeBSD 6.1 and Cygwin 1.5.x. */
197 if (result < 0 && errno == EMFILE)
198 errno = EBADF;
199
200 #if REPLACE_FCHDIR
201 if (! (result < 0 || fd == desired_fd))
202 result = _gl_register_dup (fd, result);
203 #endif
204 return result;
205 }