Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
use Symfony\Component\Console\Exception\RuntimeException;
Expand Down Expand Up @@ -1006,6 +1007,12 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
throw new RuntimeException('Unable to subscribe to signal events. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
}

foreach (array_merge($commandSignals, $this->signalsToDispatchEvent) as $signal) {
if (!SignalRegistry::isValidSignal($signal)) {
throw new InvalidArgumentException(sprintf('Signal "%d" is not valid, make sure to use one of the "SIG*" constants as defined by the "pcntl" extension.', $signal));
}
}

if (Terminal::hasSttyAvailable()) {
$sttyMode = shell_exec('stty -g');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class SignalRegistry
{
private array $signalHandlers = [];

private static array $availableSignals;

public function __construct()
{
if (\function_exists('pcntl_async_signals')) {
Expand Down Expand Up @@ -42,6 +44,13 @@ public static function isSupported(): bool
return \function_exists('pcntl_signal');
}

public static function isValidSignal(int $signal): bool
{
static::$availableSignals ??= get_defined_constants(true)['pcntl'] ?? [];

return $signal < 32 && \in_array($signal, static::$availableSignals, true);
}

/**
* @internal
*/
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet;
Expand Down Expand Up @@ -2137,6 +2138,37 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals()
$this->assertSame($expected, $tester->getDisplay(true));
}

/**
* @requires extension pcntl
*/
public function testSignalIsNotAvailableInPcntlExtension()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Signal "40" is not valid, make sure to use one of the "SIG*" constants as defined by the "pcntl" extension.');

$command = new InvalidSignalableCommand();
$application = $this->createSignalableApplication($command, null);
$application->setCatchExceptions(false);

$application->run(new ArrayInput(['signal']));
}

/**
* @requires extension pcntl
*/
public function testSignalToDispatchIsNotAvailableInPcntlExtension()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Signal "40" is not valid, make sure to use one of the "SIG*" constants as defined by the "pcntl" extension.');

$command = new SignableCommand();
$application = $this->createSignalableApplication($command, null);
$application->setSignalsToDispatchEvent(40);
$application->setCatchExceptions(false);

$application->run(new ArrayInput(['signal']));
}

/**
* @group tty
*/
Expand Down Expand Up @@ -2283,6 +2315,23 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
}
}

#[AsCommand(name: 'signal')]
class InvalidSignalableCommand extends BaseSignableCommand implements SignalableCommandInterface
{
public function getSubscribedSignals(): array
{
return [40];
}

public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
{
$this->signaled = true;
$this->signalHandlers[] = __CLASS__;

return false;
}
}

#[AsCommand(name: 'signal')]
class TerminatableCommand extends BaseSignableCommand implements SignalableCommandInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,19 @@ public function testTwoCallbacksForASignalPreviousCallbackFromAnotherRegistry()
$this->assertTrue($isHandled1);
$this->assertTrue($isHandled2);
}

public function testIsValidSignalWithPcntlConstantButValueIsTooHigh()
{
$this->assertFalse(SignalRegistry::isValidSignal(\PCNTL_ENAMETOOLONG));
}

public function testIsValidSignalWithInvalidSignalCode()
{
$this->assertFalse(SignalRegistry::isValidSignal(-12));
}

public function testIsValidSignal()
{
$this->assertTrue(SignalRegistry::isValidSignal(\SIGINT));
}
}