diff --git a/src/Net.cc b/src/Net.cc index 481804ce4c..8c7e976bbc 100644 --- a/src/Net.cc +++ b/src/Net.cc @@ -290,17 +290,6 @@ void net_run() while ( iosource_mgr->Size() || (BifConst::exit_only_after_terminate && ! terminating) ) { - // Note: only simple + portable way of detecting loss of parent - // process seems to be polling for change in PPID. There's platform - // specific ways if we do end up needing something more responsive - // and/or have to avoid overhead of polling, but maybe not worth - // the additional complexity: - // Linux: prctl(PR_SET_PDEATHSIG, ...) - // FreeBSD: procctl(PROC_PDEATHSIG_CTL) - // TODO: make this a proper timer - if ( zeek::supervised_node && zeek::supervised_node->parent_pid != getppid() ) - zeek_terminate_loop("supervised cluster node was orphaned"); - double ts; iosource::IOSource* src = iosource_mgr->FindSoonest(&ts); diff --git a/src/Supervisor.cc b/src/Supervisor.cc index 4899a65f8c..21b88279e5 100644 --- a/src/Supervisor.cc +++ b/src/Supervisor.cc @@ -13,6 +13,7 @@ #include "Reporter.h" #include "DebugLogger.h" #include "Val.h" +#include "Net.h" #include "NetVar.h" #include "zeek-config.h" #include "util.h" @@ -124,6 +125,29 @@ static std::string make_create_message(const Supervisor::NodeConfig& node) return fmt("create %s %s", node.name.data(), json_str.data()); } +ParentProcessCheckTimer::ParentProcessCheckTimer(double t, double arg_interval) + : Timer(t, TIMER_PPID_CHECK), interval(arg_interval) + { + } + +void ParentProcessCheckTimer::Dispatch(double t, int is_expire) + { + // Note: only simple + portable way of detecting loss of parent + // process seems to be polling for change in PPID. There's platform + // specific ways if we do end up needing something more responsive + // and/or have to avoid overhead of polling, but maybe not worth + // the additional complexity: + // Linux: prctl(PR_SET_PDEATHSIG, ...) + // FreeBSD: procctl(PROC_PDEATHSIG_CTL) + // Also note the Stem process has its own polling loop with similar logic. + if ( zeek::supervised_node->parent_pid != getppid() ) + zeek_terminate_loop("supervised node was orphaned"); + + if ( ! is_expire ) + timer_mgr->Add(new ParentProcessCheckTimer(network_time + interval, + interval)); + } + Supervisor::Supervisor(Supervisor::Config cfg, std::unique_ptr pipe, pid_t arg_stem_pid) @@ -714,6 +738,7 @@ std::optional Stem::Poll() // the additional complexity: // Linux: prctl(PR_SET_PDEATHSIG, ...) // FreeBSD: procctl(PROC_PDEATHSIG_CTL) + // Also note the similar polling methodology in ParentProcessCheckTimer. DBG_STEM("Stem suicide"); Shutdown(13); } diff --git a/src/Supervisor.h b/src/Supervisor.h index a8a5429f87..100496d056 100644 --- a/src/Supervisor.h +++ b/src/Supervisor.h @@ -12,6 +12,7 @@ #include #include "iosource/IOSource.h" +#include "Timer.h" #include "Pipe.h" #include "Flare.h" #include "NetVar.h" @@ -19,6 +20,18 @@ namespace zeek { +class ParentProcessCheckTimer : public Timer { +public: + + ParentProcessCheckTimer(double t, double arg_interval); + + void Dispatch(double t, int is_expire) override; + +protected: + + double interval; +}; + class Supervisor : public iosource::IOSource { public: diff --git a/src/Timer.cc b/src/Timer.cc index 1138deec79..40ac0696f4 100644 --- a/src/Timer.cc +++ b/src/Timer.cc @@ -37,6 +37,7 @@ const char* TimerNames[] = { "TCPConnectionPartialClose", "TCPConnectionResetTimer", "TriggerTimer", + "ParentProcessIDCheck", "TimerMgrExpireTimer", }; diff --git a/src/Timer.h b/src/Timer.h index 5eb0aadec8..ba3a98f61e 100644 --- a/src/Timer.h +++ b/src/Timer.h @@ -41,6 +41,7 @@ enum TimerType { TIMER_TCP_PARTIAL_CLOSE, TIMER_TCP_RESET, TIMER_TRIGGER, + TIMER_PPID_CHECK, TIMER_TIMERMGR_EXPIRE, }; const int NUM_TIMER_TYPES = int(TIMER_TIMERMGR_EXPIRE) + 1; diff --git a/src/main.cc b/src/main.cc index 6cd22b5ad2..7d7f763f62 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1481,6 +1481,9 @@ int main(int argc, char** argv) #endif + if ( zeek::supervised_node ) + timer_mgr->Add(new zeek::ParentProcessCheckTimer(1, 1)); + double time_net_start = current_time(true);; uint64_t mem_net_start_total;