diff --git a/src/detection/swap/swap_linux.c b/src/detection/swap/swap_linux.c index 487b68bcf..ee623c0c4 100644 --- a/src/detection/swap/swap_linux.c +++ b/src/detection/swap/swap_linux.c @@ -1,34 +1,13 @@ #include "swap.h" -#include -#include +#include void ffDetectSwap(FFMemoryStorage* swap) { - FILE* meminfo = fopen("/proc/meminfo", "r"); - if(meminfo == NULL) - { - ffStrbufAppendS(&swap->error, "Failed to open /proc/meminfo"); - return; - } + struct sysinfo info; + if(sysinfo(&info) != 0) + ffStrbufAppendS(&swap->error, "sysinfo() failed"); - char* line = NULL; - size_t len = 0; - - uint32_t swapTotal = 0, - swapFree = 0; - - while (getline(&line, &len, meminfo) != EOF) - { - if(!sscanf(line, "SwapTotal: %u", &swapTotal)) - sscanf(line, "SwapFree: %u", &swapFree); - } - - if(line != NULL) - free(line); - - fclose(meminfo); - - swap->bytesTotal = swapTotal * (uint64_t) 1024; - swap->bytesUsed = (swapTotal - swapFree) * (uint64_t) 1024; + swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit; + swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit; }