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