Skip to content

Commit 396cbe4

Browse files
committed
o2-sim: Harden sigkill
1 parent 43994b3 commit 396cbe4

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

run/o2sim_parallel.cxx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ void launchShutdownThread()
352352
}
353353
LOG(info) << "Shutdown timer expired ... force killing remaining children";
354354
for (auto p : gChildProcesses) {
355-
killpg(p, SIGKILL);
355+
if (p != 0 && killpg(p, 0) == 0) { // see if process still exists
356+
killpg(p, SIGKILL);
357+
}
356358
}
357359
};
358360
threads.push_back(std::thread(lambda));
@@ -440,6 +442,12 @@ int main(int argc, char* argv[])
440442
// we enable the forked version of the code by default
441443
setenv("ALICE_SIMFORKINTERNAL", "ON", 1);
442444

445+
// force execution as own process group
446+
if (setpgid(0, 0) == -1) {
447+
perror("setpgid");
448+
exit(1);
449+
}
450+
443451
TStopwatch timer;
444452
timer.Start();
445453
auto o2env = getenv("O2_ROOT");
@@ -703,7 +711,9 @@ int main(int argc, char* argv[])
703711
if (!shutdown_initiated) {
704712
shutdown_initiated = true;
705713
for (auto p : gChildProcesses) {
706-
killpg(p, SIGTERM);
714+
if (killpg(p, 0) == 0) {
715+
killpg(p, SIGTERM);
716+
}
707717
}
708718
}
709719
} else {
@@ -733,7 +743,9 @@ int main(int argc, char* argv[])
733743
LOG(info) << "Problem detected (or child received termination signal) ... shutting down whole system ";
734744
for (auto p : gChildProcesses) {
735745
LOG(info) << "TERMINATING " << p;
736-
killpg(p, SIGTERM); // <--- makes sure to shutdown "unknown" child pids via the group property
746+
if (killpg(p, 0) == 0) {
747+
killpg(p, SIGTERM); // <--- makes sure to shutdown "unknown" child pids via the group property
748+
}
737749
}
738750
LOG(error) << "SHUTTING DOWN DUE TO SIGNALED EXIT IN COMPONENT " << cpid;
739751
o2::simpubsub::publishMessage(externalpublishchannel, o2::simpubsub::simStatusString("O2SIM", "STATE", "FAILURE"));
@@ -751,7 +763,9 @@ int main(int argc, char* argv[])
751763
for (auto p : gChildProcesses) {
752764
if (p != mergerpid) {
753765
LOG(info) << "SHUTTING DOWN CHILD PROCESS (normal thread)" << p;
754-
killpg(p, SIGTERM);
766+
if (killpg(p, 0) == 0) {
767+
killpg(p, SIGTERM);
768+
}
755769
}
756770
}
757771
}

0 commit comments

Comments
 (0)