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
|
|
|
*/
|
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
#include <AK/Optional.h>
|
2019-09-06 16:34:26 +03:00
|
|
|
#include <AK/String.h>
|
2020-10-29 13:45:53 +03:00
|
|
|
#include <ctype.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>
|
2020-10-29 13:45:53 +03:00
|
|
|
#include <string.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);
|
|
|
|
}
|
|
|
|
|
2018-10-31 03:06:57 +03:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-02-18 15:16:33 +03:00
|
|
|
if (pledge("stdio proc", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-29 14:52:11 +03:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "-l")) {
|
|
|
|
for (size_t i = 0; i < NSIG; ++i) {
|
|
|
|
if (i && !(i % 5))
|
|
|
|
outln("");
|
2020-11-09 12:47:19 +03:00
|
|
|
out("{:2}) {:10}", i, getsignalname(i));
|
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;
|
|
|
|
if (argv[1][0] != '-')
|
|
|
|
print_usage_and_exit();
|
2020-10-29 13:45:53 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
if (!number.has_value()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("'{}' is not a valid signal name or number", &argv[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
|
|
|
}
|
2020-06-12 22:07:52 +03:00
|
|
|
auto pid_opt = String(argv[pid_argi]).to_int();
|
|
|
|
if (!pid_opt.has_value()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("'{}' is not a valid PID", argv[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
|
|
|
|
2020-04-26 03:41:14 +03:00
|
|
|
int rc = kill(pid, signum);
|
2019-02-28 13:45:45 +03:00
|
|
|
if (rc < 0)
|
|
|
|
perror("kill");
|
2018-10-31 03:06:57 +03:00
|
|
|
return 0;
|
|
|
|
}
|