2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2023-03-09 17:37:16 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2020-06-12 19:07:52 +00:00
|
|
|
#include <AK/Optional.h>
|
2022-02-12 08:43:49 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2018-11-06 09:46:40 +00:00
|
|
|
#include <signal.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <stdio.h>
|
2018-11-06 09:46:40 +00:00
|
|
|
#include <stdlib.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <unistd.h>
|
2018-10-31 00:06:57 +00:00
|
|
|
|
2018-11-06 09:46:40 +00:00
|
|
|
static void print_usage_and_exit()
|
|
|
|
{
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("usage: kill [-signal] <PID>");
|
2018-11-06 09:46:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-02-12 08:43:49 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-10-31 00:06:57 +00:00
|
|
|
{
|
2022-02-12 08:43:49 +00:00
|
|
|
|
|
|
|
TRY(Core::System::pledge("stdio proc"));
|
|
|
|
|
|
|
|
int argc = arguments.argc;
|
2022-02-12 10:08:44 +00:00
|
|
|
auto strings = arguments.strings;
|
2020-02-18 12:16:33 +00:00
|
|
|
|
2022-02-12 10:08:44 +00:00
|
|
|
if (argc == 2 && strings[1] == "-l") {
|
2023-05-30 17:12:00 +00:00
|
|
|
size_t valid_signal_count = 0;
|
|
|
|
for (size_t i = 1; i < NSIG; ++i) {
|
|
|
|
if (valid_signal_count && valid_signal_count % 5 == 0)
|
2020-10-29 11:52:11 +00:00
|
|
|
outln("");
|
2023-05-30 17:12:00 +00:00
|
|
|
auto const* signal_name = getsignalname(i);
|
|
|
|
// This excludes SIGCANCEL, which is intended for internal use only
|
|
|
|
if (!signal_name)
|
|
|
|
continue;
|
|
|
|
valid_signal_count++;
|
|
|
|
out("{:2}) {:10}", i, signal_name);
|
2020-10-29 11:52:11 +00:00
|
|
|
}
|
|
|
|
outln("");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:46:40 +00:00
|
|
|
if (argc != 2 && argc != 3)
|
|
|
|
print_usage_and_exit();
|
|
|
|
unsigned signum = SIGTERM;
|
|
|
|
int pid_argi = 1;
|
|
|
|
if (argc == 3) {
|
|
|
|
pid_argi = 2;
|
2022-02-12 10:08:44 +00:00
|
|
|
if (strings[1][0] != '-')
|
2018-11-06 09:46:40 +00:00
|
|
|
print_usage_and_exit();
|
2020-10-29 10:45:53 +00:00
|
|
|
|
|
|
|
Optional<unsigned> number;
|
|
|
|
|
2023-03-09 17:37:16 +00:00
|
|
|
if (is_ascii_alpha(strings[1][1])) {
|
2022-02-12 10:08:44 +00:00
|
|
|
int value = getsignalbyname(&strings[1][1]);
|
2023-05-30 17:11:56 +00:00
|
|
|
if (value > 0 && value < NSIG)
|
2020-10-29 10:45:53 +00:00
|
|
|
number = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!number.has_value())
|
2023-05-30 17:12:03 +00:00
|
|
|
number = strings[1].substring_view(1).to_uint();
|
2020-10-29 10:45:53 +00:00
|
|
|
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!number.has_value()) {
|
2022-02-12 10:08:44 +00:00
|
|
|
warnln("'{}' is not a valid signal name or number", &strings[1][1]);
|
2018-11-06 09:46:40 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2020-06-12 19:07:52 +00:00
|
|
|
signum = number.value();
|
2018-11-06 09:46:40 +00:00
|
|
|
}
|
2022-02-12 10:08:44 +00:00
|
|
|
auto pid_opt = strings[pid_argi].to_int();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!pid_opt.has_value()) {
|
2022-02-12 10:08:44 +00:00
|
|
|
warnln("'{}' is not a valid PID", strings[pid_argi]);
|
2018-11-06 09:46:40 +00:00
|
|
|
return 3;
|
2018-10-31 00:06:57 +00:00
|
|
|
}
|
2020-06-12 19:07:52 +00:00
|
|
|
pid_t pid = pid_opt.value();
|
2018-10-31 00:06:57 +00:00
|
|
|
|
2023-05-30 17:11:46 +00:00
|
|
|
TRY(Core::System::kill(pid, signum));
|
2018-10-31 00:06:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|