]>
Commit | Line | Data |
---|---|---|
e7145211 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
81dadce5 | 2 | /* |
aa29418a | 3 | * load kernel modules |
81dadce5 | 4 | * |
810adae9 | 5 | * Copyright © 2011 ProFUSION embedded systems |
81dadce5 KS |
6 | */ |
7 | ||
81dadce5 | 8 | #include <errno.h> |
b45ce692 | 9 | #include <libkmod.h> |
07630cea LP |
10 | #include <stdarg.h> |
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
81dadce5 | 13 | |
232ac0d6 | 14 | #include "module-util.h" |
07630cea | 15 | #include "string-util.h" |
07a26e42 | 16 | #include "udev-builtin.h" |
81dadce5 | 17 | |
086891e5 | 18 | static struct kmod_ctx *ctx = NULL; |
b45ce692 | 19 | |
086891e5 | 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) { |
79008bdd | 21 | log_internalv(priority, 0, file, line, fn, format, args); |
80df994c KS |
22 | } |
23 | ||
3fc2e9a2 | 24 | static int builtin_kmod(sd_device *dev, int argc, char *argv[], bool test) { |
912541b0 KS |
25 | int i; |
26 | ||
a9f4815d | 27 | if (!ctx) |
5b4d50ef KS |
28 | return 0; |
29 | ||
090be865 | 30 | if (argc < 3 || !streq(argv[1], "load")) { |
4fdf6907 | 31 | log_error("%s: expected: load <module>", argv[0]); |
d354690e | 32 | return -EINVAL; |
912541b0 KS |
33 | } |
34 | ||
4fdf6907 | 35 | for (i = 2; argv[i]; i++) |
c3ad9786 | 36 | (void) module_load_and_warn(ctx, argv[i], false); |
912541b0 | 37 | |
d354690e | 38 | return 0; |
81dadce5 KS |
39 | } |
40 | ||
7781e063 | 41 | /* called at udev startup and reload */ |
2024ed61 | 42 | static int builtin_kmod_init(void) { |
912541b0 KS |
43 | if (ctx) |
44 | return 0; | |
7c85d636 | 45 | |
912541b0 KS |
46 | ctx = kmod_new(NULL, NULL); |
47 | if (!ctx) | |
48 | return -ENOMEM; | |
b45ce692 | 49 | |
086891e5 | 50 | log_debug("Load module index"); |
2024ed61 | 51 | kmod_set_log_fn(ctx, udev_kmod_log, NULL); |
912541b0 KS |
52 | kmod_load_resources(ctx); |
53 | return 0; | |
aa29418a KS |
54 | } |
55 | ||
4f1795cc | 56 | /* called on udev shutdown and reload request */ |
2024ed61 | 57 | static void builtin_kmod_exit(void) { |
086891e5 | 58 | log_debug("Unload module index"); |
912541b0 | 59 | ctx = kmod_unref(ctx); |
4f1795cc KS |
60 | } |
61 | ||
62 | /* called every couple of seconds during event activity; 'true' if config has changed */ | |
2024ed61 | 63 | static bool builtin_kmod_validate(void) { |
086891e5 | 64 | log_debug("Validate module index"); |
7781e063 KS |
65 | if (!ctx) |
66 | return false; | |
67 | return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK); | |
aa29418a KS |
68 | } |
69 | ||
81dadce5 | 70 | const struct udev_builtin udev_builtin_kmod = { |
912541b0 KS |
71 | .name = "kmod", |
72 | .cmd = builtin_kmod, | |
73 | .init = builtin_kmod_init, | |
74 | .exit = builtin_kmod_exit, | |
75 | .validate = builtin_kmod_validate, | |
5ac0162c | 76 | .help = "Kernel module loader", |
912541b0 | 77 | .run_once = false, |
81dadce5 | 78 | }; |