Debian Codegraph @

src/shared/module-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <syslog.h>
4
5 #include "sd-dlopen.h"
6
7 #include "log.h"
8 #include "module-util.h"
9 #include "proc-cmdline.h"
10 #include "strv.h"
11
12 #if HAVE_KMOD
13
14 DLSYM_PROTOTYPE(kmod_list_next) = NULL;
15 DLSYM_PROTOTYPE(kmod_load_resources) = NULL;
16 DLSYM_PROTOTYPE(kmod_module_get_initstate) = NULL;
17 DLSYM_PROTOTYPE(kmod_module_get_module) = NULL;
18 DLSYM_PROTOTYPE(kmod_module_get_name) = NULL;
19 DLSYM_PROTOTYPE(kmod_module_new_from_lookup) = NULL;
20 DLSYM_PROTOTYPE(kmod_module_probe_insert_module) = NULL;
21 DLSYM_PROTOTYPE(kmod_module_unref) = NULL;
22 DLSYM_PROTOTYPE(kmod_module_unref_list) = NULL;
23 DLSYM_PROTOTYPE(kmod_new) = NULL;
24 DLSYM_PROTOTYPE(kmod_set_log_fn) = NULL;
25 DLSYM_PROTOTYPE(kmod_unref) = NULL;
26 DLSYM_PROTOTYPE(kmod_validate_resources) = NULL;
27
28 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
29 char ***denylist = ASSERT_PTR(data);
30 int r;
31
32 if (proc_cmdline_key_streq(key, "module_blacklist")) {
33
34 if (proc_cmdline_value_missing(key, value))
35 return 0;
36
37 r = strv_split_and_extend(denylist, value, ",", /* filter_duplicates= */ true);
38 if (r < 0)
39 return r;
40 }
41
42 return 0;
43 }
44
45 int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
46 _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
47 _cleanup_strv_free_ char **denylist = NULL;
48 bool denylist_parsed = false;
49 struct kmod_list *itr;
50 int r;
51
52 assert(ctx);
53 assert(module);
54
55 /* verbose==true means we should log at non-debug level if we
56 * fail to find or load the module. */
57
58 log_debug("Loading module: %s", module);
59
60 r = sym_kmod_module_new_from_lookup(ctx, module, &modlist);
61 if (r < 0)
62 return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
63 "Failed to look up module alias '%s': %m", module);
64
65 if (!modlist)
66 return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG,
67 SYNTHETIC_ERRNO(ENOENT),
68 "Failed to find module '%s'", module);
69
70 sym_kmod_list_foreach(itr, modlist) {
71 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
72 int state, err;
73
74 mod = sym_kmod_module_get_module(itr);
75 state = sym_kmod_module_get_initstate(mod);
76
77 switch (state) {
78 case KMOD_MODULE_BUILTIN:
79 log_full(verbose ? LOG_INFO : LOG_DEBUG,
80 "Module '%s' is built in", sym_kmod_module_get_name(mod));
81 break;
82
83 case KMOD_MODULE_LIVE:
84 log_debug("Module '%s' is already loaded", sym_kmod_module_get_name(mod));
85 break;
86
87 default:
88 err = sym_kmod_module_probe_insert_module(
89 mod,
90 KMOD_PROBE_APPLY_BLACKLIST,
91 /* extra_options= */ NULL,
92 /* run_install= */ NULL,
93 /* data= */ NULL,
94 /* print_action= */ NULL);
95 if (err == 0)
96 log_full(verbose ? LOG_INFO : LOG_DEBUG,
97 "Inserted module '%s'", sym_kmod_module_get_name(mod));
98 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
99 log_full(verbose ? LOG_INFO : LOG_DEBUG,
100 "Module '%s' is deny-listed (by kmod)", sym_kmod_module_get_name(mod));
101 else {
102 assert(err < 0);
103
104 if (err == -EPERM) {
105 if (!denylist_parsed) {
106 r = proc_cmdline_parse(parse_proc_cmdline_item, &denylist, 0);
107 if (r < 0)
108 log_full_errno(!verbose ? LOG_DEBUG : LOG_WARNING,
109 r,
110 "Failed to parse kernel command line, ignoring: %m");
111
112 denylist_parsed = true;
113 }
114 if (strv_contains(denylist, sym_kmod_module_get_name(mod))) {
115 log_full(verbose ? LOG_INFO : LOG_DEBUG,
116 "Module '%s' is deny-listed (by kernel)", sym_kmod_module_get_name(mod));
117 continue;
118 }
119 }
120
121 log_full_errno(!verbose ? LOG_DEBUG :
122 err == -ENODEV ? LOG_NOTICE :
123 err == -ENOENT ? LOG_WARNING :
124 LOG_ERR,
125 err,
126 "Failed to insert module '%s': %m",
127 sym_kmod_module_get_name(mod));
128 if (!IN_SET(err, -ENODEV, -ENOENT))
129 r = err;
130 }
131 }
132 }
133
134 return r;
135 }
136
137 _printf_(6,0) static void systemd_kmod_log(
138 void *data,
139 int priority,
140 const char *file,
141 int line,
142 const char *fn,
143 const char *format,
144 va_list args) {
145
146 log_internalv(priority, 0, file, line, fn, format, args);
147 }
148
149 int module_setup_context(struct kmod_ctx **ret) {
150 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
151 int r;
152
153 assert(ret);
154
155 r = dlopen_libkmod(LOG_DEBUG);
156 if (r < 0)
157 return r;
158
159 ctx = sym_kmod_new(NULL, NULL);
160 if (!ctx)
161 return -ENOMEM;
162
163 (void) sym_kmod_load_resources(ctx);
164 sym_kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
165
166 *ret = TAKE_PTR(ctx);
167 return 0;
168 }
169
170 #endif
171
172 int dlopen_libkmod(int log_level) {
173 #if HAVE_KMOD
174 static void *libkmod_dl = NULL;
175
176 LIBKMOD_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
177
178 return dlopen_many_sym_or_warn(
179 &libkmod_dl,
180 "libkmod.so.2",
181 log_level,
182 DLSYM_ARG(kmod_list_next),
183 DLSYM_ARG(kmod_load_resources),
184 DLSYM_ARG(kmod_module_get_initstate),
185 DLSYM_ARG(kmod_module_get_module),
186 DLSYM_ARG(kmod_module_get_name),
187 DLSYM_ARG(kmod_module_new_from_lookup),
188 DLSYM_ARG(kmod_module_probe_insert_module),
189 DLSYM_ARG(kmod_module_unref),
190 DLSYM_ARG(kmod_module_unref_list),
191 DLSYM_ARG(kmod_new),
192 DLSYM_ARG(kmod_set_log_fn),
193 DLSYM_ARG(kmod_unref),
194 DLSYM_ARG(kmod_validate_resources));
195 #else
196 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
197 "libkmod support is not compiled in.");
198 #endif
199 }