1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 07:07:34 +00:00

Userland: Teach "kill" to understand signal names (not just numbers)

You can now do things like "kill -STOP pid" :^)

The getsignalbyname() helper function should probably move to LibC
or somewhere where it can be used by other signal related programs.
This commit is contained in:
Andreas Kling 2020-10-29 11:45:53 +01:00
parent d27a8e505f
commit a6b2598fba

View file

@ -26,9 +26,11 @@
#include <AK/Optional.h>
#include <AK/String.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void print_usage_and_exit()
@ -37,6 +39,55 @@ static void print_usage_and_exit()
exit(1);
}
static const char* signal_names[] = {
"INVAL",
"HUP",
"INT",
"QUIT",
"ILL",
"TRAP",
"ABRT",
"BUS",
"FPE",
"KILL",
"USR1",
"SEGV",
"USR2",
"PIPE",
"ALRM",
"TERM",
"STKFLT",
"CHLD",
"CONT",
"STOP",
"TSTP",
"TTIN",
"TTOU",
"URG",
"XCPU",
"XFSZ",
"VTALRM",
"PROF",
"WINCH",
"IO",
"INFO",
"SYS"
};
static_assert(sizeof(signal_names) == sizeof(const char*) * 32);
int getsignalbyname(const char* name)
{
ASSERT(name);
for (size_t i = 0; i < NSIG; ++i) {
auto* signal_name = signal_names[i];
if (!strcmp(signal_name, name))
return i;
}
errno = EINVAL;
return -1;
}
int main(int argc, char** argv)
{
if (pledge("stdio proc", nullptr) < 0) {
@ -52,9 +103,20 @@ int main(int argc, char** argv)
pid_argi = 2;
if (argv[1][0] != '-')
print_usage_and_exit();
auto number = StringView(&argv[1][1]).to_uint();
Optional<unsigned> number;
if (isalpha(argv[1][1])) {
int value = getsignalbyname(&argv[1][1]);
if (value >= 0 && value < NSIG)
number = value;
}
if (!number.has_value())
number = StringView(&argv[1][1]).to_uint();
if (!number.has_value()) {
printf("'%s' is not a valid signal number\n", &argv[1][1]);
printf("'%s' is not a valid signal name or number\n", &argv[1][1]);
return 2;
}
signum = number.value();