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))