1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4
5 #include "module-util.h"
6
module_load_and_warn(struct kmod_ctx * ctx,const char * module,bool verbose)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;
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 look up module alias '%s': %m", module);
22
23 if (!modlist)
24 return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG,
25 SYNTHETIC_ERRNO(ENOENT),
26 "Failed to find module '%s'", module);
27
28 kmod_list_foreach(itr, modlist) {
29 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
30 int state, err;
31
32 mod = kmod_module_get_module(itr);
33 state = kmod_module_get_initstate(mod);
34
35 switch (state) {
36 case KMOD_MODULE_BUILTIN:
37 log_full(verbose ? LOG_INFO : LOG_DEBUG,
38 "Module '%s' is built in", kmod_module_get_name(mod));
39 break;
40
41 case KMOD_MODULE_LIVE:
42 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
43 break;
44
45 default:
46 err = kmod_module_probe_insert_module(mod, probe_flags,
47 NULL, NULL, NULL, NULL);
48 if (err == 0)
49 log_full(verbose ? LOG_INFO : LOG_DEBUG,
50 "Inserted module '%s'", kmod_module_get_name(mod));
51 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
52 log_full(verbose ? LOG_INFO : LOG_DEBUG,
53 "Module '%s' is deny-listed", kmod_module_get_name(mod));
54 else {
55 assert(err < 0);
56
57 log_full_errno(!verbose ? LOG_DEBUG :
58 err == -ENODEV ? LOG_NOTICE :
59 err == -ENOENT ? LOG_WARNING :
60 LOG_ERR,
61 err,
62 "Failed to insert module '%s': %m",
63 kmod_module_get_name(mod));
64 if (!IN_SET(err, -ENODEV, -ENOENT))
65 r = err;
66 }
67 }
68 }
69
70 return r;
71 }
72