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