about summary refs log tree commit diff
path: root/src/download-via-ssh
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-07-10T14·50+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-07-10T14·58+0200
commit8e9140cfdef9dbd1eb61e4c75c91d452ab5e4a74 (patch)
treed4480372c993c09c073e3561f3966f7595bcf2aa /src/download-via-ssh
parent1114c7bd57bcab16255d5db5e6f66ae8dece7b1e (diff)
Refactoring: Move all fork handling into a higher-order function
C++11 lambdas ftw.
Diffstat (limited to 'src/download-via-ssh')
-rw-r--r--src/download-via-ssh/download-via-ssh.cc32
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 6cbcd9891cf2..6834634f3d7b 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.