]>
Commit | Line | Data |
---|---|---|
ecd40ee4 | 1 | /* |
586fc304 LDM |
2 | * libkmod - interface to kernel module operations |
3 | * | |
e6b0e49b | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
586fc304 LDM |
5 | * |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
cb451f35 LDM |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. | |
586fc304 LDM |
10 | * |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
dea2dfee | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
586fc304 | 18 | */ |
ecd40ee4 | 19 | |
ee1d188f | 20 | #include <assert.h> |
c2e4286b | 21 | #include <ctype.h> |
ecd40ee4 | 22 | #include <errno.h> |
7f3eb0cc | 23 | #include <fnmatch.h> |
c2e4286b LDM |
24 | #include <limits.h> |
25 | #include <stdarg.h> | |
26 | #include <stddef.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
ecd40ee4 | 29 | #include <string.h> |
c2e4286b | 30 | #include <unistd.h> |
c4dc3ca8 | 31 | #include <sys/stat.h> |
c2e4286b | 32 | #include <sys/utsname.h> |
ecd40ee4 | 33 | |
0db718ed | 34 | #include <shared/hash.h> |
96573a02 LDM |
35 | #include <shared/util.h> |
36 | ||
586fc304 | 37 | #include "libkmod.h" |
83b855a6 | 38 | #include "libkmod-internal.h" |
9ba6f57b | 39 | #include "libkmod-index.h" |
ecd40ee4 | 40 | |
fd186ae9 LDM |
41 | #define KMOD_HASH_SIZE (256) |
42 | #define KMOD_LRU_MAX (128) | |
3805274b | 43 | #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1 |
fd186ae9 | 44 | |
ecd40ee4 | 45 | /** |
586fc304 LDM |
46 | * SECTION:libkmod |
47 | * @short_description: libkmod context | |
ecd40ee4 LDM |
48 | * |
49 | * The context contains the default values for the library user, | |
50 | * and is passed to all library operations. | |
51 | */ | |
52 | ||
efc2e4b4 | 53 | static const struct { |
63be91cb LDM |
54 | const char *fn; |
55 | const char *prefix; | |
56 | } index_files[] = { | |
b08314f7 LDM |
57 | [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" }, |
58 | [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " }, | |
59 | [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "}, | |
b866b216 | 60 | [KMOD_INDEX_MODULES_BUILTIN_ALIAS] = { .fn = "modules.builtin.alias", .prefix = "" }, |
3805274b | 61 | [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""}, |
a4a75029 LDM |
62 | }; |
63 | ||
efc2e4b4 | 64 | static const char *const default_config_paths[] = { |
a308abec | 65 | SYSCONFDIR "/modprobe.d", |
436da1e9 | 66 | "/run/modprobe.d", |
9319b0f4 | 67 | "/usr/local/lib/modprobe.d", |
c5b37dba | 68 | "/lib/modprobe.d", |
cb8d4d3e GSB |
69 | NULL |
70 | }; | |
71 | ||
ecd40ee4 | 72 | /** |
586fc304 | 73 | * kmod_ctx: |
ecd40ee4 LDM |
74 | * |
75 | * Opaque object representing the library context. | |
76 | */ | |
586fc304 | 77 | struct kmod_ctx { |
ecd40ee4 | 78 | int refcount; |
8d3f3ef8 | 79 | int log_priority; |
1bdd951e | 80 | void (*log_fn)(void *data, |
e4351b05 LDM |
81 | int priority, const char *file, int line, |
82 | const char *fn, const char *format, va_list args); | |
1bdd951e | 83 | void *log_data; |
1ce08a56 GSB |
84 | const void *userdata; |
85 | char *dirname; | |
d13e606f | 86 | struct kmod_config *config; |
822913d7 | 87 | struct hash *modules_by_name; |
b08314f7 LDM |
88 | struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE]; |
89 | unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE]; | |
ecd40ee4 LDM |
90 | }; |
91 | ||
71928288 LDM |
92 | void kmod_log(const struct kmod_ctx *ctx, |
93 | int priority, const char *file, int line, const char *fn, | |
94 | const char *format, ...) | |
ecd40ee4 LDM |
95 | { |
96 | va_list args; | |
97 | ||
e5c60f1c GSB |
98 | if (ctx->log_fn == NULL) |
99 | return; | |
100 | ||
ecd40ee4 | 101 | va_start(args, format); |
1bdd951e | 102 | ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args); |
ecd40ee4 LDM |
103 | va_end(args); |
104 | } | |
105 | ||
1958af88 | 106 | _printf_format_(6, 0) |
1bdd951e | 107 | static void log_filep(void *data, |
e4351b05 LDM |
108 | int priority, const char *file, int line, |
109 | const char *fn, const char *format, va_list args) | |
ecd40ee4 | 110 | { |
1bdd951e | 111 | FILE *fp = data; |
e3cb0900 GSB |
112 | #ifdef ENABLE_DEBUG |
113 | char buf[16]; | |
114 | const char *priname; | |
115 | switch (priority) { | |
116 | case LOG_EMERG: | |
117 | priname = "EMERGENCY"; | |
118 | break; | |
119 | case LOG_ALERT: | |
120 | priname = "ALERT"; | |
121 | break; | |
122 | case LOG_CRIT: | |
123 | priname = "CRITICAL"; | |
124 | break; | |
125 | case LOG_ERR: | |
126 | priname = "ERROR"; | |
127 | break; | |
128 | case LOG_WARNING: | |
129 | priname = "WARNING"; | |
130 | break; | |
131 | case LOG_NOTICE: | |
132 | priname = "NOTICE"; | |
133 | break; | |
134 | case LOG_INFO: | |
135 | priname = "INFO"; | |
136 | break; | |
137 | case LOG_DEBUG: | |
138 | priname = "DEBUG"; | |
139 | break; | |
140 | default: | |
141 | snprintf(buf, sizeof(buf), "L:%d", priority); | |
142 | priname = buf; | |
143 | } | |
144 | fprintf(fp, "libkmod: %s %s:%d %s: ", priname, file, line, fn); | |
145 | #else | |
1bdd951e | 146 | fprintf(fp, "libkmod: %s: ", fn); |
e3cb0900 | 147 | #endif |
1bdd951e | 148 | vfprintf(fp, format, args); |
ecd40ee4 LDM |
149 | } |
150 | ||
f7f28510 LDM |
151 | |
152 | /** | |
153 | * kmod_get_dirname: | |
154 | * @ctx: kmod library context | |
155 | * | |
156 | * Retrieve the absolute path used for linux modules in this context. The path | |
157 | * is computed from the arguments to kmod_new(). | |
158 | */ | |
159 | KMOD_EXPORT const char *kmod_get_dirname(const struct kmod_ctx *ctx) | |
221631d5 LDM |
160 | { |
161 | return ctx->dirname; | |
162 | } | |
163 | ||
ecd40ee4 | 164 | /** |
586fc304 LDM |
165 | * kmod_get_userdata: |
166 | * @ctx: kmod library context | |
ecd40ee4 LDM |
167 | * |
168 | * Retrieve stored data pointer from library context. This might be useful | |
be5a6dea | 169 | * to access from callbacks. |
ecd40ee4 LDM |
170 | * |
171 | * Returns: stored userdata | |
54ba8b34 | 172 | */ |
6d177553 | 173 | KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx) |
ecd40ee4 LDM |
174 | { |
175 | if (ctx == NULL) | |
176 | return NULL; | |
1ce08a56 | 177 | return (void *)ctx->userdata; |
ecd40ee4 LDM |
178 | } |
179 | ||
180 | /** | |
586fc304 LDM |
181 | * kmod_set_userdata: |
182 | * @ctx: kmod library context | |
ecd40ee4 LDM |
183 | * @userdata: data pointer |
184 | * | |
185 | * Store custom @userdata in the library context. | |
54ba8b34 | 186 | */ |
1ce08a56 | 187 | KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata) |
ecd40ee4 LDM |
188 | { |
189 | if (ctx == NULL) | |
190 | return; | |
191 | ctx->userdata = userdata; | |
192 | } | |
193 | ||
194 | static int log_priority(const char *priority) | |
195 | { | |
196 | char *endptr; | |
197 | int prio; | |
198 | ||
199 | prio = strtol(priority, &endptr, 10); | |
200 | if (endptr[0] == '\0' || isspace(endptr[0])) | |
201 | return prio; | |
202 | if (strncmp(priority, "err", 3) == 0) | |
203 | return LOG_ERR; | |
204 | if (strncmp(priority, "info", 4) == 0) | |
205 | return LOG_INFO; | |
206 | if (strncmp(priority, "debug", 5) == 0) | |
207 | return LOG_DEBUG; | |
208 | return 0; | |
209 | } | |
210 | ||
c5b37dba | 211 | static const char *dirname_default_prefix = "/lib/modules"; |
904c63aa | 212 | |
1ce08a56 | 213 | static char *get_kernel_release(const char *dirname) |
221631d5 LDM |
214 | { |
215 | struct utsname u; | |
904c63aa LDM |
216 | char *p; |
217 | ||
218 | if (dirname != NULL) | |
2e092e19 | 219 | return path_make_absolute_cwd(dirname); |
221631d5 LDM |
220 | |
221 | if (uname(&u) < 0) | |
222 | return NULL; | |
223 | ||
904c63aa LDM |
224 | if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0) |
225 | return NULL; | |
226 | ||
227 | return p; | |
221631d5 LDM |
228 | } |
229 | ||
ecd40ee4 | 230 | /** |
586fc304 | 231 | * kmod_new: |
cb8d4d3e | 232 | * @dirname: what to consider as linux module's directory, if NULL |
c5b37dba | 233 | * defaults to /lib/modules/`uname -r`. If it's relative, |
491c4902 CY |
234 | * it's treated as relative to the current working directory. |
235 | * Otherwise, give an absolute dirname. | |
cb8d4d3e | 236 | * @config_paths: ordered array of paths (directories or files) where |
be5a6dea | 237 | * to load from user-defined configuration parameters such as |
9319b0f4 LDM |
238 | * alias, blacklists, commands (install, remove). If NULL |
239 | * defaults to /etc/modprobe.d, /run/modprobe.d, | |
240 | * /usr/local/lib/modprobe.d and /lib/modprobe.d. Give an empty | |
241 | * vector if configuration should not be read. This array must | |
242 | * be null terminated. | |
cb8d4d3e | 243 | * |
e1daa4f5 LDM |
244 | * Create kmod library context. This reads the kmod configuration |
245 | * and fills in the default values. | |
246 | * | |
247 | * The initial refcount is 1, and needs to be decremented to | |
248 | * release the resources of the kmod library context. | |
249 | * | |
586fc304 | 250 | * Returns: a new kmod library context |
54ba8b34 | 251 | */ |
c35347f1 LDM |
252 | KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, |
253 | const char * const *config_paths) | |
ecd40ee4 LDM |
254 | { |
255 | const char *env; | |
52a7704f | 256 | struct kmod_ctx *ctx; |
d13e606f | 257 | int err; |
ecd40ee4 | 258 | |
52a7704f LDM |
259 | ctx = calloc(1, sizeof(struct kmod_ctx)); |
260 | if (!ctx) | |
261 | return NULL; | |
ecd40ee4 | 262 | |
52a7704f | 263 | ctx->refcount = 1; |
1bdd951e GSB |
264 | ctx->log_fn = log_filep; |
265 | ctx->log_data = stderr; | |
52a7704f | 266 | ctx->log_priority = LOG_ERR; |
ecd40ee4 | 267 | |
904c63aa | 268 | ctx->dirname = get_kernel_release(dirname); |
221631d5 | 269 | |
ecd40ee4 | 270 | /* environment overwrites config */ |
41a51c2a | 271 | env = secure_getenv("KMOD_LOG"); |
ecd40ee4 | 272 | if (env != NULL) |
52a7704f | 273 | kmod_set_log_priority(ctx, log_priority(env)); |
ecd40ee4 | 274 | |
cb8d4d3e GSB |
275 | if (config_paths == NULL) |
276 | config_paths = default_config_paths; | |
277 | err = kmod_config_new(ctx, &ctx->config, config_paths); | |
d13e606f | 278 | if (err < 0) { |
fd186ae9 LDM |
279 | ERR(ctx, "could not create config\n"); |
280 | goto fail; | |
281 | } | |
282 | ||
822913d7 | 283 | ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL); |
fd186ae9 LDM |
284 | if (ctx->modules_by_name == NULL) { |
285 | ERR(ctx, "could not create by-name hash\n"); | |
286 | goto fail; | |
d13e606f | 287 | } |
7c2ab358 | 288 | |
ae6df84a LDM |
289 | INFO(ctx, "ctx %p created\n", ctx); |
290 | DBG(ctx, "log_priority=%d\n", ctx->log_priority); | |
52a7704f LDM |
291 | |
292 | return ctx; | |
fd186ae9 LDM |
293 | |
294 | fail: | |
295 | free(ctx->modules_by_name); | |
296 | free(ctx->dirname); | |
297 | free(ctx); | |
298 | return NULL; | |
ecd40ee4 LDM |
299 | } |
300 | ||
301 | /** | |
586fc304 LDM |
302 | * kmod_ref: |
303 | * @ctx: kmod library context | |
ecd40ee4 | 304 | * |
586fc304 | 305 | * Take a reference of the kmod library context. |
ecd40ee4 | 306 | * |
586fc304 | 307 | * Returns: the passed kmod library context |
54ba8b34 | 308 | */ |
586fc304 | 309 | KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
310 | { |
311 | if (ctx == NULL) | |
312 | return NULL; | |
313 | ctx->refcount++; | |
314 | return ctx; | |
315 | } | |
316 | ||
317 | /** | |
586fc304 LDM |
318 | * kmod_unref: |
319 | * @ctx: kmod library context | |
ecd40ee4 | 320 | * |
586fc304 | 321 | * Drop a reference of the kmod library context. If the refcount |
ecd40ee4 | 322 | * reaches zero, the resources of the context will be released. |
491c4902 CY |
323 | * |
324 | * Returns: the passed kmod library context or NULL if it's freed | |
54ba8b34 | 325 | */ |
586fc304 | 326 | KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx) |
ecd40ee4 LDM |
327 | { |
328 | if (ctx == NULL) | |
329 | return NULL; | |
4d1e689a LDM |
330 | |
331 | if (--ctx->refcount > 0) | |
ecd40ee4 | 332 | return ctx; |
33bb69b9 | 333 | |
ae6df84a | 334 | INFO(ctx, "context %p released\n", ctx); |
33bb69b9 LDM |
335 | |
336 | kmod_unload_resources(ctx); | |
822913d7 | 337 | hash_free(ctx->modules_by_name); |
1ce08a56 | 338 | free(ctx->dirname); |
d13e606f GSB |
339 | if (ctx->config) |
340 | kmod_config_free(ctx->config); | |
33bb69b9 | 341 | |
ecd40ee4 LDM |
342 | free(ctx); |
343 | return NULL; | |
344 | } | |
345 | ||
346 | /** | |
586fc304 LDM |
347 | * kmod_set_log_fn: |
348 | * @ctx: kmod library context | |
ecd40ee4 | 349 | * @log_fn: function to be called for logging messages |
b5b4d8e8 | 350 | * @data: data to pass to log function |
ecd40ee4 LDM |
351 | * |
352 | * The built-in logging writes to stderr. It can be | |
353 | * overridden by a custom function, to plug log messages | |
354 | * into the user's logging functionality. | |
54ba8b34 | 355 | */ |
586fc304 | 356 | KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx, |
1bdd951e | 357 | void (*log_fn)(void *data, |
e4351b05 LDM |
358 | int priority, const char *file, |
359 | int line, const char *fn, | |
1bdd951e GSB |
360 | const char *format, va_list args), |
361 | const void *data) | |
ecd40ee4 | 362 | { |
e5c60f1c GSB |
363 | if (ctx == NULL) |
364 | return; | |
ecd40ee4 | 365 | ctx->log_fn = log_fn; |
1bdd951e | 366 | ctx->log_data = (void *)data; |
ae6df84a | 367 | INFO(ctx, "custom logging function %p registered\n", log_fn); |
ecd40ee4 LDM |
368 | } |
369 | ||
370 | /** | |
586fc304 LDM |
371 | * kmod_get_log_priority: |
372 | * @ctx: kmod library context | |
ecd40ee4 LDM |
373 | * |
374 | * Returns: the current logging priority | |
54ba8b34 | 375 | */ |
6d177553 | 376 | KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx) |
ecd40ee4 | 377 | { |
e5c60f1c GSB |
378 | if (ctx == NULL) |
379 | return -1; | |
ecd40ee4 LDM |
380 | return ctx->log_priority; |
381 | } | |
382 | ||
383 | /** | |
586fc304 LDM |
384 | * kmod_set_log_priority: |
385 | * @ctx: kmod library context | |
ecd40ee4 LDM |
386 | * @priority: the new logging priority |
387 | * | |
388 | * Set the current logging priority. The value controls which messages | |
389 | * are logged. | |
54ba8b34 | 390 | */ |
586fc304 | 391 | KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority) |
ecd40ee4 | 392 | { |
e5c60f1c GSB |
393 | if (ctx == NULL) |
394 | return; | |
ecd40ee4 LDM |
395 | ctx->log_priority = priority; |
396 | } | |
7f3eb0cc | 397 | |
fd186ae9 | 398 | struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, |
8bdeca11 | 399 | const char *key) |
fd186ae9 LDM |
400 | { |
401 | struct kmod_module *mod; | |
402 | ||
822913d7 | 403 | mod = hash_find(ctx->modules_by_name, key); |
fd186ae9 | 404 | |
8bdeca11 | 405 | DBG(ctx, "get module name='%s' found=%p\n", key, mod); |
fd186ae9 LDM |
406 | |
407 | return mod; | |
408 | } | |
409 | ||
8bdeca11 LDM |
410 | void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
411 | const char *key) | |
fd186ae9 | 412 | { |
8bdeca11 | 413 | DBG(ctx, "add %p key='%s'\n", mod, key); |
fd186ae9 | 414 | |
822913d7 | 415 | hash_add(ctx->modules_by_name, key, mod); |
fd186ae9 LDM |
416 | } |
417 | ||
8bdeca11 LDM |
418 | void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, |
419 | const char *key) | |
fd186ae9 | 420 | { |
8bdeca11 | 421 | DBG(ctx, "del %p key='%s'\n", mod, key); |
fd186ae9 | 422 | |
822913d7 | 423 | hash_del(ctx->modules_by_name, key); |
fd186ae9 | 424 | } |
9ba6f57b | 425 | |
a009482c | 426 | static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx, |
810803db | 427 | enum kmod_index index_number, |
7b30f4f4 | 428 | const char *name, |
9ba6f57b LDM |
429 | struct kmod_list **list) |
430 | { | |
6f1bc6e3 | 431 | int err, nmatch = 0; |
0fbdfef3 | 432 | struct index_file *idx; |
9ba6f57b LDM |
433 | struct index_value *realnames, *realname; |
434 | ||
65a84f55 | 435 | if (ctx->indexes[index_number] != NULL) { |
3e245be1 | 436 | DBG(ctx, "use mmaped index '%s' for name=%s\n", |
63be91cb | 437 | index_files[index_number].fn, name); |
65a84f55 LDM |
438 | realnames = index_mm_searchwild(ctx->indexes[index_number], |
439 | name); | |
9fd58f30 | 440 | } else { |
65a84f55 LDM |
441 | char fn[PATH_MAX]; |
442 | ||
3b209959 | 443 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
63be91cb | 444 | index_files[index_number].fn); |
9ba6f57b | 445 | |
65a84f55 | 446 | DBG(ctx, "file=%s name=%s\n", fn, name); |
9ba6f57b | 447 | |
65a84f55 LDM |
448 | idx = index_file_open(fn); |
449 | if (idx == NULL) | |
450 | return -ENOSYS; | |
9ba6f57b | 451 | |
65a84f55 LDM |
452 | realnames = index_searchwild(idx, name); |
453 | index_file_close(idx); | |
454 | } | |
4272d087 | 455 | |
a955f71f | 456 | for (realname = realnames; realname; realname = realname->next) { |
9ba6f57b LDM |
457 | struct kmod_module *mod; |
458 | ||
ee3b3ff2 | 459 | err = kmod_module_new_from_alias(ctx, name, realname->value, &mod); |
9ba6f57b | 460 | if (err < 0) { |
dfa96f15 GSB |
461 | ERR(ctx, "Could not create module for alias=%s realname=%s: %s\n", |
462 | name, realname->value, strerror(-err)); | |
23fc91c6 | 463 | goto fail; |
9ba6f57b LDM |
464 | } |
465 | ||
466 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 467 | nmatch++; |
9ba6f57b LDM |
468 | } |
469 | ||
9ba6f57b | 470 | index_values_free(realnames); |
23fc91c6 LDM |
471 | return nmatch; |
472 | ||
473 | fail: | |
474 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
e84d912b | 475 | index_values_free(realnames); |
9ba6f57b | 476 | return err; |
7b30f4f4 LDM |
477 | |
478 | } | |
479 | ||
7b30f4f4 LDM |
480 | int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, |
481 | struct kmod_list **list) | |
482 | { | |
0c010fae | 483 | if (!strstartswith(name, "symbol:")) |
7b30f4f4 LDM |
484 | return 0; |
485 | ||
b08314f7 LDM |
486 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL, |
487 | name, list); | |
9ba6f57b LDM |
488 | } |
489 | ||
49e61ca3 LDM |
490 | int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, |
491 | struct kmod_list **list) | |
492 | { | |
b08314f7 LDM |
493 | return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS, |
494 | name, list); | |
49e61ca3 LDM |
495 | } |
496 | ||
fd44a98a | 497 | static char *lookup_builtin_file(struct kmod_ctx *ctx, const char *name) |
3805274b | 498 | { |
fd44a98a | 499 | char *line; |
3805274b | 500 | |
ee1d188f LDM |
501 | if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) { |
502 | DBG(ctx, "use mmaped index '%s' modname=%s\n", | |
503 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn, | |
504 | name); | |
505 | line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN], | |
506 | name); | |
507 | } else { | |
508 | struct index_file *idx; | |
509 | char fn[PATH_MAX]; | |
510 | ||
511 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, | |
512 | index_files[KMOD_INDEX_MODULES_BUILTIN].fn); | |
513 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
514 | ||
515 | idx = index_file_open(fn); | |
516 | if (idx == NULL) { | |
517 | DBG(ctx, "could not open builtin file '%s'\n", fn); | |
fd44a98a | 518 | return NULL; |
ee1d188f LDM |
519 | } |
520 | ||
521 | line = index_search(idx, name); | |
522 | index_file_close(idx); | |
523 | } | |
524 | ||
fd44a98a HJ |
525 | return line; |
526 | } | |
527 | ||
b866b216 AG |
528 | int kmod_lookup_alias_from_kernel_builtin_file(struct kmod_ctx *ctx, |
529 | const char *name, | |
530 | struct kmod_list **list) | |
531 | { | |
532 | struct kmod_list *l; | |
89443220 LDM |
533 | int ret; |
534 | ||
535 | assert(*list == NULL); | |
536 | ||
537 | ret = kmod_lookup_alias_from_alias_bin(ctx, | |
538 | KMOD_INDEX_MODULES_BUILTIN_ALIAS, | |
539 | name, list); | |
540 | ||
541 | kmod_list_foreach(l, *list) { | |
542 | struct kmod_module *mod = l->data; | |
543 | kmod_module_set_builtin(mod, true); | |
b866b216 AG |
544 | } |
545 | ||
546 | return ret; | |
547 | } | |
548 | ||
fd44a98a HJ |
549 | int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name, |
550 | struct kmod_list **list) | |
551 | { | |
552 | char *line; | |
553 | int err = 0; | |
554 | ||
555 | assert(*list == NULL); | |
556 | ||
557 | line = lookup_builtin_file(ctx, name); | |
ee1d188f LDM |
558 | if (line != NULL) { |
559 | struct kmod_module *mod; | |
560 | ||
561 | err = kmod_module_new_from_name(ctx, name, &mod); | |
562 | if (err < 0) { | |
563 | ERR(ctx, "Could not create module from name %s: %s\n", | |
564 | name, strerror(-err)); | |
565 | goto finish; | |
566 | } | |
567 | ||
fd44a98a HJ |
568 | /* already mark it as builtin since it's being created from |
569 | * this index */ | |
ee1d188f LDM |
570 | kmod_module_set_builtin(mod, true); |
571 | *list = kmod_list_append(*list, mod); | |
572 | if (*list == NULL) | |
573 | err = -ENOMEM; | |
3805274b LDM |
574 | } |
575 | ||
ee1d188f LDM |
576 | finish: |
577 | free(line); | |
3805274b LDM |
578 | return err; |
579 | } | |
580 | ||
fd44a98a HJ |
581 | bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name) |
582 | { | |
583 | _cleanup_free_ char *line; | |
584 | ||
585 | line = lookup_builtin_file(ctx, name); | |
586 | ||
587 | return line != NULL; | |
588 | } | |
589 | ||
671d4894 | 590 | char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) |
1eb2ef69 LDM |
591 | { |
592 | struct index_file *idx; | |
593 | char fn[PATH_MAX]; | |
594 | char *line; | |
595 | ||
b08314f7 | 596 | if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) { |
85132101 | 597 | DBG(ctx, "use mmaped index '%s' modname=%s\n", |
b08314f7 LDM |
598 | index_files[KMOD_INDEX_MODULES_DEP].fn, name); |
599 | return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP], | |
600 | name); | |
85132101 GSB |
601 | } |
602 | ||
a4a75029 | 603 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, |
b08314f7 | 604 | index_files[KMOD_INDEX_MODULES_DEP].fn); |
1eb2ef69 LDM |
605 | |
606 | DBG(ctx, "file=%s modname=%s\n", fn, name); | |
607 | ||
608 | idx = index_file_open(fn); | |
609 | if (idx == NULL) { | |
adca3cd2 | 610 | DBG(ctx, "could not open moddep file '%s'\n", fn); |
1eb2ef69 LDM |
611 | return NULL; |
612 | } | |
613 | ||
614 | line = index_search(idx, name); | |
615 | index_file_close(idx); | |
616 | ||
617 | return line; | |
618 | } | |
619 | ||
64700e47 LDM |
620 | int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, |
621 | struct kmod_list **list) | |
622 | { | |
1eb2ef69 | 623 | char *line; |
64700e47 LDM |
624 | int n = 0; |
625 | ||
626 | /* | |
627 | * Module names do not contain ':'. Return early if we know it will | |
628 | * not be found. | |
629 | */ | |
630 | if (strchr(name, ':')) | |
631 | return 0; | |
632 | ||
671d4894 | 633 | line = kmod_search_moddep(ctx, name); |
64700e47 LDM |
634 | if (line != NULL) { |
635 | struct kmod_module *mod; | |
636 | ||
637 | n = kmod_module_new_from_name(ctx, name, &mod); | |
638 | if (n < 0) { | |
dfa96f15 GSB |
639 | ERR(ctx, "Could not create module from name %s: %s\n", |
640 | name, strerror(-n)); | |
64700e47 LDM |
641 | goto finish; |
642 | } | |
643 | ||
644 | *list = kmod_list_append(*list, mod); | |
671d4894 | 645 | kmod_module_parse_depline(mod, line); |
64700e47 LDM |
646 | } |
647 | ||
648 | finish: | |
649 | free(line); | |
64700e47 LDM |
650 | |
651 | return n; | |
652 | } | |
653 | ||
7f3eb0cc LDM |
654 | int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, |
655 | struct kmod_list **list) | |
656 | { | |
d13e606f | 657 | struct kmod_config *config = ctx->config; |
7f3eb0cc | 658 | struct kmod_list *l; |
23fc91c6 | 659 | int err, nmatch = 0; |
7f3eb0cc LDM |
660 | |
661 | kmod_list_foreach(l, config->aliases) { | |
662 | const char *aliasname = kmod_alias_get_name(l); | |
663 | const char *modname = kmod_alias_get_modname(l); | |
664 | ||
665 | if (fnmatch(aliasname, name, 0) == 0) { | |
666 | struct kmod_module *mod; | |
667 | ||
ee3b3ff2 LDM |
668 | err = kmod_module_new_from_alias(ctx, aliasname, |
669 | modname, &mod); | |
7f3eb0cc | 670 | if (err < 0) { |
dfa96f15 GSB |
671 | ERR(ctx, "Could not create module for alias=%s modname=%s: %s\n", |
672 | name, modname, strerror(-err)); | |
23fc91c6 | 673 | goto fail; |
7f3eb0cc LDM |
674 | } |
675 | ||
676 | *list = kmod_list_append(*list, mod); | |
23fc91c6 | 677 | nmatch++; |
7f3eb0cc LDM |
678 | } |
679 | } | |
680 | ||
23fc91c6 LDM |
681 | return nmatch; |
682 | ||
683 | fail: | |
684 | *list = kmod_list_remove_n_latest(*list, nmatch); | |
685 | return err; | |
7f3eb0cc | 686 | } |
1487a64f | 687 | |
f4fc5523 LDM |
688 | int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, |
689 | struct kmod_list **list) | |
690 | { | |
691 | struct kmod_config *config = ctx->config; | |
692 | struct kmod_list *l, *node; | |
693 | int err, nmatch = 0; | |
694 | ||
695 | kmod_list_foreach(l, config->install_commands) { | |
696 | const char *modname = kmod_command_get_modname(l); | |
697 | ||
698 | if (streq(modname, name)) { | |
699 | const char *cmd = kmod_command_get_command(l); | |
700 | struct kmod_module *mod; | |
701 | ||
702 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
703 | if (err < 0) { | |
dfa96f15 GSB |
704 | ERR(ctx, "Could not create module from name %s: %s\n", |
705 | modname, strerror(-err)); | |
f4fc5523 LDM |
706 | return err; |
707 | } | |
708 | ||
709 | node = kmod_list_append(*list, mod); | |
710 | if (node == NULL) { | |
711 | ERR(ctx, "out of memory\n"); | |
712 | return -ENOMEM; | |
713 | } | |
714 | ||
715 | *list = node; | |
716 | nmatch = 1; | |
717 | ||
718 | kmod_module_set_install_commands(mod, cmd); | |
719 | ||
720 | /* | |
721 | * match only the first one, like modprobe from | |
722 | * module-init-tools does | |
723 | */ | |
724 | break; | |
725 | } | |
726 | } | |
727 | ||
728 | if (nmatch) | |
729 | return nmatch; | |
730 | ||
731 | kmod_list_foreach(l, config->remove_commands) { | |
732 | const char *modname = kmod_command_get_modname(l); | |
733 | ||
734 | if (streq(modname, name)) { | |
735 | const char *cmd = kmod_command_get_command(l); | |
736 | struct kmod_module *mod; | |
737 | ||
738 | err = kmod_module_new_from_name(ctx, modname, &mod); | |
739 | if (err < 0) { | |
dfa96f15 GSB |
740 | ERR(ctx, "Could not create module from name %s: %s\n", |
741 | modname, strerror(-err)); | |
f4fc5523 LDM |
742 | return err; |
743 | } | |
744 | ||
745 | node = kmod_list_append(*list, mod); | |
746 | if (node == NULL) { | |
747 | ERR(ctx, "out of memory\n"); | |
748 | return -ENOMEM; | |
749 | } | |
750 | ||
751 | *list = node; | |
752 | nmatch = 1; | |
753 | ||
754 | kmod_module_set_remove_commands(mod, cmd); | |
755 | ||
756 | /* | |
757 | * match only the first one, like modprobe from | |
758 | * module-init-tools does | |
759 | */ | |
760 | break; | |
761 | } | |
762 | } | |
763 | ||
764 | return nmatch; | |
765 | } | |
766 | ||
ece09aac LDM |
767 | void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited) |
768 | { | |
769 | struct hash_iter iter; | |
770 | const void *v; | |
771 | ||
772 | hash_iter_init(ctx->modules_by_name, &iter); | |
773 | while (hash_iter_next(&iter, NULL, &v)) | |
774 | kmod_module_set_visited((struct kmod_module *)v, visited); | |
775 | } | |
776 | ||
450bd1b4 MM |
777 | void kmod_set_modules_required(struct kmod_ctx *ctx, bool required) |
778 | { | |
779 | struct hash_iter iter; | |
780 | const void *v; | |
781 | ||
782 | hash_iter_init(ctx->modules_by_name, &iter); | |
783 | while (hash_iter_next(&iter, NULL, &v)) | |
784 | kmod_module_set_required((struct kmod_module *)v, required); | |
785 | } | |
786 | ||
c4dc3ca8 LDM |
787 | static bool is_cache_invalid(const char *path, unsigned long long stamp) |
788 | { | |
789 | struct stat st; | |
790 | ||
791 | if (stat(path, &st) < 0) | |
792 | return true; | |
793 | ||
6068aaae | 794 | if (stamp != stat_mstamp(&st)) |
c4dc3ca8 LDM |
795 | return true; |
796 | ||
797 | return false; | |
798 | } | |
799 | ||
800 | /** | |
801 | * kmod_validate_resources: | |
802 | * @ctx: kmod library context | |
803 | * | |
804 | * Check if indexes and configuration files changed on disk and the current | |
805 | * context is not valid anymore. | |
806 | * | |
f4cc6ea5 | 807 | * Returns: KMOD_RESOURCES_OK if resources are still valid, |
c4dc3ca8 LDM |
808 | * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call |
809 | * kmod_unload_resources() and kmod_load_resources() or | |
810 | * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created. | |
811 | */ | |
812 | KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx) | |
813 | { | |
814 | struct kmod_list *l; | |
815 | size_t i; | |
816 | ||
817 | if (ctx == NULL || ctx->config == NULL) | |
818 | return KMOD_RESOURCES_MUST_RECREATE; | |
819 | ||
820 | kmod_list_foreach(l, ctx->config->paths) { | |
821 | struct kmod_config_path *cf = l->data; | |
822 | ||
823 | if (is_cache_invalid(cf->path, cf->stamp)) | |
824 | return KMOD_RESOURCES_MUST_RECREATE; | |
825 | } | |
826 | ||
b08314f7 | 827 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
c4dc3ca8 LDM |
828 | char path[PATH_MAX]; |
829 | ||
830 | if (ctx->indexes[i] == NULL) | |
831 | continue; | |
832 | ||
833 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, | |
63be91cb | 834 | index_files[i].fn); |
c4dc3ca8 LDM |
835 | |
836 | if (is_cache_invalid(path, ctx->indexes_stamp[i])) | |
837 | return KMOD_RESOURCES_MUST_RELOAD; | |
838 | } | |
839 | ||
840 | return KMOD_RESOURCES_OK; | |
841 | } | |
842 | ||
be5a6dea LDM |
843 | /** |
844 | * kmod_load_resources: | |
845 | * @ctx: kmod library context | |
846 | * | |
847 | * Load indexes and keep them open in @ctx. This way it's faster to lookup | |
848 | * information within the indexes. If this function is not called before a | |
849 | * search, the necessary index is always opened and closed. | |
850 | * | |
851 | * If user will do more than one or two lookups, insertions, deletions, most | |
852 | * likely it's good to call this function first. Particularly in a daemon like | |
853 | * udev that on bootup issues hundreds of calls to lookup the index, calling | |
854 | * this function will speedup the searches. | |
855 | * | |
856 | * Returns: 0 on success or < 0 otherwise. | |
857 | */ | |
33bb69b9 LDM |
858 | KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx) |
859 | { | |
d8d1d540 | 860 | int ret = 0; |
33bb69b9 LDM |
861 | size_t i; |
862 | ||
863 | if (ctx == NULL) | |
864 | return -ENOENT; | |
865 | ||
b08314f7 | 866 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
6de8f6e9 | 867 | char path[PATH_MAX]; |
3e676766 | 868 | |
16ca3666 | 869 | if (ctx->indexes[i] != NULL) { |
63be91cb LDM |
870 | INFO(ctx, "Index %s already loaded\n", |
871 | index_files[i].fn); | |
3e676766 LDM |
872 | continue; |
873 | } | |
79d57fcb | 874 | |
6de8f6e9 | 875 | snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname, |
63be91cb | 876 | index_files[i].fn); |
3bd7187f LDM |
877 | ret = index_mm_open(ctx, path, &ctx->indexes_stamp[i], |
878 | &ctx->indexes[i]); | |
d8d1d540 LDM |
879 | |
880 | /* | |
881 | * modules.builtin.alias are considered optional since it's | |
882 | * recently added and older installations may not have it; | |
883 | * we allow failing for any reason | |
884 | */ | |
885 | if (ret) { | |
886 | if (i != KMOD_INDEX_MODULES_BUILTIN_ALIAS) | |
887 | break; | |
888 | ret = 0; | |
889 | } | |
33bb69b9 LDM |
890 | } |
891 | ||
3bd7187f LDM |
892 | if (ret) |
893 | kmod_unload_resources(ctx); | |
33bb69b9 | 894 | |
3bd7187f | 895 | return ret; |
33bb69b9 LDM |
896 | } |
897 | ||
be5a6dea LDM |
898 | /** |
899 | * kmod_unload_resources: | |
900 | * @ctx: kmod library context | |
901 | * | |
902 | * Unload all the indexes. This will free the resources to maintain the index | |
903 | * open and all subsequent searches will need to open and close the index. | |
904 | * | |
905 | * User is free to call kmod_load_resources() and kmod_unload_resources() as | |
906 | * many times as wanted during the lifecycle of @ctx. For example, if a daemon | |
907 | * knows that when starting up it will lookup a lot of modules, it could call | |
908 | * kmod_load_resources() and after the first burst of searches is gone, it | |
909 | * could free the resources by calling kmod_unload_resources(). | |
910 | * | |
911 | * Returns: 0 on success or < 0 otherwise. | |
912 | */ | |
33bb69b9 LDM |
913 | KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx) |
914 | { | |
915 | size_t i; | |
916 | ||
917 | if (ctx == NULL) | |
918 | return; | |
919 | ||
b08314f7 | 920 | for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) { |
33bb69b9 LDM |
921 | if (ctx->indexes[i] != NULL) { |
922 | index_mm_close(ctx->indexes[i]); | |
923 | ctx->indexes[i] = NULL; | |
9fd58f30 | 924 | ctx->indexes_stamp[i] = 0; |
33bb69b9 LDM |
925 | } |
926 | } | |
927 | } | |
bd3f5535 | 928 | |
0224482e LDM |
929 | /** |
930 | * kmod_dump_index: | |
931 | * @ctx: kmod library context | |
d7152f62 CY |
932 | * @type: index to dump, valid indexes are |
933 | * KMOD_INDEX_MODULES_DEP: index of module dependencies; | |
934 | * KMOD_INDEX_MODULES_ALIAS: index of module aliases; | |
935 | * KMOD_INDEX_MODULES_SYMBOL: index of symbol aliases; | |
936 | * KMOD_INDEX_MODULES_BUILTIN: index of builtin module. | |
0224482e LDM |
937 | * @fd: file descriptor to dump index to |
938 | * | |
09e9ae58 LDM |
939 | * Dump index to file descriptor. Note that this function doesn't use stdio.h |
940 | * so call fflush() before calling this function to be sure data is written in | |
941 | * order. | |
0224482e LDM |
942 | * |
943 | * Returns: 0 on success or < 0 otherwise. | |
944 | */ | |
758428a7 LDM |
945 | KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type, |
946 | int fd) | |
947 | { | |
948 | if (ctx == NULL) | |
949 | return -ENOSYS; | |
950 | ||
951 | if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE) | |
952 | return -ENOENT; | |
953 | ||
954 | if (ctx->indexes[type] != NULL) { | |
955 | DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn); | |
956 | index_mm_dump(ctx->indexes[type], fd, | |
957 | index_files[type].prefix); | |
958 | } else { | |
959 | char fn[PATH_MAX]; | |
960 | struct index_file *idx; | |
961 | ||
962 | snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname, | |
963 | index_files[type].fn); | |
964 | ||
965 | DBG(ctx, "file=%s\n", fn); | |
966 | ||
967 | idx = index_file_open(fn); | |
968 | if (idx == NULL) | |
969 | return -ENOSYS; | |
970 | ||
971 | index_dump(idx, fd, index_files[type].prefix); | |
972 | index_file_close(idx); | |
973 | } | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
e7fc2c86 | 978 | const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx) |
c1c9c446 | 979 | { |
e7fc2c86 | 980 | return ctx->config; |
8b5ee618 | 981 | } |