From a9cb1783151607dc0e6bb5b38d054f4a4e3e10a1 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Tue, 20 Sep 2022 15:12:24 +0200 Subject: [PATCH] Better error messages for memory & swap --- src/detection/memory/memory.c | 6 ++++++ src/detection/memory/memory.h | 1 + src/detection/memory/memory_apple.c | 30 +++++++++++++++++++++++++---- src/detection/memory/memory_bsd.c | 27 +++++++++++++++++++++----- src/detection/memory/memory_linux.c | 11 ++++++++--- src/modules/memory.c | 4 ++-- 6 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/detection/memory/memory.c b/src/detection/memory/memory.c index bc6981186..14b86cc39 100644 --- a/src/detection/memory/memory.c +++ b/src/detection/memory/memory.c @@ -5,6 +5,9 @@ void ffDetectMemoryImpl(FFMemoryResult* memory); static void calculatePercentage(FFMemoryStorage* storage) { + if(storage->error.length == 0) + return; + if(storage->bytesTotal == 0) storage->percentage = 0; else @@ -14,6 +17,9 @@ static void calculatePercentage(FFMemoryStorage* storage) const FFMemoryResult* ffDetectMemory() { FF_DETECTION_INTERNAL_GUARD(FFMemoryResult, + ffStrbufInitA(&result.ram.error, 0); + ffStrbufInitA(&result.swap.error, 0); + ffDetectMemoryImpl(&result); calculatePercentage(&result.ram); calculatePercentage(&result.swap); diff --git a/src/detection/memory/memory.h b/src/detection/memory/memory.h index 85beaf255..a46d259c1 100644 --- a/src/detection/memory/memory.h +++ b/src/detection/memory/memory.h @@ -7,6 +7,7 @@ typedef struct FFMemoryStorage { + FFstrbuf error; uint64_t bytesUsed; uint64_t bytesTotal; uint8_t percentage; diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index 548098a65..8124e7265 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -4,27 +4,49 @@ #include #include -void ffDetectMemoryImpl(FFMemoryResult* memory) +static void detectRam(FFMemoryStorage* ram) { - memset(memory, 0, sizeof(FFMemoryResult)); - memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("hw.memsize", 0); + if(ram->bytesTotal == 0) + { + ffStrbufAppendS(&ram->error, "Failed to read hw.memsize"); + return; + } uint32_t pagesize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0); if(pagesize == 0) + { + ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize"); return; + } mach_msg_type_number_t count = HOST_VM_INFO_COUNT; vm_statistics_data_t vmstat; if(host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) (&vmstat), &count) != KERN_SUCCESS) + { + ffStrbufAppendS(&ram->error, "Failed to read vm statistics"); return; + } memory->ram.bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize; +} +static void detectSwap(FFMemoryStorage* swap) +{ struct xsw_usage swap; size_t size = sizeof(swap); - if (sysctlbyname("vm.swapusage", &swap, &size, 0, 0) == -1) + if(sysctlbyname("vm.swapusage", &swap, &size, 0, 0) != 0) + { + ffStrbufAppendS(&swap->error, "Failed to read vm.swapusage"); return; + } + memory->swap.bytesTotal = swap.xsu_total; memory->swap.bytesUsed = swap.xsu_used; } + +void ffDetectMemoryImpl(FFMemoryResult* memory) +{ + detectRam(&memory->ram); + detectSwap(&memory->swap); +} diff --git a/src/detection/memory/memory_bsd.c b/src/detection/memory/memory_bsd.c index a98cefdda..0f34283b9 100644 --- a/src/detection/memory/memory_bsd.c +++ b/src/detection/memory/memory_bsd.c @@ -1,17 +1,34 @@ #include "memory.h" #include "common/sysctl.h" -void ffDetectMemoryImpl(FFMemoryResult* memory) +static void detectRam(FFMemoryStorage* ram) { uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0); + if(pageSize == 0) + { + ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize"); + return; + } memory->ram.bytesTotal = (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_page_count", 0) * pageSize; - + if(ram->bytesTotal == 0) + { + ffStrbufAppendS(&ram->error, "Failed to read vm.stats.vm.v_page_count"); + return; + } + memory->ram.bytesUsed = memory->ram.bytesTotal - (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_free_count", 0) * pageSize - (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_inactive_count", 0) * pageSize ; - - memory->swap.bytesTotal = 0; - memory->swap.bytesUsed = 0; +} + +static void detectSwap(FFMemoryStorage* swap) +{ + ffStrbufAppendS(&swap->error, "Not implemented"); +} +void ffDetectMemoryImpl(FFMemoryResult* memory) +{ + detectRam(&memory->ram); + detectSwap(&memory->swap); } diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c index e6cf87bb2..b83819295 100644 --- a/src/detection/memory/memory_linux.c +++ b/src/detection/memory/memory_linux.c @@ -5,11 +5,13 @@ void ffDetectMemoryImpl(FFMemoryResult* memory) { - memset(memory, 0, sizeof(FFMemoryResult)); - FILE* meminfo = fopen("/proc/meminfo", "r"); if(meminfo == NULL) + { + ffStrbufAppendS(&memory->ram.error, "Failed to open /proc/meminfo"); + ffStrbufAppendS(&memory->swap.error, "Failed to open /proc/meminfo"); return; + } char* line = NULL; size_t len = 0; @@ -41,7 +43,10 @@ void ffDetectMemoryImpl(FFMemoryResult* memory) fclose(meminfo); memory->ram.bytesTotal = memTotal * (uint64_t) 1024; - memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; + if(memory->ram.bytesTotal == 0) + ffStrbufAppendS(&memory->ram.error, "Failed to read MemTotal"); + else + memory->ram.bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; memory->swap.bytesTotal = swapTotal * (uint64_t) 1024; memory->swap.bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024; diff --git a/src/modules/memory.c b/src/modules/memory.c index 40fe380f4..45cabec28 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -10,9 +10,9 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage) { - if(storage->bytesUsed == 0 && storage->bytesTotal == 0) + if(storage->error.length > 0) { - ffPrintError(instance, name, 0, moduleArgs, "Failed to detect memory"); + ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars); return; }