2019-06-07 09:49:31 +00:00
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <LibCore/CProcessStatisticsReader.h>
|
2019-05-08 16:52:37 +00:00
|
|
|
#include <signal.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <stdio.h>
|
2019-05-08 16:52:37 +00:00
|
|
|
#include <stdlib.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <unistd.h>
|
2019-05-08 16:52:37 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-07-10 11:56:28 +00:00
|
|
|
auto processes = CProcessStatisticsReader().get_all();
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2019-05-16 16:47:47 +00:00
|
|
|
for (auto& it : processes) {
|
2019-06-07 09:49:31 +00:00
|
|
|
if (it.value.name == process_name) {
|
2019-05-16 16:47:47 +00:00
|
|
|
int ret = kill(it.value.pid, signum);
|
|
|
|
if (ret < 0)
|
|
|
|
perror("kill");
|
|
|
|
}
|
2019-05-08 16:52:37 +00:00
|
|
|
}
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2019-05-08 16:52:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
unsigned signum = SIGTERM;
|
|
|
|
int name_argi = 1;
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2019-05-08 16:52:37 +00:00
|
|
|
if (argc != 2 && argc != 3)
|
|
|
|
print_usage_and_exit();
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2019-05-08 16:52:37 +00:00
|
|
|
if (argc == 3) {
|
2019-05-16 16:47:47 +00:00
|
|
|
name_argi = 2;
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2019-05-08 16:52:37 +00:00
|
|
|
if (argv[1][0] != '-')
|
|
|
|
print_usage_and_exit();
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2019-05-16 16:47:47 +00:00
|
|
|
signum = String(&argv[1][1]).to_uint(ok);
|
2019-05-08 16:52:37 +00:00
|
|
|
if (!ok) {
|
|
|
|
printf("'%s' is not a valid signal number\n", &argv[1][1]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kill_all(argv[name_argi], signum);
|
|
|
|
}
|