]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-kmod.c
d8575b72bd5ed59f448eb2fc2927dcbf599ea41b
[thirdparty/systemd.git] / src / udev / udev-builtin-kmod.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * load kernel modules
4 *
5 * Copyright © 2011 ProFUSION embedded systems
6 */
7
8 #include <errno.h>
9 #include <libkmod.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "module-util.h"
15 #include "string-util.h"
16 #include "udev-builtin.h"
17
18 static struct kmod_ctx *ctx = NULL;
19
20 _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) {
21 log_internalv(priority, 0, file, line, fn, format, args);
22 }
23
24 static int builtin_kmod(sd_device *dev, int argc, char *argv[], bool test) {
25 int i;
26
27 if (!ctx)
28 return 0;
29
30 if (argc < 3 || !streq(argv[1], "load")) {
31 log_error("%s: expected: load <module>", argv[0]);
32 return -EINVAL;
33 }
34
35 for (i = 2; argv[i]; i++)
36 (void) module_load_and_warn(ctx, argv[i], false);
37
38 return 0;
39 }
40
41 /* called at udev startup and reload */
42 static int builtin_kmod_init(void) {
43 if (ctx)
44 return 0;
45
46 ctx = kmod_new(NULL, NULL);
47 if (!ctx)
48 return -ENOMEM;
49
50 log_debug("Load module index");
51 kmod_set_log_fn(ctx, udev_kmod_log, NULL);
52 kmod_load_resources(ctx);
53 return 0;
54 }
55
56 /* called on udev shutdown and reload request */
57 static void builtin_kmod_exit(void) {
58 log_debug("Unload module index");
59 ctx = kmod_unref(ctx);
60 }
61
62 /* called every couple of seconds during event activity; 'true' if config has changed */
63 static bool builtin_kmod_validate(void) {
64 log_debug("Validate module index");
65 if (!ctx)
66 return false;
67 return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
68 }
69
70 const struct udev_builtin udev_builtin_kmod = {
71 .name = "kmod",
72 .cmd = builtin_kmod,
73 .init = builtin_kmod_init,
74 .exit = builtin_kmod_exit,
75 .validate = builtin_kmod_validate,
76 .help = "Kernel module loader",
77 .run_once = false,
78 };