From a6b2598fba21b28414b04f80d9305b0a5a1f640d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 29 Oct 2020 11:45:53 +0100 Subject: [PATCH] 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. --- Userland/kill.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/Userland/kill.cpp b/Userland/kill.cpp index 2e9889929f..7858625d4f 100644 --- a/Userland/kill.cpp +++ b/Userland/kill.cpp @@ -26,9 +26,11 @@ #include #include +#include #include #include #include +#include #include 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 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();