ladybird/Userland/Utilities/killall.cpp
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00

71 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibCore/ProcessStatisticsReader.h>
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void print_usage_and_exit()
{
printf("usage: killall [-signal] process_name\n");
exit(1);
}
static int kill_all(const String& process_name, const unsigned signum)
{
auto processes = Core::ProcessStatisticsReader().get_all();
if (!processes.has_value())
return 1;
for (auto& it : processes.value()) {
if (it.value.name == process_name) {
int ret = kill(it.value.pid, signum);
if (ret < 0)
perror("kill");
}
}
return 0;
}
int main(int argc, char** argv)
{
unsigned signum = SIGTERM;
int name_argi = 1;
if (argc != 2 && argc != 3)
print_usage_and_exit();
if (argc == 3) {
name_argi = 2;
if (argv[1][0] != '-')
print_usage_and_exit();
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 = String(&argv[1][1]).to_uint();
if (!number.has_value()) {
printf("'%s' is not a valid signal name or number\n", &argv[1][1]);
return 2;
}
signum = number.value();
}
return kill_all(argv[name_argi], signum);
}