]> git.ipfire.org Git - thirdparty/kmod.git/blame - libkmod/libkmod.c
Fix common misspelling with codespell
[thirdparty/kmod.git] / libkmod / libkmod.c
CommitLineData
ecd40ee4 1/*
586fc304
LDM
2 * libkmod - interface to kernel module operations
3 *
a66a6a99 4 * Copyright (C) 2011-2012 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
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
ecd40ee4
LDM
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <stddef.h>
24#include <stdarg.h>
1eb2ef69 25#include <limits.h>
ecd40ee4
LDM
26#include <unistd.h>
27#include <errno.h>
7f3eb0cc 28#include <fnmatch.h>
ecd40ee4
LDM
29#include <string.h>
30#include <ctype.h>
221631d5 31#include <sys/utsname.h>
c4dc3ca8 32#include <sys/stat.h>
ecd40ee4 33
586fc304
LDM
34#include "libkmod.h"
35#include "libkmod-private.h"
9ba6f57b 36#include "libkmod-index.h"
ecd40ee4 37
fd186ae9
LDM
38#define KMOD_HASH_SIZE (256)
39#define KMOD_LRU_MAX (128)
b08314f7 40#define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_SYMBOL + 1
fd186ae9 41
ecd40ee4 42/**
586fc304
LDM
43 * SECTION:libkmod
44 * @short_description: libkmod context
ecd40ee4
LDM
45 *
46 * The context contains the default values for the library user,
47 * and is passed to all library operations.
48 */
49
63be91cb
LDM
50static struct _index_files {
51 const char *fn;
52 const char *prefix;
53} index_files[] = {
b08314f7
LDM
54 [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" },
55 [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " },
56 [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "},
a4a75029
LDM
57};
58
cb8d4d3e
GSB
59static const char *default_config_paths[] = {
60 "/run/modprobe.d",
a308abec
KS
61 SYSCONFDIR "/modprobe.d",
62 ROOTPREFIX "/lib/modprobe.d",
cb8d4d3e
GSB
63 NULL
64};
65
ecd40ee4 66/**
586fc304 67 * kmod_ctx:
ecd40ee4
LDM
68 *
69 * Opaque object representing the library context.
70 */
586fc304 71struct kmod_ctx {
ecd40ee4 72 int refcount;
8d3f3ef8 73 int log_priority;
1bdd951e 74 void (*log_fn)(void *data,
e4351b05
LDM
75 int priority, const char *file, int line,
76 const char *fn, const char *format, va_list args);
1bdd951e 77 void *log_data;
1ce08a56
GSB
78 const void *userdata;
79 char *dirname;
d13e606f 80 struct kmod_config *config;
822913d7 81 struct hash *modules_by_name;
b08314f7
LDM
82 struct index_mm *indexes[_KMOD_INDEX_MODULES_SIZE];
83 unsigned long long indexes_stamp[_KMOD_INDEX_MODULES_SIZE];
ecd40ee4
LDM
84};
85
1bdd951e 86void kmod_log(const struct kmod_ctx *ctx,
e4351b05
LDM
87 int priority, const char *file, int line, const char *fn,
88 const char *format, ...)
ecd40ee4
LDM
89{
90 va_list args;
91
e5c60f1c
GSB
92 if (ctx->log_fn == NULL)
93 return;
94
ecd40ee4 95 va_start(args, format);
1bdd951e 96 ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args);
ecd40ee4
LDM
97 va_end(args);
98}
99
1bdd951e 100static void log_filep(void *data,
e4351b05
LDM
101 int priority, const char *file, int line,
102 const char *fn, const char *format, va_list args)
ecd40ee4 103{
1bdd951e
GSB
104 FILE *fp = data;
105 fprintf(fp, "libkmod: %s: ", fn);
106 vfprintf(fp, format, args);
ecd40ee4
LDM
107}
108
1ce08a56 109const char *kmod_get_dirname(const struct kmod_ctx *ctx)
221631d5
LDM
110{
111 return ctx->dirname;
112}
113
ecd40ee4 114/**
586fc304
LDM
115 * kmod_get_userdata:
116 * @ctx: kmod library context
ecd40ee4
LDM
117 *
118 * Retrieve stored data pointer from library context. This might be useful
be5a6dea 119 * to access from callbacks.
ecd40ee4
LDM
120 *
121 * Returns: stored userdata
54ba8b34 122 */
6d177553 123KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx)
ecd40ee4
LDM
124{
125 if (ctx == NULL)
126 return NULL;
1ce08a56 127 return (void *)ctx->userdata;
ecd40ee4
LDM
128}
129
130/**
586fc304
LDM
131 * kmod_set_userdata:
132 * @ctx: kmod library context
ecd40ee4
LDM
133 * @userdata: data pointer
134 *
135 * Store custom @userdata in the library context.
54ba8b34 136 */
1ce08a56 137KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata)
ecd40ee4
LDM
138{
139 if (ctx == NULL)
140 return;
141 ctx->userdata = userdata;
142}
143
144static int log_priority(const char *priority)
145{
146 char *endptr;
147 int prio;
148
149 prio = strtol(priority, &endptr, 10);
150 if (endptr[0] == '\0' || isspace(endptr[0]))
151 return prio;
152 if (strncmp(priority, "err", 3) == 0)
153 return LOG_ERR;
154 if (strncmp(priority, "info", 4) == 0)
155 return LOG_INFO;
156 if (strncmp(priority, "debug", 5) == 0)
157 return LOG_DEBUG;
158 return 0;
159}
160
a308abec 161static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules";
904c63aa 162
1ce08a56 163static char *get_kernel_release(const char *dirname)
221631d5
LDM
164{
165 struct utsname u;
904c63aa
LDM
166 char *p;
167
168 if (dirname != NULL)
2e092e19 169 return path_make_absolute_cwd(dirname);
221631d5
LDM
170
171 if (uname(&u) < 0)
172 return NULL;
173
904c63aa
LDM
174 if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
175 return NULL;
176
177 return p;
221631d5
LDM
178}
179
ecd40ee4 180/**
586fc304 181 * kmod_new:
cb8d4d3e 182 * @dirname: what to consider as linux module's directory, if NULL
2f47c7fa
LDM
183 * defaults to $rootprefix/lib/modules/`uname -r`. If it's relative,
184 * it's treated as relative to current the current working
185 * directory. Otherwise, give an absolute dirname.
cb8d4d3e 186 * @config_paths: ordered array of paths (directories or files) where
be5a6dea
LDM
187 * to load from user-defined configuration parameters such as
188 * alias, blacklists, commands (install, remove). If
189 * NULL defaults to /run/modprobe.d, /etc/modprobe.d and
2f47c7fa
LDM
190 * $rootprefix/lib/modprobe.d. Give an empty vector if
191 * configuration should not be read. This array must be null
192 * terminated.
cb8d4d3e 193 *
e1daa4f5
LDM
194 * Create kmod library context. This reads the kmod configuration
195 * and fills in the default values.
196 *
197 * The initial refcount is 1, and needs to be decremented to
198 * release the resources of the kmod library context.
199 *
586fc304 200 * Returns: a new kmod library context
54ba8b34 201 */
c35347f1
LDM
202KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
203 const char * const *config_paths)
ecd40ee4
LDM
204{
205 const char *env;
52a7704f 206 struct kmod_ctx *ctx;
d13e606f 207 int err;
ecd40ee4 208
52a7704f
LDM
209 ctx = calloc(1, sizeof(struct kmod_ctx));
210 if (!ctx)
211 return NULL;
ecd40ee4 212
52a7704f 213 ctx->refcount = 1;
1bdd951e
GSB
214 ctx->log_fn = log_filep;
215 ctx->log_data = stderr;
52a7704f 216 ctx->log_priority = LOG_ERR;
ecd40ee4 217
904c63aa 218 ctx->dirname = get_kernel_release(dirname);
221631d5 219
ecd40ee4 220 /* environment overwrites config */
586fc304 221 env = getenv("KMOD_LOG");
ecd40ee4 222 if (env != NULL)
52a7704f 223 kmod_set_log_priority(ctx, log_priority(env));
ecd40ee4 224
cb8d4d3e
GSB
225 if (config_paths == NULL)
226 config_paths = default_config_paths;
227 err = kmod_config_new(ctx, &ctx->config, config_paths);
d13e606f 228 if (err < 0) {
fd186ae9
LDM
229 ERR(ctx, "could not create config\n");
230 goto fail;
231 }
232
822913d7 233 ctx->modules_by_name = hash_new(KMOD_HASH_SIZE, NULL);
fd186ae9
LDM
234 if (ctx->modules_by_name == NULL) {
235 ERR(ctx, "could not create by-name hash\n");
236 goto fail;
d13e606f 237 }
7c2ab358 238
ae6df84a
LDM
239 INFO(ctx, "ctx %p created\n", ctx);
240 DBG(ctx, "log_priority=%d\n", ctx->log_priority);
52a7704f
LDM
241
242 return ctx;
fd186ae9
LDM
243
244fail:
245 free(ctx->modules_by_name);
246 free(ctx->dirname);
247 free(ctx);
248 return NULL;
ecd40ee4
LDM
249}
250
251/**
586fc304
LDM
252 * kmod_ref:
253 * @ctx: kmod library context
ecd40ee4 254 *
586fc304 255 * Take a reference of the kmod library context.
ecd40ee4 256 *
586fc304 257 * Returns: the passed kmod library context
54ba8b34 258 */
586fc304 259KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
ecd40ee4
LDM
260{
261 if (ctx == NULL)
262 return NULL;
263 ctx->refcount++;
264 return ctx;
265}
266
267/**
586fc304
LDM
268 * kmod_unref:
269 * @ctx: kmod library context
ecd40ee4 270 *
586fc304 271 * Drop a reference of the kmod library context. If the refcount
ecd40ee4 272 * reaches zero, the resources of the context will be released.
54ba8b34 273 */
586fc304 274KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
ecd40ee4
LDM
275{
276 if (ctx == NULL)
277 return NULL;
4d1e689a
LDM
278
279 if (--ctx->refcount > 0)
ecd40ee4 280 return ctx;
33bb69b9 281
ae6df84a 282 INFO(ctx, "context %p released\n", ctx);
33bb69b9
LDM
283
284 kmod_unload_resources(ctx);
822913d7 285 hash_free(ctx->modules_by_name);
1ce08a56 286 free(ctx->dirname);
d13e606f
GSB
287 if (ctx->config)
288 kmod_config_free(ctx->config);
33bb69b9 289
ecd40ee4
LDM
290 free(ctx);
291 return NULL;
292}
293
294/**
586fc304
LDM
295 * kmod_set_log_fn:
296 * @ctx: kmod library context
ecd40ee4 297 * @log_fn: function to be called for logging messages
b5b4d8e8 298 * @data: data to pass to log function
ecd40ee4
LDM
299 *
300 * The built-in logging writes to stderr. It can be
301 * overridden by a custom function, to plug log messages
302 * into the user's logging functionality.
54ba8b34 303 */
586fc304 304KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
1bdd951e 305 void (*log_fn)(void *data,
e4351b05
LDM
306 int priority, const char *file,
307 int line, const char *fn,
1bdd951e
GSB
308 const char *format, va_list args),
309 const void *data)
ecd40ee4 310{
e5c60f1c
GSB
311 if (ctx == NULL)
312 return;
ecd40ee4 313 ctx->log_fn = log_fn;
1bdd951e 314 ctx->log_data = (void *)data;
ae6df84a 315 INFO(ctx, "custom logging function %p registered\n", log_fn);
ecd40ee4
LDM
316}
317
318/**
586fc304
LDM
319 * kmod_get_log_priority:
320 * @ctx: kmod library context
ecd40ee4
LDM
321 *
322 * Returns: the current logging priority
54ba8b34 323 */
6d177553 324KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
ecd40ee4 325{
e5c60f1c
GSB
326 if (ctx == NULL)
327 return -1;
ecd40ee4
LDM
328 return ctx->log_priority;
329}
330
331/**
586fc304
LDM
332 * kmod_set_log_priority:
333 * @ctx: kmod library context
ecd40ee4
LDM
334 * @priority: the new logging priority
335 *
336 * Set the current logging priority. The value controls which messages
337 * are logged.
54ba8b34 338 */
586fc304 339KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
ecd40ee4 340{
e5c60f1c
GSB
341 if (ctx == NULL)
342 return;
ecd40ee4
LDM
343 ctx->log_priority = priority;
344}
7f3eb0cc 345
fd186ae9 346struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
8bdeca11 347 const char *key)
fd186ae9
LDM
348{
349 struct kmod_module *mod;
350
822913d7 351 mod = hash_find(ctx->modules_by_name, key);
fd186ae9 352
8bdeca11 353 DBG(ctx, "get module name='%s' found=%p\n", key, mod);
fd186ae9
LDM
354
355 return mod;
356}
357
8bdeca11
LDM
358void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
359 const char *key)
fd186ae9 360{
8bdeca11 361 DBG(ctx, "add %p key='%s'\n", mod, key);
fd186ae9 362
822913d7 363 hash_add(ctx->modules_by_name, key, mod);
fd186ae9
LDM
364}
365
8bdeca11
LDM
366void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
367 const char *key)
fd186ae9 368{
8bdeca11 369 DBG(ctx, "del %p key='%s'\n", mod, key);
fd186ae9 370
822913d7 371 hash_del(ctx->modules_by_name, key);
fd186ae9 372}
9ba6f57b 373
a009482c 374static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
810803db 375 enum kmod_index index_number,
7b30f4f4 376 const char *name,
9ba6f57b
LDM
377 struct kmod_list **list)
378{
6f1bc6e3 379 int err, nmatch = 0;
0fbdfef3 380 struct index_file *idx;
9ba6f57b
LDM
381 struct index_value *realnames, *realname;
382
65a84f55 383 if (ctx->indexes[index_number] != NULL) {
3e245be1 384 DBG(ctx, "use mmaped index '%s' for name=%s\n",
63be91cb 385 index_files[index_number].fn, name);
65a84f55
LDM
386 realnames = index_mm_searchwild(ctx->indexes[index_number],
387 name);
9fd58f30 388 } else {
65a84f55
LDM
389 char fn[PATH_MAX];
390
3b209959 391 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
63be91cb 392 index_files[index_number].fn);
9ba6f57b 393
65a84f55 394 DBG(ctx, "file=%s name=%s\n", fn, name);
9ba6f57b 395
65a84f55
LDM
396 idx = index_file_open(fn);
397 if (idx == NULL)
398 return -ENOSYS;
9ba6f57b 399
65a84f55
LDM
400 realnames = index_searchwild(idx, name);
401 index_file_close(idx);
402 }
4272d087 403
a955f71f 404 for (realname = realnames; realname; realname = realname->next) {
9ba6f57b
LDM
405 struct kmod_module *mod;
406
ee3b3ff2 407 err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
9ba6f57b
LDM
408 if (err < 0) {
409 ERR(ctx, "%s\n", strerror(-err));
23fc91c6 410 goto fail;
9ba6f57b
LDM
411 }
412
413 *list = kmod_list_append(*list, mod);
23fc91c6 414 nmatch++;
9ba6f57b
LDM
415 }
416
9ba6f57b 417 index_values_free(realnames);
23fc91c6
LDM
418 return nmatch;
419
420fail:
421 *list = kmod_list_remove_n_latest(*list, nmatch);
9ba6f57b 422 return err;
7b30f4f4
LDM
423
424}
425
7b30f4f4
LDM
426int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name,
427 struct kmod_list **list)
428{
0c010fae 429 if (!strstartswith(name, "symbol:"))
7b30f4f4
LDM
430 return 0;
431
b08314f7
LDM
432 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_SYMBOL,
433 name, list);
9ba6f57b
LDM
434}
435
49e61ca3
LDM
436int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
437 struct kmod_list **list)
438{
b08314f7
LDM
439 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_MODULES_ALIAS,
440 name, list);
49e61ca3
LDM
441}
442
671d4894 443char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
1eb2ef69
LDM
444{
445 struct index_file *idx;
446 char fn[PATH_MAX];
447 char *line;
448
b08314f7 449 if (ctx->indexes[KMOD_INDEX_MODULES_DEP]) {
85132101 450 DBG(ctx, "use mmaped index '%s' modname=%s\n",
b08314f7
LDM
451 index_files[KMOD_INDEX_MODULES_DEP].fn, name);
452 return index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_DEP],
453 name);
85132101
GSB
454 }
455
a4a75029 456 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
b08314f7 457 index_files[KMOD_INDEX_MODULES_DEP].fn);
1eb2ef69
LDM
458
459 DBG(ctx, "file=%s modname=%s\n", fn, name);
460
461 idx = index_file_open(fn);
462 if (idx == NULL) {
b787b569 463 ERR(ctx, "could not open moddep file '%s'\n", fn);
1eb2ef69
LDM
464 return NULL;
465 }
466
467 line = index_search(idx, name);
468 index_file_close(idx);
469
470 return line;
471}
472
64700e47
LDM
473int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
474 struct kmod_list **list)
475{
1eb2ef69 476 char *line;
64700e47
LDM
477 int n = 0;
478
479 /*
480 * Module names do not contain ':'. Return early if we know it will
481 * not be found.
482 */
483 if (strchr(name, ':'))
484 return 0;
485
671d4894 486 line = kmod_search_moddep(ctx, name);
64700e47
LDM
487 if (line != NULL) {
488 struct kmod_module *mod;
489
490 n = kmod_module_new_from_name(ctx, name, &mod);
491 if (n < 0) {
492 ERR(ctx, "%s\n", strerror(-n));
493 goto finish;
494 }
495
496 *list = kmod_list_append(*list, mod);
671d4894 497 kmod_module_parse_depline(mod, line);
64700e47
LDM
498 }
499
500finish:
501 free(line);
64700e47
LDM
502
503 return n;
504}
505
7f3eb0cc
LDM
506int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
507 struct kmod_list **list)
508{
d13e606f 509 struct kmod_config *config = ctx->config;
7f3eb0cc 510 struct kmod_list *l;
23fc91c6 511 int err, nmatch = 0;
7f3eb0cc
LDM
512
513 kmod_list_foreach(l, config->aliases) {
514 const char *aliasname = kmod_alias_get_name(l);
515 const char *modname = kmod_alias_get_modname(l);
516
517 if (fnmatch(aliasname, name, 0) == 0) {
518 struct kmod_module *mod;
519
ee3b3ff2
LDM
520 err = kmod_module_new_from_alias(ctx, aliasname,
521 modname, &mod);
7f3eb0cc 522 if (err < 0) {
d01c67e3 523 ERR(ctx, "%s\n", strerror(-err));
23fc91c6 524 goto fail;
7f3eb0cc
LDM
525 }
526
527 *list = kmod_list_append(*list, mod);
23fc91c6 528 nmatch++;
7f3eb0cc
LDM
529 }
530 }
531
23fc91c6
LDM
532 return nmatch;
533
534fail:
535 *list = kmod_list_remove_n_latest(*list, nmatch);
536 return err;
7f3eb0cc 537}
1487a64f 538
f4fc5523
LDM
539int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
540 struct kmod_list **list)
541{
542 struct kmod_config *config = ctx->config;
543 struct kmod_list *l, *node;
544 int err, nmatch = 0;
545
546 kmod_list_foreach(l, config->install_commands) {
547 const char *modname = kmod_command_get_modname(l);
548
549 if (streq(modname, name)) {
550 const char *cmd = kmod_command_get_command(l);
551 struct kmod_module *mod;
552
553 err = kmod_module_new_from_name(ctx, modname, &mod);
554 if (err < 0) {
555 ERR(ctx, "%s\n", strerror(-err));
556 return err;
557 }
558
559 node = kmod_list_append(*list, mod);
560 if (node == NULL) {
561 ERR(ctx, "out of memory\n");
562 return -ENOMEM;
563 }
564
565 *list = node;
566 nmatch = 1;
567
568 kmod_module_set_install_commands(mod, cmd);
569
570 /*
571 * match only the first one, like modprobe from
572 * module-init-tools does
573 */
574 break;
575 }
576 }
577
578 if (nmatch)
579 return nmatch;
580
581 kmod_list_foreach(l, config->remove_commands) {
582 const char *modname = kmod_command_get_modname(l);
583
584 if (streq(modname, name)) {
585 const char *cmd = kmod_command_get_command(l);
586 struct kmod_module *mod;
587
588 err = kmod_module_new_from_name(ctx, modname, &mod);
589 if (err < 0) {
590 ERR(ctx, "%s\n", strerror(-err));
591 return err;
592 }
593
594 node = kmod_list_append(*list, mod);
595 if (node == NULL) {
596 ERR(ctx, "out of memory\n");
597 return -ENOMEM;
598 }
599
600 *list = node;
601 nmatch = 1;
602
603 kmod_module_set_remove_commands(mod, cmd);
604
605 /*
606 * match only the first one, like modprobe from
607 * module-init-tools does
608 */
609 break;
610 }
611 }
612
613 return nmatch;
614}
615
c4dc3ca8
LDM
616static bool is_cache_invalid(const char *path, unsigned long long stamp)
617{
618 struct stat st;
619
620 if (stat(path, &st) < 0)
621 return true;
622
623 if (stamp != ts_usec(&st.st_mtim))
624 return true;
625
626 return false;
627}
628
629/**
630 * kmod_validate_resources:
631 * @ctx: kmod library context
632 *
633 * Check if indexes and configuration files changed on disk and the current
634 * context is not valid anymore.
635 *
f4cc6ea5 636 * Returns: KMOD_RESOURCES_OK if resources are still valid,
c4dc3ca8
LDM
637 * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
638 * kmod_unload_resources() and kmod_load_resources() or
639 * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
640 */
641KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
642{
643 struct kmod_list *l;
644 size_t i;
645
646 if (ctx == NULL || ctx->config == NULL)
647 return KMOD_RESOURCES_MUST_RECREATE;
648
649 kmod_list_foreach(l, ctx->config->paths) {
650 struct kmod_config_path *cf = l->data;
651
652 if (is_cache_invalid(cf->path, cf->stamp))
653 return KMOD_RESOURCES_MUST_RECREATE;
654 }
655
b08314f7 656 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
c4dc3ca8
LDM
657 char path[PATH_MAX];
658
659 if (ctx->indexes[i] == NULL)
660 continue;
661
662 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
63be91cb 663 index_files[i].fn);
c4dc3ca8
LDM
664
665 if (is_cache_invalid(path, ctx->indexes_stamp[i]))
666 return KMOD_RESOURCES_MUST_RELOAD;
667 }
668
669 return KMOD_RESOURCES_OK;
670}
671
be5a6dea
LDM
672/**
673 * kmod_load_resources:
674 * @ctx: kmod library context
675 *
676 * Load indexes and keep them open in @ctx. This way it's faster to lookup
677 * information within the indexes. If this function is not called before a
678 * search, the necessary index is always opened and closed.
679 *
680 * If user will do more than one or two lookups, insertions, deletions, most
681 * likely it's good to call this function first. Particularly in a daemon like
682 * udev that on bootup issues hundreds of calls to lookup the index, calling
683 * this function will speedup the searches.
684 *
685 * Returns: 0 on success or < 0 otherwise.
686 */
33bb69b9
LDM
687KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
688{
689 size_t i;
690
691 if (ctx == NULL)
692 return -ENOENT;
693
b08314f7 694 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
6de8f6e9 695 char path[PATH_MAX];
3e676766 696
16ca3666 697 if (ctx->indexes[i] != NULL) {
63be91cb
LDM
698 INFO(ctx, "Index %s already loaded\n",
699 index_files[i].fn);
3e676766
LDM
700 continue;
701 }
79d57fcb 702
6de8f6e9 703 snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
63be91cb 704 index_files[i].fn);
9fd58f30
LDM
705 ctx->indexes[i] = index_mm_open(ctx, path, true,
706 &ctx->indexes_stamp[i]);
3e676766
LDM
707 if (ctx->indexes[i] == NULL)
708 goto fail;
33bb69b9
LDM
709 }
710
711 return 0;
712
713fail:
714 kmod_unload_resources(ctx);
715 return -ENOMEM;
716}
717
be5a6dea
LDM
718/**
719 * kmod_unload_resources:
720 * @ctx: kmod library context
721 *
722 * Unload all the indexes. This will free the resources to maintain the index
723 * open and all subsequent searches will need to open and close the index.
724 *
725 * User is free to call kmod_load_resources() and kmod_unload_resources() as
726 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
727 * knows that when starting up it will lookup a lot of modules, it could call
728 * kmod_load_resources() and after the first burst of searches is gone, it
729 * could free the resources by calling kmod_unload_resources().
730 *
731 * Returns: 0 on success or < 0 otherwise.
732 */
33bb69b9
LDM
733KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
734{
735 size_t i;
736
737 if (ctx == NULL)
738 return;
739
b08314f7 740 for (i = 0; i < _KMOD_INDEX_MODULES_SIZE; i++) {
33bb69b9
LDM
741 if (ctx->indexes[i] != NULL) {
742 index_mm_close(ctx->indexes[i]);
743 ctx->indexes[i] = NULL;
9fd58f30 744 ctx->indexes_stamp[i] = 0;
33bb69b9
LDM
745 }
746 }
747}
bd3f5535 748
0224482e
LDM
749/**
750 * kmod_dump_index:
751 * @ctx: kmod library context
752 * @type: index to dump
753 * @fd: file descriptor to dump index to
754 *
755 * Dump index to file descriptor
756 *
757 * Returns: 0 on success or < 0 otherwise.
758 */
758428a7
LDM
759KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
760 int fd)
761{
762 if (ctx == NULL)
763 return -ENOSYS;
764
765 if (type < 0 || type >= _KMOD_INDEX_MODULES_SIZE)
766 return -ENOENT;
767
768 if (ctx->indexes[type] != NULL) {
769 DBG(ctx, "use mmaped index '%s'\n", index_files[type].fn);
770 index_mm_dump(ctx->indexes[type], fd,
771 index_files[type].prefix);
772 } else {
773 char fn[PATH_MAX];
774 struct index_file *idx;
775
776 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
777 index_files[type].fn);
778
779 DBG(ctx, "file=%s\n", fn);
780
781 idx = index_file_open(fn);
782 if (idx == NULL)
783 return -ENOSYS;
784
785 index_dump(idx, fd, index_files[type].prefix);
786 index_file_close(idx);
787 }
788
789 return 0;
790}
791
c1c9c446
LDM
792const struct kmod_list *kmod_get_blacklists(const struct kmod_ctx *ctx)
793{
794 return ctx->config->blacklists;
795}
796
bd3f5535
GSB
797const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx)
798{
799 return ctx->config->options;
800}
801
802const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx)
803{
804 return ctx->config->install_commands;
805}
806
807const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx)
808{
809 return ctx->config->remove_commands;
810}
1c522600
GSB
811
812const struct kmod_list *kmod_get_softdeps(const struct kmod_ctx *ctx)
813{
814 return ctx->config->softdeps;
815}
8b5ee618
LDM
816
817const struct kmod_list *kmod_get_aliases(const struct kmod_ctx *ctx)
818{
819 return ctx->config->aliases;
820}