1 /* Copy a file descriptor, applying specific flags.
2 Copyright (C) 2009 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 2, or (at your option)
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 along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "binary-io.h"
29 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
30 /* Native Woe32 API. */
34 /* Get declarations of the Win32 API functions. */
35 # define WIN32_LEAN_AND_MEAN
38 /* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
39 # define OPEN_MAX_MAX 0x10000
51 dup3 (int oldfd, int newfd, int flags)
55 /* Try the system call first, if it exists. (We may be running with a glibc
56 that has the function but with an older kernel that lacks it.) */
58 /* Cache the information whether the system call really exists. */
59 static int have_dup3_really; /* 0 = unknown, 1 = yes, -1 = no */
60 if (have_dup3_really >= 0)
62 int result = dup3 (oldfd, newfd, flags);
63 if (!(result < 0 && errno == ENOSYS))
68 result = _gl_register_dup (oldfd, newfd);
72 have_dup3_really = -1;
77 if (oldfd < 0 || newfd < 0 || newfd >= getdtablesize ())
89 /* Check the supported flags.
90 Note that O_NONBLOCK is not supported, because setting it on newfd
91 would implicitly also set it on oldfd. */
92 if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
98 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
99 /* Native Woe32 API. */
101 if (flags & O_CLOEXEC)
103 /* Neither dup() nor dup2() can create a file descriptor with
104 O_CLOEXEC = O_NOINHERIT set. We need to use the low-level function
105 _open_osfhandle for this. Iterate until all file descriptors less
106 than newfd are filled up. */
107 HANDLE curr_process = GetCurrentProcess ();
108 HANDLE old_handle = (HANDLE) _get_osfhandle (oldfd);
109 unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
110 unsigned int fds_to_close_bound = 0;
113 if (old_handle == INVALID_HANDLE_VALUE)
115 /* oldfd is not open, or is an unassigned standard file
129 if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
130 old_handle, /* SourceHandle */
131 curr_process, /* TargetProcessHandle */
132 (PHANDLE) &new_handle, /* TargetHandle */
133 (DWORD) 0, /* DesiredAccess */
134 FALSE, /* InheritHandle */
135 DUPLICATE_SAME_ACCESS)) /* Options */
137 errno = EBADF; /* arbitrary */
141 duplicated_fd = _open_osfhandle ((long) new_handle, flags);
142 if (duplicated_fd < 0)
144 CloseHandle (new_handle);
148 if (duplicated_fd > newfd)
149 /* Shouldn't happen, since newfd is still closed. */
151 if (duplicated_fd == newfd)
157 /* Set the bit duplicated_fd in fds_to_close[]. */
158 index = (unsigned int) duplicated_fd / CHAR_BIT;
159 if (index >= fds_to_close_bound)
161 if (index >= sizeof (fds_to_close))
162 /* Need to increase OPEN_MAX_MAX. */
164 memset (fds_to_close + fds_to_close_bound, '\0',
165 index + 1 - fds_to_close_bound);
166 fds_to_close_bound = index + 1;
168 fds_to_close[index] |= 1 << ((unsigned int) duplicated_fd % CHAR_BIT);
171 /* Close the previous fds that turned out to be too small. */
173 int saved_errno = errno;
174 unsigned int duplicated_fd;
176 for (duplicated_fd = 0;
177 duplicated_fd < fds_to_close_bound * CHAR_BIT;
179 if ((fds_to_close[duplicated_fd / CHAR_BIT]
180 >> (duplicated_fd % CHAR_BIT))
182 close (duplicated_fd);
189 result = _gl_register_dup (oldfd, newfd);
194 if (dup2 (oldfd, newfd) < 0)
200 if (dup2 (oldfd, newfd) < 0)
203 /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/dup.html>
204 says that initially, the FD_CLOEXEC flag is cleared on newfd. */
206 if (flags & O_CLOEXEC)
210 if ((fcntl_flags = fcntl (newfd, F_GETFD, 0)) < 0
211 || fcntl (newfd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
213 int saved_errno = errno;
223 if (flags & O_BINARY)
224 setmode (newfd, O_BINARY);
225 else if (flags & O_TEXT)
226 setmode (newfd, O_TEXT);
230 newfd = _gl_register_dup (oldfd, newfd);