Make start up more efficient by using close_range() to close fds

Resolves: RHEL-154768

Signed-off-by: Siteshwar Vashisht <svashisht@redhat.com>
This commit is contained in:
Siteshwar Vashisht 2026-07-03 10:38:57 +02:00
parent c0f9d188fe
commit c75681427d
2 changed files with 58 additions and 1 deletions

50
lsof-close_range.patch Normal file
View File

@ -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 <linux/if_ether.h>
#include <linux/netlink.h>
+#include <sys/syscall.h>
/*
* 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))

View File

@ -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 <svashisht@redhat.com> - 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 <mboddu@redhat.com> - 4.94.0-3
- Rebuilt for IMA sigs, glibc 2.34, aarch64 flags
Related: rhbz#1991688