Index: lib/libc/include/libc_private.h =================================================================== --- lib/libc/include/libc_private.h +++ lib/libc/include/libc_private.h @@ -331,6 +331,7 @@ int __sys_clock_nanosleep(__clockid_t, int, const struct timespec *, struct timespec *); int __sys_close(int); +int __sys_close_range(unsigned, unsigned, int); int __sys_connect(int, const struct sockaddr *, __socklen_t); int __sys_fcntl(int, int, ...); int __sys_fdatasync(int); Index: lib/libc/sys/Makefile.inc =================================================================== --- lib/libc/sys/Makefile.inc +++ lib/libc/sys/Makefile.inc @@ -45,6 +45,7 @@ PSEUDO+= _getdirentries.o SRCS+= brk.c +SRCS+= closefrom.c SRCS+= pipe.c SRCS+= shm_open.c SRCS+= vadvise.c Index: lib/libc/sys/closefrom.c =================================================================== --- /dev/null +++ lib/libc/sys/closefrom.c @@ -0,0 +1,50 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Kyle Evans + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include "libc_private.h" + +__weak_reference(closefrom, _closefrom); +__weak_reference(closefrom, __sys_closefrom); + +#define CLOSE_RANGE_OSREL 1300091 + +void +closefrom(int lowfd) +{ + + if (__getosreldate() >= CLOSE_RANGE_OSREL) + __sys_close_range(lowfd, ~0U, 0); + else + /* Fallback to closefrom(2) on older kernels. */ + syscall(SYS_freebsd12_closefrom, lowfd); +} Index: sys/compat/freebsd32/syscalls.master =================================================================== --- sys/compat/freebsd32/syscalls.master +++ sys/compat/freebsd32/syscalls.master @@ -980,7 +980,7 @@ 507 AUE_JAIL_SET STD { int freebsd32_jail_set(struct iovec32 *iovp, \ unsigned int iovcnt, int flags); } 508 AUE_JAIL_REMOVE NOPROTO { int jail_remove(int jid); } -509 AUE_CLOSEFROM NOPROTO { int closefrom(int lowfd); } +509 AUE_CLOSEFROM COMPAT12|NOPROTO { int closefrom(int lowfd); } 510 AUE_SEMCTL NOSTD { int freebsd32_semctl(int semid, int semnum, \ int cmd, union semun32 *arg); } 511 AUE_MSGCTL NOSTD { int freebsd32_msgctl(int msqid, int cmd, \ Index: sys/kern/kern_descrip.c =================================================================== --- sys/kern/kern_descrip.c +++ sys/kern/kern_descrip.c @@ -1372,17 +1372,18 @@ return (kern_close_range(td, uap->lowfd, uap->highfd)); } +#ifdef COMPAT_FREEBSD12 /* * Close open file descriptors. */ #ifndef _SYS_SYSPROTO_H_ -struct closefrom_args { +struct freebsd12_closefrom_args { int lowfd; }; #endif /* ARGSUSED */ int -sys_closefrom(struct thread *td, struct closefrom_args *uap) +freebsd12_closefrom(struct thread *td, struct freebsd12_closefrom_args *uap) { u_int lowfd; @@ -1395,6 +1396,7 @@ lowfd = MAX(0, uap->lowfd); return (kern_close_range(td, lowfd, ~0U)); } +#endif /* COMPAT_FREEBSD12 */ #if defined(COMPAT_43) /* Index: sys/kern/syscalls.master =================================================================== --- sys/kern/syscalls.master +++ sys/kern/syscalls.master @@ -2776,7 +2776,7 @@ int jid ); } -509 AUE_CLOSEFROM STD { +509 AUE_CLOSEFROM COMPAT12 { int closefrom( int lowfd ); Index: tools/build/depend-cleanup.sh =================================================================== --- tools/build/depend-cleanup.sh +++ tools/build/depend-cleanup.sh @@ -34,3 +34,5 @@ # Date Rev Description # 20200310 r358851 rename of openmp's ittnotify_static.c to .cpp clean_dep lib/libomp ittnotify_static c +# 2020041X rXXXXXX closefrom +clean_dep lib/libc closefrom c