From b953ba3b1a93fc44771b900ff519931e3380aa82 Mon Sep 17 00:00:00 2001 From: Marc <34656315+MarcT512@users.noreply.github.com> Date: Wed, 15 May 2024 11:33:23 +0100 Subject: [PATCH] Add files via upload Add an optimized fallback in closefrom_shim() for Linux, if the platform doesn't support closefrom or close_range. Iterate /proc/self/fd and close anything which is not that directory itself. --- lib/misc.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/misc.c b/lib/misc.c index 4eef8ab2..8bbde55c 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -227,7 +227,28 @@ void closefrom_shim(struct lsof_context *ctx, int low) { if (MaxFd > low && syscall(SYS_close_range, low, MaxFd - 1, 0) == 0) return; # endif - /* slow fallback */ +# if defined(LINUX_LSOF_H) + /* optimized fallback for Linux: + * iterate /proc/self/fd and close fd's that are not that directory itself + */ + long fd; + char *endp; + struct dirent *dent; + DIR *dirp; + + dirp = opendir("/proc/self/fd"); + if (dirp) { + while ((dent = readdir(dirp)) != NULL) { + fd = strtol(dent->d_name, &endp, 10); + if (dent->d_name != endp && *endp == '\0' && fd >= 0 && + fd < INT_MAX && fd >= low && fd != dirfd(dirp)) + (void)close((int)fd); + } + (void)closedir(dirp); + return; + } +# endif + /* slow brute force fallback */ for (i = low; i < MaxFd; i++) (void)close(i); #endif /* !defined(HAS_CLOSEFROM) */