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