From 400f5c14e6d02de1e6f928ef542679cb591a0a1c Mon Sep 17 00:00:00 2001 From: Olivier Valentin Date: Mon, 9 Dec 2024 16:02:52 +0100 Subject: [PATCH] Skip parsing of zombie processes. --- collector/lib/ProcfsScraper.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/collector/lib/ProcfsScraper.cpp b/collector/lib/ProcfsScraper.cpp index a06779c95e..2348f8d92c 100644 --- a/collector/lib/ProcfsScraper.cpp +++ b/collector/lib/ProcfsScraper.cpp @@ -144,6 +144,26 @@ bool GetSocketINodes(int dirfd, uint64_t pid, UnorderedSet* sock_ino return true; } +// IsZombieProcess fetches the current state of the process pointed to by dirfd, and +// - returns true, if the process state is 'Z' +// - returns false otherwise +bool IsZombieProcess(int dirfd) { + FileHandle stat_file(FDHandle(openat(dirfd, "stat", O_RDONLY)), "r"); + if (!stat_file.valid()) { + return false; + } + + char linebuf[512]; + + if (fgets(linebuf, sizeof(linebuf), stat_file.get()) == nullptr) { + return false; + } + + auto state = ExtractProcessState(linebuf); + + return state && *state == 'Z'; +} + // GetContainerID retrieves the container ID of the process represented by dirfd. The container ID is extracted from // the cgroup. std::optional GetContainerID(int dirfd) { @@ -492,6 +512,10 @@ bool ReadContainerConnections(const char* proc_path, std::shared_ptr