diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-07-10 16:50:51 +0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-07-10 16:58:09 +0200 |
commit | 8e9140cfdef9dbd1eb61e4c75c91d452ab5e4a74 (patch) | |
tree | d4480372c993c09c073e3561f3966f7595bcf2aa /src/download-via-ssh/download-via-ssh.cc | |
parent | 1114c7bd57bcab16255d5db5e6f66ae8dece7b1e (diff) |
Refactoring: Move all fork handling into a higher-order function
C++11 lambdas ftw.
Diffstat (limited to 'src/download-via-ssh/download-via-ssh.cc')
-rw-r--r-- | src/download-via-ssh/download-via-ssh.cc | 32 |
1 files changed, 8 insertions, 24 deletions
diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index 6cbcd9891..6834634f3 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -24,30 +24,14 @@ static std::pair<FdSink, FdSource> connect(const string & conn) Pipe to, from; to.create(); from.create(); - pid_t child = fork(); - switch (child) { - case -1: - throw SysError("unable to fork"); - case 0: - try { - restoreAffinity(); - if (dup2(to.readSide, STDIN_FILENO) == -1) - throw SysError("dupping stdin"); - if (dup2(from.writeSide, STDOUT_FILENO) == -1) - throw SysError("dupping stdout"); - execlp("ssh" - , "ssh" - , "-x" - , "-T" - , conn.c_str() - , "nix-store --serve" - , NULL); - throw SysError("executing ssh"); - } catch (std::exception & e) { - std::cerr << "error: " << e.what() << std::endl; - } - _exit(1); - } + startProcess([&]() { + if (dup2(to.readSide, STDIN_FILENO) == -1) + throw SysError("dupping stdin"); + if (dup2(from.writeSide, STDOUT_FILENO) == -1) + throw SysError("dupping stdout"); + execlp("ssh", "ssh", "-x", "-T", conn.c_str(), "nix-store --serve", NULL); + throw SysError("executing ssh"); + }); // If child exits unexpectedly, we'll EPIPE or EOF early. // If we exit unexpectedly, child will EPIPE or EOF early. // So no need to keep track of it. |