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