diff options
author | Tom Hubrecht <github@mail.hubrecht.ovh> | 2024-05-28 13:44:39 +0200 |
---|---|---|
committer | Tom Hubrecht <github@mail.hubrecht.ovh> | 2024-05-29 11:01:34 +0200 |
commit | e81ed5f12d0702ed402faa8b5ee725f2203db60c (patch) | |
tree | 5f085e353b2b8db23d391347f9d09e294cee7834 /src/libstore/build/child.cc | |
parent | 2473e1253d37d4bbd3f7aad2ff269ad84747d527 (diff) |
util.{hh,cc}: Split out child.{hh,cc}
Change-Id: Iec4824e071f537b17dd62dbb8c01b8eec14e9783
Diffstat (limited to 'src/libstore/build/child.cc')
-rw-r--r-- | src/libstore/build/child.cc | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/libstore/build/child.cc b/src/libstore/build/child.cc new file mode 100644 index 000000000..a82a5eec9 --- /dev/null +++ b/src/libstore/build/child.cc @@ -0,0 +1,33 @@ +#include "current-process.hh" +#include "logging.hh" + +namespace nix { + +void commonChildInit() +{ + logger = makeSimpleLogger(); + + const static std::string pathNullDevice = "/dev/null"; + restoreProcessContext(false); + + /* Put the child in a separate session (and thus a separate + process group) so that it has no controlling terminal (meaning + that e.g. ssh cannot open /dev/tty) and it doesn't receive + terminal signals. */ + if (setsid() == -1) + throw SysError("creating a new session"); + + /* Dup stderr to stdout. */ + if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1) + throw SysError("cannot dup stderr into stdout"); + + /* Reroute stdin to /dev/null. */ + int fdDevNull = open(pathNullDevice.c_str(), O_RDWR); + if (fdDevNull == -1) + throw SysError("cannot open '%1%'", pathNullDevice); + if (dup2(fdDevNull, STDIN_FILENO) == -1) + throw SysError("cannot dup null device into stdin"); + close(fdDevNull); +} + +} |