Use PR_SET_PDEATHSIG to ensure child cleanup

This commit is contained in:
Eelco Dolstra 2014-08-21 15:31:43 +02:00
parent 163fdf292e
commit 809ca33806
3 changed files with 13 additions and 3 deletions

View file

@ -19,6 +19,10 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#endif #endif
#ifdef __linux__
#include <sys/prctl.h>
#endif
extern char * * environ; extern char * * environ;
@ -847,7 +851,8 @@ void killUser(uid_t uid)
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
pid_t startProcess(std::function<void()> fun, const string & errorPrefix) pid_t startProcess(std::function<void()> fun,
bool dieWithParent, const string & errorPrefix)
{ {
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) throw SysError("unable to fork"); if (pid == -1) throw SysError("unable to fork");
@ -855,6 +860,10 @@ pid_t startProcess(std::function<void()> fun, const string & errorPrefix)
if (pid == 0) { if (pid == 0) {
_writeToStderr = 0; _writeToStderr = 0;
try { try {
#if __linux__
if (dieWithParent && prctl(PR_SET_PDEATHSIG, SIGKILL) == -1)
throw SysError("setting death signal");
#endif
restoreAffinity(); restoreAffinity();
fun(); fun();
} catch (std::exception & e) { } catch (std::exception & e) {

View file

@ -267,7 +267,8 @@ void killUser(uid_t uid);
/* Fork a process that runs the given function, and return the child /* Fork a process that runs the given function, and return the child
pid to the caller. */ pid to the caller. */
pid_t startProcess(std::function<void()> fun, const string & errorPrefix = "error: "); pid_t startProcess(std::function<void()> fun, bool dieWithParent = true,
const string & errorPrefix = "error: ");
/* Run a program and return its stdout in a string (i.e., like the /* Run a program and return its stdout in a string (i.e., like the

View file

@ -779,7 +779,7 @@ static void daemonLoop(char * * argv)
processConnection(trusted); processConnection(trusted);
_exit(0); _exit(0);
}, "unexpected Nix daemon error: "); }, false, "unexpected Nix daemon error: ");
} catch (Interrupted & e) { } catch (Interrupted & e) {
throw; throw;