summaryrefslogtreecommitdiff
path: root/nix/libutil/util.cc
diff options
context:
space:
mode:
authorReepca Russelstein <reepca@russelstein.xyz>2025-04-17 23:32:03 -0500
committerJohn Kehayias <john.kehayias@protonmail.com>2025-06-24 10:07:56 -0400
commitbe8aca065118aa4485c02f991c51bea89034defa (patch)
tree79331f4087c66b62b47aa98100d57cb43e604163 /nix/libutil/util.cc
parent7173c2c0cad8afc9d8d1ad26f345b5a04f47716a (diff)
daemon: add and use spawn.cc and spawn.hh.
This adds a mechanism for manipulating and running "spawn phases" similarly to how builder-side code manipulates "build phases". The main difference is that spawn phases take a (reference to a) single structure that they can both read from and write to, with their writes being visible to subsequent phases. The base structure type for this is SpawnContext. It also adds some predefined phase sequences, namely basicSpawnPhases and cloneSpawnPhases, and exposes each of the actions performed by these phases. Finally, it modifies build.cc to replace runChild() with use of this new code. * nix/libutil/util.cc (keepOnExec, waitForMessage): new functions. * nix/libutil.util.hh (keepOnExec, waitForMessage): add prototypes. * nix/libutil/spawn.cc, nix/libutil/spawn.hh: new files. (addPhaseAfter, addPhaseBefore, prependPhase, appendPhase, deletePhase, replacePhase, reset_writeToStderrAction, restoreAffinityAction, setsidAction, earlyIOSetupAction, dropAmbientCapabilitiesAction, chrootAction, chdirAction, closeMostFDsAction, setPersonalityAction, oomSacrificeAction, setIDsAction, restoreSIGPIPEAction, setupSuccessAction, execAction, getBasicSpawnPhases, usernsInitSyncAction, usernsSetIDsAction, initLoopbackAction, setHostAndDomainAction, makeFilesystemsPrivateAction, makeChrootSeparateFilesystemAction, statfsToMountFlags, bindMount, mountIntoChroot, mountIntoChrootAction, mountProcAction, mountDevshmAction, mountDevptsAction, pivotRootAction, lockMountsAction, getCloneSpawnPhases, runChildSetup, runChildSetupEntry, cloneChild, idMapToIdentityMap, unshareAndInitUserns): new procedures. * nix/local.mk (libutil_a_SOURCES): add spawn.cc. (libutil_headers): add spawn.hh. * nix/libstore/build.cc (restoreSIGPIPE, DerivationGoal::runChild, childEntry): removed procedures. (DerivationGoal::{dirsInChroot,env,readiness}): removed. (execBuilderOrBuiltin, execBuilderOrBuiltinAction, clearRootWritePermsAction): new procedures. (DerivationGoal::startBuilder): modified to use a CloneSpawnContext if chroot builds are available, otherwise a SpawnContext. Change-Id: Ifd50110de077378ee151502eda62b99973d083bf Change-Id: I76e10d3f928cc30566e1e6ca79077196972349f8 spawn.cc, util.cc, util.hh changes Change-Id: I287320e63197cb4f65665ee5b3fdb3a0e125ebac Signed-off-by: John Kehayias <john.kehayias@protonmail.com>
Diffstat (limited to 'nix/libutil/util.cc')
-rw-r--r--nix/libutil/util.cc19
1 files changed, 19 insertions, 0 deletions
diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc
index c406325cdc..e71e6c170a 100644
--- a/nix/libutil/util.cc
+++ b/nix/libutil/util.cc
@@ -724,6 +724,18 @@ string drainFD(int fd)
}
+/* Wait on FD until MESSAGE has been read. */
+void waitForMessage(int fd, const char *message)
+{
+ size_t size = strlen(message);
+ char str[size] = { '\0' };
+ readFull(fd, (unsigned char*)str, size);
+ if (strncmp(str, message, size) != 0)
+ throw Error(format("did not receive message '%1%' on file descriptor %2%")
+ % message % fd);
+}
+
+
//////////////////////////////////////////////////////////////////////
@@ -1140,6 +1152,13 @@ void closeOnExec(int fd)
throw SysError("setting close-on-exec flag");
}
+void keepOnExec(int fd)
+{
+ int prev;
+ if ((prev = fcntl(fd, F_GETFD, 0)) == -1 ||
+ fcntl(fd, F_SETFD, prev & ~FD_CLOEXEC) == -1)
+ throw SysError("clearing close-on-exec flag");
+}
//////////////////////////////////////////////////////////////////////