Systemd/src/udev/udev-builtin-kmod.c

79 lines
2.1 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0+ */
2011-12-21 22:30:48 +01:00
/*
2011-12-21 23:10:56 +01:00
* load kernel modules
2011-12-21 22:30:48 +01:00
*
* Copyright © 2011 ProFUSION embedded systems
2011-12-21 22:30:48 +01:00
*/
#include <errno.h>
2011-12-23 18:19:18 +01:00
#include <libkmod.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
2011-12-21 22:30:48 +01:00
#include "module-util.h"
#include "string-util.h"
2011-12-21 22:30:48 +01:00
#include "udev.h"
static struct kmod_ctx *ctx = NULL;
2011-12-23 18:19:18 +01:00
_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(struct udev_device *dev, int argc, char *argv[], bool test) {
int i;
2012-10-26 00:28:23 +02:00
if (!ctx)
return 0;
2013-02-13 18:13:22 +01:00
if (argc < 3 || !streq(argv[1], "load")) {
log_error("%s: expected: load <module>", argv[0]);
return EXIT_FAILURE;
}
for (i = 2; argv[i]; i++)
(void) module_load_and_warn(ctx, argv[i], false);
return EXIT_SUCCESS;
2011-12-21 22:30:48 +01:00
}
/* called at udev startup and reload */
static int builtin_kmod_init(struct udev *udev) {
if (ctx)
return 0;
ctx = kmod_new(NULL, NULL);
if (!ctx)
return -ENOMEM;
2011-12-23 18:19:18 +01:00
log_debug("Load module index");
kmod_set_log_fn(ctx, udev_kmod_log, udev);
kmod_load_resources(ctx);
return 0;
2011-12-21 23:10:56 +01:00
}
/* called on udev shutdown and reload request */
static void builtin_kmod_exit(struct udev *udev) {
log_debug("Unload module index");
ctx = kmod_unref(ctx);
}
/* called every couple of seconds during event activity; 'true' if config has changed */
static bool builtin_kmod_validate(struct udev *udev) {
log_debug("Validate module index");
if (!ctx)
return false;
return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
2011-12-21 23:10:56 +01:00
}
2011-12-21 22:30:48 +01:00
const struct udev_builtin udev_builtin_kmod = {
.name = "kmod",
.cmd = builtin_kmod,
.init = builtin_kmod_init,
.exit = builtin_kmod_exit,
.validate = builtin_kmod_validate,
.help = "Kernel module loader",
.run_once = false,
2011-12-21 22:30:48 +01:00
};