From e406b7480620abad9457fc11e0b88af5a9192a2c Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Fri, 20 Oct 2023 12:42:22 -0700 Subject: [PATCH] refactor: move pkill to binext Signed-off-by: Varun Patil --- lib/Migration/Repair.php | 5 ++--- lib/Service/BinExt.php | 46 ++++++++++++++++++++++++++++++++++++++-- lib/Util.php | 42 ------------------------------------ 3 files changed, 46 insertions(+), 47 deletions(-) diff --git a/lib/Migration/Repair.php b/lib/Migration/Repair.php index b4398d5e..29f6b8ab 100644 --- a/lib/Migration/Repair.php +++ b/lib/Migration/Repair.php @@ -6,7 +6,6 @@ namespace OCA\Memories\Migration; use OCA\Memories\Db\AddMissingIndices; use OCA\Memories\Service\BinExt; -use OCA\Memories\Util; use OCP\IConfig; use OCP\Migration\IOutput; use OCP\Migration\IRepairStep; @@ -26,8 +25,8 @@ class Repair implements IRepairStep AddMissingIndices::run($output); // kill any instances of go-vod and exiftool - Util::pkill(BinExt::getName('go-vod')); - Util::pkill(BinExt::getName('exiftool')); + BinExt::pkill(BinExt::getName('go-vod')); + BinExt::pkill(BinExt::getName('exiftool')); // detect exiftool if ($path = BinExt::detectExiftool()) { diff --git a/lib/Service/BinExt.php b/lib/Service/BinExt.php index 1222b1de..d7a94d86 100644 --- a/lib/Service/BinExt.php +++ b/lib/Service/BinExt.php @@ -229,7 +229,7 @@ class BinExt // Check if disabled if (Util::getSystemConfig('memories.vod.disable')) { // Make sure it's dead, in case the user just disabled it - Util::pkill(self::getName('go-vod')); + self::pkill(self::getName('go-vod')); return null; } @@ -271,7 +271,7 @@ class BinExt file_put_contents($configFile, json_encode($env, JSON_PRETTY_PRINT)); // Kill the transcoder in case it's running - Util::pkill(self::getName('go-vod')); + self::pkill(self::getName('go-vod')); // Start transcoder /** @psalm-suppress ForbiddenCode */ @@ -451,4 +451,46 @@ class BinExt /** @psalm-suppress ForbiddenCode */ return shell_exec("{$path} -e 'print $^V;'") ?: 'unknown version'; } + + /** + * Kill all instances of a process by name. + * Similar to pkill, which may not be available on all systems. + * + * @param string $name Process name (only the first 12 characters are used) + */ + public static function pkill(string $name): void + { + // don't kill everything + if (empty($name)) { + return; + } + + // only use the first 12 characters + $name = substr($name, 0, 12); + + // check if ps or busybox is available + $ps = 'ps'; + + /** @psalm-suppress ForbiddenCode */ + if (!shell_exec('which ps')) { + if (!shell_exec('which busybox')) { + return; + } + + $ps = 'busybox ps'; + } + + // get pids using ps as array + /** @psalm-suppress ForbiddenCode */ + $pids = shell_exec("{$ps} -eao pid,comm | grep {$name} | awk '{print $1}'"); + if (null === $pids || empty($pids)) { + return; + } + $pids = array_filter(explode("\n", $pids)); + + // kill all pids + foreach ($pids as $pid) { + posix_kill((int) $pid, 9); // SIGKILL + } + } } diff --git a/lib/Util.php b/lib/Util.php index c4b5c3ab..44bef187 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -482,46 +482,4 @@ class Util return null; } - - /** - * Kill all instances of a process by name. - * Similar to pkill, which may not be available on all systems. - * - * @param string $name Process name (only the first 12 characters are used) - */ - public static function pkill(string $name): void - { - // don't kill everything - if (empty($name)) { - return; - } - - // only use the first 12 characters - $name = substr($name, 0, 12); - - // check if ps or busybox is available - $ps = 'ps'; - - /** @psalm-suppress ForbiddenCode */ - if (!shell_exec('which ps')) { - if (!shell_exec('which busybox')) { - return; - } - - $ps = 'busybox ps'; - } - - // get pids using ps as array - /** @psalm-suppress ForbiddenCode */ - $pids = shell_exec("{$ps} -eao pid,comm | grep {$name} | awk '{print $1}'"); - if (null === $pids || empty($pids)) { - return; - } - $pids = array_filter(explode("\n", $pids)); - - // kill all pids - foreach ($pids as $pid) { - posix_kill((int) $pid, 9); // SIGKILL - } - } }