summaryrefslogtreecommitdiffstats
path: root/src/detection/diskio/diskio_nbsd.c
blob: 9ca9578a8edb6cecc82a0205cb8710e5e7f32b87 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "diskio.h"
#include "common/io.h"
#include "common/strutil.h"
#include "common/mallocHelper.h"

#include <sys/iostat.h>
#include <sys/sysctl.h>

const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
    int mib[] = { CTL_HW, HW_IOSTATS, sizeof(struct io_sysctl) };
    size_t len;
    if (sysctl(mib, ARRAY_SIZE(mib), NULL, &len, NULL, 0) < 0) {
        return "sysctl({HW_IOSTATS}, NULL) failed";
    }
    uint32_t nDrive = (uint32_t) (len / sizeof(struct io_sysctl));

    FF_AUTO_FREE struct io_sysctl* stats = malloc(len);

    if (sysctl(mib, ARRAY_SIZE(mib), stats, &len, NULL, 0) < 0) {
        return "sysctl({HW_IOSTATS}, stats) failed";
    }

    char path[64] = "/dev/";

    for (uint32_t i = 0; i < nDrive; ++i) {
        struct io_sysctl* st = &stats[i];

        if (options->namePrefix.length && strncmp(st->name, options->namePrefix.chars, options->namePrefix.length) != 0) {
            continue;
        }

        // Skip partitions
        char* end = ffStrCopy(&path[5], st->name, ARRAY_SIZE(path) - 8);
        *end++ = 'c';
        *end = '\0';
        if (!ffPathExists(path, FF_PATHTYPE_ANY)) {
            continue;
        }

        FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
        ffStrbufInitNS(&device->devPath, (uint32_t) (end - path), path);
        ffStrbufInitS(&device->name, st->name);
        device->bytesRead = st->rbytes;
        device->readCount = st->rxfer;
        device->bytesWritten = st->wbytes;
        device->writeCount = st->wxfer;
    }

    return NULL;
}