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