mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:47:44 +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:
parent
174639b7f0
commit
0ba4ceb2cd
5 changed files with 164 additions and 81 deletions
|
@ -2,55 +2,32 @@
|
|||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <LibCore/CProcessStatisticsReader.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/ArgsParser.h>
|
||||
#include <AK/HashMap.h>
|
||||
|
||||
static int pid_of(const String& process_name, bool single_shot, bool omit_pid, pid_t pid)
|
||||
{
|
||||
bool displayed_at_least_one = false;
|
||||
|
||||
CFile file("/proc/all");
|
||||
if (!file.open(CIODevice::ReadOnly)) {
|
||||
fprintf(stderr, "pidof failed to open /proc/all\n");
|
||||
return 2;
|
||||
}
|
||||
HashMap<pid_t, CProcessStatistics> processes = CProcessStatisticsReader().get_map();
|
||||
|
||||
for (auto& it : processes) {
|
||||
if (it.value.name == process_name) {
|
||||
if (!omit_pid || (omit_pid && it.value.pid != pid)) {
|
||||
printf("%d ", it.value.pid);
|
||||
displayed_at_least_one = true;
|
||||
|
||||
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 current_pid = parts[0].to_uint(ok);
|
||||
String name = parts[11];
|
||||
|
||||
if (!ok) {
|
||||
fprintf(stderr, "pidof failed : couldn't convert %s to a valid pid\n", parts[0].characters());
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (name == process_name) {
|
||||
if (!omit_pid || (omit_pid && current_pid != pid)) {
|
||||
printf("%d ", current_pid);
|
||||
displayed_at_least_one = true;
|
||||
|
||||
if (single_shot)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (single_shot)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (displayed_at_least_one)
|
||||
printf("\n");
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -72,16 +49,16 @@ int main(int argc, char** argv)
|
|||
bool ok = false;
|
||||
String pid_str = args.get("o");
|
||||
|
||||
if (pid_str == "%PPID")
|
||||
pid = getppid();
|
||||
else
|
||||
pid = pid_str.to_uint(ok);
|
||||
if (pid_str == "%PPID")
|
||||
pid = getppid();
|
||||
else
|
||||
pid = pid_str.to_uint(ok);
|
||||
}
|
||||
|
||||
// We should have one single value : the process name
|
||||
Vector<String> values = args.get_single_values();
|
||||
if (values.size() == 0) {
|
||||
args_parser.print_usage();
|
||||
args_parser.print_usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue