从 PHP 中的信号编号获取信号名称
get signal name from signal number in PHP
假设我有一个信号编号,假设它是 15
,我怎样才能将其恢复为信号名称? (在 Linux 上,15 是 SIGTERM,所以在这种情况下,如果 运行 在 Linux 上,我想要字符串 "SIGTERM")
Linux 有 char *strsignal(int sig),不幸的是 PHP 似乎没有,但是对于 get_defined_constants(true)
,可能可以实现类似的东西,这是我的尝试:
function strsignal(int $signo): ?string
{
foreach (get_defined_constants(true)['pcntl'] as $name => $num) {
// the _ is to ignore SIG_IGN and SIG_DFL and SIG_ERR and SIG_BLOCK and SIG_UNBLOCK and SIG_SETMARK, and maybe more, who knows
if ($num === $signo && substr($name, 0, 3) === "SIG" && $name[3] !== "_") {
return $name;
}
}
return null;
}
和var_dump(strsignal(15));
returnsstring(7) "SIGTERM"
:)
- 在 php 上游此处请求 posix_strsignal():https://bugs.php.net/bug.php?id=78700
假设我有一个信号编号,假设它是 15
,我怎样才能将其恢复为信号名称? (在 Linux 上,15 是 SIGTERM,所以在这种情况下,如果 运行 在 Linux 上,我想要字符串 "SIGTERM")
Linux 有 char *strsignal(int sig),不幸的是 PHP 似乎没有,但是对于 get_defined_constants(true)
,可能可以实现类似的东西,这是我的尝试:
function strsignal(int $signo): ?string
{
foreach (get_defined_constants(true)['pcntl'] as $name => $num) {
// the _ is to ignore SIG_IGN and SIG_DFL and SIG_ERR and SIG_BLOCK and SIG_UNBLOCK and SIG_SETMARK, and maybe more, who knows
if ($num === $signo && substr($name, 0, 3) === "SIG" && $name[3] !== "_") {
return $name;
}
}
return null;
}
和var_dump(strsignal(15));
returnsstring(7) "SIGTERM"
:)
- 在 php 上游此处请求 posix_strsignal():https://bugs.php.net/bug.php?id=78700