Skip to content

Commit

Permalink
refactor: move pkill to binext
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <radialapps@gmail.com>
  • Loading branch information
pulsejet committed Oct 20, 2023
1 parent fd851b5 commit e406b74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 47 deletions.
5 changes: 2 additions & 3 deletions lib/Migration/Repair.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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;
Expand All @@ -26,8 +25,8 @@ public function run(IOutput $output): void
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()) {
Expand Down
46 changes: 44 additions & 2 deletions lib/Service/BinExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static function startGoVod(): ?string
// 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;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public static function startGoVod(): ?string
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 */
Expand Down Expand Up @@ -451,4 +451,46 @@ public static function testSystemPerl(string $path): string
/** @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
}
}
}
42 changes: 0 additions & 42 deletions lib/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,46 +482,4 @@ public static function callerNativeVersion(): ?string

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
}
}
}

0 comments on commit e406b74

Please sign in to comment.