2020-11-09 05:25:50 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
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
|
|
|
*
|
2018-06-12 17:15:23 +02:00
|
|
|
* Copyright © 2011 ProFUSION embedded systems
|
2011-12-21 22:30:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-12-21 22:30:48 +01:00
|
|
|
|
2017-10-08 15:55:24 +02:00
|
|
|
#include "module-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-08-29 09:12:22 +02:00
|
|
|
#include "udev-builtin.h"
|
2011-12-21 22:30:48 +01:00
|
|
|
|
2014-11-27 19:48:02 +01:00
|
|
|
static struct kmod_ctx *ctx = NULL;
|
2011-12-23 18:19:18 +01:00
|
|
|
|
2014-11-27 19:48:02 +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) {
|
2014-11-27 20:20:23 +01:00
|
|
|
log_internalv(priority, 0, file, line, fn, format, args);
|
2011-12-25 18:59:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-13 18:22:14 +02:00
|
|
|
static int builtin_kmod(sd_device *dev, int argc, char *argv[], bool test) {
|
2012-01-10 01:34:15 +01:00
|
|
|
int i;
|
|
|
|
|
2012-10-26 00:28:23 +02:00
|
|
|
if (!ctx)
|
2012-10-25 22:58:02 +02:00
|
|
|
return 0;
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (argc < 3 || !streq(argv[1], "load"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"%s: expected: load <module>", argv[0]);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-07-06 12:47:39 +02:00
|
|
|
for (i = 2; argv[i]; i++)
|
2018-07-06 12:28:09 +02:00
|
|
|
(void) module_load_and_warn(ctx, argv[i], false);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-10-23 11:26:06 +02:00
|
|
|
return 0;
|
2011-12-21 22:30:48 +01:00
|
|
|
}
|
|
|
|
|
2012-08-08 16:27:11 +02:00
|
|
|
/* called at udev startup and reload */
|
2018-08-22 12:57:32 +02:00
|
|
|
static int builtin_kmod_init(void) {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (ctx)
|
|
|
|
return 0;
|
2011-12-25 16:37:15 +01:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
ctx = kmod_new(NULL, NULL);
|
|
|
|
if (!ctx)
|
|
|
|
return -ENOMEM;
|
2011-12-23 18:19:18 +01:00
|
|
|
|
2014-11-27 19:48:02 +01:00
|
|
|
log_debug("Load module index");
|
2018-08-22 12:57:32 +02:00
|
|
|
kmod_set_log_fn(ctx, udev_kmod_log, NULL);
|
2012-01-10 01:34:15 +01:00
|
|
|
kmod_load_resources(ctx);
|
|
|
|
return 0;
|
2011-12-21 23:10:56 +01:00
|
|
|
}
|
|
|
|
|
2011-12-27 03:49:43 +01:00
|
|
|
/* called on udev shutdown and reload request */
|
2018-08-22 12:57:32 +02:00
|
|
|
static void builtin_kmod_exit(void) {
|
2014-11-27 19:48:02 +01:00
|
|
|
log_debug("Unload module index");
|
2012-01-10 01:34:15 +01:00
|
|
|
ctx = kmod_unref(ctx);
|
2011-12-27 03:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called every couple of seconds during event activity; 'true' if config has changed */
|
2018-08-22 12:57:32 +02:00
|
|
|
static bool builtin_kmod_validate(void) {
|
2014-11-27 19:48:02 +01:00
|
|
|
log_debug("Validate module index");
|
2012-08-08 16:27:11 +02:00
|
|
|
if (!ctx)
|
|
|
|
return false;
|
|
|
|
return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
|
2011-12-21 23:10:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-25 01:21:11 +02:00
|
|
|
const UdevBuiltin udev_builtin_kmod = {
|
2012-01-10 01:34:15 +01:00
|
|
|
.name = "kmod",
|
|
|
|
.cmd = builtin_kmod,
|
|
|
|
.init = builtin_kmod_init,
|
|
|
|
.exit = builtin_kmod_exit,
|
|
|
|
.validate = builtin_kmod_validate,
|
2015-01-05 13:19:55 +01:00
|
|
|
.help = "Kernel module loader",
|
2012-01-10 01:34:15 +01:00
|
|
|
.run_once = false,
|
2011-12-21 22:30:48 +01:00
|
|
|
};
|