]> git.ipfire.org Git - thirdparty/kmod.git/blame - libkmod/libkmod.c
Log and break early if index is already open
[thirdparty/kmod.git] / libkmod / libkmod.c
CommitLineData
ecd40ee4 1/*
586fc304
LDM
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011 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>
ecd40ee4 32
586fc304
LDM
33#include "libkmod.h"
34#include "libkmod-private.h"
9ba6f57b 35#include "libkmod-index.h"
ecd40ee4 36
fd186ae9
LDM
37#define KMOD_HASH_SIZE (256)
38#define KMOD_LRU_MAX (128)
39
ecd40ee4 40/**
586fc304
LDM
41 * SECTION:libkmod
42 * @short_description: libkmod context
ecd40ee4
LDM
43 *
44 * The context contains the default values for the library user,
45 * and is passed to all library operations.
46 */
47
a4a75029
LDM
48enum kmod_index {
49 KMOD_INDEX_DEP = 0,
50 KMOD_INDEX_ALIAS,
51 KMOD_INDEX_SYMBOL,
52 _KMOD_INDEX_LAST,
53};
54
55static const char* index_files[] = {
56 [KMOD_INDEX_DEP] = "modules.dep",
57 [KMOD_INDEX_ALIAS] = "modules.alias",
58 [KMOD_INDEX_SYMBOL] = "modules.symbols",
59};
60
cb8d4d3e
GSB
61static const char *default_config_paths[] = {
62 "/run/modprobe.d",
63 "/etc/modprobe.d",
64 "/lib/modprobe.d",
65 NULL
66};
67
ecd40ee4 68/**
586fc304 69 * kmod_ctx:
ecd40ee4
LDM
70 *
71 * Opaque object representing the library context.
72 */
586fc304 73struct kmod_ctx {
ecd40ee4 74 int refcount;
8d3f3ef8 75 int log_priority;
1bdd951e 76 void (*log_fn)(void *data,
e4351b05
LDM
77 int priority, const char *file, int line,
78 const char *fn, const char *format, va_list args);
1bdd951e 79 void *log_data;
1ce08a56
GSB
80 const void *userdata;
81 char *dirname;
d13e606f 82 struct kmod_config *config;
fd186ae9 83 struct kmod_hash *modules_by_name;
33bb69b9 84 struct index_mm *indexes[_KMOD_INDEX_LAST];
ecd40ee4
LDM
85};
86
1bdd951e 87void kmod_log(const struct kmod_ctx *ctx,
e4351b05
LDM
88 int priority, const char *file, int line, const char *fn,
89 const char *format, ...)
ecd40ee4
LDM
90{
91 va_list args;
92
e5c60f1c
GSB
93 if (ctx->log_fn == NULL)
94 return;
95
ecd40ee4 96 va_start(args, format);
1bdd951e 97 ctx->log_fn(ctx->log_data, priority, file, line, fn, format, args);
ecd40ee4
LDM
98 va_end(args);
99}
100
1bdd951e 101static void log_filep(void *data,
e4351b05
LDM
102 int priority, const char *file, int line,
103 const char *fn, const char *format, va_list args)
ecd40ee4 104{
1bdd951e
GSB
105 FILE *fp = data;
106 fprintf(fp, "libkmod: %s: ", fn);
107 vfprintf(fp, format, args);
ecd40ee4
LDM
108}
109
1ce08a56 110const char *kmod_get_dirname(const struct kmod_ctx *ctx)
221631d5
LDM
111{
112 return ctx->dirname;
113}
114
ecd40ee4 115/**
586fc304
LDM
116 * kmod_get_userdata:
117 * @ctx: kmod library context
ecd40ee4
LDM
118 *
119 * Retrieve stored data pointer from library context. This might be useful
be5a6dea 120 * to access from callbacks.
ecd40ee4
LDM
121 *
122 * Returns: stored userdata
54ba8b34 123 */
6d177553 124KMOD_EXPORT void *kmod_get_userdata(const struct kmod_ctx *ctx)
ecd40ee4
LDM
125{
126 if (ctx == NULL)
127 return NULL;
1ce08a56 128 return (void *)ctx->userdata;
ecd40ee4
LDM
129}
130
131/**
586fc304
LDM
132 * kmod_set_userdata:
133 * @ctx: kmod library context
ecd40ee4
LDM
134 * @userdata: data pointer
135 *
136 * Store custom @userdata in the library context.
54ba8b34 137 */
1ce08a56 138KMOD_EXPORT void kmod_set_userdata(struct kmod_ctx *ctx, const void *userdata)
ecd40ee4
LDM
139{
140 if (ctx == NULL)
141 return;
142 ctx->userdata = userdata;
143}
144
145static int log_priority(const char *priority)
146{
147 char *endptr;
148 int prio;
149
150 prio = strtol(priority, &endptr, 10);
151 if (endptr[0] == '\0' || isspace(endptr[0]))
152 return prio;
153 if (strncmp(priority, "err", 3) == 0)
154 return LOG_ERR;
155 if (strncmp(priority, "info", 4) == 0)
156 return LOG_INFO;
157 if (strncmp(priority, "debug", 5) == 0)
158 return LOG_DEBUG;
159 return 0;
160}
161
904c63aa
LDM
162static const char *dirname_default_prefix = "/lib/modules";
163
1ce08a56 164static char *get_kernel_release(const char *dirname)
221631d5
LDM
165{
166 struct utsname u;
904c63aa
LDM
167 char *p;
168
169 if (dirname != NULL)
170 return strdup(dirname);
221631d5
LDM
171
172 if (uname(&u) < 0)
173 return NULL;
174
904c63aa
LDM
175 if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
176 return NULL;
177
178 return p;
221631d5
LDM
179}
180
ecd40ee4 181/**
586fc304 182 * kmod_new:
ecd40ee4 183 *
586fc304 184 * Create kmod library context. This reads the kmod configuration
ecd40ee4
LDM
185 * and fills in the default values.
186 *
187 * The initial refcount is 1, and needs to be decremented to
586fc304 188 * release the resources of the kmod library context.
ecd40ee4 189 *
cb8d4d3e 190 * @dirname: what to consider as linux module's directory, if NULL
be5a6dea 191 * defaults to /lib/modules/`uname -r`.
cb8d4d3e 192 * @config_paths: ordered array of paths (directories or files) where
be5a6dea
LDM
193 * to load from user-defined configuration parameters such as
194 * alias, blacklists, commands (install, remove). If
195 * NULL defaults to /run/modprobe.d, /etc/modprobe.d and
196 * /lib/modprobe.d. Give an empty vector if configuration should
197 * not be read. This array must be null terminated.
cb8d4d3e 198 *
586fc304 199 * Returns: a new kmod library context
54ba8b34 200 */
c35347f1
LDM
201KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
202 const char * const *config_paths)
ecd40ee4
LDM
203{
204 const char *env;
52a7704f 205 struct kmod_ctx *ctx;
d13e606f 206 int err;
ecd40ee4 207
52a7704f
LDM
208 ctx = calloc(1, sizeof(struct kmod_ctx));
209 if (!ctx)
210 return NULL;
ecd40ee4 211
52a7704f 212 ctx->refcount = 1;
1bdd951e
GSB
213 ctx->log_fn = log_filep;
214 ctx->log_data = stderr;
52a7704f 215 ctx->log_priority = LOG_ERR;
ecd40ee4 216
904c63aa 217 ctx->dirname = get_kernel_release(dirname);
221631d5 218
ecd40ee4 219 /* environment overwrites config */
586fc304 220 env = getenv("KMOD_LOG");
ecd40ee4 221 if (env != NULL)
52a7704f 222 kmod_set_log_priority(ctx, log_priority(env));
ecd40ee4 223
cb8d4d3e
GSB
224 if (config_paths == NULL)
225 config_paths = default_config_paths;
226 err = kmod_config_new(ctx, &ctx->config, config_paths);
d13e606f 227 if (err < 0) {
fd186ae9
LDM
228 ERR(ctx, "could not create config\n");
229 goto fail;
230 }
231
232 ctx->modules_by_name = kmod_hash_new(KMOD_HASH_SIZE, NULL);
233 if (ctx->modules_by_name == NULL) {
234 ERR(ctx, "could not create by-name hash\n");
235 goto fail;
d13e606f 236 }
7c2ab358 237
ae6df84a
LDM
238 INFO(ctx, "ctx %p created\n", ctx);
239 DBG(ctx, "log_priority=%d\n", ctx->log_priority);
52a7704f
LDM
240
241 return ctx;
fd186ae9
LDM
242
243fail:
244 free(ctx->modules_by_name);
245 free(ctx->dirname);
246 free(ctx);
247 return NULL;
ecd40ee4
LDM
248}
249
250/**
586fc304
LDM
251 * kmod_ref:
252 * @ctx: kmod library context
ecd40ee4 253 *
586fc304 254 * Take a reference of the kmod library context.
ecd40ee4 255 *
586fc304 256 * Returns: the passed kmod library context
54ba8b34 257 */
586fc304 258KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
ecd40ee4
LDM
259{
260 if (ctx == NULL)
261 return NULL;
262 ctx->refcount++;
263 return ctx;
264}
265
266/**
586fc304
LDM
267 * kmod_unref:
268 * @ctx: kmod library context
ecd40ee4 269 *
586fc304 270 * Drop a reference of the kmod library context. If the refcount
ecd40ee4 271 * reaches zero, the resources of the context will be released.
54ba8b34 272 */
586fc304 273KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
ecd40ee4
LDM
274{
275 if (ctx == NULL)
276 return NULL;
4d1e689a
LDM
277
278 if (--ctx->refcount > 0)
ecd40ee4 279 return ctx;
33bb69b9 280
ae6df84a 281 INFO(ctx, "context %p released\n", ctx);
33bb69b9
LDM
282
283 kmod_unload_resources(ctx);
fd186ae9 284 kmod_hash_free(ctx->modules_by_name);
1ce08a56 285 free(ctx->dirname);
d13e606f
GSB
286 if (ctx->config)
287 kmod_config_free(ctx->config);
33bb69b9 288
ecd40ee4
LDM
289 free(ctx);
290 return NULL;
291}
292
293/**
586fc304
LDM
294 * kmod_set_log_fn:
295 * @ctx: kmod library context
ecd40ee4
LDM
296 * @log_fn: function to be called for logging messages
297 *
298 * The built-in logging writes to stderr. It can be
299 * overridden by a custom function, to plug log messages
300 * into the user's logging functionality.
54ba8b34 301 */
586fc304 302KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
1bdd951e 303 void (*log_fn)(void *data,
e4351b05
LDM
304 int priority, const char *file,
305 int line, const char *fn,
1bdd951e
GSB
306 const char *format, va_list args),
307 const void *data)
ecd40ee4 308{
e5c60f1c
GSB
309 if (ctx == NULL)
310 return;
ecd40ee4 311 ctx->log_fn = log_fn;
1bdd951e 312 ctx->log_data = (void *)data;
ae6df84a 313 INFO(ctx, "custom logging function %p registered\n", log_fn);
ecd40ee4
LDM
314}
315
316/**
586fc304
LDM
317 * kmod_get_log_priority:
318 * @ctx: kmod library context
ecd40ee4
LDM
319 *
320 * Returns: the current logging priority
54ba8b34 321 */
6d177553 322KMOD_EXPORT int kmod_get_log_priority(const struct kmod_ctx *ctx)
ecd40ee4 323{
e5c60f1c
GSB
324 if (ctx == NULL)
325 return -1;
ecd40ee4
LDM
326 return ctx->log_priority;
327}
328
329/**
586fc304
LDM
330 * kmod_set_log_priority:
331 * @ctx: kmod library context
ecd40ee4
LDM
332 * @priority: the new logging priority
333 *
334 * Set the current logging priority. The value controls which messages
335 * are logged.
54ba8b34 336 */
586fc304 337KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
ecd40ee4 338{
e5c60f1c
GSB
339 if (ctx == NULL)
340 return;
ecd40ee4
LDM
341 ctx->log_priority = priority;
342}
7f3eb0cc 343
fd186ae9
LDM
344struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
345 const char *name)
346{
347 struct kmod_module *mod;
348
349 mod = kmod_hash_find(ctx->modules_by_name, name);
350
351 DBG(ctx, "get module name='%s' found=%p\n", name, mod);
352
353 return mod;
354}
355
356void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod)
357{
358 const char *name = kmod_module_get_name(mod);
359
360 DBG(ctx, "add %p name='%s'\n", mod, name);
361
362 kmod_hash_add(ctx->modules_by_name, name, mod);
363}
364
365void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod)
366{
367 const char *name = kmod_module_get_name(mod);
368
369 DBG(ctx, "del %p name='%s'\n", mod, name);
370
371 kmod_hash_del(ctx->modules_by_name, name);
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
GSB
384 DBG(ctx, "use mmaped index '%s' for name=%s\n",
385 index_files[index_number], name);
65a84f55
LDM
386 realnames = index_mm_searchwild(ctx->indexes[index_number],
387 name);
388 } else{
389 char fn[PATH_MAX];
390
3b209959 391 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
810803db 392 index_files[index_number]);
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
9ba6f57b
LDM
404 for (realname = realnames; realname; realname = realnames->next) {
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{
429 if (!startswith(name, "symbol:"))
430 return 0;
431
810803db
LDM
432 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_SYMBOL, name,
433 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{
810803db
LDM
439 return kmod_lookup_alias_from_alias_bin(ctx, KMOD_INDEX_ALIAS, name,
440 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
85132101
GSB
449 if (ctx->indexes[KMOD_INDEX_DEP]) {
450 DBG(ctx, "use mmaped index '%s' modname=%s\n",
451 index_files[KMOD_INDEX_DEP], name);
452 return index_mm_search(ctx->indexes[KMOD_INDEX_DEP], name);
453 }
454
a4a75029
LDM
455 snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
456 index_files[KMOD_INDEX_DEP]);
1eb2ef69
LDM
457
458 DBG(ctx, "file=%s modname=%s\n", fn, name);
459
460 idx = index_file_open(fn);
461 if (idx == NULL) {
d01c67e3 462 ERR(ctx, "Could not open moddep file '%s'\n", fn);
1eb2ef69
LDM
463 return NULL;
464 }
465
466 line = index_search(idx, name);
467 index_file_close(idx);
468
469 return line;
470}
471
64700e47
LDM
472int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name,
473 struct kmod_list **list)
474{
1eb2ef69 475 char *line;
64700e47
LDM
476 int n = 0;
477
478 /*
479 * Module names do not contain ':'. Return early if we know it will
480 * not be found.
481 */
482 if (strchr(name, ':'))
483 return 0;
484
671d4894 485 line = kmod_search_moddep(ctx, name);
64700e47
LDM
486 if (line != NULL) {
487 struct kmod_module *mod;
488
489 n = kmod_module_new_from_name(ctx, name, &mod);
490 if (n < 0) {
491 ERR(ctx, "%s\n", strerror(-n));
492 goto finish;
493 }
494
495 *list = kmod_list_append(*list, mod);
671d4894 496 kmod_module_parse_depline(mod, line);
64700e47
LDM
497 }
498
499finish:
500 free(line);
64700e47
LDM
501
502 return n;
503}
504
7f3eb0cc
LDM
505int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
506 struct kmod_list **list)
507{
d13e606f 508 struct kmod_config *config = ctx->config;
7f3eb0cc 509 struct kmod_list *l;
23fc91c6 510 int err, nmatch = 0;
7f3eb0cc
LDM
511
512 kmod_list_foreach(l, config->aliases) {
513 const char *aliasname = kmod_alias_get_name(l);
514 const char *modname = kmod_alias_get_modname(l);
515
516 if (fnmatch(aliasname, name, 0) == 0) {
517 struct kmod_module *mod;
518
ee3b3ff2
LDM
519 err = kmod_module_new_from_alias(ctx, aliasname,
520 modname, &mod);
7f3eb0cc 521 if (err < 0) {
d01c67e3 522 ERR(ctx, "%s\n", strerror(-err));
23fc91c6 523 goto fail;
7f3eb0cc
LDM
524 }
525
526 *list = kmod_list_append(*list, mod);
23fc91c6 527 nmatch++;
7f3eb0cc
LDM
528 }
529 }
530
23fc91c6
LDM
531 return nmatch;
532
533fail:
534 *list = kmod_list_remove_n_latest(*list, nmatch);
535 return err;
7f3eb0cc 536}
1487a64f 537
54ba8b34
LDM
538/**
539 * kmod_module_get_filtered_blacklist:
540 * @ctx: kmod library context
be5a6dea 541 * @input: list of kmod_module to be filtered with blacklist
54ba8b34
LDM
542 * @output: where to save the new list
543 *
544 * Given a list @input, this function filter it out with config's blacklist
545 * ans save it in @output.
546 *
547 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
548 * list.
549 */
c35347f1
LDM
550KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
551 const struct kmod_list *input,
552 struct kmod_list **output)
1487a64f
GSB
553{
554 const struct kmod_config *config;
555 const struct kmod_list *li;
556
557 if (ctx == NULL || output == NULL)
558 return -ENOENT;
559
560 *output = NULL;
561 if (input == NULL)
562 return 0;
563
564 config = ctx->config;
565 kmod_list_foreach(li, input) {
566 struct kmod_module *mod = li->data;
567 const struct kmod_list *lb;
568 struct kmod_list *node;
569 bool filtered = false;
570 kmod_list_foreach(lb, config->blacklists) {
571 const char *name = lb->data;
572 if (streq(name, kmod_module_get_name(mod))) {
573 filtered = true;
574 break;
575 }
576 }
577 if (filtered)
578 continue;
579
580 node = kmod_list_append(*output, mod);
581 if (node == NULL)
582 goto fail;
583 *output = node;
584 kmod_module_ref(mod);
585 }
586 return 0;
587
588fail:
589 kmod_module_unref_list(*output);
590 *output = NULL;
591 return -ENOMEM;
592}
33bb69b9 593
be5a6dea
LDM
594/**
595 * kmod_load_resources:
596 * @ctx: kmod library context
597 *
598 * Load indexes and keep them open in @ctx. This way it's faster to lookup
599 * information within the indexes. If this function is not called before a
600 * search, the necessary index is always opened and closed.
601 *
602 * If user will do more than one or two lookups, insertions, deletions, most
603 * likely it's good to call this function first. Particularly in a daemon like
604 * udev that on bootup issues hundreds of calls to lookup the index, calling
605 * this function will speedup the searches.
606 *
607 * Returns: 0 on success or < 0 otherwise.
608 */
33bb69b9
LDM
609KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
610{
79d57fcb 611 char path[PATH_MAX];
33bb69b9
LDM
612 size_t i;
613
614 if (ctx == NULL)
615 return -ENOENT;
616
617 for (i = 0; i < ARRAY_SIZE(index_files); i++) {
3e676766
LDM
618 const char *fn = index_files[i];
619 size_t fnlen = strlen(fn);
620 const char *prefix = "";
621 const char *suffix = "";
622
33bb69b9 623 if (ctx->indexes[i] == NULL) {
3e676766
LDM
624 INFO(ctx, "Index %s already loaded\n", fn);
625 continue;
626 }
79d57fcb 627
3e676766
LDM
628 if (fn[0] != '/')
629 prefix = ctx->dirname;
79d57fcb 630
3e676766
LDM
631 if (fnlen < 4 || !streq(fn + fnlen - 4, ".bin"))
632 suffix = ".bin";
79d57fcb 633
3e676766
LDM
634 snprintf(path, sizeof(path), "%s/%s%s",
635 prefix, fn, suffix);
636 fn = path;
33bb69b9 637
3e676766
LDM
638 ctx->indexes[i] = index_mm_open(ctx, fn, true);
639 if (ctx->indexes[i] == NULL)
640 goto fail;
33bb69b9
LDM
641 }
642
643 return 0;
644
645fail:
646 kmod_unload_resources(ctx);
647 return -ENOMEM;
648}
649
be5a6dea
LDM
650/**
651 * kmod_unload_resources:
652 * @ctx: kmod library context
653 *
654 * Unload all the indexes. This will free the resources to maintain the index
655 * open and all subsequent searches will need to open and close the index.
656 *
657 * User is free to call kmod_load_resources() and kmod_unload_resources() as
658 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
659 * knows that when starting up it will lookup a lot of modules, it could call
660 * kmod_load_resources() and after the first burst of searches is gone, it
661 * could free the resources by calling kmod_unload_resources().
662 *
663 * Returns: 0 on success or < 0 otherwise.
664 */
33bb69b9
LDM
665KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
666{
667 size_t i;
668
669 if (ctx == NULL)
670 return;
671
672 for (i = 0; i < ARRAY_SIZE(index_files); i++) {
673 if (ctx->indexes[i] != NULL) {
674 index_mm_close(ctx->indexes[i]);
675 ctx->indexes[i] = NULL;
676 }
677 }
678}
bd3f5535 679
bd3f5535
GSB
680const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx)
681{
682 return ctx->config->options;
683}
684
685const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx)
686{
687 return ctx->config->install_commands;
688}
689
690const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx)
691{
692 return ctx->config->remove_commands;
693}