diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-07-10T14·50+0200 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2014-07-10T14·58+0200 |
commit | 8e9140cfdef9dbd1eb61e4c75c91d452ab5e4a74 (patch) | |
tree | d4480372c993c09c073e3561f3966f7595bcf2aa /src/nix-daemon | |
parent | 1114c7bd57bcab16255d5db5e6f66ae8dece7b1e (diff) |
Refactoring: Move all fork handling into a higher-order function
C++11 lambdas ftw.
Diffstat (limited to 'src/nix-daemon')
-rw-r--r-- | src/nix-daemon/nix-daemon.cc | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/src/nix-daemon/nix-daemon.cc b/src/nix-daemon/nix-daemon.cc index 0464ac9653e9..265131c61376 100644 --- a/src/nix-daemon/nix-daemon.cc +++ b/src/nix-daemon/nix-daemon.cc @@ -872,40 +872,27 @@ static void daemonLoop() printMsg(lvlInfo, format("accepted connection from pid %1%, uid %2%") % clientPid % clientUid); /* Fork a child to handle the connection. */ - pid_t child; - child = fork(); - - switch (child) { - - case -1: - throw SysError("unable to fork"); - - case 0: - try { /* child */ - - /* Background the daemon. */ - if (setsid() == -1) - throw SysError(format("creating a new session")); - - /* Restore normal handling of SIGCHLD. */ - setSigChldAction(false); - - /* For debugging, stuff the pid into argv[1]. */ - if (clientPid != -1 && argvSaved[1]) { - string processName = int2String(clientPid); - strncpy(argvSaved[1], processName.c_str(), strlen(argvSaved[1])); - } + startProcess([&]() { + /* Background the daemon. */ + if (setsid() == -1) + throw SysError(format("creating a new session")); + + /* Restore normal handling of SIGCHLD. */ + setSigChldAction(false); + + /* For debugging, stuff the pid into argv[1]. */ + if (clientPid != -1 && argvSaved[1]) { + string processName = int2String(clientPid); + strncpy(argvSaved[1], processName.c_str(), strlen(argvSaved[1])); + } - /* Handle the connection. */ - from.fd = remote; - to.fd = remote; - processConnection(trusted); + /* Handle the connection. */ + from.fd = remote; + to.fd = remote; + processConnection(trusted); - } catch (std::exception & e) { - writeToStderr("unexpected Nix daemon error: " + string(e.what()) + "\n"); - } - exit(0); - } + _exit(0); + }, "unexpected Nix daemon error: "); } catch (Interrupted & e) { throw; |