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. */
32 /* Get declarations of the Win32 API functions. */
33 # define WIN32_LEAN_AND_MEAN
36 /* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
37 # define OPEN_MAX_MAX 0x10000
49 dup3 (int oldfd, int newfd, int flags)
53 /* Try the system call first, if it exists. (We may be running with a glibc
54 that has the function but with an older kernel that lacks it.) */
56 int result = dup3 (oldfd, newfd, flags);
57 if (!(result < 0 && errno == ENOSYS))
62 if (oldfd < 0 || newfd < 0 || newfd >= getdtablesize ())
74 /* Check the supported flags.
75 Note that O_NONBLOCK is not supported, because setting it on newfd
76 would implicitly also set it on oldfd. */
77 if ((flags & ~(O_CLOEXEC | O_BINARY | O_TEXT)) != 0)
83 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
84 /* Native Woe32 API. */
86 if (flags & O_CLOEXEC)
88 /* Neither dup() nor dup2() can create a file descriptor with
89 O_CLOEXEC = O_NOINHERIT set. We need to use the low-level function
90 _open_osfhandle for this. Iterate until all file descriptors less
91 than newfd are filled up. */
92 HANDLE curr_process = GetCurrentProcess ();
93 HANDLE old_handle = (HANDLE) _get_osfhandle (oldfd);
94 unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
95 unsigned int fds_to_close_bound = 0;
98 if (old_handle == INVALID_HANDLE_VALUE)
100 /* oldfd is not open, or is an unassigned standard file
114 if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
115 old_handle, /* SourceHandle */
116 curr_process, /* TargetProcessHandle */
117 (PHANDLE) &new_handle, /* TargetHandle */
118 (DWORD) 0, /* DesiredAccess */
119 FALSE, /* InheritHandle */
120 DUPLICATE_SAME_ACCESS)) /* Options */
122 errno = EBADF; /* arbitrary */
126 duplicated_fd = _open_osfhandle ((long) new_handle, flags);
127 if (duplicated_fd < 0)
129 CloseHandle (new_handle);
133 if (duplicated_fd > newfd)
134 /* Shouldn't happen, since newfd is still closed. */
136 if (duplicated_fd == newfd)
142 /* Set the bit duplicated_fd in fds_to_close[]. */
143 index = (unsigned int) duplicated_fd / CHAR_BIT;
144 if (index >= fds_to_close_bound)
146 if (index >= sizeof (fds_to_close))
147 /* Need to increase OPEN_MAX_MAX. */
149 memset (fds_to_close + fds_to_close_bound, '\0',
150 index + 1 - fds_to_close_bound);
151 fds_to_close_bound = index + 1;
153 fds_to_close[index] |= 1 << ((unsigned int) duplicated_fd % CHAR_BIT);
156 /* Close the previous fds that turned out to be too small. */
158 int saved_errno = errno;
159 unsigned int duplicated_fd;
161 for (duplicated_fd = 0;
162 duplicated_fd < fds_to_close_bound * CHAR_BIT;
164 if ((fds_to_close[duplicated_fd / CHAR_BIT]
165 >> (duplicated_fd % CHAR_BIT))
167 close (duplicated_fd);
175 if (dup2 (oldfd, newfd) < 0)
181 if (dup2 (oldfd, newfd) < 0)
184 /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/dup.html>
185 says that initially, the FD_CLOEXEC flag is cleared on newfd. */
187 if (flags & O_CLOEXEC)
191 if ((fcntl_flags = fcntl (newfd, F_GETFD, 0)) < 0
192 || fcntl (newfd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
194 int saved_errno = errno;
204 if (flags & O_BINARY)
205 setmode (newfd, O_BINARY);
206 else if (flags & O_TEXT)
207 setmode (newfd, O_TEXT);