include/strawberry/platform_process.hpp answers what /proc answers on Linux: running PIDs (proc_listallpids), executable path (proc_pidpath), parent and start time (sysctl KERN_PROC_PID, readable for every process), argv (KERN_PROCARGS2) and a boot id (kern.bootsessionuuid). A process is identified by its start time in microseconds since the epoch plus the boot id, so node ids survive PID reuse as on Linux. It is header-only, so the per-test source lists stay as they are. Users on macOS: resolve_process_ref (default root), the pipeline's image hashing and exec-decision actor, and YaraScanner::scan_process (scans the image by path). A synthetic procfs root still takes the procfs code, so the existing procfs tests run on macOS too. The resource governor samples Mach/sysctl on macOS: own CPU from getrusage, RSS from task_info, load from getloadavg, available memory from host_statistics64 (free + inactive + purgeable + speculative). The behavior engine knows macOS debuggers (debugserver, dtrace) and kext loaders (kernelmanagerd, kmutil), and macOS temp and Downloads paths count as drop locations. The Linux lists are unchanged. The KERN_PROCARGS2 parser is portable: a property test (exact argv recovery, truncation never invents data, total on junk) and the fuzz_procargs target run on every platform. test_platform_process checks the live helpers against this process; the daemon test reads the stopped state through sysctl on macOS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
2.7 KiB
C++
76 lines
2.7 KiB
C++
// Live process facts without procfs (macOS libproc/sysctl), plus the portable
|
|
// KERN_PROCARGS2 parser on every platform.
|
|
#include "strawberry/behavior_engine.hpp"
|
|
#include "strawberry/platform_process.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include <unistd.h>
|
|
|
|
using namespace strawberry;
|
|
|
|
namespace {
|
|
|
|
std::string procargs(int argc, const std::string& body) {
|
|
std::string buffer(sizeof(argc), '\0');
|
|
std::memcpy(buffer.data(), &argc, sizeof(argc));
|
|
return buffer + body;
|
|
}
|
|
|
|
void test_procargs2_examples() {
|
|
std::cout << "Test KERN_PROCARGS2 examples:" << std::endl;
|
|
using namespace std::string_literals;
|
|
assert(platform::procargs2_cmdline(procargs(2, "/bin/ls\0\0\0ls\0-la\0HOME=/x\0"s)) == "ls -la");
|
|
assert(platform::procargs2_cmdline(procargs(1, "/bin/sh\0sh\0"s)) == "sh");
|
|
assert(platform::procargs2_cmdline(procargs(3, "/bin/a\0a\0b"s)) == "a b"); // truncated
|
|
assert(platform::procargs2_cmdline(procargs(0, "/bin/a\0a\0"s)).empty());
|
|
assert(platform::procargs2_cmdline(procargs(-1, "/bin/a\0a\0"s)).empty());
|
|
assert(platform::procargs2_cmdline("ab").empty());
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
|
|
#if defined(__APPLE__)
|
|
void test_live_process_facts() {
|
|
std::cout << "Test libproc facts for this process:" << std::endl;
|
|
const int self = static_cast<int>(::getpid());
|
|
const auto pids = platform::list_pids();
|
|
assert(std::binary_search(pids.begin(), pids.end(), self));
|
|
|
|
const auto snapshot = platform::process_snapshot(self);
|
|
assert(snapshot && snapshot->pid == self);
|
|
assert(snapshot->parent_pid == static_cast<int>(::getppid()));
|
|
assert(snapshot->exe.find("test_platform_process") != std::string::npos);
|
|
assert(snapshot->start_time > 0);
|
|
assert(platform::process_start_time(self) == snapshot->start_time);
|
|
assert(platform::process_cmdline(self).find("test_platform_process") != std::string::npos);
|
|
assert(platform::boot_id().size() == 32);
|
|
|
|
const int fd = platform::open_process_image(self);
|
|
assert(fd >= 0);
|
|
::close(fd);
|
|
|
|
// The default procfs root means the live system on macOS.
|
|
const auto ref = resolve_process_ref(self);
|
|
assert(ref && ref->exe == snapshot->exe);
|
|
assert(ref->node_id == make_process_node_id(self, snapshot->start_time, platform::boot_id()));
|
|
assert(!platform::process_snapshot(-5));
|
|
std::cout << " PASSED" << std::endl;
|
|
}
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
std::cout << "Running platform process tests..." << std::endl << std::endl;
|
|
test_procargs2_examples();
|
|
#if defined(__APPLE__)
|
|
test_live_process_facts();
|
|
#endif
|
|
std::cout << std::endl << "All tests passed!" << std::endl;
|
|
return 0;
|
|
}
|