summaryrefslogtreecommitdiff
path: root/nix/libutil
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2020-09-11 12:34:25 +0200
committerLudovic Courtès <ludo@gnu.org>2020-09-14 15:42:55 +0200
commitee9dff34f9317509cb2b833d07a0d5e01a36a4ae (patch)
tree47027d9453eb3fe69f61bdc31d6d480a20e1217a /nix/libutil
parent27cc51c269fbe9d2ca65711d281c63ae441a9b4a (diff)
daemon: Move 'Agent' to libutil.
* nix/libstore/build.cc (DerivationGoal::tryBuildHook): Add "offload" to 'args' and pass settings.guixProgram as the first argument to Agent::Agent. (pathNullDevice, commonChildInit, Agent, Agent::Agent) (Agent::~Agent): Move to... * nix/libutil/util.cc: ... here. * nix/libutil/util.hh (struct Agent, commonChildInit): New declarations.
Diffstat (limited to 'nix/libutil')
-rw-r--r--nix/libutil/util.cc84
-rw-r--r--nix/libutil/util.hh25
2 files changed, 109 insertions, 0 deletions
diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc
index 17d145b4c64..59a29813593 100644
--- a/nix/libutil/util.cc
+++ b/nix/libutil/util.cc
@@ -1142,5 +1142,89 @@ void ignoreException()
1142 } 1142 }
1143} 1143}
1144 1144
1145static const string pathNullDevice = "/dev/null";
1146
1147/* Common initialisation performed in child processes. */
1148void commonChildInit(Pipe & logPipe)
1149{
1150 /* Put the child in a separate session (and thus a separate
1151 process group) so that it has no controlling terminal (meaning
1152 that e.g. ssh cannot open /dev/tty) and it doesn't receive
1153 terminal signals. */
1154 if (setsid() == -1)
1155 throw SysError(format("creating a new session"));
1156
1157 /* Dup the write side of the logger pipe into stderr. */
1158 if (dup2(logPipe.writeSide, STDERR_FILENO) == -1)
1159 throw SysError("cannot pipe standard error into log file");
1160
1161 /* Dup stderr to stdout. */
1162 if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1)
1163 throw SysError("cannot dup stderr into stdout");
1164
1165 /* Reroute stdin to /dev/null. */
1166 int fdDevNull = open(pathNullDevice.c_str(), O_RDWR);
1167 if (fdDevNull == -1)
1168 throw SysError(format("cannot open `%1%'") % pathNullDevice);
1169 if (dup2(fdDevNull, STDIN_FILENO) == -1)
1170 throw SysError("cannot dup null device into stdin");
1171 close(fdDevNull);
1172}
1173
1174//////////////////////////////////////////////////////////////////////
1175
1176Agent::Agent(const string &command, const Strings &args)
1177{
1178 debug(format("starting agent '%1%'") % command);
1179
1180 /* Create a pipe to get the output of the child. */
1181 fromAgent.create();
1182
1183 /* Create the communication pipes. */
1184 toAgent.create();
1185
1186 /* Create a pipe to get the output of the builder. */
1187 builderOut.create();
1188
1189 /* Fork the hook. */
1190 pid = startProcess([&]() {
1191
1192 commonChildInit(fromAgent);
1193
1194 if (chdir("/") == -1) throw SysError("changing into `/");
1195
1196 /* Dup the communication pipes. */
1197 if (dup2(toAgent.readSide, STDIN_FILENO) == -1)
1198 throw SysError("dupping to-hook read side");
1199
1200 /* Use fd 4 for the builder's stdout/stderr. */
1201 if (dup2(builderOut.writeSide, 4) == -1)
1202 throw SysError("dupping builder's stdout/stderr");
1203
1204 Strings allArgs;
1205 allArgs.push_back(command);
1206 allArgs.insert(allArgs.end(), args.begin(), args.end()); // append
1207
1208 execv(command.c_str(), stringsToCharPtrs(allArgs).data());
1209
1210 throw SysError(format("executing `%1%'") % command);
1211 });
1212
1213 pid.setSeparatePG(true);
1214 fromAgent.writeSide.close();
1215 toAgent.readSide.close();
1216}
1217
1218
1219Agent::~Agent()
1220{
1221 try {
1222 toAgent.writeSide.close();
1223 pid.kill(true);
1224 } catch (...) {
1225 ignoreException();
1226 }
1227}
1228
1145 1229
1146} 1230}
diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh
index 9e3c14bdd48..13cff44316d 100644
--- a/nix/libutil/util.hh
+++ b/nix/libutil/util.hh
@@ -264,6 +264,29 @@ public:
264 void setKillSignal(int signal); 264 void setKillSignal(int signal);
265}; 265};
266 266
267/* An "agent" is a helper program that runs in the background and that we talk
268 to over pipes, such as the "guix offload" program. */
269struct Agent
270{
271 /* Pipes for talking to the agent. */
272 Pipe toAgent;
273
274 /* Pipe for the agent's standard output/error. */
275 Pipe fromAgent;
276
277 /* Pipe for build standard output/error--e.g., for build processes started
278 by "guix offload". */
279 Pipe builderOut;
280
281 /* The process ID of the agent. */
282 Pid pid;
283
284 /* The command and arguments passed to the agent. */
285 Agent(const string &command, const Strings &args);
286
287 ~Agent();
288};
289
267 290
268/* Kill all processes running under the specified uid by sending them 291/* Kill all processes running under the specified uid by sending them
269 a SIGKILL. */ 292 a SIGKILL. */
@@ -295,6 +318,8 @@ void closeMostFDs(const set<int> & exceptions);
295/* Set the close-on-exec flag for the given file descriptor. */ 318/* Set the close-on-exec flag for the given file descriptor. */
296void closeOnExec(int fd); 319void closeOnExec(int fd);
297 320
321/* Common initialisation performed in child processes. */
322void commonChildInit(Pipe & logPipe);
298 323
299/* User interruption. */ 324/* User interruption. */
300 325