1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-28 22:01:15 +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

@ -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();
startProcess([&]() {
/* Background the daemon. */
if (setsid() == -1)
throw SysError(format("creating a new session"));
switch (child) {
/* Restore normal handling of SIGCHLD. */
setSigChldAction(false);
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]));
}
/* Handle the connection. */
from.fd = remote;
to.fd = remote;
processConnection(trusted);
} catch (std::exception & e) {
writeToStderr("unexpected Nix daemon error: " + string(e.what()) + "\n");
/* 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]));
}
exit(0);
}
/* Handle the connection. */
from.fd = remote;
to.fd = remote;
processConnection(trusted);
_exit(0);
}, "unexpected Nix daemon error: ");
} catch (Interrupted & e) {
throw;