]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/module-util.c
Merge pull request #11239 from poettering/news-v240-final
[thirdparty/systemd.git] / src / shared / module-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4
5 #include "module-util.h"
6
7 int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
8 const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
9 struct kmod_list *itr;
10 _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
11 int r = 0;
12
13 /* verbose==true means we should log at non-debug level if we
14 * fail to find or load the module. */
15
16 log_debug("Loading module: %s", module);
17
18 r = kmod_module_new_from_lookup(ctx, module, &modlist);
19 if (r < 0)
20 return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
21 "Failed to lookup module alias '%s': %m", module);
22
23 if (!modlist) {
24 log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
25 "Failed to find module '%s'", module);
26 return -ENOENT;
27 }
28
29 kmod_list_foreach(itr, modlist) {
30 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
31 int state, err;
32
33 mod = kmod_module_get_module(itr);
34 state = kmod_module_get_initstate(mod);
35
36 switch (state) {
37 case KMOD_MODULE_BUILTIN:
38 log_full(verbose ? LOG_INFO : LOG_DEBUG,
39 "Module '%s' is builtin", kmod_module_get_name(mod));
40 break;
41
42 case KMOD_MODULE_LIVE:
43 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
44 break;
45
46 default:
47 err = kmod_module_probe_insert_module(mod, probe_flags,
48 NULL, NULL, NULL, NULL);
49 if (err == 0)
50 log_full(verbose ? LOG_INFO : LOG_DEBUG,
51 "Inserted module '%s'", kmod_module_get_name(mod));
52 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
53 log_full(verbose ? LOG_INFO : LOG_DEBUG,
54 "Module '%s' is blacklisted", kmod_module_get_name(mod));
55 else {
56 assert(err < 0);
57
58 log_full_errno(!verbose ? LOG_DEBUG :
59 err == -ENODEV ? LOG_NOTICE :
60 err == -ENOENT ? LOG_WARNING :
61 LOG_ERR,
62 err,
63 "Failed to insert module '%s': %m",
64 kmod_module_get_name(mod));
65 if (!IN_SET(err, -ENODEV, -ENOENT))
66 r = err;
67 }
68 }
69 }
70
71 return r;
72 }