parent
c6184dec6c
commit
af765a8eab
3 changed files with 19 additions and 5 deletions
|
@ -2556,7 +2556,7 @@ void DerivationGoal::runChild()
|
||||||
throw SysError(format("changing into '%1%'") % tmpDir);
|
throw SysError(format("changing into '%1%'") % tmpDir);
|
||||||
|
|
||||||
/* Close all other file descriptors. */
|
/* Close all other file descriptors. */
|
||||||
closeMostFDs(set<int>());
|
closeMostFDs({STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO});
|
||||||
|
|
||||||
#if __linux__
|
#if __linux__
|
||||||
/* Change the personality to 32-bit if we're doing an
|
/* Change the personality to 32-bit if we're doing an
|
||||||
|
|
|
@ -310,6 +310,7 @@ string readLine(int fd)
|
||||||
while (1) {
|
while (1) {
|
||||||
checkInterrupt();
|
checkInterrupt();
|
||||||
char ch;
|
char ch;
|
||||||
|
// FIXME: inefficient
|
||||||
ssize_t rd = read(fd, &ch, 1);
|
ssize_t rd = read(fd, &ch, 1);
|
||||||
if (rd == -1) {
|
if (rd == -1) {
|
||||||
if (errno != EINTR)
|
if (errno != EINTR)
|
||||||
|
@ -962,11 +963,24 @@ string runProgram(Path program, bool searchPath, const Strings & args,
|
||||||
|
|
||||||
void closeMostFDs(const set<int> & exceptions)
|
void closeMostFDs(const set<int> & exceptions)
|
||||||
{
|
{
|
||||||
|
#if __linux__
|
||||||
|
try {
|
||||||
|
for (auto & s : readDirectory("/proc/self/fd")) {
|
||||||
|
auto fd = std::stoi(s.name);
|
||||||
|
if (!exceptions.count(fd)) {
|
||||||
|
debug("closing leaked FD %d", fd);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} catch (SysError &) {
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int maxFD = 0;
|
int maxFD = 0;
|
||||||
maxFD = sysconf(_SC_OPEN_MAX);
|
maxFD = sysconf(_SC_OPEN_MAX);
|
||||||
for (int fd = 0; fd < maxFD; ++fd)
|
for (int fd = 0; fd < maxFD; ++fd)
|
||||||
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO
|
if (!exceptions.count(fd))
|
||||||
&& exceptions.find(fd) == exceptions.end())
|
|
||||||
close(fd); /* ignore result */
|
close(fd); /* ignore result */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -261,8 +261,8 @@ public:
|
||||||
list of strings. */
|
list of strings. */
|
||||||
std::vector<char *> stringsToCharPtrs(const Strings & ss);
|
std::vector<char *> stringsToCharPtrs(const Strings & ss);
|
||||||
|
|
||||||
/* Close all file descriptors except stdin, stdout, stderr, and those
|
/* Close all file descriptors except those listed in the given set.
|
||||||
listed in the given set. Good practice in child processes. */
|
Good practice in child processes. */
|
||||||
void closeMostFDs(const set<int> & exceptions);
|
void closeMostFDs(const set<int> & exceptions);
|
||||||
|
|
||||||
/* Set the close-on-exec flag for the given file descriptor. */
|
/* Set the close-on-exec flag for the given file descriptor. */
|
||||||
|
|
Loading…
Reference in a new issue