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
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.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>
|
2020-10-29 10:45:53 +00:00
|
|
|
#include <ctype.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") {
|
2020-10-29 11:52:11 +00:00
|
|
|
for (size_t i = 0; i < NSIG; ++i) {
|
|
|
|
if (i && !(i % 5))
|
|
|
|
outln("");
|
2020-11-09 09:47:19 +00:00
|
|
|
out("{:2}) {:10}", i, getsignalname(i));
|
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;
|
|
|
|
|
2022-02-12 10:08:44 +00:00
|
|
|
if (isalpha(strings[1][1])) {
|
|
|
|
int value = getsignalbyname(&strings[1][1]);
|
2020-10-29 10:45:53 +00:00
|
|
|
if (value >= 0 && value < NSIG)
|
|
|
|
number = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!number.has_value())
|
2022-02-12 10:08:44 +00:00
|
|
|
number = strings[1].substring_view(1, 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
|
|
|
|
2020-04-26 00:41:14 +00:00
|
|
|
int rc = kill(pid, signum);
|
2019-02-28 10:45:45 +00:00
|
|
|
if (rc < 0)
|
|
|
|
perror("kill");
|
2018-10-31 00:06:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|