summaryrefslogtreecommitdiffstats
path: root/src/detection/swap/swap_sunos.c
blob: fa3260d50fabf473ee2a37c28fcd36b5540cdff4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "swap.h"
#include <sys/stat.h>
#include <sys/swap.h>
#include <limits.h>
#include <stdalign.h>

enum { FFMaxNSwap = 8 };

const char* ffDetectSwap(FFlist* result) {
    char strings[FFMaxNSwap][PATH_MAX];
    alignas(swaptbl_t) uint8_t buffer[sizeof(swaptbl_t) + sizeof(swapent_t) * (FFMaxNSwap - 1)] = {};
    swaptbl_t* table = (swaptbl_t*) buffer;
    table->swt_n = FFMaxNSwap;
    for (int i = 0; i < FFMaxNSwap; ++i) {
        table->swt_ent[i].ste_path = strings[i];
    }

    int size = swapctl(SC_LIST, table);
    if (size < 0) {
        return "swapctl() failed";
    }

    uint32_t pageSize = instance.state.platform.sysinfo.pageSize;
    for (int i = 0; i < size; ++i) {
        FFSwapResult* swap = FF_LIST_ADD(FFSwapResult, *result);
        ffStrbufInitS(&swap->name, table->swt_ent[i].ste_path);
        swap->bytesTotal = (uint64_t) table->swt_ent[i].ste_pages * pageSize;
        swap->bytesUsed = swap->bytesTotal - (uint64_t) table->swt_ent[i].ste_free * pageSize;
    }

    return NULL;
}