diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2020-09-11 12:34:25 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2020-09-14 15:42:55 +0200 |
| commit | ee9dff34f9317509cb2b833d07a0d5e01a36a4ae (patch) | |
| tree | 47027d9453eb3fe69f61bdc31d6d480a20e1217a | |
| parent | 27cc51c269fbe9d2ca65711d281c63ae441a9b4a (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.
| -rw-r--r-- | nix/libstore/build.cc | 118 | ||||
| -rw-r--r-- | nix/libutil/util.cc | 84 | ||||
| -rw-r--r-- | nix/libutil/util.hh | 25 |
3 files changed, 111 insertions, 116 deletions
diff --git a/nix/libstore/build.cc b/nix/libstore/build.cc index 346f92a8c3e..88f8d11103e 100644 --- a/nix/libstore/build.cc +++ b/nix/libstore/build.cc | |||
| @@ -80,9 +80,6 @@ namespace nix { | |||
| 80 | using std::map; | 80 | using std::map; |
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | static string pathNullDevice = "/dev/null"; | ||
| 84 | |||
| 85 | |||
| 86 | /* Forward definition. */ | 83 | /* Forward definition. */ |
| 87 | class Worker; | 84 | class Worker; |
| 88 | struct Agent; | 85 | struct Agent; |
| @@ -397,33 +394,6 @@ void Goal::trace(const format & f) | |||
| 397 | ////////////////////////////////////////////////////////////////////// | 394 | ////////////////////////////////////////////////////////////////////// |
| 398 | 395 | ||
| 399 | 396 | ||
| 400 | /* Common initialisation performed in child processes. */ | ||
| 401 | static void commonChildInit(Pipe & logPipe) | ||
| 402 | { | ||
| 403 | /* Put the child in a separate session (and thus a separate | ||
| 404 | process group) so that it has no controlling terminal (meaning | ||
| 405 | that e.g. ssh cannot open /dev/tty) and it doesn't receive | ||
| 406 | terminal signals. */ | ||
| 407 | if (setsid() == -1) | ||
| 408 | throw SysError(format("creating a new session")); | ||
| 409 | |||
| 410 | /* Dup the write side of the logger pipe into stderr. */ | ||
| 411 | if (dup2(logPipe.writeSide, STDERR_FILENO) == -1) | ||
| 412 | throw SysError("cannot pipe standard error into log file"); | ||
| 413 | |||
| 414 | /* Dup stderr to stdout. */ | ||
| 415 | if (dup2(STDERR_FILENO, STDOUT_FILENO) == -1) | ||
| 416 | throw SysError("cannot dup stderr into stdout"); | ||
| 417 | |||
| 418 | /* Reroute stdin to /dev/null. */ | ||
| 419 | int fdDevNull = open(pathNullDevice.c_str(), O_RDWR); | ||
| 420 | if (fdDevNull == -1) | ||
| 421 | throw SysError(format("cannot open `%1%'") % pathNullDevice); | ||
| 422 | if (dup2(fdDevNull, STDIN_FILENO) == -1) | ||
| 423 | throw SysError("cannot dup null device into stdin"); | ||
| 424 | close(fdDevNull); | ||
| 425 | } | ||
| 426 | |||
| 427 | /* Restore default handling of SIGPIPE, otherwise some programs will | 397 | /* Restore default handling of SIGPIPE, otherwise some programs will |
| 428 | randomly say "Broken pipe". */ | 398 | randomly say "Broken pipe". */ |
| 429 | static void restoreSIGPIPE() | 399 | static void restoreSIGPIPE() |
| @@ -586,91 +556,6 @@ void UserLock::kill() | |||
| 586 | killUser(uid); | 556 | killUser(uid); |
| 587 | } | 557 | } |
| 588 | 558 | ||
| 589 | |||
| 590 | ////////////////////////////////////////////////////////////////////// | ||
| 591 | |||
| 592 | |||
| 593 | /* An "agent" is a helper program that runs in the background and that we talk | ||
| 594 | to over pipes, such as the "guix offload" program. */ | ||
| 595 | struct Agent | ||
| 596 | { | ||
| 597 | /* Pipes for talking to the agent. */ | ||
| 598 | Pipe toAgent; | ||
| 599 | |||
| 600 | /* Pipe for the agent's standard output/error. */ | ||
| 601 | Pipe fromAgent; | ||
| 602 | |||
| 603 | /* Pipe for build standard output/error--e.g., for build processes started | ||
| 604 | by "guix offload". */ | ||
| 605 | Pipe builderOut; | ||
| 606 | |||
| 607 | /* The process ID of the agent. */ | ||
| 608 | Pid pid; | ||
| 609 | |||
| 610 | /* The 'guix' sub-command and arguments passed to the agent. */ | ||
| 611 | Agent(const string &command, const Strings &args); | ||
| 612 | |||
| 613 | ~Agent(); | ||
| 614 | }; | ||
| 615 | |||
| 616 | |||
| 617 | Agent::Agent(const string &command, const Strings &args) | ||
| 618 | { | ||
| 619 | debug(format("starting agent '%1%'") % command); | ||
| 620 | |||
| 621 | const Path &buildHook = settings.guixProgram; | ||
| 622 | |||
| 623 | /* Create a pipe to get the output of the child. */ | ||
| 624 | fromAgent.create(); | ||
| 625 | |||
| 626 | /* Create the communication pipes. */ | ||
| 627 | toAgent.create(); | ||
| 628 | |||
| 629 | /* Create a pipe to get the output of the builder. */ | ||
| 630 | builderOut.create(); | ||
| 631 | |||
| 632 | /* Fork the hook. */ | ||
| 633 | pid = startProcess([&]() { | ||
| 634 | |||
| 635 | commonChildInit(fromAgent); | ||
| 636 | |||
| 637 | if (chdir("/") == -1) throw SysError("changing into `/"); | ||
| 638 | |||
| 639 | /* Dup the communication pipes. */ | ||
| 640 | if (dup2(toAgent.readSide, STDIN_FILENO) == -1) | ||
| 641 | throw SysError("dupping to-hook read side"); | ||
| 642 | |||
| 643 | /* Use fd 4 for the builder's stdout/stderr. */ | ||
| 644 | if (dup2(builderOut.writeSide, 4) == -1) | ||
| 645 | throw SysError("dupping builder's stdout/stderr"); | ||
| 646 | |||
| 647 | Strings allArgs; | ||
| 648 | allArgs.push_back(buildHook); | ||
| 649 | allArgs.push_back(command); | ||
| 650 | allArgs.insert(allArgs.end(), args.begin(), args.end()); // append | ||
| 651 | |||
| 652 | execv(buildHook.c_str(), stringsToCharPtrs(allArgs).data()); | ||
| 653 | |||
| 654 | throw SysError(format("executing `%1% %2%'") % buildHook % command); | ||
| 655 | }); | ||
| 656 | |||
| 657 | pid.setSeparatePG(true); | ||
| 658 | fromAgent.writeSide.close(); | ||
| 659 | toAgent.readSide.close(); | ||
| 660 | } | ||
| 661 | |||
| 662 | |||
| 663 | Agent::~Agent() | ||
| 664 | { | ||
| 665 | try { | ||
| 666 | toAgent.writeSide.close(); | ||
| 667 | pid.kill(true); | ||
| 668 | } catch (...) { | ||
| 669 | ignoreException(); | ||
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | |||
| 674 | ////////////////////////////////////////////////////////////////////// | 559 | ////////////////////////////////////////////////////////////////////// |
| 675 | 560 | ||
| 676 | 561 | ||
| @@ -1593,13 +1478,14 @@ HookReply DerivationGoal::tryBuildHook() | |||
| 1593 | 1478 | ||
| 1594 | if (!worker.hook) { | 1479 | if (!worker.hook) { |
| 1595 | Strings args = { | 1480 | Strings args = { |
| 1481 | "offload", | ||
| 1596 | settings.thisSystem.c_str(), | 1482 | settings.thisSystem.c_str(), |
| 1597 | (format("%1%") % settings.maxSilentTime).str().c_str(), | 1483 | (format("%1%") % settings.maxSilentTime).str().c_str(), |
| 1598 | (format("%1%") % settings.printBuildTrace).str().c_str(), | 1484 | (format("%1%") % settings.printBuildTrace).str().c_str(), |
| 1599 | (format("%1%") % settings.buildTimeout).str().c_str() | 1485 | (format("%1%") % settings.buildTimeout).str().c_str() |
| 1600 | }; | 1486 | }; |
| 1601 | 1487 | ||
| 1602 | worker.hook = std::make_shared<Agent>("offload", args); | 1488 | worker.hook = std::make_shared<Agent>(settings.guixProgram, args); |
| 1603 | } | 1489 | } |
| 1604 | 1490 | ||
| 1605 | /* Tell the hook about system features (beyond the system type) | 1491 | /* Tell the hook about system features (beyond the system type) |
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 | ||
| 1145 | static const string pathNullDevice = "/dev/null"; | ||
| 1146 | |||
| 1147 | /* Common initialisation performed in child processes. */ | ||
| 1148 | void 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 | |||
| 1176 | Agent::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 | |||
| 1219 | Agent::~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. */ | ||
| 269 | struct 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. */ |
| 296 | void closeOnExec(int fd); | 319 | void closeOnExec(int fd); |
| 297 | 320 | ||
| 321 | /* Common initialisation performed in child processes. */ | ||
| 322 | void commonChildInit(Pipe & logPipe); | ||
| 298 | 323 | ||
| 299 | /* User interruption. */ | 324 | /* User interruption. */ |
| 300 | 325 | ||
