From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 5D36C3858C3A; Mon, 15 Jan 2024 12:55:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5D36C3858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1705323324; bh=5aRpRycCHg2m6oYqviKifrVqg3WrkVypqoSqfqhE9DM=; h=From:To:Subject:Date:From; b=H9LjICLQZNYwM6YwR2YhqLlLddNsRBtYv+jFIC7vkTAPXYj2jxHooj4yWxtsraJZ1 rOTZOqTLM7DeFwxnWZItl3tyLO/BnlkaodHE068t9LzHL7ZgthgIyGFncOvDSm5zCb ETVd/AZYjGejxYhPIxStVEsqLzn2n8vp47QRd2Y8= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org, newlib-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: introduce close_range(2) X-Act-Checkin: newlib-cygwin X-Git-Author: Christian Franke X-Git-Refname: refs/heads/main X-Git-Oldrev: 4ecfb5b7292af4baf0a2f199f5351b67c8faf4cc X-Git-Newrev: d8c0fb090ca637bba5337fb3b0a1f3ee107b21dd Message-Id: <20240115125524.5D36C3858C3A@sourceware.org> Date: Mon, 15 Jan 2024 12:55:24 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dd8c0fb090ca= 637bba5337fb3b0a1f3ee107b21dd commit d8c0fb090ca637bba5337fb3b0a1f3ee107b21dd Author: Christian Franke AuthorDate: Mon Jan 15 12:13:30 2024 +0100 Commit: Corinna Vinschen CommitDate: Mon Jan 15 12:52:56 2024 +0100 Cygwin: introduce close_range(2) =20 This function closes or sets the close-on-exec flag for a specified range of file descriptors. It is available on FreeBSD and Linux. =20 Signed-off-by: Christian Franke Diff: --- newlib/libc/include/sys/unistd.h | 6 +++++ winsup/cygwin/cygwin.din | 1 + winsup/cygwin/include/cygwin/version.h | 3 ++- winsup/cygwin/release/3.5.0 | 2 ++ winsup/cygwin/syscalls.cc | 42 ++++++++++++++++++++++++++++++= ++++ winsup/doc/new-features.xml | 4 ++++ winsup/doc/posix.xml | 5 ++++ 7 files changed, 62 insertions(+), 1 deletion(-) diff --git a/newlib/libc/include/sys/unistd.h b/newlib/libc/include/sys/uni= std.h index 25532251c542..00901540fc5f 100644 --- a/newlib/libc/include/sys/unistd.h +++ b/newlib/libc/include/sys/unistd.h @@ -26,6 +26,12 @@ int chown (const char *__path, uid_t __owner, gid_t = __group); int chroot (const char *__path); #endif int close (int __fildes); +#if defined(__CYGWIN__) && (__BSD_VISIBLE || __GNU_VISIBLE) +/* Available on FreeBSD (__BSD_VISIBLE) and Linux (__GNU_VISIBLE). */ +int close_range (unsigned int __firstfd, unsigned int __lastfd, int __= flags); +/* CLOSE_RANGE_UNSHARE (1 << 1) */ /* Linux-specific, not supported. = */ +#define CLOSE_RANGE_CLOEXEC (1 << 2) +#endif #if __POSIX_VISIBLE >=3D 199209 size_t confstr (int __name, char *__buf, size_t __len); #endif diff --git a/winsup/cygwin/cygwin.din b/winsup/cygwin/cygwin.din index 9b76ce67a9a4..9e354acc6195 100644 --- a/winsup/cygwin/cygwin.din +++ b/winsup/cygwin/cygwin.din @@ -347,6 +347,7 @@ clog10l NOSIGFE clogf NOSIGFE clogl NOSIGFE close SIGFE +close_range SIGFE closedir SIGFE closelog SIGFE cnd_broadcast SIGFE diff --git a/winsup/cygwin/include/cygwin/version.h b/winsup/cygwin/include= /cygwin/version.h index c8177c2b17d7..3036878c4d07 100644 --- a/winsup/cygwin/include/cygwin/version.h +++ b/winsup/cygwin/include/cygwin/version.h @@ -484,12 +484,13 @@ details. */ 347: Add c16rtomb, c32rtomb, mbrtoc16, mbrtoc32. 348: Add c8rtomb, mbrtoc. 349: Add fallocate. + 350: Add close_range. =20 Note that we forgot to bump the api for ualarm, strtoll, strtoull, sigaltstack, sethostname. */ =20 #define CYGWIN_VERSION_API_MAJOR 0 -#define CYGWIN_VERSION_API_MINOR 349 +#define CYGWIN_VERSION_API_MINOR 350 =20 /* There is also a compatibity version number associated with the shared m= emory regions. It is incremented when incompatible changes are made to the s= hared diff --git a/winsup/cygwin/release/3.5.0 b/winsup/cygwin/release/3.5.0 index d0a6c2fc8302..6209064a6bac 100644 --- a/winsup/cygwin/release/3.5.0 +++ b/winsup/cygwin/release/3.5.0 @@ -43,6 +43,8 @@ What's new: =20 - New API calls: c8rtomb, c16rtomb, c32rtomb, mbrtoc8, mbrtoc16, mbrtoc32. =20 +- New API call: close_range (available on FreeBSD and Linux). + - New API call: fallocate (Linux-specific). =20 - Implement OSS-based sound mixer device (/dev/mixer). diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 486db1db67a6..9d88b60b0f92 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -85,6 +85,48 @@ close_all_files (bool norelease) cygheap->fdtab.unlock (); } =20 +/* Close or set the close-on-exec flag for all open file descriptors + from firstfd to lastfd. CLOSE_RANGE_UNSHARE is not supported. + Available on FreeBSD since 13 and Linux since 5.9 */ +extern "C" int +close_range (unsigned int firstfd, unsigned int lastfd, int flags) +{ + pthread_testcancel (); + + if (!(firstfd <=3D lastfd && !(flags & ~CLOSE_RANGE_CLOEXEC))) + { + set_errno (EINVAL); + return -1; + } + + cygheap->fdtab.lock (); + + unsigned int size =3D (lastfd < cygheap->fdtab.size ? lastfd + 1 : + cygheap->fdtab.size); + + for (unsigned int i =3D firstfd; i < size; i++) + { + cygheap_fdget cfd ((int) i, false, false); + if (cfd < 0) + continue; + + if (flags & CLOSE_RANGE_CLOEXEC) + { + syscall_printf ("set FD_CLOEXEC on fd %u", i); + cfd->fcntl (F_SETFD, FD_CLOEXEC); + } + else + { + syscall_printf ("closing fd %u", i); + cfd->close_with_arch (); + cfd.release (); + } + } + + cygheap->fdtab.unlock (); + return 0; +} + extern "C" int dup (int fd) { diff --git a/winsup/doc/new-features.xml b/winsup/doc/new-features.xml index 6ae42003122a..0abe1c41c974 100644 --- a/winsup/doc/new-features.xml +++ b/winsup/doc/new-features.xml @@ -74,6 +74,10 @@ posix_spawn_file_actions_addfchdir_np. New API calls: c8rtomb, c16rtomb, c32rtomb, mbrtoc8, mbrtoc16, mbrtoc32. =20 + +New API call: close_range (available on FreeBSD and Linux). + + New API call: fallocate (Linux-specific). diff --git a/winsup/doc/posix.xml b/winsup/doc/posix.xml index 1a4eee1ab1b8..89056915bf50 100644 --- a/winsup/doc/posix.xml +++ b/winsup/doc/posix.xml @@ -1143,6 +1143,7 @@ also IEEE Std 1003.1-2017 (POSIX.1-2017). cfmakeraw cfsetspeed clearerr_unlocked + close_range daemon dn_comp dn_expand @@ -1297,6 +1298,7 @@ also IEEE Std 1003.1-2017 (POSIX.1-2017). clog10 clog10f clog10l + close_range (see chapter "Implementation= Notes") crypt_r (available in external "crypt" library) dladdr (see chapter "Implementation Note= s") dremf @@ -1656,6 +1658,9 @@ CLOCK_REALTIME and CLOCK_MONOTONIC. clock_= setres, clock_settime, and timer_create currently support only CLOCK_REALTIME. =20 +close_range does not support the Linux-specific +flag CLOSE_RANGE_UNSHARE. + POSIX file locks via fcntl or lockf, as well as BSD flock locks are advisory locks. They don't interact with Windows mandatory locks, nor