mirror of
https://codeberg.org/guix/guix.git
synced 2025-10-02 02:15:12 +00:00
daemon: Use ‘close_range’ where available.
* nix/libutil/util.cc (closeMostFDs) [HAVE_CLOSE_RANGE]: Use ‘close_range’ when ‘exceptions’ is empty. * config-daemon.ac: Check for <linux/close_range.h> and the ‘close_range’ symbol. Change-Id: I12fa3bde58b003fcce5ea5a1fee1dcf9a92c0359
This commit is contained in:
parent
92205bab4d
commit
f03e6eff2f
2 changed files with 20 additions and 8 deletions
|
@ -23,6 +23,10 @@
|
|||
#include <sys/prctl.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LINUX_CLOSE_RANGE_H
|
||||
# include <linux/close_range.h>
|
||||
#endif
|
||||
|
||||
|
||||
extern char * * environ;
|
||||
|
||||
|
@ -1087,12 +1091,19 @@ string runProgram(Path program, bool searchPath, const Strings & args)
|
|||
|
||||
void closeMostFDs(const set<int> & exceptions)
|
||||
{
|
||||
int maxFD = 0;
|
||||
maxFD = sysconf(_SC_OPEN_MAX);
|
||||
for (int fd = 0; fd < maxFD; ++fd)
|
||||
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO
|
||||
&& exceptions.find(fd) == exceptions.end())
|
||||
close(fd); /* ignore result */
|
||||
#ifdef HAVE_CLOSE_RANGE
|
||||
if (exceptions.empty())
|
||||
close_range(3, ~0U, 0);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
int maxFD = 0;
|
||||
maxFD = sysconf(_SC_OPEN_MAX);
|
||||
for (int fd = 0; fd < maxFD; ++fd)
|
||||
if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO
|
||||
&& exceptions.find(fd) == exceptions.end())
|
||||
close(fd); /* ignore result */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue