From cac9023a361a6ad3e98134cdbb6ceeb68304d047 Mon Sep 17 00:00:00 2001 From: swenzel Date: Wed, 28 May 2025 18:30:19 +0200 Subject: [PATCH] o2-sim: Harden sigkill --- run/o2sim_parallel.cxx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/run/o2sim_parallel.cxx b/run/o2sim_parallel.cxx index 24be9743e6a03..3e28428938b20 100644 --- a/run/o2sim_parallel.cxx +++ b/run/o2sim_parallel.cxx @@ -352,7 +352,9 @@ void launchShutdownThread() } LOG(info) << "Shutdown timer expired ... force killing remaining children"; for (auto p : gChildProcesses) { - killpg(p, SIGKILL); + if (p != 0 && killpg(p, 0) == 0) { // see if process still exists + killpg(p, SIGKILL); + } } }; threads.push_back(std::thread(lambda)); @@ -440,6 +442,12 @@ int main(int argc, char* argv[]) // we enable the forked version of the code by default setenv("ALICE_SIMFORKINTERNAL", "ON", 1); + // force execution as own process group + if (setpgid(0, 0) == -1) { + perror("setpgid"); + exit(1); + } + TStopwatch timer; timer.Start(); auto o2env = getenv("O2_ROOT"); @@ -703,7 +711,9 @@ int main(int argc, char* argv[]) if (!shutdown_initiated) { shutdown_initiated = true; for (auto p : gChildProcesses) { - killpg(p, SIGTERM); + if (killpg(p, 0) == 0) { + killpg(p, SIGTERM); + } } } } else { @@ -733,7 +743,9 @@ int main(int argc, char* argv[]) LOG(info) << "Problem detected (or child received termination signal) ... shutting down whole system "; for (auto p : gChildProcesses) { LOG(info) << "TERMINATING " << p; - killpg(p, SIGTERM); // <--- makes sure to shutdown "unknown" child pids via the group property + if (killpg(p, 0) == 0) { + killpg(p, SIGTERM); // <--- makes sure to shutdown "unknown" child pids via the group property + } } LOG(error) << "SHUTTING DOWN DUE TO SIGNALED EXIT IN COMPONENT " << cpid; o2::simpubsub::publishMessage(externalpublishchannel, o2::simpubsub::simStatusString("O2SIM", "STATE", "FAILURE")); @@ -751,7 +763,9 @@ int main(int argc, char* argv[]) for (auto p : gChildProcesses) { if (p != mergerpid) { LOG(info) << "SHUTTING DOWN CHILD PROCESS (normal thread)" << p; - killpg(p, SIGTERM); + if (killpg(p, 0) == 0) { + killpg(p, SIGTERM); + } } } }