2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
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.
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.
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
32 #include <sys/utsname.h>
35 #include <shared/hash.h>
36 #include <shared/util.h>
39 #include "libkmod-internal.h"
40 #include "libkmod-index.h"
42 #define KMOD_HASH_SIZE (256)
43 #define KMOD_LRU_MAX (128)
44 #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1
48 * @short_description: libkmod context
50 * The context contains the default values for the library user,
51 * and is passed to all library operations.
54 static struct _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
= ""},
64 static const char *default_config_paths
[] = {
65 SYSCONFDIR
"/modprobe.d",
74 * Opaque object representing the library context.
79 void (*log_fn
)(void *data
,
80 int priority
, const char *file
, int line
,
81 const char *fn
, const char *format
, va_list args
);
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
];
91 void kmod_log(const struct kmod_ctx
*ctx
,
92 int priority
, const char *file
, int line
, const char *fn
,
93 const char *format
, ...)
97 if (ctx
->log_fn
== NULL
)
100 va_start(args
, format
);
101 ctx
->log_fn(ctx
->log_data
, priority
, file
, line
, fn
, format
, args
);
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
)
116 priname
= "EMERGENCY";
122 priname
= "CRITICAL";
140 snprintf(buf
, sizeof(buf
), "L:%d", priority
);
143 fprintf(fp
, "libkmod: %s %s:%d %s: ", priname
, file
, line
, fn
);
145 fprintf(fp
, "libkmod: %s: ", fn
);
147 vfprintf(fp
, format
, args
);
150 const char *kmod_get_dirname(const struct kmod_ctx
*ctx
)
157 * @ctx: kmod library context
159 * Retrieve stored data pointer from library context. This might be useful
160 * to access from callbacks.
162 * Returns: stored userdata
164 KMOD_EXPORT
void *kmod_get_userdata(const struct kmod_ctx
*ctx
)
168 return (void *)ctx
->userdata
;
173 * @ctx: kmod library context
174 * @userdata: data pointer
176 * Store custom @userdata in the library context.
178 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, const void *userdata
)
182 ctx
->userdata
= userdata
;
185 static int log_priority(const char *priority
)
190 prio
= strtol(priority
, &endptr
, 10);
191 if (endptr
[0] == '\0' || isspace(endptr
[0]))
193 if (strncmp(priority
, "err", 3) == 0)
195 if (strncmp(priority
, "info", 4) == 0)
197 if (strncmp(priority
, "debug", 5) == 0)
202 static const char *dirname_default_prefix
= "/lib/modules";
204 static char *get_kernel_release(const char *dirname
)
210 return path_make_absolute_cwd(dirname
);
215 if (asprintf(&p
, "%s/%s", dirname_default_prefix
, u
.release
) < 0)
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.
234 * Create kmod library context. This reads the kmod configuration
235 * and fills in the default values.
237 * The initial refcount is 1, and needs to be decremented to
238 * release the resources of the kmod library context.
240 * Returns: a new kmod library context
242 KMOD_EXPORT
struct kmod_ctx
*kmod_new(const char *dirname
,
243 const char * const *config_paths
)
246 struct kmod_ctx
*ctx
;
249 ctx
= calloc(1, sizeof(struct kmod_ctx
));
254 ctx
->log_fn
= log_filep
;
255 ctx
->log_data
= stderr
;
256 ctx
->log_priority
= LOG_ERR
;
258 ctx
->dirname
= get_kernel_release(dirname
);
260 /* environment overwrites config */
261 env
= secure_getenv("KMOD_LOG");
263 kmod_set_log_priority(ctx
, log_priority(env
));
265 if (config_paths
== NULL
)
266 config_paths
= default_config_paths
;
267 err
= kmod_config_new(ctx
, &ctx
->config
, config_paths
);
269 ERR(ctx
, "could not create config\n");
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");
279 INFO(ctx
, "ctx %p created\n", ctx
);
280 DBG(ctx
, "log_priority=%d\n", ctx
->log_priority
);
285 free(ctx
->modules_by_name
);
293 * @ctx: kmod library context
295 * Take a reference of the kmod library context.
297 * Returns: the passed kmod library context
299 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
309 * @ctx: kmod library context
311 * Drop a reference of the kmod library context. If the refcount
312 * reaches zero, the resources of the context will be released.
314 * Returns: the passed kmod library context or NULL if it's freed
316 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
321 if (--ctx
->refcount
> 0)
324 INFO(ctx
, "context %p released\n", ctx
);
326 kmod_unload_resources(ctx
);
327 hash_free(ctx
->modules_by_name
);
330 kmod_config_free(ctx
->config
);
338 * @ctx: kmod library context
339 * @log_fn: function to be called for logging messages
340 * @data: data to pass to log function
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.
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
),
355 ctx
->log_fn
= log_fn
;
356 ctx
->log_data
= (void *)data
;
357 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
361 * kmod_get_log_priority:
362 * @ctx: kmod library context
364 * Returns: the current logging priority
366 KMOD_EXPORT
int kmod_get_log_priority(const struct kmod_ctx
*ctx
)
370 return ctx
->log_priority
;
374 * kmod_set_log_priority:
375 * @ctx: kmod library context
376 * @priority: the new logging priority
378 * Set the current logging priority. The value controls which messages
381 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
385 ctx
->log_priority
= priority
;
388 struct kmod_module
*kmod_pool_get_module(struct kmod_ctx
*ctx
,
391 struct kmod_module
*mod
;
393 mod
= hash_find(ctx
->modules_by_name
, key
);
395 DBG(ctx
, "get module name='%s' found=%p\n", key
, mod
);
400 void kmod_pool_add_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
403 DBG(ctx
, "add %p key='%s'\n", mod
, key
);
405 hash_add(ctx
->modules_by_name
, key
, mod
);
408 void kmod_pool_del_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
411 DBG(ctx
, "del %p key='%s'\n", mod
, key
);
413 hash_del(ctx
->modules_by_name
, key
);
416 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx
*ctx
,
417 enum kmod_index index_number
,
419 struct kmod_list
**list
)
422 struct index_file
*idx
;
423 struct index_value
*realnames
, *realname
;
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
],
433 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
434 index_files
[index_number
].fn
);
436 DBG(ctx
, "file=%s name=%s\n", fn
, name
);
438 idx
= index_file_open(fn
);
442 realnames
= index_searchwild(idx
, name
);
443 index_file_close(idx
);
446 for (realname
= realnames
; realname
; realname
= realname
->next
) {
447 struct kmod_module
*mod
;
449 err
= kmod_module_new_from_alias(ctx
, name
, realname
->value
, &mod
);
451 ERR(ctx
, "Could not create module for alias=%s realname=%s: %s\n",
452 name
, realname
->value
, strerror(-err
));
456 *list
= kmod_list_append(*list
, mod
);
460 index_values_free(realnames
);
464 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
465 index_values_free(realnames
);
470 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx
*ctx
, const char *name
,
471 struct kmod_list
**list
)
473 if (!strstartswith(name
, "symbol:"))
476 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_SYMBOL
,
480 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx
*ctx
, const char *name
,
481 struct kmod_list
**list
)
483 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_ALIAS
,
487 int kmod_lookup_alias_from_builtin_file(struct kmod_ctx
*ctx
, const char *name
,
488 struct kmod_list
**list
)
493 assert(*list
== NULL
);
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
,
499 line
= index_mm_search(ctx
->indexes
[KMOD_INDEX_MODULES_BUILTIN
],
502 struct index_file
*idx
;
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
);
509 idx
= index_file_open(fn
);
511 DBG(ctx
, "could not open builtin file '%s'\n", fn
);
515 line
= index_search(idx
, name
);
516 index_file_close(idx
);
520 struct kmod_module
*mod
;
522 err
= kmod_module_new_from_name(ctx
, name
, &mod
);
524 ERR(ctx
, "Could not create module from name %s: %s\n",
525 name
, strerror(-err
));
529 kmod_module_set_builtin(mod
, true);
530 *list
= kmod_list_append(*list
, mod
);
540 char *kmod_search_moddep(struct kmod_ctx
*ctx
, const char *name
)
542 struct index_file
*idx
;
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
],
553 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
554 index_files
[KMOD_INDEX_MODULES_DEP
].fn
);
556 DBG(ctx
, "file=%s modname=%s\n", fn
, name
);
558 idx
= index_file_open(fn
);
560 DBG(ctx
, "could not open moddep file '%s'\n", fn
);
564 line
= index_search(idx
, name
);
565 index_file_close(idx
);
570 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx
*ctx
, const char *name
,
571 struct kmod_list
**list
)
577 * Module names do not contain ':'. Return early if we know it will
580 if (strchr(name
, ':'))
583 line
= kmod_search_moddep(ctx
, name
);
585 struct kmod_module
*mod
;
587 n
= kmod_module_new_from_name(ctx
, name
, &mod
);
589 ERR(ctx
, "Could not create module from name %s: %s\n",
594 *list
= kmod_list_append(*list
, mod
);
595 kmod_module_parse_depline(mod
, line
);
604 int kmod_lookup_alias_from_config(struct kmod_ctx
*ctx
, const char *name
,
605 struct kmod_list
**list
)
607 struct kmod_config
*config
= ctx
->config
;
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
);
615 if (fnmatch(aliasname
, name
, 0) == 0) {
616 struct kmod_module
*mod
;
618 err
= kmod_module_new_from_alias(ctx
, aliasname
,
621 ERR(ctx
, "Could not create module for alias=%s modname=%s: %s\n",
622 name
, modname
, strerror(-err
));
626 *list
= kmod_list_append(*list
, mod
);
634 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
638 int kmod_lookup_alias_from_commands(struct kmod_ctx
*ctx
, const char *name
,
639 struct kmod_list
**list
)
641 struct kmod_config
*config
= ctx
->config
;
642 struct kmod_list
*l
, *node
;
645 kmod_list_foreach(l
, config
->install_commands
) {
646 const char *modname
= kmod_command_get_modname(l
);
648 if (streq(modname
, name
)) {
649 const char *cmd
= kmod_command_get_command(l
);
650 struct kmod_module
*mod
;
652 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
654 ERR(ctx
, "Could not create module from name %s: %s\n",
655 modname
, strerror(-err
));
659 node
= kmod_list_append(*list
, mod
);
661 ERR(ctx
, "out of memory\n");
668 kmod_module_set_install_commands(mod
, cmd
);
671 * match only the first one, like modprobe from
672 * module-init-tools does
681 kmod_list_foreach(l
, config
->remove_commands
) {
682 const char *modname
= kmod_command_get_modname(l
);
684 if (streq(modname
, name
)) {
685 const char *cmd
= kmod_command_get_command(l
);
686 struct kmod_module
*mod
;
688 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
690 ERR(ctx
, "Could not create module from name %s: %s\n",
691 modname
, strerror(-err
));
695 node
= kmod_list_append(*list
, mod
);
697 ERR(ctx
, "out of memory\n");
704 kmod_module_set_remove_commands(mod
, cmd
);
707 * match only the first one, like modprobe from
708 * module-init-tools does
717 void kmod_set_modules_visited(struct kmod_ctx
*ctx
, bool visited
)
719 struct hash_iter iter
;
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
);
727 void kmod_set_modules_required(struct kmod_ctx
*ctx
, bool required
)
729 struct hash_iter iter
;
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
);
737 static bool is_cache_invalid(const char *path
, unsigned long long stamp
)
741 if (stat(path
, &st
) < 0)
744 if (stamp
!= stat_mstamp(&st
))
751 * kmod_validate_resources:
752 * @ctx: kmod library context
754 * Check if indexes and configuration files changed on disk and the current
755 * context is not valid anymore.
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.
762 KMOD_EXPORT
int kmod_validate_resources(struct kmod_ctx
*ctx
)
767 if (ctx
== NULL
|| ctx
->config
== NULL
)
768 return KMOD_RESOURCES_MUST_RECREATE
;
770 kmod_list_foreach(l
, ctx
->config
->paths
) {
771 struct kmod_config_path
*cf
= l
->data
;
773 if (is_cache_invalid(cf
->path
, cf
->stamp
))
774 return KMOD_RESOURCES_MUST_RECREATE
;
777 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
780 if (ctx
->indexes
[i
] == NULL
)
783 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
786 if (is_cache_invalid(path
, ctx
->indexes_stamp
[i
]))
787 return KMOD_RESOURCES_MUST_RELOAD
;
790 return KMOD_RESOURCES_OK
;
794 * kmod_load_resources:
795 * @ctx: kmod library context
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.
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.
806 * Returns: 0 on success or < 0 otherwise.
808 KMOD_EXPORT
int kmod_load_resources(struct kmod_ctx
*ctx
)
815 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
818 if (ctx
->indexes
[i
] != NULL
) {
819 INFO(ctx
, "Index %s already loaded\n",
824 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
826 ctx
->indexes
[i
] = index_mm_open(ctx
, path
,
827 &ctx
->indexes_stamp
[i
]);
828 if (ctx
->indexes
[i
] == NULL
)
835 kmod_unload_resources(ctx
);
840 * kmod_unload_resources:
841 * @ctx: kmod library context
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.
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().
852 * Returns: 0 on success or < 0 otherwise.
854 KMOD_EXPORT
void kmod_unload_resources(struct kmod_ctx
*ctx
)
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;
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
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
884 * Returns: 0 on success or < 0 otherwise.
886 KMOD_EXPORT
int kmod_dump_index(struct kmod_ctx
*ctx
, enum kmod_index type
,
892 if (type
< 0 || type
>= _KMOD_INDEX_MODULES_SIZE
)
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
);
901 struct index_file
*idx
;
903 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
904 index_files
[type
].fn
);
906 DBG(ctx
, "file=%s\n", fn
);
908 idx
= index_file_open(fn
);
912 index_dump(idx
, fd
, index_files
[type
].prefix
);
913 index_file_close(idx
);
919 const struct kmod_config
*kmod_get_config(const struct kmod_ctx
*ctx
)