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