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