]>
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 | ||
baaa35ad ZJS |
30 | if (argc < 3 || !streq(argv[1], "load")) |
31 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
32 | "%s: expected: load <module>", argv[0]); | |
912541b0 | 33 | |
4fdf6907 | 34 | for (i = 2; argv[i]; i++) |
c3ad9786 | 35 | (void) module_load_and_warn(ctx, argv[i], false); |
912541b0 | 36 | |
d354690e | 37 | return 0; |
81dadce5 KS |
38 | } |
39 | ||
7781e063 | 40 | /* called at udev startup and reload */ |
2024ed61 | 41 | static int builtin_kmod_init(void) { |
912541b0 KS |
42 | if (ctx) |
43 | return 0; | |
7c85d636 | 44 | |
912541b0 KS |
45 | ctx = kmod_new(NULL, NULL); |
46 | if (!ctx) | |
47 | return -ENOMEM; | |
b45ce692 | 48 | |
086891e5 | 49 | log_debug("Load module index"); |
2024ed61 | 50 | kmod_set_log_fn(ctx, udev_kmod_log, NULL); |
912541b0 KS |
51 | kmod_load_resources(ctx); |
52 | return 0; | |
aa29418a KS |
53 | } |
54 | ||
4f1795cc | 55 | /* called on udev shutdown and reload request */ |
2024ed61 | 56 | static void builtin_kmod_exit(void) { |
086891e5 | 57 | log_debug("Unload module index"); |
912541b0 | 58 | ctx = kmod_unref(ctx); |
4f1795cc KS |
59 | } |
60 | ||
61 | /* called every couple of seconds during event activity; 'true' if config has changed */ | |
2024ed61 | 62 | static bool builtin_kmod_validate(void) { |
086891e5 | 63 | log_debug("Validate module index"); |
7781e063 KS |
64 | if (!ctx) |
65 | return false; | |
66 | return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK); | |
aa29418a KS |
67 | } |
68 | ||
81dadce5 | 69 | const struct udev_builtin udev_builtin_kmod = { |
912541b0 KS |
70 | .name = "kmod", |
71 | .cmd = builtin_kmod, | |
72 | .init = builtin_kmod_init, | |
73 | .exit = builtin_kmod_exit, | |
74 | .validate = builtin_kmod_validate, | |
5ac0162c | 75 | .help = "Kernel module loader", |
912541b0 | 76 | .run_once = false, |
81dadce5 | 77 | }; |