ohmu
·
2026-10-15
1#include "swap.h"
2#include "common/debug.h"
3#include "common/FFlist.h"
4#include "common/mallocHelper.h"
5
6#include <errno.h>
7#include <string.h>
8#include <sys/types.h>
9#include <sys/swap.h>
10#include <sys/param.h>
11#include <unistd.h>
12
13const char* ffDetectSwap(FFlist* result) {
14 int nswap = swapctl(SWAP_NSWAP, 0, 0);
15 if (nswap < 0) {
16 FF_DEBUG("swapctl(SWAP_NSWAP) failed: %s", strerror(errno));
17 return "swapctl(SWAP_NSWAP) failed";
18 }
19 if (nswap == 0) {
20 return nullptr;
21 }
22
23 FF_AUTO_FREE struct swapent* swdev = malloc((uint32_t) nswap * sizeof(*swdev));
24
25 if (swapctl(SWAP_STATS, swdev, nswap) < 0) {
26 FF_DEBUG("swapctl(SWAP_STATS) failed: %s", strerror(errno));
27 return "swapctl(SWAP_STATS) failed";
28 }
29
30 for (int i = 0; i < nswap; i++) {
31 if (swdev[i].se_flags & SWF_ENABLE) {
32 FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
33 ffStrbufInitS(&swap->name, swdev[i].se_path);
34 swap->bytesUsed = (uint64_t) swdev[i].se_inuse * DEV_BSIZE;
35 swap->bytesTotal = (uint64_t) swdev[i].se_nblks * DEV_BSIZE;
36 }
37 }
38
39 return nullptr;
40}