Add fallback to old mnexec -c method (#1183)

If opendir("/proc/self/fd") fails, fall back to the old
method of closing all file decriptors from 3-1024 (getdtablesize()).
This commit is contained in:
lantz
2023-05-02 16:17:49 -07:00
committed by GitHub
parent 1b4c7d706d
commit 0cefa0b8de
+8 -4
View File
@@ -98,6 +98,7 @@ void cgroup(char *gname)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int c; int c;
int fd;
DIR *dir; DIR *dir;
struct dirent *de; struct dirent *de;
char path[PATH_MAX]; char path[PATH_MAX];
@@ -110,11 +111,14 @@ int main(int argc, char *argv[])
switch(c) { switch(c) {
case 'c': case 'c':
/* close file descriptors except stdin/out/error */ /* close file descriptors except stdin/out/error */
dir = opendir("/proc/self/fd"); if ((dir = opendir("/proc/self/fd"))) {
while ((de = readdir(dir))) { while ((de = readdir(dir)))
int fd = atoi(de->d_name); if ((fd = atoi(de->d_name)) > 2)
if (fd>2) close(fd); close(fd);
} }
/* fall back to old method if needed */
else for (fd = getdtablesize(); fd > 2; fd--)
close(fd);
break; break;
case 'd': case 'd':
/* detach from tty */ /* detach from tty */