aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Hubrecht <github@mail.hubrecht.ovh>2024-05-28 13:44:39 +0200
committerTom Hubrecht <github@mail.hubrecht.ovh>2024-05-29 11:01:34 +0200
commite81ed5f12d0702ed402faa8b5ee725f2203db60c (patch)
tree5f085e353b2b8db23d391347f9d09e294cee7834 /src
parent2473e1253d37d4bbd3f7aad2ff269ad84747d527 (diff)
util.{hh,cc}: Split out child.{hh,cc}
Change-Id: Iec4824e071f537b17dd62dbb8c01b8eec14e9783
Diffstat (limited to 'src')
-rw-r--r--src/libstore/build/child.cc33
-rw-r--r--src/libstore/build/child.hh11
-rw-r--r--src/libstore/build/hook-instance.cc1
-rw-r--r--src/libstore/build/local-derivation-goal.cc1
-rw-r--r--src/libstore/meson.build2
-rw-r--r--src/libutil/util.cc29
-rw-r--r--src/libutil/util.hh6
7 files changed, 48 insertions, 35 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);
+}
+
+}
diff --git a/src/libstore/build/child.hh b/src/libstore/build/child.hh
new file mode 100644
index 000000000..3dfc552b9
--- /dev/null
+++ b/src/libstore/build/child.hh
@@ -0,0 +1,11 @@
+#pragma once
+///@file
+
+namespace nix {
+
+/**
+ * Common initialisation performed in child processes.
+ */
+void commonChildInit();
+
+}
diff --git a/src/libstore/build/hook-instance.cc b/src/libstore/build/hook-instance.cc
index 108722cfb..86f72486e 100644
--- a/src/libstore/build/hook-instance.cc
+++ b/src/libstore/build/hook-instance.cc
@@ -1,3 +1,4 @@
+#include "child.hh"
#include "file-system.hh"
#include "globals.hh"
#include "hook-instance.hh"
diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc
index c90910d29..d385ffb34 100644
--- a/src/libstore/build/local-derivation-goal.cc
+++ b/src/libstore/build/local-derivation-goal.cc
@@ -14,6 +14,7 @@
#include "cgroup.hh"
#include "personality.hh"
#include "namespaces.hh"
+#include "child.hh"
#include <regex>
#include <queue>
diff --git a/src/libstore/meson.build b/src/libstore/meson.build
index 98549f6d9..f776e9621 100644
--- a/src/libstore/meson.build
+++ b/src/libstore/meson.build
@@ -79,6 +79,7 @@ libstore_sources = files(
'store-api.cc',
'uds-remote-store.cc',
'worker-protocol.cc',
+ 'build/child.cc',
'build/derivation-goal.cc',
'build/drv-output-substitution-goal.cc',
'build/entry-points.cc',
@@ -96,6 +97,7 @@ libstore_sources = files(
libstore_headers = files(
'binary-cache-store.hh',
+ 'build/child.hh',
'build/derivation-goal.hh',
'build/drv-output-substitution-goal.hh',
'build/goal.hh',
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 7f9413f8d..aff26e32b 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -559,33 +559,4 @@ std::string showBytes(uint64_t bytes)
return fmt("%.2f MiB", bytes / (1024.0 * 1024.0));
}
-
-// FIXME: move to libstore/build
-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);
-}
-
}
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index de5f0a2ee..e704cd709 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -407,12 +407,6 @@ struct MaintainCount
/**
- * Common initialisation performed in child processes.
- */
-void commonChildInit();
-
-
-/**
* Bind a Unix domain socket to a path.
*/
void bind(int fd, const std::string & path);