]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/module-util.c
tree-wide: avoid some loaded terms
[thirdparty/systemd.git] / src / shared / module-util.c
CommitLineData
3cb9b42a
ZJS
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include <errno.h>
4
5#include "module-util.h"
6
c3ad9786 7int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
3cb9b42a
ZJS
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
c3ad9786
ZJS
13 /* verbose==true means we should log at non-debug level if we
14 * fail to find or load the module. */
15
3cb9b42a
ZJS
16 log_debug("Loading module: %s", module);
17
18 r = kmod_module_new_from_lookup(ctx, module, &modlist);
19 if (r < 0)
c3ad9786 20 return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
105a1a36 21 "Failed to look up module alias '%s': %m", module);
3cb9b42a
ZJS
22
23 if (!modlist) {
c3ad9786
ZJS
24 log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
25 "Failed to find module '%s'", module);
3cb9b42a
ZJS
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:
c3ad9786 38 log_full(verbose ? LOG_INFO : LOG_DEBUG,
c82a2b5b 39 "Module '%s' is built in", kmod_module_get_name(mod));
3cb9b42a
ZJS
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);
3cb9b42a 49 if (err == 0)
c3ad9786
ZJS
50 log_full(verbose ? LOG_INFO : LOG_DEBUG,
51 "Inserted module '%s'", kmod_module_get_name(mod));
3cb9b42a 52 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
c3ad9786 53 log_full(verbose ? LOG_INFO : LOG_DEBUG,
6b000af4 54 "Module '%s' is deny-listed", kmod_module_get_name(mod));
3cb9b42a
ZJS
55 else {
56 assert(err < 0);
57
c3ad9786 58 log_full_errno(!verbose ? LOG_DEBUG :
9b38ec87
ZJS
59 err == -ENODEV ? LOG_NOTICE :
60 err == -ENOENT ? LOG_WARNING :
61 LOG_ERR,
3cb9b42a
ZJS
62 err,
63 "Failed to insert module '%s': %m",
64 kmod_module_get_name(mod));
9b38ec87 65 if (!IN_SET(err, -ENODEV, -ENOENT))
3cb9b42a
ZJS
66 r = err;
67 }
68 }
69 }
70
71 return r;
72}