From 293bf9e88acd3979f27e81bafbcd44a77c10d0e6 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Tue, 28 Feb 2023 08:47:15 +0100 Subject: [PATCH] system: fix historic oversight in pid vs. name/pidfile reading related to #6351 If we kill a process and want to wait for it we will have to cache the PID file and check this one until it's gone. --- src/etc/inc/util.inc | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/etc/inc/util.inc b/src/etc/inc/util.inc index a85bcd889..55d654fe0 100644 --- a/src/etc/inc/util.inc +++ b/src/etc/inc/util.inc @@ -30,49 +30,44 @@ * POSSIBILITY OF SUCH DAMAGE. */ -require_once("IPv6.inc"); +require_once 'IPv6.inc'; function killbyname($procname, $sig = 'TERM', $waitforit = false) { - if (!is_process_running($procname)) { - return; - } - - mwexecf('/bin/pkill -%s %s', array($sig, $procname)); - - if (!$waitforit) { - return; - } - - while (is_process_running($procname)) { - usleep(200 * 1000); - } + _killbypid(shell_safe('/bin/pgrep -anx %s', $procname), $sig, $waitforit); } -function killbypid($pidfile, $sig = 'TERM', $waitforit = false) +function killbypid($pid_or_file, $sig = 'TERM', $waitforit = false) { - if (is_numeric($pidfile) && $pidfile > 1) { - mwexecf('/bin/kill -%s %s', array($sig, $pidfile)); - } elseif (isvalidpid($pidfile)) { - mwexecf('/bin/pkill -%s -F %s', array($sig, $pidfile)); - } else { + $pid = $pid_or_file; + + if (strpos($pid_or_file, '/') !== false) { + $pid = trim(@file_get_contents($pid_or_file) ?? ''); + } + + _killbypid($pid, $sig, $waitforit); +} + +function _killbypid($pid, $sig, $waitforit) +{ + if (!is_numeric($pid) || $pid <= 1) { return; } + mwexecf('/bin/kill -%s %s', [$sig, $pid]); + if (!$waitforit) { return; } - while (isvalidpid($pidfile)) { + while (mwexecf('/bin/kill -0 %s', $pid, true) == 0) { usleep(200 * 1000); } } function isvalidpid($pidfile) { - if (is_numeric($pidfile) && $pidfile > 1) { - return mwexecf('/bin/kill -0 %s', $pidfile, true) == 0; - } elseif (file_exists($pidfile)) { + if (file_exists($pidfile)) { return mwexecf('/bin/pgrep -nF %s', $pidfile, true) == 0; }