Add compat support for closefrom cribbed from libbsd

We had compat support in older dhcpcd but we want it faster
when linux supports faster, because, you know, fast.
This commit is contained in:
Roy Marples 2024-04-20 20:06:22 +01:00
parent 5dd6ee1437
commit 6a6c13f46d
3 changed files with 122 additions and 0 deletions

77
compat/closefrom.c Normal file
View File

@ -0,0 +1,77 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-2018
* Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#ifdef __linux__
# include <sys/syscall.h>
# if defined(__NR_close_range) && !defined(SYS_close_range)
# define SYS_close_range __NR_close_range
# endif
#endif
#include <fcntl.h>
#include <unistd.h>
#if defined(__linux__) && defined(SYS_close_range)
static inline int
sys_close_range(unsigned int fd, unsigned int max_fd, unsigned int flags)
{
return (int)syscall(SYS_close_range, fd, max_fd, flags);
}
#endif
/*
* Close all file descriptors greater than or equal to lowfd.
* This is the expensive (fallback) method.
*/
static int
closefrom_fallback(int lowfd)
{
int fd, maxfd;
#ifdef _SC_OPEN_MAX
maxfd = (int)sysconf(_SC_OPEN_MAX);
#else
maxfd = getdtablesize();
#endif
if (maxfd == -1)
return -1;
for (fd = lowfd; fd < maxfd; fd++)
close(fd);
return 0;
}
/*
* * Close all file descriptors greater than or equal to lowfd.
* * We try the fast way first, falling back on the slow method.
* */
void
closefrom(int lowfd)
{
#if defined(__linux__) && defined(SYS_close_range)
if (sys_close_range((unsigned int)lowfd, UINT_MAX, 0) == 0)
return;
#endif
closefrom_fallback(lowfd);
}

24
compat/closefrom.h Normal file
View File

@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2004-2005, 2007, 2010, 2012-2015, 2017-2018
* Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef CLOSEFROM_H
#define CLOSEFROM_H
void closefrom(int);
#endif

21
configure vendored
View File

@ -811,6 +811,27 @@ fi
rm -f _clock_gettime.c _clock_gettime
$abort && exit 1
if [ -z "$CLOSEFROM" ]; then
printf "Testing for closefrom ... "
cat <<EOF >_closefrom.c
#include <unistd.h>
int main(void) {
return closefrom(3);
}
EOF
if $XCC _closefrom.c -o _closefrom 2>&3; then
CLOSEFROM=yes
else
CLOSEFROM=no
fi
echo "$CLOSEFROM"
fi
rm -f _closefrom.c _closefrom
if [ "$CLOSEFROM" = no ]; then
echo "COMPAT_SRCS+= compat/closefrom.c" >>$CONFIG_MK
echo "#include \"compat/closefrom.h\"" >>$CONFIG_H
fi
printf "Testing ioctl request type ... "
cat <<EOF >_ioctl.c
#include <sys/ioctl.h>