From 0e9b7f0cfd4aee527a79ac4c1f36c6f5f5c927b9 Mon Sep 17 00:00:00 2001 From: phantom10111 Date: Thu, 6 Feb 2014 21:38:50 +0100 Subject: [PATCH] Close all unneeded file descriptors before exec. --- jni/termExec.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/jni/termExec.cpp b/jni/termExec.cpp index 0f9c79e..ccd6d87 100644 --- a/jni/termExec.cpp +++ b/jni/termExec.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #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);