1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-01 12:37:59 +02:00

Refactoring: Move all fork handling into a higher-order function

C++11 lambdas ftw.
This commit is contained in:
Eelco Dolstra 2014-07-10 16:50:51 +02:00
parent 1114c7bd57
commit 8e9140cfde
7 changed files with 128 additions and 206 deletions

View file

@ -939,27 +939,14 @@ static void opServe(Strings opFlags, Strings opArgs)
Pipe fromDecompressor;
fromDecompressor.create();
Pid pid;
pid = fork();
switch (pid) {
case -1:
throw SysError("unable to fork");
case 0: /* child */
try {
fromDecompressor.readSide.close();
if (dup2(fromDecompressor.writeSide, STDOUT_FILENO) == -1)
throw SysError("dupping stdout");
// FIXME: use absolute path.
execlp(compression.c_str(), compression.c_str(), "-d", NULL);
throw SysError(format("executing `%1%'") % compression);
} catch (std::exception & e) {
std::cerr << "error: " << e.what() << std::endl;
}
_exit(1);
}
Pid pid = startProcess([&]() {
fromDecompressor.readSide.close();
if (dup2(fromDecompressor.writeSide, STDOUT_FILENO) == -1)
throw SysError("dupping stdout");
// FIXME: use absolute path.
execlp(compression.c_str(), compression.c_str(), "-d", NULL);
throw SysError(format("executing `%1%'") % compression);
});
fromDecompressor.writeSide.close();