summaryrefslogtreecommitdiff
path: root/nix
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2025-06-26 12:12:29 +0200
committerLudovic Courtès <ludo@gnu.org>2025-10-16 15:14:37 +0200
commitd1910384d3581dcbc564353a098089d1a52c08d6 (patch)
tree9a34ab5d84323b2bb8d166d62d17901871181eed /nix
parentc7b8f3ec1a25a31bc08e74910a5632c766da4971 (diff)
daemon: Export as little as needed from libutil/spawn.cc.
* nix/libutil/spawn.cc (reset_writeToStderrAction, restoreAffinityAction) (setsidAction, earlyIOSetupAction, dropAmbientCapabilitiesAction) (chrootAction, chdirAction, closeMostFDsAction, setPersonalityAction) (oomSacrificeAction, setIDsAction, setNoNewPrivsAction) (addSeccompFilterAction, restoreSIGPIPEAction, setupSuccessAction) (usernsInitSyncAction, usernsSetIDsAction, initLoopbackAction) (setHostAndDomainAction, makeFilesystemsPrivateAction) (makeChrootSeparateFilesystemAction, bindMount) (mountIntoChroot, mountIntoChrootAction, mountProcAction) (mountDevshmAction, mountDevptsAction, pivotRootAction) (idMapToIdentityMap, lockMountsAction, runChildSetupEntry): Add ‘static’ qualifier. * nix/libutil/spawn.hh: Remove the corresponding ‘extern’ declarations. Change-Id: I3156d72d866f22fa31aa9a843f116771763ccb61
Diffstat (limited to 'nix')
-rw-r--r--nix/libutil/spawn.cc70
-rw-r--r--nix/libutil/spawn.hh43
2 files changed, 37 insertions, 76 deletions
diff --git a/nix/libutil/spawn.cc b/nix/libutil/spawn.cc
index d3f4e5bf19a..7855275494f 100644
--- a/nix/libutil/spawn.cc
+++ b/nix/libutil/spawn.cc
@@ -144,19 +144,19 @@ void replacePhase(Phases & phases, string replaceLabel, Action newAction)
144 144
145/* A curated selection of predefined actions */ 145/* A curated selection of predefined actions */
146 146
147void reset_writeToStderrAction(SpawnContext & ctx) 147static void reset_writeToStderrAction(SpawnContext & ctx)
148{ 148{
149 _writeToStderr = 0; 149 _writeToStderr = 0;
150} 150}
151 151
152 152
153void restoreAffinityAction(SpawnContext & ctx) 153static void restoreAffinityAction(SpawnContext & ctx)
154{ 154{
155 restoreAffinity(); 155 restoreAffinity();
156} 156}
157 157
158 158
159void setsidAction(SpawnContext & ctx) 159static void setsidAction(SpawnContext & ctx)
160{ 160{
161 /* Puts the current process in a separate session, which implies a 161 /* Puts the current process in a separate session, which implies a
162 separate process group, so it doesn't receive group-directed signals 162 separate process group, so it doesn't receive group-directed signals
@@ -168,7 +168,7 @@ void setsidAction(SpawnContext & ctx)
168} 168}
169 169
170 170
171void earlyIOSetupAction(SpawnContext & ctx) 171static void earlyIOSetupAction(SpawnContext & ctx)
172{ 172{
173 for(auto i = ctx.earlyCloseFDs.begin(); i != ctx.earlyCloseFDs.end(); i++) 173 for(auto i = ctx.earlyCloseFDs.begin(); i != ctx.earlyCloseFDs.end(); i++)
174 if(close(*i) == -1) 174 if(close(*i) == -1)
@@ -198,7 +198,7 @@ void earlyIOSetupAction(SpawnContext & ctx)
198} 198}
199 199
200 200
201void dropAmbientCapabilitiesAction(SpawnContext & ctx) 201static void dropAmbientCapabilitiesAction(SpawnContext & ctx)
202{ 202{
203 /* Drop ambient capabilities such as CAP_CHOWN that might have been granted 203 /* Drop ambient capabilities such as CAP_CHOWN that might have been granted
204 when starting guix-daemon. */ 204 when starting guix-daemon. */
@@ -211,7 +211,7 @@ void dropAmbientCapabilitiesAction(SpawnContext & ctx)
211} 211}
212 212
213 213
214void chrootAction(SpawnContext & ctx) 214static void chrootAction(SpawnContext & ctx)
215{ 215{
216 if(ctx.doChroot) 216 if(ctx.doChroot)
217#if HAVE_CHROOT 217#if HAVE_CHROOT
@@ -223,7 +223,7 @@ void chrootAction(SpawnContext & ctx)
223} 223}
224 224
225 225
226void chdirAction(SpawnContext & ctx) 226static void chdirAction(SpawnContext & ctx)
227{ 227{
228 if(ctx.setcwd) 228 if(ctx.setcwd)
229 if(chdir(ctx.cwd.c_str()) == -1) 229 if(chdir(ctx.cwd.c_str()) == -1)
@@ -231,7 +231,7 @@ void chdirAction(SpawnContext & ctx)
231} 231}
232 232
233 233
234void closeMostFDsAction(SpawnContext & ctx) 234static void closeMostFDsAction(SpawnContext & ctx)
235{ 235{
236 if(ctx.closeMostFDs) closeMostFDs(ctx.preserveFDs); 236 if(ctx.closeMostFDs) closeMostFDs(ctx.preserveFDs);
237 for(auto i = ctx.preserveFDs.begin(); i != ctx.preserveFDs.end(); i++) 237 for(auto i = ctx.preserveFDs.begin(); i != ctx.preserveFDs.end(); i++)
@@ -239,7 +239,7 @@ void closeMostFDsAction(SpawnContext & ctx)
239} 239}
240 240
241 241
242void setPersonalityAction(SpawnContext & ctx) 242static void setPersonalityAction(SpawnContext & ctx)
243{ 243{
244 if(ctx.setPersona) 244 if(ctx.setPersona)
245#ifdef __linux__ 245#ifdef __linux__
@@ -251,7 +251,7 @@ void setPersonalityAction(SpawnContext & ctx)
251} 251}
252 252
253 253
254void oomSacrificeAction(SpawnContext & ctx) 254static void oomSacrificeAction(SpawnContext & ctx)
255{ 255{
256#ifdef __linux__ 256#ifdef __linux__
257 if(ctx.oomSacrifice) 257 if(ctx.oomSacrifice)
@@ -265,7 +265,7 @@ void oomSacrificeAction(SpawnContext & ctx)
265} 265}
266 266
267 267
268void setIDsAction(SpawnContext & ctx) 268static void setIDsAction(SpawnContext & ctx)
269{ 269{
270 if(ctx.setSupplementaryGroups) 270 if(ctx.setSupplementaryGroups)
271 if(setgroups(ctx.supplementaryGroups.size(), 271 if(setgroups(ctx.supplementaryGroups.size(),
@@ -285,7 +285,7 @@ void setIDsAction(SpawnContext & ctx)
285 throw SysError("setuid failed"); 285 throw SysError("setuid failed");
286} 286}
287 287
288void setNoNewPrivsAction(SpawnContext & ctx) 288static void setNoNewPrivsAction(SpawnContext & ctx)
289{ 289{
290 if(ctx.setNoNewPrivs) 290 if(ctx.setNoNewPrivs)
291#if __linux__ && defined(PR_SET_NO_NEW_PRIVS) 291#if __linux__ && defined(PR_SET_NO_NEW_PRIVS)
@@ -296,7 +296,7 @@ void setNoNewPrivsAction(SpawnContext & ctx)
296#endif 296#endif
297} 297}
298 298
299void addSeccompFilterAction(SpawnContext & ctx) 299static void addSeccompFilterAction(SpawnContext & ctx)
300{ 300{
301 if(ctx.addSeccompFilter) { 301 if(ctx.addSeccompFilter) {
302#if __linux__ && defined(PR_SET_SECCOMP) && defined(SECCOMP_MODE_FILTER) 302#if __linux__ && defined(PR_SET_SECCOMP) && defined(SECCOMP_MODE_FILTER)
@@ -316,7 +316,7 @@ void addSeccompFilterAction(SpawnContext & ctx)
316} 316}
317 317
318 318
319void restoreSIGPIPEAction(SpawnContext & ctx) 319static void restoreSIGPIPEAction(SpawnContext & ctx)
320{ 320{
321 /* Restore default handling of SIGPIPE, otherwise some programs will 321 /* Restore default handling of SIGPIPE, otherwise some programs will
322 randomly say "Broken pipe". */ 322 randomly say "Broken pipe". */
@@ -328,14 +328,14 @@ void restoreSIGPIPEAction(SpawnContext & ctx)
328} 328}
329 329
330 330
331void setupSuccessAction(SpawnContext & ctx) 331static void setupSuccessAction(SpawnContext & ctx)
332{ 332{
333 if(ctx.signalSetupSuccess) 333 if(ctx.signalSetupSuccess)
334 writeFull(STDERR_FILENO, "\n"); 334 writeFull(STDERR_FILENO, "\n");
335} 335}
336 336
337 337
338void execAction(SpawnContext & ctx) 338void execAction(SpawnContext & ctx) // kept public for use in 'build.cc'
339{ 339{
340 Strings envStrs; 340 Strings envStrs;
341 std::vector<char *> envPtrs; 341 std::vector<char *> envPtrs;
@@ -378,7 +378,7 @@ Phases getBasicSpawnPhases()
378} 378}
379 379
380 380
381void usernsInitSyncAction(SpawnContext & sctx) 381static void usernsInitSyncAction(SpawnContext & sctx)
382{ 382{
383#if CLONE_ENABLED 383#if CLONE_ENABLED
384 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 384 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -397,7 +397,7 @@ void usernsInitSyncAction(SpawnContext & sctx)
397} 397}
398 398
399 399
400void usernsSetIDsAction(SpawnContext & sctx) 400static void usernsSetIDsAction(SpawnContext & sctx)
401{ 401{
402#if CLONE_ENABLED 402#if CLONE_ENABLED
403 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 403 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -420,7 +420,7 @@ void usernsSetIDsAction(SpawnContext & sctx)
420} 420}
421 421
422 422
423void initLoopbackAction(SpawnContext & sctx) 423static void initLoopbackAction(SpawnContext & sctx)
424{ 424{
425#if CLONE_ENABLED 425#if CLONE_ENABLED
426 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 426 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -440,7 +440,7 @@ void initLoopbackAction(SpawnContext & sctx)
440} 440}
441 441
442 442
443void setHostAndDomainAction(SpawnContext & sctx) 443static void setHostAndDomainAction(SpawnContext & sctx)
444{ 444{
445#if CLONE_ENABLED 445#if CLONE_ENABLED
446 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 446 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -456,7 +456,7 @@ void setHostAndDomainAction(SpawnContext & sctx)
456} 456}
457 457
458 458
459void makeFilesystemsPrivateAction(SpawnContext & sctx) 459static void makeFilesystemsPrivateAction(SpawnContext & sctx)
460{ 460{
461#if CLONE_ENABLED && HAVE_SYS_MOUNT_H && defined(MS_REC) && defined(MS_PRIVATE) 461#if CLONE_ENABLED && HAVE_SYS_MOUNT_H && defined(MS_REC) && defined(MS_PRIVATE)
462 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 462 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -468,7 +468,7 @@ void makeFilesystemsPrivateAction(SpawnContext & sctx)
468} 468}
469 469
470 470
471void makeChrootSeparateFilesystemAction(SpawnContext & sctx) 471static void makeChrootSeparateFilesystemAction(SpawnContext & sctx)
472{ 472{
473#if CLONE_ENABLED && HAVE_SYS_MOUNT_H && defined(MS_BIND) 473#if CLONE_ENABLED && HAVE_SYS_MOUNT_H && defined(MS_BIND)
474 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 474 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -521,7 +521,7 @@ static int statfsToMountFlags(int f_flags)
521} 521}
522 522
523 523
524void bindMount(Path source, Path target, bool readOnly) 524static void bindMount(Path source, Path target, bool readOnly)
525{ 525{
526#if HAVE_SYS_MOUNT_H && defined(MS_BIND) 526#if HAVE_SYS_MOUNT_H && defined(MS_BIND)
527 struct stat st; 527 struct stat st;
@@ -595,9 +595,9 @@ void bindMount(Path source, Path target, bool readOnly)
595} 595}
596 596
597 597
598void mountIntoChroot(std::map<Path, Path> filesInChroot, 598static void mountIntoChroot(std::map<Path, Path> filesInChroot,
599 set<Path> readOnlyFiles, 599 set<Path> readOnlyFiles,
600 Path chrootRootDir) 600 Path chrootRootDir)
601{ 601{
602#if HAVE_SYS_MOUNT_H && defined(MS_BIND) 602#if HAVE_SYS_MOUNT_H && defined(MS_BIND)
603 for(auto i = filesInChroot.begin(); i != filesInChroot.end(); i++) { 603 for(auto i = filesInChroot.begin(); i != filesInChroot.end(); i++) {
@@ -612,7 +612,7 @@ void mountIntoChroot(std::map<Path, Path> filesInChroot,
612} 612}
613 613
614 614
615void mountIntoChrootAction(SpawnContext & sctx) 615static void mountIntoChrootAction(SpawnContext & sctx)
616{ 616{
617#if CLONE_ENABLED && HAVE_SYS_MOUNT_H && defined(MS_BIND) 617#if CLONE_ENABLED && HAVE_SYS_MOUNT_H && defined(MS_BIND)
618 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 618 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -623,7 +623,7 @@ void mountIntoChrootAction(SpawnContext & sctx)
623} 623}
624 624
625 625
626void mountProcAction(SpawnContext & sctx) 626static void mountProcAction(SpawnContext & sctx)
627{ 627{
628#if CLONE_ENABLED && HAVE_SYS_MOUNT_H 628#if CLONE_ENABLED && HAVE_SYS_MOUNT_H
629 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 629 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -637,7 +637,7 @@ void mountProcAction(SpawnContext & sctx)
637} 637}
638 638
639 639
640void mountDevshmAction(SpawnContext & sctx) 640static void mountDevshmAction(SpawnContext & sctx)
641{ 641{
642#if CLONE_ENABLED && HAVE_SYS_MOUNT_H 642#if CLONE_ENABLED && HAVE_SYS_MOUNT_H
643 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 643 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -651,7 +651,7 @@ void mountDevshmAction(SpawnContext & sctx)
651} 651}
652 652
653 653
654void mountDevptsAction(SpawnContext & sctx) 654static void mountDevptsAction(SpawnContext & sctx)
655{ 655{
656#if CLONE_ENABLED && HAVE_SYS_MOUNT_H 656#if CLONE_ENABLED && HAVE_SYS_MOUNT_H
657 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 657 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -673,7 +673,7 @@ void mountDevptsAction(SpawnContext & sctx)
673} 673}
674 674
675 675
676void pivotRootAction(SpawnContext & sctx) 676static void pivotRootAction(SpawnContext & sctx)
677{ 677{
678#if CLONE_ENABLED && HAVE_SYS_MOUNT_H 678#if CLONE_ENABLED && HAVE_SYS_MOUNT_H
679 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 679 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -700,7 +700,7 @@ void pivotRootAction(SpawnContext & sctx)
700} 700}
701 701
702 702
703string idMapToIdentityMap(const string & map) 703static string idMapToIdentityMap(const string & map)
704{ 704{
705 std::vector<string> mapLines = 705 std::vector<string> mapLines =
706 tokenizeString<std::vector<string> >(map, "\n"); 706 tokenizeString<std::vector<string> >(map, "\n");
@@ -720,7 +720,7 @@ string idMapToIdentityMap(const string & map)
720 * processes in it after unshare is called. So fork a child and have it do 720 * processes in it after unshare is called. So fork a child and have it do
721 * the initialization. */ 721 * the initialization. */
722void unshareAndInitUserns(int flags, const string & uidMap, 722void unshareAndInitUserns(int flags, const string & uidMap,
723 const string & gidMap, bool allowSetgroups) 723 const string & gidMap, bool allowSetgroups)
724{ 724{
725#if CLONE_ENABLED 725#if CLONE_ENABLED
726 pid_t pid_ = getpid(); 726 pid_t pid_ = getpid();
@@ -767,7 +767,7 @@ void unshareAndInitUserns(int flags, const string & uidMap,
767} 767}
768 768
769 769
770void lockMountsAction(SpawnContext & sctx) 770static void lockMountsAction(SpawnContext & sctx)
771{ 771{
772#if CLONE_ENABLED && HAVE_SYS_MOUNT_H 772#if CLONE_ENABLED && HAVE_SYS_MOUNT_H
773 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx; 773 CloneSpawnContext & ctx = (CloneSpawnContext &) sctx;
@@ -868,7 +868,7 @@ void runChildSetup(SpawnContext & ctx)
868} 868}
869 869
870 870
871int runChildSetupEntry(void *data) 871static int runChildSetupEntry(void *data)
872{ 872{
873 runChildSetup(* (SpawnContext *)data); 873 runChildSetup(* (SpawnContext *)data);
874 return 1; 874 return 1;
diff --git a/nix/libutil/spawn.hh b/nix/libutil/spawn.hh
index 5e75bcfb097..ac6f0d1c41e 100644
--- a/nix/libutil/spawn.hh
+++ b/nix/libutil/spawn.hh
@@ -115,48 +115,13 @@ void deletePhase(Phases & phases, string delLabel);
115 115
116void replacePhase(Phases & phases, string replaceLabel, Action newAction); 116void replacePhase(Phases & phases, string replaceLabel, Action newAction);
117 117
118Action reset_writeToStderrAction; 118Action execAction; // used from 'build.cc'
119Action restoreAffinityAction;
120Action setsidAction;
121Action earlyIOSetupAction;
122Action dropAmbientCapabilitiesAction;
123Action chrootAction;
124Action chdirAction;
125Action closeMostFDsAction;
126Action setPersonalityAction;
127Action oomSacrificeAction;
128Action setIDsAction;
129Action setNoNewPrivsAction;
130Action addSeccompFilterAction;
131Action restoreSIGPIPEAction;
132Action setupSuccessAction;
133Action execAction;
134 119
135Phases getBasicSpawnPhases(); 120Phases getBasicSpawnPhases();
136
137void bindMount(Path source, Path target, bool readOnly);
138
139void mountIntoChroot(std::map<Path, Path> filesInChroot,
140 set<Path> readOnlyFiles,
141 Path chrootRootDir);
142
143Action usernsInitSyncAction;
144Action usernsSetIDsAction;
145Action initLoopbackAction;
146Action setHostAndDomainAction;
147Action makeFilesystemsPrivateAction;
148Action makeChrootSeparateFilesystemAction;
149Action mountIntoChrootAction;
150Action mountProcAction;
151Action mountDevshmAction;
152Action mountDevptsAction;
153Action pivotRootAction;
154Action lockMountsAction;
155
156Phases getCloneSpawnPhases(); 121Phases getCloneSpawnPhases();
157 122
158/* Helpers */ 123/* Helpers */
159string idMapToIdentityMap(const string & map); 124
160void unshareAndInitUserns(int flags, const string & uidMap, 125void unshareAndInitUserns(int flags, const string & uidMap,
161 const string & gidMap, bool allowSetgroups); 126 const string & gidMap, bool allowSetgroups);
162 127
@@ -164,10 +129,6 @@ void unshareAndInitUserns(int flags, const string & uidMap,
164 * exiting in all cases. */ 129 * exiting in all cases. */
165void runChildSetup(SpawnContext & ctx); 130void runChildSetup(SpawnContext & ctx);
166 131
167/* Helper to call runChildSetup that can be passed to the variant of clone
168 * that expects a callback. */
169int runChildSetupEntry(void *data);
170
171/* Create a new process using clone that will immediately call runChildSetup 132/* Create a new process using clone that will immediately call runChildSetup
172 * with the provided CloneSpawnContext. Return the pid of the new process. */ 133 * with the provided CloneSpawnContext. Return the pid of the new process. */
173int cloneChild(CloneSpawnContext & ctx); 134int cloneChild(CloneSpawnContext & ctx);