diff --git a/execution/src/posix/execution.cc b/execution/src/posix/execution.cc index 6dc0eb34..50a6a66a 100644 --- a/execution/src/posix/execution.cc +++ b/execution/src/posix/execution.cc @@ -317,10 +317,26 @@ namespace leatherman { namespace execution { #ifdef HAS_CLOSEFROM closefrom(STDERR_FILENO + 1); #else - for (uint64_t i = (STDERR_FILENO + 1); i < max_fd; ++i) { - close(i); + uint64_t fd; + const char* fdpath = "/proc/self/fd"; + std::list fd_list; + + if (is_directory(fdpath)) { + for (const directory_entry& dent : directory_iterator(fdpath)) { + fd = atol(dent.path().filename().c_str()); + if (fd >= (STDERR_FILENO + 1)) { + fd_list.push_back(fd); + } + } + for (auto fd : fd_list) { + close(fd); + } + } else { + for (uint64_t i = (STDERR_FILENO + 1); i < max_fd; ++i) { + close(i); + } } -#endif +#endif // HAS_CLOSEFROM // Execute the given program; this should not return if successful execve(program, const_cast(argv), const_cast(envp));