1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:07:35 +00:00

Added CProcessStatisticsReader to avoid having to parse /proc/all (#35)

* Added CProcessStatisticsReader to avoid having to parse /proc/all

* Took @awesomekling's feedbacks into account

* Fixed indentation and replaced tabs by spaces
This commit is contained in:
GuillaumeGas 2019-05-16 18:47:47 +02:00 committed by Andreas Kling
parent 174639b7f0
commit 0ba4ceb2cd
5 changed files with 164 additions and 81 deletions

View file

@ -2,7 +2,7 @@
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <LibCore/CFile.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <AK/AKString.h>
static void print_usage_and_exit()
@ -13,40 +13,16 @@ static void print_usage_and_exit()
static int kill_all(const String& process_name, const unsigned signum)
{
CFile file("/proc/all");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "killall failed to open /proc/all\n");
return 3;
HashMap<pid_t, CProcessStatistics> processes = CProcessStatisticsReader().get_map();
for (auto& it : processes) {
if (it.value.name == process_name) {
int ret = kill(it.value.pid, signum);
if (ret < 0)
perror("kill");
}
}
for (;;) {
auto line = file.read_line(1024);
if (line.is_empty())
break;
auto chomped = String((const char*)line.pointer(), line.size() - 1, Chomp);
auto parts = chomped.split_view(',');
if (parts.size() < 18)
break;
bool ok = false;
pid_t pid = parts[0].to_uint(ok);
String name = parts[11];
if (!ok) {
fprintf(stderr, "killall failed : couldn't convert %s to a valid pid\n", parts[0].characters());
return 4;
}
if (name == process_name) {
int ret = kill(pid, signum);
if (ret < 0)
perror("kill");
}
}
return 0;
}
@ -60,12 +36,12 @@ int main(int argc, char** argv)
print_usage_and_exit();
if (argc == 3) {
name_argi = 2;
name_argi = 2;
if (argv[1][0] != '-')
print_usage_and_exit();
signum = String(&argv[1][1]).to_uint(ok);
signum = String(&argv[1][1]).to_uint(ok);
if (!ok) {
printf("'%s' is not a valid signal number\n", &argv[1][1]);
return 2;