Close all unneeded file descriptors before exec.
This commit is contained in:
parent
2edd51c4b8
commit
0e9b7f0cfd
@ -43,6 +43,7 @@
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <signal.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "termExec.h"
|
||||
|
||||
@ -121,8 +122,6 @@ static int create_subprocess(const char *cmd,
|
||||
}
|
||||
|
||||
if(pid == 0){
|
||||
close(ptm);
|
||||
|
||||
int pts;
|
||||
|
||||
setsid();
|
||||
@ -134,6 +133,25 @@ static int create_subprocess(const char *cmd,
|
||||
dup2(pts, 1);
|
||||
dup2(pts, 2);
|
||||
|
||||
DIR *dir = opendir("/proc/self/fd");
|
||||
if(dir != NULL) {
|
||||
int badfd = dirfd(dir);
|
||||
|
||||
while(true) {
|
||||
struct dirent *entry = readdir(dir);
|
||||
if(entry == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
int fd = atoi(entry->d_name);
|
||||
if(fd > 2 && fd != badfd) {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
if (envp) {
|
||||
for (; *envp; ++envp) {
|
||||
putenv(*envp);
|
||||
|
Loading…
Reference in New Issue
Block a user