/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * load kernel modules
 *
 * Copyright © 2011 ProFUSION embedded systems
 */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>


#include "device-util.h"
#include "module-util.h"
#include "string-util.h"
#include "strv.h"
#include "udev-builtin.h"

static struct kmod_ctx *ctx = NULL;
static struct kmod_ctx *opt_ctx = NULL;


_printf_(6,0) static void udev_kmod_log(void *data, int priority, const char *file, int line, const char *fn, const char *format, va_list args) {
        log_internalv(priority, 0, file, line, fn, format, args);
}

static int builtin_kmod(UdevEvent *event, int argc, char *argv[], bool test) {
        sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
        int r;

        if (!ctx || !opt_ctx)
                return 0;

        if (argc < 2 || !streq(argv[1], "load"))
                return log_device_warning_errno(dev, SYNTHETIC_ERRNO(EINVAL),
                                                "%s: expected: load [module…]", argv[0]);

        char **modules = strv_skip(argv, 2);
        if (strv_isempty(modules)) {
                const char *modalias;

                r = sd_device_get_property_value(dev, "MODALIAS", &modalias);
                if (r < 0)
                        return log_device_warning_errno(dev, r, "Failed to read property \"MODALIAS\".");
                (void) module_load_and_warn(ctx, modalias, /* verbose = */ false);
                (void) module_load_and_warn(opt_ctx, modalias, /* verbose = */ false);
        } else {
                STRV_FOREACH(module, modules) {
                        (void) module_load_and_warn(ctx, *module, /* verbose = */ false);
                        (void) module_load_and_warn(opt_ctx, *module, /* verbose = */ false);

                }
        }

        return 0;
}

/* called at udev startup and reload */
static int builtin_kmod_init(void) {
        if (!ctx) {
                ctx = kmod_new(NULL, NULL);
                if (!ctx)
                        return -ENOMEM;

                kmod_set_log_fn(ctx, udev_kmod_log, NULL);
                kmod_load_resources(ctx);
                log_debug("Loaded kernel module index from default /lib/modules path.");
        }

        if (!opt_ctx) {
                struct utsname u;
                if (uname(&u) < 0)
                        return -errno;

                char opt_path[4096];
                snprintf(opt_path, sizeof(opt_path),
                        "/opt/system/lib/modules/%s", u.release);

                opt_ctx = kmod_new(opt_path, NULL);
                if (!opt_ctx)
                        return -ENOMEM;

                kmod_set_log_fn(opt_ctx, udev_kmod_log, NULL);
                kmod_load_resources(opt_ctx);
                log_debug("Loaded kernel module index from /opt/system/lib/modules.");
        }

        return 0;
}

/* called on udev shutdown and reload request */
static void builtin_kmod_exit(void) {
        log_debug("Unload kernel module index.");
        ctx = kmod_unref(ctx);
        opt_ctx = kmod_unref(opt_ctx);

}

/* called every couple of seconds during event activity; 'true' if config has changed */
static bool builtin_kmod_should_reload(void) {
        bool reload = false;

        if (ctx && kmod_validate_resources(ctx) != KMOD_RESOURCES_OK) {
                log_debug("Default kernel module index needs reloading.");
                reload = true;
        }

        if (opt_ctx && kmod_validate_resources(opt_ctx) != KMOD_RESOURCES_OK) {
                log_debug("Optional kernel module index needs reloading.");
                reload = true;
        }
        return reload;
}

const UdevBuiltin udev_builtin_kmod = {
        .name = "kmod",
        .cmd = builtin_kmod,
        .init = builtin_kmod_init,
        .exit = builtin_kmod_exit,
        .should_reload = builtin_kmod_should_reload,
        .help = "Kernel module loader",
        .run_once = false,
};
