2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-19 08:37:01 +03:00
|
|
|
* Copyright (c) 2022, Zachary Penn <zack@sysdevs.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
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-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2022-02-19 08:37:01 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-10-29 13:49:47 +03:00
|
|
|
#include <ctype.h>
|
2019-05-08 19:52:37 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static void print_usage_and_exit()
|
|
|
|
{
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("usage: killall [-signal] process_name");
|
2019-05-08 19:52:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-05-30 22:25:58 +03:00
|
|
|
static ErrorOr<int> kill_all(StringView process_name, unsigned const signum)
|
2019-05-08 19:52:37 +03:00
|
|
|
{
|
2022-12-08 16:50:31 +03:00
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
2019-06-07 12:49:31 +03:00
|
|
|
|
2022-12-08 16:50:31 +03:00
|
|
|
for (auto& process : all_processes.processes) {
|
2021-05-23 12:08:32 +03:00
|
|
|
if (process.name == process_name) {
|
2023-05-30 22:26:13 +03:00
|
|
|
if (auto maybe_error = Core::System::kill(process.pid, signum); maybe_error.is_error())
|
|
|
|
warnln("{}", maybe_error.release_error());
|
2019-05-16 19:47:47 +03:00
|
|
|
}
|
2019-05-08 19:52:37 +03:00
|
|
|
}
|
2019-06-07 12:49:31 +03:00
|
|
|
|
2019-05-08 19:52:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-19 08:37:01 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-05-08 19:52:37 +03:00
|
|
|
{
|
|
|
|
unsigned signum = SIGTERM;
|
|
|
|
int name_argi = 1;
|
2019-06-07 12:49:31 +03:00
|
|
|
|
2022-02-19 08:37:01 +03:00
|
|
|
if (arguments.argc != 2 && arguments.argc != 3)
|
2019-05-08 19:52:37 +03:00
|
|
|
print_usage_and_exit();
|
2019-06-07 12:49:31 +03:00
|
|
|
|
2022-02-19 08:37:01 +03:00
|
|
|
if (arguments.argc == 3) {
|
2019-05-16 19:47:47 +03:00
|
|
|
name_argi = 2;
|
2019-06-07 12:49:31 +03:00
|
|
|
|
2022-02-19 08:37:01 +03:00
|
|
|
if (arguments.argv[1][0] != '-')
|
2019-05-08 19:52:37 +03:00
|
|
|
print_usage_and_exit();
|
2019-06-07 12:49:31 +03:00
|
|
|
|
2020-10-29 13:49:47 +03:00
|
|
|
Optional<unsigned> number;
|
|
|
|
|
2023-03-09 20:37:16 +03:00
|
|
|
if (is_ascii_alpha(arguments.argv[1][1])) {
|
2022-02-19 08:37:01 +03:00
|
|
|
int value = getsignalbyname(&arguments.argv[1][1]);
|
2023-05-30 22:26:07 +03:00
|
|
|
if (value > 0 && value < NSIG)
|
2020-10-29 13:49:47 +03:00
|
|
|
number = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!number.has_value())
|
2023-05-30 22:25:58 +03:00
|
|
|
number = arguments.strings[1].substring_view(1).to_uint();
|
2020-10-29 13:49:47 +03:00
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
if (!number.has_value()) {
|
2022-02-19 08:37:01 +03:00
|
|
|
warnln("'{}' is not a valid signal name or number", &arguments.argv[1][1]);
|
2019-05-08 19:52:37 +03:00
|
|
|
return 2;
|
|
|
|
}
|
2020-06-12 22:07:52 +03:00
|
|
|
signum = number.value();
|
2019-05-08 19:52:37 +03:00
|
|
|
}
|
|
|
|
|
2022-02-19 08:37:01 +03:00
|
|
|
return kill_all(arguments.strings[name_argi], signum);
|
2019-05-08 19:52:37 +03:00
|
|
|
}
|