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