From 1b4c7d706d090b9c60bfed87b1e0e86596285528 Mon Sep 17 00:00:00 2001 From: lantz Date: Tue, 2 May 2023 15:27:27 -0700 Subject: [PATCH] Use /proc/self/fd for open fd list (#1182) mnexec -c attempts to close all open files except for standard in/out/error. Previously we just closed every fd>2, but this seems to take a long time on docker. So now we read /proc/self/fd to see which file descriptors are actually in use. --- mnexec.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mnexec.c b/mnexec.c index d3f173d..cffb45e 100644 --- a/mnexec.c +++ b/mnexec.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #if !defined(VERSION) #define VERSION "(devel)" @@ -95,19 +98,23 @@ void cgroup(char *gname) int main(int argc, char *argv[]) { int c; - int fd; + DIR *dir; + struct dirent *de; char path[PATH_MAX]; int nsid; int pid; char *cwd = get_current_dir_name(); - static struct sched_param sp; + while ((c = getopt(argc, argv, "+cdnpa:g:r:vh")) != -1) switch(c) { case 'c': /* close file descriptors except stdin/out/error */ - for (fd = getdtablesize(); fd > 2; fd--) - close(fd); + dir = opendir("/proc/self/fd"); + while ((de = readdir(dir))) { + int fd = atoi(de->d_name); + if (fd>2) close(fd); + } break; case 'd': /* detach from tty */