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.
This commit is contained in:
lantz
2023-05-02 15:27:27 -07:00
committed by GitHub
parent 5e909c6e4f
commit 1b4c7d706d
+11 -4
View File
@@ -24,6 +24,9 @@
#include <sched.h> #include <sched.h>
#include <ctype.h> #include <ctype.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#if !defined(VERSION) #if !defined(VERSION)
#define VERSION "(devel)" #define VERSION "(devel)"
@@ -95,19 +98,23 @@ void cgroup(char *gname)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int c; int c;
int fd; DIR *dir;
struct dirent *de;
char path[PATH_MAX]; char path[PATH_MAX];
int nsid; int nsid;
int pid; int pid;
char *cwd = get_current_dir_name(); char *cwd = get_current_dir_name();
static struct sched_param sp; static struct sched_param sp;
while ((c = getopt(argc, argv, "+cdnpa:g:r:vh")) != -1) while ((c = getopt(argc, argv, "+cdnpa:g:r:vh")) != -1)
switch(c) { switch(c) {
case 'c': case 'c':
/* close file descriptors except stdin/out/error */ /* close file descriptors except stdin/out/error */
for (fd = getdtablesize(); fd > 2; fd--) dir = opendir("/proc/self/fd");
close(fd); while ((de = readdir(dir))) {
int fd = atoi(de->d_name);
if (fd>2) close(fd);
}
break; break;
case 'd': case 'd':
/* detach from tty */ /* detach from tty */