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>. */
35 #if 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
);
169 /* Forward declarations, because we '#undef fcntl' in the middle of this
171 /* Our implementation of fcntl (fd, F_DUPFD, target). */
172 static int rpl_fcntl_DUPFD (int fd
, int target
);
173 /* Our implementation of fcntl (fd, F_DUPFD_CLOEXEC, target). */
174 static int rpl_fcntl_DUPFD_CLOEXEC (int fd
, int target
);
176 /* Adds support for fcntl on directories. */
177 static int klibc_fcntl (int fd
, int action
, /* arg */...);
181 /* Perform the specified ACTION on the file descriptor FD, possibly
182 using the argument ARG further described below. This replacement
183 handles the following actions, and forwards all others on to the
184 native fcntl. An unrecognized ACTION returns -1 with errno set to
187 F_DUPFD - duplicate FD, with int ARG being the minimum target fd.
188 If successful, return the duplicate, which will be inheritable;
189 otherwise return -1 and set errno.
191 F_DUPFD_CLOEXEC - duplicate FD, with int ARG being the minimum
192 target fd. If successful, return the duplicate, which will not be
193 inheritable; otherwise return -1 and set errno.
195 F_GETFD - ARG need not be present. If successful, return a
196 non-negative value containing the descriptor flags of FD (only
197 FD_CLOEXEC is portable, but other flags may be present); otherwise
198 return -1 and set errno. */
201 fcntl (int fd
, int action
, /* arg */...)
204 # define fcntl klibc_fcntl
209 va_start (arg
, action
);
214 int target
= va_arg (arg
, int);
215 result
= rpl_fcntl_DUPFD (fd
, target
);
219 case F_DUPFD_CLOEXEC
:
221 int target
= va_arg (arg
, int);
222 result
= rpl_fcntl_DUPFD_CLOEXEC (fd
, target
);
229 # if defined _WIN32 && ! defined __CYGWIN__
230 HANDLE handle
= (HANDLE
) _get_osfhandle (fd
);
232 if (handle
== INVALID_HANDLE_VALUE
233 || GetHandleInformation (handle
, &flags
) == 0)
236 result
= (flags
& HANDLE_FLAG_INHERIT
) ? 0 : FD_CLOEXEC
;
238 /* Use dup2 to reject invalid file descriptors. No way to
239 access this information, so punt. */
240 if (0 <= dup2 (fd
, fd
))
245 #endif /* !HAVE_FCNTL */
247 /* Implementing F_SETFD on mingw is not trivial - there is no
248 API for changing the O_NOINHERIT bit on an fd, and merely
249 changing the HANDLE_FLAG_INHERIT bit on the underlying handle
250 can lead to odd state. It may be possible by duplicating the
251 handle, using _open_osfhandle with the right flags, then
252 using dup2 to move the duplicate onto the original, but that
253 is not supported for now. */
260 #ifdef F_BARRIERFSYNC /* macOS */
263 #ifdef F_CHKCLEAN /* macOS */
266 #ifdef F_CLOSEM /* NetBSD, HP-UX */
269 #ifdef F_FLUSH_DATA /* macOS */
272 #ifdef F_FREEZE_FS /* macOS */
275 #ifdef F_FULLFSYNC /* macOS */
278 #ifdef F_GETCONFINED /* macOS */
281 #ifdef F_GETDEFAULTPROTLEVEL /* macOS */
282 case F_GETDEFAULTPROTLEVEL
:
284 #ifdef F_GETFD /* POSIX */
287 #ifdef F_GETFL /* POSIX */
290 #ifdef F_GETLEASE /* Linux */
293 #ifdef F_GETNOSIGPIPE /* macOS */
296 #ifdef F_GETOWN /* POSIX */
299 #ifdef F_GETPIPE_SZ /* Linux */
302 #ifdef F_GETPROTECTIONCLASS /* macOS */
303 case F_GETPROTECTIONCLASS
:
305 #ifdef F_GETPROTECTIONLEVEL /* macOS */
306 case F_GETPROTECTIONLEVEL
:
308 #ifdef F_GET_SEALS /* Linux */
311 #ifdef F_GETSIG /* Linux */
314 #ifdef F_MAXFD /* NetBSD */
317 #ifdef F_RECYCLE /* macOS */
320 #ifdef F_SETFIFOENH /* HP-UX */
323 #ifdef F_THAW_FS /* macOS */
326 /* These actions take no argument. */
327 result
= fcntl (fd
, action
);
330 #ifdef F_ADD_SEALS /* Linux */
333 #ifdef F_BADFD /* Solaris */
336 #ifdef F_CHECK_OPENEVT /* macOS */
337 case F_CHECK_OPENEVT
:
339 #ifdef F_DUP2FD /* FreeBSD, AIX, Solaris */
342 #ifdef F_DUP2FD_CLOEXEC /* FreeBSD, Solaris */
343 case F_DUP2FD_CLOEXEC
:
345 #ifdef F_DUP2FD_CLOFORK /* Solaris */
346 case F_DUP2FD_CLOFORK
:
348 #ifdef F_DUPFD /* POSIX */
351 #ifdef F_DUPFD_CLOEXEC /* POSIX */
352 case F_DUPFD_CLOEXEC
:
354 #ifdef F_DUPFD_CLOFORK /* Solaris */
355 case F_DUPFD_CLOFORK
:
357 #ifdef F_GETXFL /* Solaris */
360 #ifdef F_GLOBAL_NOCACHE /* macOS */
361 case F_GLOBAL_NOCACHE
:
363 #ifdef F_MAKECOMPRESSED /* macOS */
364 case F_MAKECOMPRESSED
:
366 #ifdef F_MOVEDATAEXTENTS /* macOS */
367 case F_MOVEDATAEXTENTS
:
369 #ifdef F_NOCACHE /* macOS */
372 #ifdef F_NODIRECT /* macOS */
375 #ifdef F_NOTIFY /* Linux */
378 #ifdef F_OPLKACK /* IRIX */
381 #ifdef F_OPLKREG /* IRIX */
384 #ifdef F_RDAHEAD /* macOS */
387 #ifdef F_SETBACKINGSTORE /* macOS */
388 case F_SETBACKINGSTORE
:
390 #ifdef F_SETCONFINED /* macOS */
393 #ifdef F_SETFD /* POSIX */
396 #ifdef F_SETFL /* POSIX */
399 #ifdef F_SETLEASE /* Linux */
402 #ifdef F_SETNOSIGPIPE /* macOS */
405 #ifdef F_SETOWN /* POSIX */
408 #ifdef F_SETPIPE_SZ /* Linux */
411 #ifdef F_SETPROTECTIONCLASS /* macOS */
412 case F_SETPROTECTIONCLASS
:
414 #ifdef F_SETSIG /* Linux */
417 #ifdef F_SINGLE_WRITER /* macOS */
418 case F_SINGLE_WRITER
:
420 /* These actions take an 'int' argument. */
422 int x
= va_arg (arg
, int);
423 result
= fcntl (fd
, action
, x
);
428 /* Other actions take a pointer argument. */
430 void *p
= va_arg (arg
, void *);
431 result
= fcntl (fd
, action
, p
);
446 rpl_fcntl_DUPFD (int fd
, int target
)
450 result
= dupfd (fd
, target
, 0);
451 #elif FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR
452 /* Detect invalid target; needed for cygwin 1.5.x. */
453 if (target
< 0 || getdtablesize () <= target
)
460 /* Haiku alpha 2 loses fd flags on original. */
461 int flags
= fcntl (fd
, F_GETFD
);
466 result
= fcntl (fd
, F_DUPFD
, target
);
467 if (0 <= result
&& fcntl (fd
, F_SETFD
, flags
) == -1)
469 int saved_errno
= errno
;
476 result
= _gl_register_dup (fd
, result
);
481 result
= fcntl (fd
, F_DUPFD
, target
);
487 rpl_fcntl_DUPFD_CLOEXEC (int fd
, int target
)
491 result
= dupfd (fd
, target
, O_CLOEXEC
);
492 #else /* HAVE_FCNTL */
493 # if defined __HAIKU__
494 /* On Haiku, the system fcntl (fd, F_DUPFD_CLOEXEC, target) sets
495 the FD_CLOEXEC flag on fd, not on target. Therefore avoid the
496 system fcntl in this case. */
497 # define have_dupfd_cloexec -1
499 /* Try the system call first, if the headers claim it exists
500 (that is, if GNULIB_defined_F_DUPFD_CLOEXEC is 0), since we
501 may be running with a glibc that has the macro but with an
502 older kernel that does not support it. Cache the
503 information on whether the system call really works, but
504 avoid caching failure if the corresponding F_DUPFD fails
505 for any reason. 0 = unknown, 1 = yes, -1 = no. */
506 static int have_dupfd_cloexec
= GNULIB_defined_F_DUPFD_CLOEXEC
? -1 : 0;
507 if (0 <= have_dupfd_cloexec
)
509 result
= fcntl (fd
, F_DUPFD_CLOEXEC
, target
);
510 if (0 <= result
|| errno
!= EINVAL
)
512 have_dupfd_cloexec
= 1;
515 result
= _gl_register_dup (fd
, result
);
520 result
= rpl_fcntl_DUPFD (fd
, target
);
522 have_dupfd_cloexec
= -1;
527 result
= rpl_fcntl_DUPFD (fd
, target
);
528 if (0 <= result
&& have_dupfd_cloexec
== -1)
530 int flags
= fcntl (result
, F_GETFD
);
531 if (flags
< 0 || fcntl (result
, F_SETFD
, flags
| FD_CLOEXEC
) == -1)
533 int saved_errno
= errno
;
539 #endif /* HAVE_FCNTL */
548 klibc_fcntl (int fd
, int action
, /* arg */...);
555 va_start (arg_ptr
, action
);
556 arg
= va_arg (arg_ptr
, int);
557 result
= fcntl (fd
, action
, arg
);
558 /* EPERM for F_DUPFD, ENOTSUP for others */
559 if (result
== -1 && (errno
== EPERM
|| errno
== ENOTSUP
)
560 && !fstat (fd
, &sbuf
) && S_ISDIR (sbuf
.st_mode
))
567 /* Find available fd */
568 while (fcntl (arg
, F_GETFL
) != -1 || errno
!= EBADF
)
571 result
= dup2 (fd
, arg
);
574 /* Using underlying APIs is right ? */
576 if (DosQueryFHState (fd
, &ulMode
))
579 result
= (ulMode
& OPEN_FLAGS_NOINHERIT
) ? FD_CLOEXEC
: 0;
583 if (arg
& ~FD_CLOEXEC
)
586 if (DosQueryFHState (fd
, &ulMode
))
589 if (arg
& FD_CLOEXEC
)
590 ulMode
|= OPEN_FLAGS_NOINHERIT
;
592 ulMode
&= ~OPEN_FLAGS_NOINHERIT
;
594 /* Filter supported flags. */
595 ulMode
&= (OPEN_FLAGS_WRITE_THROUGH
| OPEN_FLAGS_FAIL_ON_ERROR
596 | OPEN_FLAGS_NO_CACHE
| OPEN_FLAGS_NOINHERIT
);
598 if (DosSetFHState (fd
, ulMode
))