#include "strawberry/process_monitor.hpp" #include #include #include #include #include #include #include #include #include #if defined(__linux__) #include #include #include #include #endif #ifndef STRAWBERRY_HAVE_LIBBPF #define STRAWBERRY_HAVE_LIBBPF 0 #endif namespace { #if defined(__linux__) && STRAWBERRY_HAVE_LIBBPF bool ends_with(const std::string& value, const std::string& suffix) { return value.size() >= suffix.size() && value.compare(value.size() - suffix.size(), suffix.size(), suffix) == 0; } bool ensure_tracefs_mounted() { if (::mount("tracefs", "/sys/kernel/tracing", "tracefs", 0, nullptr) == 0) { return true; } if (errno == EBUSY) { return true; } std::cerr << "warning: mount(tracefs) failed: " << std::strerror(errno) << std::endl; return false; } #endif } // namespace int main() { #if !defined(__linux__) || !STRAWBERRY_HAVE_LIBBPF std::cout << "SKIP: process_monitor_live_smoke requires a Linux build with libbpf support" << std::endl; return 0; #else ::setenv("STRAWBERRY_PROCESS_MONITOR_BACKEND", "bpf", 1); (void) ensure_tracefs_mounted(); strawberry::ProcessMonitor monitor; if (!monitor.initialize()) { std::cerr << "Failed to initialize ProcessMonitor with forced BPF backend." << std::endl; std::cerr << "Tip: run this on Linux with sufficient privileges (for example, inside a privileged container)." << std::endl; return 1; } std::mutex mutex; std::condition_variable cv; std::vector events; if (!monitor.start([&](const strawberry::ProcessInfo& info) { { std::lock_guard lock(mutex); events.push_back(info); } cv.notify_all(); })) { std::cerr << "Failed to start ProcessMonitor." << std::endl; return 1; } pid_t child = ::fork(); if (child < 0) { std::cerr << "fork() failed: " << std::strerror(errno) << std::endl; monitor.stop(); return 1; } if (child == 0) { char* const argv[] = { const_cast("echo"), const_cast("strawberry_process_monitor_live_smoke"), nullptr, }; ::execv("/bin/echo", argv); std::perror("execv"); _exit(127); } int status = 0; (void) ::waitpid(child, &status, 0); strawberry::ProcessInfo matched_event; bool observed = false; { std::unique_lock lock(mutex); observed = cv.wait_for(lock, std::chrono::seconds(5), [&]() { for (const auto& event : events) { if (event.executable_path == "/bin/echo" || ends_with(event.executable_path, "/echo")) { matched_event = event; return true; } } return false; }); } const bool fork_tracepoint = monitor.fork_tracepoint_attached(); monitor.stop(); // Optional enrichment, but it should attach on current kernels (it failed // on 7.x until the tracepoint struct used CO-RE). When attached, the // exec event must carry a parent pid from it. std::cout << "fork tracepoint attached: " << (fork_tracepoint ? "yes" : "no") << std::endl; if (!observed) { std::cerr << "Did not observe the expected exec event for child PID " << child << std::endl; std::lock_guard lock(mutex); for (const auto& event : events) { std::cerr << "Observed event: pid=" << event.pid << " executable_path=" << event.executable_path << " cmdline=" << event.cmdline << std::endl; } return 1; } if (matched_event.image_identity.empty()) { std::cerr << "Observed exec event, but the executable image was not resolved." << std::endl; return 1; } if (matched_event.cmdline.find("strawberry_process_monitor_live_smoke") == std::string::npos) { std::cerr << "Observed exec event, but the cmdline did not contain the expected argument." << std::endl; std::cerr << "cmdline=" << matched_event.cmdline << std::endl; return 1; } if (fork_tracepoint && matched_event.parent_pid <= 0) { std::cerr << "fork tracepoint attached, but the exec event has no parent pid" << std::endl; return 1; } if (matched_event.pid != child) { std::cout << "NOTE: observed exec event path match, but callback pid " << matched_event.pid << " differed from child pid " << child << " (possible namespace/kernel pid difference)." << std::endl; } std::cout << "Observed exec event:" << std::endl; std::cout << " pid=" << matched_event.pid << std::endl; std::cout << " executable_path=" << matched_event.executable_path << std::endl; std::cout << " cmdline=" << matched_event.cmdline << std::endl; std::cout << " image=" << matched_event.image_identity << std::endl; std::cout << "PASS: live libbpf exec-event smoke succeeded" << std::endl; return 0; #endif }