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