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