From 8e9140cfdef9dbd1eb61e4c75c91d452ab5e4a74 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 10 Jul 2014 16:50:51 +0200 Subject: Refactoring: Move all fork handling into a higher-order function C++11 lambdas ftw. --- src/nix-store/nix-store.cc | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) (limited to 'src/nix-store') diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index bb5a9e2e0ba2..28b205b1fd7d 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -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(); -- cgit 1.4.1