From c75681427da9e868086a3133709f64d6093b67b9 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Fri, 3 Jul 2026 10:38:57 +0200 Subject: [PATCH] Make start up more efficient by using close_range() to close fds Resolves: RHEL-154768 Signed-off-by: Siteshwar Vashisht --- lsof-close_range.patch | 50 ++++++++++++++++++++++++++++++++++++++++++ lsof.spec | 9 +++++++- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lsof-close_range.patch diff --git a/lsof-close_range.patch b/lsof-close_range.patch new file mode 100644 index 0000000..70abfcf --- /dev/null +++ b/lsof-close_range.patch @@ -0,0 +1,50 @@ +diff --git a/00DIST b/00DIST +index 9cd9c3b..08918a4 100644 +--- a/00DIST ++++ b/00DIST +@@ -5208,5 +5208,13 @@ July 14, 2018 + The target runs check.bash. + + ++ [linux] use close_range instead of calling close repeatedly ++ At the starting up, lsof closes its file descriptors greater ++ than 2 by calling close(2) repeatedly. As reported in #186, ++ it can take long time. Linux 5.9 introduced close_range(2). ++ The new system call can close multiple file descriptors faster. ++ @qianzhangyl reported the original issue (#186). ++ ++ + The lsof-org team at GitHub + November 11, 2020 +diff --git a/dialects/linux/dlsof.h b/dialects/linux/dlsof.h +index 16f04e2..721ded4 100644 +--- a/dialects/linux/dlsof.h ++++ b/dialects/linux/dlsof.h +@@ -71,6 +71,7 @@ + #include + #include + ++#include + + /* + * This definition is needed for the common function prototype definitions +diff --git a/main.c b/main.c +index e58aa1a..b52956c 100644 +--- a/main.c ++++ b/main.c +@@ -127,8 +127,15 @@ main(argc, argv) + #if defined(HAS_CLOSEFROM) + (void) closefrom(3); + #else /* !defined(HAS_CLOSEFROM) */ ++#if defined(SYS_close_range) ++ if (MaxFd > 3 && syscall(SYS_close_range, 3, MaxFd - 1, 0) == 0) ++ goto closed; ++#endif + for (i = 3; i < MaxFd; i++) + (void) close(i); ++#if defined(SYS_close_range) ++ closed: ++#endif + #endif /* !defined(HAS_CLOSEFROM) */ + + while (((i = open("/dev/null", O_RDWR, 0)) >= 0) && (i < 2)) diff --git a/lsof.spec b/lsof.spec index e07cb05..ee469b5 100644 --- a/lsof.spec +++ b/lsof.spec @@ -1,7 +1,7 @@ Summary: A utility which lists open files on a Linux/UNIX system Name: lsof Version: 4.94.0 -Release: 3%{?dist} +Release: 4%{?dist} # Sendmail .. lib/snpf.c # LGPLv2+ .. lib/regex.c, regex.h License: zlib and Sendmail and LGPLv2+ @@ -19,6 +19,8 @@ Source1: upstream2downstream.sh # BZ#1260300 - move lsof man page to section 1 Patch0: lsof-man-page-section.patch +# RHEL-154768 - lsof spins on the CPU forever when *nofile* limit is high +Patch1: lsof-close_range.patch BuildRequires: gcc BuildRequires: libselinux-devel @@ -33,6 +35,7 @@ about files that are open by the processes running on a UNIX system. %prep %setup -q -n %{lsofrh} %patch0 -p1 -b .man-page-section +%patch1 -p1 -b .lsof-close_range %build ./Configure -n linux @@ -52,6 +55,10 @@ install -p -m 0644 lsof.1 ${RPM_BUILD_ROOT}%{_mandir}/man1/lsof.1 %{_mandir}/man*/* %changelog +* Fri Jul 03 2026 Siteshwar Vashisht - 4.94.0-4 +- Make start up more efficient by using close_range() to close fds + Resolves: RHEL-154768 + * Mon Aug 09 2021 Mohan Boddu - 4.94.0-3 - Rebuilt for IMA sigs, glibc 2.34, aarch64 flags Related: rhbz#1991688