Skip to content

Commit

Permalink
Swap (FreeBSD): fix impl
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Dec 1, 2023
1 parent 7168cc2 commit 36298b8
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/detection/swap/swap_bsd.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
#include "swap.h"
#include "common/sysctl.h"

#include <vm/vm_param.h>

const char* ffDetectSwap(FFSwapResult* swap)
{
swap->bytesTotal = (uint64_t)ffSysctlGetInt64("vm.swap_total", 0);
swap->bytesUsed = (uint64_t)ffSysctlGetInt64("vm.swap_reserved", 0);
int mib[16];
size_t mibsize = sizeof(mib) / sizeof(*mib);
if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0)
return "sysctlnametomib(\"vm.swap_info\") failed";

swap->bytesUsed = swap->bytesTotal = 0;

for (int n = 0; ; ++n)
{
mib[mibsize] = n;
struct xswdev xsw;
size_t size = sizeof(xsw);
if (sysctl(mib, (uint32_t) (mibsize + 1), &xsw, &size, NULL, 0) < 0)
break;
if (xsw.xsw_version != XSWDEV_VERSION)
return "xswdev version mismatch";
swap->bytesUsed += (uint64_t) xsw.xsw_used;
swap->bytesTotal += (uint64_t) xsw.xsw_nblks;
}

swap->bytesUsed *= instance.state.platform.pageSize;
swap->bytesTotal *= instance.state.platform.pageSize;

return NULL;
}

0 comments on commit 36298b8

Please sign in to comment.