2022-03-19 04:37:41 +00:00
|
|
|
/* clang-format off */
|
2021-10-26 17:49:30 +00:00
|
|
|
/* Provide file descriptor control.
|
|
|
|
|
|
|
|
Copyright (C) 2009-2020 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
/* Written by Eric Blake <ebb9@byu.net>. */
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/sysv/consts/f.h"
|
2022-04-06 07:22:54 +00:00
|
|
|
#include "third_party/make/config.h"
|
2021-10-26 17:49:30 +00:00
|
|
|
|
|
|
|
/* Specification. */
|
2022-04-06 07:22:54 +00:00
|
|
|
#include "third_party/make/fcntl.h"
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
#if defined _WIN32 && !defined __CYGWIN__
|
2021-10-26 17:49:30 +00:00
|
|
|
/* Get declarations of the native Windows API functions. */
|
|
|
|
|
|
|
|
/* Upper bound on getdtablesize(). See lib/getdtablesize.c. */
|
2022-03-19 04:37:41 +00:00
|
|
|
#define OPEN_MAX_MAX 0x10000
|
2021-10-26 17:49:30 +00:00
|
|
|
|
|
|
|
/* Duplicate OLDFD into the first available slot of at least NEWFD,
|
|
|
|
which must be positive, with FLAGS determining whether the duplicate
|
|
|
|
will be inheritable. */
|
2022-03-19 04:37:41 +00:00
|
|
|
static int dupfd(int oldfd, int newfd, int flags) {
|
2021-10-26 17:49:30 +00:00
|
|
|
/* Mingw has no way to create an arbitrary fd. Iterate until all
|
|
|
|
file descriptors less than newfd are filled up. */
|
2022-03-19 04:37:41 +00:00
|
|
|
HANDLE curr_process = GetCurrentProcess();
|
|
|
|
HANDLE old_handle = (HANDLE)_get_osfhandle(oldfd);
|
2021-10-26 17:49:30 +00:00
|
|
|
unsigned char fds_to_close[OPEN_MAX_MAX / CHAR_BIT];
|
|
|
|
unsigned int fds_to_close_bound = 0;
|
|
|
|
int result;
|
|
|
|
BOOL inherit = flags & O_CLOEXEC ? FALSE : TRUE;
|
|
|
|
int mode;
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
if (newfd < 0 || getdtablesize() <= newfd) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (old_handle == INVALID_HANDLE_VALUE ||
|
|
|
|
(mode = setmode(oldfd, O_BINARY)) == -1) {
|
|
|
|
/* oldfd is not open, or is an unassigned standard file
|
|
|
|
descriptor. */
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setmode(oldfd, mode);
|
2021-10-26 17:49:30 +00:00
|
|
|
flags |= mode;
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
for (;;) {
|
|
|
|
HANDLE new_handle;
|
|
|
|
int duplicated_fd;
|
|
|
|
unsigned int index;
|
|
|
|
|
|
|
|
if (!DuplicateHandle(curr_process, /* SourceProcessHandle */
|
|
|
|
old_handle, /* SourceHandle */
|
|
|
|
curr_process, /* TargetProcessHandle */
|
|
|
|
(PHANDLE)&new_handle, /* TargetHandle */
|
|
|
|
(DWORD)0, /* DesiredAccess */
|
|
|
|
inherit, /* InheritHandle */
|
|
|
|
DUPLICATE_SAME_ACCESS)) /* Options */
|
2021-10-26 17:49:30 +00:00
|
|
|
{
|
2022-03-19 04:37:41 +00:00
|
|
|
switch (GetLastError()) {
|
|
|
|
case ERROR_TOO_MANY_OPEN_FILES:
|
|
|
|
errno = EMFILE;
|
|
|
|
break;
|
|
|
|
case ERROR_INVALID_HANDLE:
|
|
|
|
case ERROR_INVALID_TARGET_HANDLE:
|
|
|
|
case ERROR_DIRECT_ACCESS_HANDLE:
|
|
|
|
errno = EBADF;
|
2021-10-26 17:49:30 +00:00
|
|
|
break;
|
2022-03-19 04:37:41 +00:00
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
case ERROR_INVALID_FUNCTION:
|
|
|
|
case ERROR_INVALID_ACCESS:
|
|
|
|
errno = EINVAL;
|
2021-10-26 17:49:30 +00:00
|
|
|
break;
|
2022-03-19 04:37:41 +00:00
|
|
|
default:
|
|
|
|
errno = EACCES;
|
2021-10-26 17:49:30 +00:00
|
|
|
break;
|
2022-03-19 04:37:41 +00:00
|
|
|
}
|
|
|
|
result = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
duplicated_fd = _open_osfhandle((intptr_t)new_handle, flags);
|
|
|
|
if (duplicated_fd < 0) {
|
|
|
|
CloseHandle(new_handle);
|
|
|
|
result = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (newfd <= duplicated_fd) {
|
|
|
|
result = duplicated_fd;
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
/* Set the bit duplicated_fd in fds_to_close[]. */
|
|
|
|
index = (unsigned int)duplicated_fd / CHAR_BIT;
|
|
|
|
if (fds_to_close_bound <= index) {
|
|
|
|
if (sizeof fds_to_close <= index) /* Need to increase OPEN_MAX_MAX. */
|
|
|
|
abort();
|
|
|
|
memset(fds_to_close + fds_to_close_bound, '\0',
|
|
|
|
index + 1 - fds_to_close_bound);
|
|
|
|
fds_to_close_bound = index + 1;
|
|
|
|
}
|
|
|
|
fds_to_close[index] |= 1 << ((unsigned int)duplicated_fd % CHAR_BIT);
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:49:30 +00:00
|
|
|
/* Close the previous fds that turned out to be too small. */
|
|
|
|
{
|
|
|
|
int saved_errno = errno;
|
|
|
|
unsigned int duplicated_fd;
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
for (duplicated_fd = 0; duplicated_fd < fds_to_close_bound * CHAR_BIT;
|
2021-10-26 17:49:30 +00:00
|
|
|
duplicated_fd++)
|
2022-03-19 04:37:41 +00:00
|
|
|
if ((fds_to_close[duplicated_fd / CHAR_BIT] >>
|
|
|
|
(duplicated_fd % CHAR_BIT)) &
|
|
|
|
1)
|
|
|
|
close(duplicated_fd);
|
2021-10-26 17:49:30 +00:00
|
|
|
|
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
#if REPLACE_FCHDIR
|
|
|
|
if (0 <= result) result = _gl_register_dup(oldfd, result);
|
|
|
|
#endif
|
2021-10-26 17:49:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif /* W32 */
|
|
|
|
|
|
|
|
/* Forward declarations, because we '#undef fcntl' in the middle of this
|
|
|
|
compilation unit. */
|
|
|
|
/* Our implementation of fcntl (fd, F_DUPFD, target). */
|
2022-03-19 04:37:41 +00:00
|
|
|
static int rpl_fcntl_DUPFD(int fd, int target);
|
2021-10-26 17:49:30 +00:00
|
|
|
/* Our implementation of fcntl (fd, F_DUPFD_CLOEXEC, target). */
|
2022-03-19 04:37:41 +00:00
|
|
|
static int rpl_fcntl_DUPFD_CLOEXEC(int fd, int target);
|
2021-10-26 17:49:30 +00:00
|
|
|
#ifdef __KLIBC__
|
|
|
|
/* Adds support for fcntl on directories. */
|
2022-03-19 04:37:41 +00:00
|
|
|
static int klibc_fcntl(int fd, int action, /* arg */...);
|
2021-10-26 17:49:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Perform the specified ACTION on the file descriptor FD, possibly
|
|
|
|
using the argument ARG further described below. This replacement
|
|
|
|
handles the following actions, and forwards all others on to the
|
|
|
|
native fcntl. An unrecognized ACTION returns -1 with errno set to
|
|
|
|
EINVAL.
|
|
|
|
|
|
|
|
F_DUPFD - duplicate FD, with int ARG being the minimum target fd.
|
|
|
|
If successful, return the duplicate, which will be inheritable;
|
|
|
|
otherwise return -1 and set errno.
|
|
|
|
|
|
|
|
F_DUPFD_CLOEXEC - duplicate FD, with int ARG being the minimum
|
|
|
|
target fd. If successful, return the duplicate, which will not be
|
|
|
|
inheritable; otherwise return -1 and set errno.
|
|
|
|
|
|
|
|
F_GETFD - ARG need not be present. If successful, return a
|
|
|
|
non-negative value containing the descriptor flags of FD (only
|
|
|
|
FD_CLOEXEC is portable, but other flags may be present); otherwise
|
|
|
|
return -1 and set errno. */
|
|
|
|
|
2022-06-15 23:19:50 +00:00
|
|
|
int fcntl_(int fd, int action, /* arg */...)
|
2021-10-26 17:49:30 +00:00
|
|
|
#undef fcntl
|
|
|
|
#ifdef __KLIBC__
|
2022-06-15 23:19:50 +00:00
|
|
|
#define fcntl_ klibc_fcntl
|
2021-10-26 17:49:30 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
va_list arg;
|
|
|
|
int result = -1;
|
2022-03-19 04:37:41 +00:00
|
|
|
va_start(arg, action);
|
|
|
|
if (action == F_DUPFD) {
|
|
|
|
int target = va_arg(arg, int);
|
|
|
|
result = rpl_fcntl_DUPFD(fd, target);
|
|
|
|
}
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
else if (action == F_DUPFD_CLOEXEC) {
|
|
|
|
int target = va_arg(arg, int);
|
|
|
|
result = rpl_fcntl_DUPFD_CLOEXEC(fd, target);
|
|
|
|
}
|
2021-10-26 17:49:30 +00:00
|
|
|
|
|
|
|
#if !HAVE_FCNTL
|
2022-03-19 04:37:41 +00:00
|
|
|
else if (action == F_GETFD) {
|
|
|
|
#if defined _WIN32 && !defined __CYGWIN__
|
|
|
|
HANDLE handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
DWORD flags;
|
|
|
|
if (handle == INVALID_HANDLE_VALUE ||
|
|
|
|
GetHandleInformation(handle, &flags) == 0)
|
|
|
|
errno = EBADF;
|
|
|
|
else
|
|
|
|
result = (flags & HANDLE_FLAG_INHERIT) ? 0 : FD_CLOEXEC;
|
|
|
|
#else /* !W32 */
|
|
|
|
/* Use dup2 to reject invalid file descriptors. No way to
|
|
|
|
access this information, so punt. */
|
|
|
|
if (0 <= dup2(fd, fd)) result = 0;
|
|
|
|
#endif /* !W32 */
|
|
|
|
} /* F_GETFD */
|
2021-10-26 17:49:30 +00:00
|
|
|
#endif /* !HAVE_FCNTL */
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
/* Implementing F_SETFD on mingw is not trivial - there is no
|
|
|
|
API for changing the O_NOINHERIT bit on an fd, and merely
|
|
|
|
changing the HANDLE_FLAG_INHERIT bit on the underlying handle
|
|
|
|
can lead to odd state. It may be possible by duplicating the
|
|
|
|
handle, using _open_osfhandle with the right flags, then
|
|
|
|
using dup2 to move the duplicate onto the original, but that
|
|
|
|
is not supported for now. */
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
else {
|
2021-10-26 18:15:32 +00:00
|
|
|
#if 0
|
2021-10-26 17:49:30 +00:00
|
|
|
switch (action)
|
|
|
|
{
|
2022-03-19 04:37:41 +00:00
|
|
|
#ifdef F_BARRIERFSYNC /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_BARRIERFSYNC:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_CHKCLEAN /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_CHKCLEAN:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_CLOSEM /* NetBSD, HP-UX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_CLOSEM:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_FLUSH_DATA /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_FLUSH_DATA:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_FREEZE_FS /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_FREEZE_FS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_FULLFSYNC /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_FULLFSYNC:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETCONFINED /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETCONFINED:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETDEFAULTPROTLEVEL /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETDEFAULTPROTLEVEL:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETFD /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETFD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETFL /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETFL:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETLEASE /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETLEASE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETNOSIGPIPE /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETNOSIGPIPE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETOWN /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETOWN:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETPIPE_SZ /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETPIPE_SZ:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETPROTECTIONCLASS /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETPROTECTIONCLASS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETPROTECTIONLEVEL /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETPROTECTIONLEVEL:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GET_SEALS /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GET_SEALS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETSIG /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETSIG:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_MAXFD /* NetBSD */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_MAXFD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_RECYCLE /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_RECYCLE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETFIFOENH /* HP-UX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETFIFOENH:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_THAW_FS /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_THAW_FS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
2021-10-26 17:49:30 +00:00
|
|
|
/* These actions take no argument. */
|
|
|
|
result = fcntl (fd, action);
|
|
|
|
break;
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
#ifdef F_ADD_SEALS /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_ADD_SEALS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_BADFD /* Solaris */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_BADFD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_CHECK_OPENEVT /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_CHECK_OPENEVT:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_DUP2FD /* FreeBSD, AIX, Solaris */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_DUP2FD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_DUP2FD_CLOEXEC /* FreeBSD, Solaris */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_DUP2FD_CLOEXEC:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_DUP2FD_CLOFORK /* Solaris */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_DUP2FD_CLOFORK:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_DUPFD /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_DUPFD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_DUPFD_CLOEXEC /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_DUPFD_CLOEXEC:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_DUPFD_CLOFORK /* Solaris */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_DUPFD_CLOFORK:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GETXFL /* Solaris */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GETXFL:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_GLOBAL_NOCACHE /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_GLOBAL_NOCACHE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_MAKECOMPRESSED /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_MAKECOMPRESSED:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_MOVEDATAEXTENTS /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_MOVEDATAEXTENTS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_NOCACHE /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_NOCACHE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_NODIRECT /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_NODIRECT:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_NOTIFY /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_NOTIFY:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_OPLKACK /* IRIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_OPLKACK:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_OPLKREG /* IRIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_OPLKREG:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_RDAHEAD /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_RDAHEAD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETBACKINGSTORE /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETBACKINGSTORE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETCONFINED /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETCONFINED:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETFD /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETFD:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETFL /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETFL:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETLEASE /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETLEASE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETNOSIGPIPE /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETNOSIGPIPE:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETOWN /* POSIX */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETOWN:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETPIPE_SZ /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETPIPE_SZ:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETPROTECTIONCLASS /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETPROTECTIONCLASS:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SETSIG /* Linux */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SETSIG:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
|
|
|
#ifdef F_SINGLE_WRITER /* macOS */
|
2021-10-26 17:49:30 +00:00
|
|
|
case F_SINGLE_WRITER:
|
2022-03-19 04:37:41 +00:00
|
|
|
#endif
|
2021-10-26 17:49:30 +00:00
|
|
|
/* These actions take an 'int' argument. */
|
|
|
|
{
|
|
|
|
int x = va_arg (arg, int);
|
|
|
|
result = fcntl (fd, action, x);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Other actions take a pointer argument. */
|
|
|
|
{
|
|
|
|
void *p = va_arg (arg, void *);
|
|
|
|
result = fcntl (fd, action, p);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
2022-03-19 04:37:41 +00:00
|
|
|
errno = EINVAL;
|
2021-10-26 17:49:30 +00:00
|
|
|
#endif
|
2022-03-19 04:37:41 +00:00
|
|
|
}
|
|
|
|
va_end(arg);
|
2021-10-26 17:49:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
static int rpl_fcntl_DUPFD(int fd, int target) {
|
2021-10-26 17:49:30 +00:00
|
|
|
int result;
|
|
|
|
#if !HAVE_FCNTL
|
2022-03-19 04:37:41 +00:00
|
|
|
result = dupfd(fd, target, 0);
|
2021-10-26 17:49:30 +00:00
|
|
|
#elif FCNTL_DUPFD_BUGGY || REPLACE_FCHDIR
|
|
|
|
/* Detect invalid target; needed for cygwin 1.5.x. */
|
2022-03-19 04:37:41 +00:00
|
|
|
if (target < 0 || getdtablesize() <= target) {
|
|
|
|
result = -1;
|
|
|
|
errno = EINVAL;
|
|
|
|
} else {
|
|
|
|
/* Haiku alpha 2 loses fd flags on original. */
|
|
|
|
int flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags < 0)
|
2021-10-26 17:49:30 +00:00
|
|
|
result = -1;
|
2022-03-19 04:37:41 +00:00
|
|
|
else {
|
|
|
|
result = fcntl(fd, F_DUPFD, target);
|
|
|
|
if (0 <= result && fcntl(fd, F_SETFD, flags) == -1) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
close(result);
|
2021-10-26 17:49:30 +00:00
|
|
|
result = -1;
|
2022-03-19 04:37:41 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
#if REPLACE_FCHDIR
|
|
|
|
if (0 <= result) result = _gl_register_dup(fd, result);
|
|
|
|
#endif
|
2021-10-26 17:49:30 +00:00
|
|
|
}
|
2022-03-19 04:37:41 +00:00
|
|
|
}
|
2021-10-26 17:49:30 +00:00
|
|
|
#else
|
2022-03-19 04:37:41 +00:00
|
|
|
result = fcntl(fd, F_DUPFD, target);
|
2021-10-26 17:49:30 +00:00
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
static int rpl_fcntl_DUPFD_CLOEXEC(int fd, int target) {
|
2021-10-26 17:49:30 +00:00
|
|
|
int result;
|
|
|
|
#if !HAVE_FCNTL
|
2022-03-19 04:37:41 +00:00
|
|
|
result = dupfd(fd, target, O_CLOEXEC);
|
2021-10-26 17:49:30 +00:00
|
|
|
#else /* HAVE_FCNTL */
|
2022-03-19 04:37:41 +00:00
|
|
|
#if defined __HAIKU__
|
2021-10-26 17:49:30 +00:00
|
|
|
/* On Haiku, the system fcntl (fd, F_DUPFD_CLOEXEC, target) sets
|
|
|
|
the FD_CLOEXEC flag on fd, not on target. Therefore avoid the
|
|
|
|
system fcntl in this case. */
|
2022-03-19 04:37:41 +00:00
|
|
|
#define have_dupfd_cloexec -1
|
|
|
|
#else
|
2021-10-26 17:49:30 +00:00
|
|
|
/* Try the system call first, if the headers claim it exists
|
|
|
|
(that is, if GNULIB_defined_F_DUPFD_CLOEXEC is 0), since we
|
|
|
|
may be running with a glibc that has the macro but with an
|
|
|
|
older kernel that does not support it. Cache the
|
|
|
|
information on whether the system call really works, but
|
|
|
|
avoid caching failure if the corresponding F_DUPFD fails
|
|
|
|
for any reason. 0 = unknown, 1 = yes, -1 = no. */
|
|
|
|
static int have_dupfd_cloexec = GNULIB_defined_F_DUPFD_CLOEXEC ? -1 : 0;
|
2022-03-19 04:37:41 +00:00
|
|
|
if (0 <= have_dupfd_cloexec) {
|
|
|
|
result = fcntl(fd, F_DUPFD_CLOEXEC, target);
|
|
|
|
if (0 <= result || errno != EINVAL) {
|
|
|
|
have_dupfd_cloexec = 1;
|
|
|
|
#if REPLACE_FCHDIR
|
|
|
|
if (0 <= result) result = _gl_register_dup(fd, result);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
result = rpl_fcntl_DUPFD(fd, target);
|
|
|
|
if (result >= 0) have_dupfd_cloexec = -1;
|
2021-10-26 17:49:30 +00:00
|
|
|
}
|
2022-03-19 04:37:41 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
result = rpl_fcntl_DUPFD(fd, target);
|
|
|
|
if (0 <= result && have_dupfd_cloexec == -1) {
|
|
|
|
int flags = fcntl(result, F_GETFD);
|
|
|
|
if (flags < 0 || fcntl(result, F_SETFD, flags | FD_CLOEXEC) == -1) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
close(result);
|
|
|
|
errno = saved_errno;
|
|
|
|
result = -1;
|
2021-10-26 17:49:30 +00:00
|
|
|
}
|
2022-03-19 04:37:41 +00:00
|
|
|
}
|
2021-10-26 17:49:30 +00:00
|
|
|
#endif /* HAVE_FCNTL */
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef fcntl
|
|
|
|
|
|
|
|
#ifdef __KLIBC__
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
static int klibc_fcntl(int fd, int action, /* arg */...) {
|
2021-10-26 17:49:30 +00:00
|
|
|
va_list arg_ptr;
|
|
|
|
int arg;
|
|
|
|
struct stat sbuf;
|
|
|
|
int result;
|
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
va_start(arg_ptr, action);
|
|
|
|
arg = va_arg(arg_ptr, int);
|
|
|
|
result = fcntl(fd, action, arg);
|
2021-10-26 17:49:30 +00:00
|
|
|
/* EPERM for F_DUPFD, ENOTSUP for others */
|
2022-03-19 04:37:41 +00:00
|
|
|
if (result == -1 && (errno == EPERM || errno == ENOTSUP) &&
|
|
|
|
!fstat(fd, &sbuf) && S_ISDIR(sbuf.st_mode)) {
|
|
|
|
ULONG ulMode;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
switch (action) {
|
|
|
|
case F_DUPFD:
|
|
|
|
/* Find available fd */
|
|
|
|
while (fcntl(arg, F_GETFL) != -1 || errno != EBADF) arg++;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
result = dup2(fd, arg);
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
/* Using underlying APIs is right ? */
|
|
|
|
case F_GETFD:
|
|
|
|
if (DosQueryFHState(fd, &ulMode)) break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
result = (ulMode & OPEN_FLAGS_NOINHERIT) ? FD_CLOEXEC : 0;
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
case F_SETFD:
|
|
|
|
if (arg & ~FD_CLOEXEC) break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
if (DosQueryFHState(fd, &ulMode)) break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
if (arg & FD_CLOEXEC)
|
|
|
|
ulMode |= OPEN_FLAGS_NOINHERIT;
|
|
|
|
else
|
|
|
|
ulMode &= ~OPEN_FLAGS_NOINHERIT;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
/* Filter supported flags. */
|
|
|
|
ulMode &= (OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR |
|
|
|
|
OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT);
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
if (DosSetFHState(fd, ulMode)) break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
result = 0;
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
case F_GETFL:
|
|
|
|
result = 0;
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
case F_SETFL:
|
|
|
|
if (arg != 0) break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
result = 0;
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
break;
|
2021-10-26 17:49:30 +00:00
|
|
|
}
|
2022-03-19 04:37:41 +00:00
|
|
|
}
|
2021-10-26 17:49:30 +00:00
|
|
|
|
2022-03-19 04:37:41 +00:00
|
|
|
va_end(arg_ptr);
|
2021-10-26 17:49:30 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|