diff --git a/src/leader.cpp b/src/leader.cpp index 5958d78..4d8d82f 100644 --- a/src/leader.cpp +++ b/src/leader.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -45,6 +46,39 @@ struct Script { bool runOnAbort; }; +static void aggressive_recursive_kill(pid_t parent) { + DIR* proc = opendir("/proc"); + if(!proc) + return; + + while(struct dirent* de = readdir(proc)) { + if(!isdigit(*de->d_name)) + continue; + + char status_file[640]; + sprintf(status_file, "/proc/%s/status", de->d_name); + + FILE* status_fp = fopen(status_file, "rb"); + if(!status_fp) + continue; + + char status_buffer[512]; + int n = fread(status_buffer, 1, 512, status_fp); + if(char* p = (char*)memmem(status_buffer, n, "PPid:\t", 6)) { + pid_t ppid = strtol(p + 6, NULL, 10); + if(ppid == parent) { + pid_t pid = atoi(de->d_name); + aggressive_recursive_kill(pid); + fprintf(stderr, "[laminar] sending SIGKILL to pid %d\n", pid); + kill(pid, SIGKILL); + } + } + fclose(status_fp); + } + closedir(proc); +} + + class Leader final : public kj::TaskSet::ErrorHandler { public: Leader(kj::AsyncIoContext& ioContext, kj::Filesystem& fs, const char* jobName, uint runNumber); @@ -67,6 +101,7 @@ private: pid_t currentScriptPid; std::queue