master ohmu/fastfetch-openbsd / src / common / impl / kmod_nbsd.c
ohmu  ·  2026-10-15
 1#include "common/kmod.h"
 2#include "common/strutil.h"
 3
 4#include <sys/module.h>
 5#include <sys/param.h>
 6
 7typedef struct [[gnu::packed]] FFNbsdModList {
 8    int len;
 9    modstat_t mods[];
10} FFNbsdModList;
11
12bool ffKmodLoaded(const char* modName) {
13    static FFNbsdModList* list = nullptr;
14
15    if (list == nullptr) {
16        struct iovec iov = {};
17
18        for (size_t len = 8192;; len = iov.iov_len) {
19            iov.iov_len = len;
20            iov.iov_base = realloc(iov.iov_base, len);
21            if (modctl(MODCTL_STAT, &iov) < 0) {
22                free(iov.iov_base);
23                return true; // ignore errors
24            }
25
26            if (len >= iov.iov_len) {
27                break;
28            }
29        }
30        list = (FFNbsdModList*) iov.iov_base;
31    }
32
33    for (int i = 0; i < list->len; i++) {
34        if (ffStrEquals(list->mods[i].ms_name, modName)) {
35            return true;
36        }
37    }
38
39    return false;
40}