ohmu
·
2026-10-15
1#include "processes.h"
2
3#include "common/debug.h"
4#include "common/io.h"
5#include "common/strutil.h"
6
7#include <procfs.h>
8
9const char* ffDetectProcesses(const FFProcessesOptions* options, FFProcessesResult* result) {
10 FF_AUTO_CLOSE_DIR DIR* dir = opendir("/proc");
11 if (dir == nullptr) {
12 FF_DEBUG("opendir(\"/proc\") failed: %s", strerror(errno));
13 return "opendir(\"/proc\") failed";
14 }
15
16 int procfd = dirfd(dir);
17
18 struct dirent* entry;
19 while ((entry = readdir(dir)) != nullptr) {
20 if (ffCharIsDigit(entry->d_name[0])) {
21 char path[32];
22 char* pidEnd = ffStrCopy(path, entry->d_name, sizeof(path));
23 if (!options->countKprocs) {
24 pstatus_t status;
25 strcpy(pidEnd, "/status");
26 if (ffReadFileDataRelative(procfd, path, sizeof(status), &status) != (ssize_t) sizeof(status) ||
27 status.pr_flags & PR_ISSYS) {
28 continue; // The process may have exited, no permission or is a kernel process
29 }
30 }
31
32 strcpy(pidEnd, "/psinfo");
33 psinfo_t info;
34 if (ffReadFileDataRelative(procfd, path, sizeof(info), &info) == (ssize_t) sizeof(info)) {
35 ++result->processes;
36 result->threads += (uint32_t) info.pr_nlwp;
37 }
38 }
39 }
40
41 return nullptr;
42}