1 /* Provide file descriptor control.
3 Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Eric Blake <ebb9@byu.net>. */
31 # define rpl_fcntl fcntl
35 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
36 /* Get declarations of the native Windows API functions. */
37 # define WIN32_LEAN_AND_MEAN
40 /* Get _get_osfhandle. */
41 # if GNULIB_MSVC_NOTHROW
42 # include "msvc-nothrow.h"
47 /* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
48 # define OPEN_MAX_MAX 0x10000
50 /* Duplicate OLDFD into the first available slot of at least NEWFD,
51 which must be positive, with FLAGS determining whether the duplicate
52 will be inheritable. */
54 dupfd (int oldfd
, int newfd
, int flags
)
56 /* Mingw has no way to create an arbitrary fd. Iterate until all
57 file descriptors less than newfd are filled up. */
58 HANDLE curr_process
= GetCurrentProcess ();
59 HANDLE old_handle
= (HANDLE
) _get_osfhandle (oldfd
);
60 unsigned char fds_to_close
[OPEN_MAX_MAX
/ CHAR_BIT
];
61 unsigned int fds_to_close_bound
= 0;
63 BOOL inherit
= flags
& O_CLOEXEC
? FALSE
: TRUE
;
66 if (newfd
< 0 || getdtablesize () <= newfd
)
71 if (old_handle
== INVALID_HANDLE_VALUE
72 || (mode
= setmode (oldfd
, O_BINARY
)) == -1)
74 /* oldfd is not open, or is an unassigned standard file
79 setmode (oldfd
, mode
);
88 if (!DuplicateHandle (curr_process
, /* SourceProcessHandle */
89 old_handle
, /* SourceHandle */
90 curr_process
, /* TargetProcessHandle */
91 (PHANDLE
) &new_handle
, /* TargetHandle */
92 (DWORD
) 0, /* DesiredAccess */
93 inherit
, /* InheritHandle */
94 DUPLICATE_SAME_ACCESS
)) /* Options */
96 switch (GetLastError ())
98 case ERROR_TOO_MANY_OPEN_FILES
:
101 case ERROR_INVALID_HANDLE
:
102 case ERROR_INVALID_TARGET_HANDLE
:
103 case ERROR_DIRECT_ACCESS_HANDLE
:
106 case ERROR_INVALID_PARAMETER
:
107 case ERROR_INVALID_FUNCTION
:
108 case ERROR_INVALID_ACCESS
:
118 duplicated_fd
= _open_osfhandle ((intptr_t) new_handle
, flags
);
119 if (duplicated_fd
< 0)
121 CloseHandle (new_handle
);
125 if (newfd
<= duplicated_fd
)
127 result
= duplicated_fd
;
131 /* Set the bit duplicated_fd in fds_to_close[]. */
132 index
= (unsigned int) duplicated_fd
/ CHAR_BIT
;
133 if (fds_to_close_bound
<= index
)
135 if (sizeof fds_to_close
<= index
)
136 /* Need to increase OPEN_MAX_MAX. */
138 memset (fds_to_close
+ fds_to_close_bound
, '\0',
139 index
+ 1 - fds_to_close_bound
);
140 fds_to_close_bound
= index
+ 1;
142 fds_to_close
[index
] |= 1 << ((unsigned int) duplicated_fd
% CHAR_BIT
);
145 /* Close the previous fds that turned out to be too small. */
147 int saved_errno
= errno
;
148 unsigned int duplicated_fd
;
150 for (duplicated_fd
= 0;
151 duplicated_fd
< fds_to_close_bound
* CHAR_BIT
;
153 if ((fds_to_close
[duplicated_fd
/ CHAR_BIT
]
154 >> (duplicated_fd
% CHAR_BIT
))
156 close (duplicated_fd
);
163 result
= _gl_register_dup (oldfd
, result
);
175 klibc_fcntl (int fd
, int action
, /* arg */...)
182 va_start (arg_ptr
, action
);
183 arg
= va_arg (arg_ptr
, int);
184 result
= fcntl (fd
, action
, arg
);
185 /* EPERM for F_DUPFD, ENOTSUP for others */
186 if (result
== -1 && (errno
== EPERM
|| errno
== ENOTSUP
)
187 && !fstat (fd
, &sbuf
) && S_ISDIR (sbuf
.st_mode
))
194 /* Find available fd */
195 while (fcntl (arg
, F_GETFL
) != -1 || errno
!= EBADF
)
198 result
= dup2 (fd
, arg
);
201 /* Using underlying APIs is right ? */
203 if (DosQueryFHState (fd
, &ulMode
))
206 result
= (ulMode
& OPEN_FLAGS_NOINHERIT
) ? FD_CLOEXEC
: 0;
210 if (arg
& ~FD_CLOEXEC
)
213 if (DosQueryFHState (fd
, &ulMode
))
216 if (arg
& FD_CLOEXEC
)
217 ulMode
|= OPEN_FLAGS_NOINHERIT
;
219 ulMode
&= ~OPEN_FLAGS_NOINHERIT
;
221 /* Filter supported flags. */
222 ulMode
&= (OPEN_FLAGS_WRITE_THROUGH
| OPEN_FLAGS_FAIL_ON_ERROR
223 | OPEN_FLAGS_NO_CACHE
| OPEN_FLAGS_NOINHERIT
);
225 if (DosSetFHState (fd
, ulMode
))
253 # define fcntl klibc_fcntl
256 /* Perform the specified ACTION on the file descriptor FD, possibly
257 using the argument ARG further described below. This replacement
258 handles the following actions, and forwards all others on to the
259 native fcntl. An unrecognized ACTION returns -1 with errno set to
262 F_DUPFD - duplicate FD, with int ARG being the minimum target fd.
263 If successful, return the duplicate, which will be inheritable;
264 otherwise return -1 and set errno.
266 F_DUPFD_CLOEXEC - duplicate FD, with int ARG being the minimum
267 target fd. If successful, return the duplicate, which will not be
268 inheritable; otherwise return -1 and set errno.
270 F_GETFD - ARG need not be present. If successful, return a
271 non-negative value containing the descriptor flags of FD (only
272 FD_CLOEXEC is portable, but other flags may be present); otherwise
273 return -1 and set errno. */
276 rpl_fcntl (int fd
, int action
, /* arg */...)
280 va_start (arg
, action
);
287 int target
= va_arg (arg
, int);
288 result
= dupfd (fd
, target
, 0);
291 #elif FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR
294 int target
= va_arg (arg
, int);
295 /* Detect invalid target; needed for cygwin 1.5.x. */
296 if (target
< 0 || getdtablesize () <= target
)
300 /* Haiku alpha 2 loses fd flags on original. */
301 int flags
= fcntl (fd
, F_GETFD
);
307 result
= fcntl (fd
, action
, target
);
308 if (0 <= result
&& fcntl (fd
, F_SETFD
, flags
) == -1)
310 int saved_errno
= errno
;
317 result
= _gl_register_dup (fd
, result
);
322 #endif /* FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR */
324 case F_DUPFD_CLOEXEC
:
326 int target
= va_arg (arg
, int);
329 result
= dupfd (fd
, target
, O_CLOEXEC
);
331 #else /* HAVE_FCNTL */
332 /* Try the system call first, if the headers claim it exists
333 (that is, if GNULIB_defined_F_DUPFD_CLOEXEC is 0), since we
334 may be running with a glibc that has the macro but with an
335 older kernel that does not support it. Cache the
336 information on whether the system call really works, but
337 avoid caching failure if the corresponding F_DUPFD fails
338 for any reason. 0 = unknown, 1 = yes, -1 = no. */
339 static int have_dupfd_cloexec
= GNULIB_defined_F_DUPFD_CLOEXEC
? -1 : 0;
340 if (0 <= have_dupfd_cloexec
)
342 result
= fcntl (fd
, action
, target
);
343 if (0 <= result
|| errno
!= EINVAL
)
345 have_dupfd_cloexec
= 1;
348 result
= _gl_register_dup (fd
, result
);
353 result
= rpl_fcntl (fd
, F_DUPFD
, target
);
356 have_dupfd_cloexec
= -1;
360 result
= rpl_fcntl (fd
, F_DUPFD
, target
);
361 if (0 <= result
&& have_dupfd_cloexec
== -1)
363 int flags
= fcntl (result
, F_GETFD
);
364 if (flags
< 0 || fcntl (result
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
366 int saved_errno
= errno
;
373 #endif /* HAVE_FCNTL */
374 } /* F_DUPFD_CLOEXEC */
379 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
380 HANDLE handle
= (HANDLE
) _get_osfhandle (fd
);
382 if (handle
== INVALID_HANDLE_VALUE
383 || GetHandleInformation (handle
, &flags
) == 0)
386 result
= (flags
& HANDLE_FLAG_INHERIT
) ? 0 : FD_CLOEXEC
;
388 /* Use dup2 to reject invalid file descriptors. No way to
389 access this information, so punt. */
390 if (0 <= dup2 (fd
, fd
))
395 #endif /* !HAVE_FCNTL */
397 /* Implementing F_SETFD on mingw is not trivial - there is no
398 API for changing the O_NOINHERIT bit on an fd, and merely
399 changing the HANDLE_FLAG_INHERIT bit on the underlying handle
400 can lead to odd state. It may be possible by duplicating the
401 handle, using _open_osfhandle with the right flags, then
402 using dup2 to move the duplicate onto the original, but that
403 is not supported for now. */
408 void *p
= va_arg (arg
, void *);
409 result
= fcntl (fd
, action
, p
);