2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2012 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>
36 #include "libkmod-private.h"
37 #include "libkmod-index.h"
39 #define KMOD_HASH_SIZE (256)
40 #define KMOD_LRU_MAX (128)
41 #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1
45 * @short_description: libkmod context
47 * The context contains the default values for the library user,
48 * and is passed to all library operations.
51 static struct _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
= ""},
61 static const char *default_config_paths
[] = {
62 SYSCONFDIR
"/modprobe.d",
64 ROOTPREFIX
"/lib/modprobe.d",
71 * Opaque object representing the library context.
76 void (*log_fn
)(void *data
,
77 int priority
, const char *file
, int line
,
78 const char *fn
, const char *format
, va_list args
);
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
];
88 void kmod_log(const struct kmod_ctx
*ctx
,
89 int priority
, const char *file
, int line
, const char *fn
,
90 const char *format
, ...)
94 if (ctx
->log_fn
== NULL
)
97 va_start(args
, format
);
98 ctx
->log_fn(ctx
->log_data
, priority
, file
, line
, fn
, format
, args
);
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
)
112 priname
= "EMERGENCY";
118 priname
= "CRITICAL";
136 snprintf(buf
, sizeof(buf
), "L:%d", priority
);
139 fprintf(fp
, "libkmod: %s %s:%d %s: ", priname
, file
, line
, fn
);
141 fprintf(fp
, "libkmod: %s: ", fn
);
143 vfprintf(fp
, format
, args
);
146 const char *kmod_get_dirname(const struct kmod_ctx
*ctx
)
153 * @ctx: kmod library context
155 * Retrieve stored data pointer from library context. This might be useful
156 * to access from callbacks.
158 * Returns: stored userdata
160 KMOD_EXPORT
void *kmod_get_userdata(const struct kmod_ctx
*ctx
)
164 return (void *)ctx
->userdata
;
169 * @ctx: kmod library context
170 * @userdata: data pointer
172 * Store custom @userdata in the library context.
174 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, const void *userdata
)
178 ctx
->userdata
= userdata
;
181 static int log_priority(const char *priority
)
186 prio
= strtol(priority
, &endptr
, 10);
187 if (endptr
[0] == '\0' || isspace(endptr
[0]))
189 if (strncmp(priority
, "err", 3) == 0)
191 if (strncmp(priority
, "info", 4) == 0)
193 if (strncmp(priority
, "debug", 5) == 0)
198 static const char *dirname_default_prefix
= ROOTPREFIX
"/lib/modules";
200 static char *get_kernel_release(const char *dirname
)
206 return path_make_absolute_cwd(dirname
);
211 if (asprintf(&p
, "%s/%s", dirname_default_prefix
, u
.release
) < 0)
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
231 * Create kmod library context. This reads the kmod configuration
232 * and fills in the default values.
234 * The initial refcount is 1, and needs to be decremented to
235 * release the resources of the kmod library context.
237 * Returns: a new kmod library context
239 KMOD_EXPORT
struct kmod_ctx
*kmod_new(const char *dirname
,
240 const char * const *config_paths
)
243 struct kmod_ctx
*ctx
;
246 ctx
= calloc(1, sizeof(struct kmod_ctx
));
251 ctx
->log_fn
= log_filep
;
252 ctx
->log_data
= stderr
;
253 ctx
->log_priority
= LOG_ERR
;
255 ctx
->dirname
= get_kernel_release(dirname
);
257 /* environment overwrites config */
258 env
= getenv("KMOD_LOG");
260 kmod_set_log_priority(ctx
, log_priority(env
));
262 if (config_paths
== NULL
)
263 config_paths
= default_config_paths
;
264 err
= kmod_config_new(ctx
, &ctx
->config
, config_paths
);
266 ERR(ctx
, "could not create config\n");
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");
276 INFO(ctx
, "ctx %p created\n", ctx
);
277 DBG(ctx
, "log_priority=%d\n", ctx
->log_priority
);
282 free(ctx
->modules_by_name
);
290 * @ctx: kmod library context
292 * Take a reference of the kmod library context.
294 * Returns: the passed kmod library context
296 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
306 * @ctx: kmod library context
308 * Drop a reference of the kmod library context. If the refcount
309 * reaches zero, the resources of the context will be released.
311 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
316 if (--ctx
->refcount
> 0)
319 INFO(ctx
, "context %p released\n", ctx
);
321 kmod_unload_resources(ctx
);
322 hash_free(ctx
->modules_by_name
);
325 kmod_config_free(ctx
->config
);
333 * @ctx: kmod library context
334 * @log_fn: function to be called for logging messages
335 * @data: data to pass to log function
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.
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
),
350 ctx
->log_fn
= log_fn
;
351 ctx
->log_data
= (void *)data
;
352 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
356 * kmod_get_log_priority:
357 * @ctx: kmod library context
359 * Returns: the current logging priority
361 KMOD_EXPORT
int kmod_get_log_priority(const struct kmod_ctx
*ctx
)
365 return ctx
->log_priority
;
369 * kmod_set_log_priority:
370 * @ctx: kmod library context
371 * @priority: the new logging priority
373 * Set the current logging priority. The value controls which messages
376 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
380 ctx
->log_priority
= priority
;
383 struct kmod_module
*kmod_pool_get_module(struct kmod_ctx
*ctx
,
386 struct kmod_module
*mod
;
388 mod
= hash_find(ctx
->modules_by_name
, key
);
390 DBG(ctx
, "get module name='%s' found=%p\n", key
, mod
);
395 void kmod_pool_add_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
398 DBG(ctx
, "add %p key='%s'\n", mod
, key
);
400 hash_add(ctx
->modules_by_name
, key
, mod
);
403 void kmod_pool_del_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
406 DBG(ctx
, "del %p key='%s'\n", mod
, key
);
408 hash_del(ctx
->modules_by_name
, key
);
411 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx
*ctx
,
412 enum kmod_index index_number
,
414 struct kmod_list
**list
)
417 struct index_file
*idx
;
418 struct index_value
*realnames
, *realname
;
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
],
428 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
429 index_files
[index_number
].fn
);
431 DBG(ctx
, "file=%s name=%s\n", fn
, name
);
433 idx
= index_file_open(fn
);
437 realnames
= index_searchwild(idx
, name
);
438 index_file_close(idx
);
441 for (realname
= realnames
; realname
; realname
= realname
->next
) {
442 struct kmod_module
*mod
;
444 err
= kmod_module_new_from_alias(ctx
, name
, realname
->value
, &mod
);
446 ERR(ctx
, "Could not create module for alias=%s realname=%s: %s\n",
447 name
, realname
->value
, strerror(-err
));
451 *list
= kmod_list_append(*list
, mod
);
455 index_values_free(realnames
);
459 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
464 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx
*ctx
, const char *name
,
465 struct kmod_list
**list
)
467 if (!strstartswith(name
, "symbol:"))
470 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_SYMBOL
,
474 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx
*ctx
, const char *name
,
475 struct kmod_list
**list
)
477 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_ALIAS
,
481 int kmod_lookup_alias_from_builtin_file(struct kmod_ctx
*ctx
, const char *name
,
482 struct kmod_list
**list
)
487 assert(*list
== NULL
);
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
,
493 line
= index_mm_search(ctx
->indexes
[KMOD_INDEX_MODULES_BUILTIN
],
496 struct index_file
*idx
;
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
);
503 idx
= index_file_open(fn
);
505 DBG(ctx
, "could not open builtin file '%s'\n", fn
);
509 line
= index_search(idx
, name
);
510 index_file_close(idx
);
514 struct kmod_module
*mod
;
516 err
= kmod_module_new_from_name(ctx
, name
, &mod
);
518 ERR(ctx
, "Could not create module from name %s: %s\n",
519 name
, strerror(-err
));
523 kmod_module_set_builtin(mod
, true);
524 *list
= kmod_list_append(*list
, mod
);
534 char *kmod_search_moddep(struct kmod_ctx
*ctx
, const char *name
)
536 struct index_file
*idx
;
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
],
547 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
548 index_files
[KMOD_INDEX_MODULES_DEP
].fn
);
550 DBG(ctx
, "file=%s modname=%s\n", fn
, name
);
552 idx
= index_file_open(fn
);
554 DBG(ctx
, "could not open moddep file '%s'\n", fn
);
558 line
= index_search(idx
, name
);
559 index_file_close(idx
);
564 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx
*ctx
, const char *name
,
565 struct kmod_list
**list
)
571 * Module names do not contain ':'. Return early if we know it will
574 if (strchr(name
, ':'))
577 line
= kmod_search_moddep(ctx
, name
);
579 struct kmod_module
*mod
;
581 n
= kmod_module_new_from_name(ctx
, name
, &mod
);
583 ERR(ctx
, "Could not create module from name %s: %s\n",
588 *list
= kmod_list_append(*list
, mod
);
589 kmod_module_parse_depline(mod
, line
);
598 int kmod_lookup_alias_from_config(struct kmod_ctx
*ctx
, const char *name
,
599 struct kmod_list
**list
)
601 struct kmod_config
*config
= ctx
->config
;
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
);
609 if (fnmatch(aliasname
, name
, 0) == 0) {
610 struct kmod_module
*mod
;
612 err
= kmod_module_new_from_alias(ctx
, aliasname
,
615 ERR(ctx
, "Could not create module for alias=%s modname=%s: %s\n",
616 name
, modname
, strerror(-err
));
620 *list
= kmod_list_append(*list
, mod
);
628 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
632 int kmod_lookup_alias_from_commands(struct kmod_ctx
*ctx
, const char *name
,
633 struct kmod_list
**list
)
635 struct kmod_config
*config
= ctx
->config
;
636 struct kmod_list
*l
, *node
;
639 kmod_list_foreach(l
, config
->install_commands
) {
640 const char *modname
= kmod_command_get_modname(l
);
642 if (streq(modname
, name
)) {
643 const char *cmd
= kmod_command_get_command(l
);
644 struct kmod_module
*mod
;
646 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
648 ERR(ctx
, "Could not create module from name %s: %s\n",
649 modname
, strerror(-err
));
653 node
= kmod_list_append(*list
, mod
);
655 ERR(ctx
, "out of memory\n");
662 kmod_module_set_install_commands(mod
, cmd
);
665 * match only the first one, like modprobe from
666 * module-init-tools does
675 kmod_list_foreach(l
, config
->remove_commands
) {
676 const char *modname
= kmod_command_get_modname(l
);
678 if (streq(modname
, name
)) {
679 const char *cmd
= kmod_command_get_command(l
);
680 struct kmod_module
*mod
;
682 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
684 ERR(ctx
, "Could not create module from name %s: %s\n",
685 modname
, strerror(-err
));
689 node
= kmod_list_append(*list
, mod
);
691 ERR(ctx
, "out of memory\n");
698 kmod_module_set_remove_commands(mod
, cmd
);
701 * match only the first one, like modprobe from
702 * module-init-tools does
711 void kmod_set_modules_visited(struct kmod_ctx
*ctx
, bool visited
)
713 struct hash_iter iter
;
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
);
721 static bool is_cache_invalid(const char *path
, unsigned long long stamp
)
725 if (stat(path
, &st
) < 0)
728 if (stamp
!= stat_mstamp(&st
))
735 * kmod_validate_resources:
736 * @ctx: kmod library context
738 * Check if indexes and configuration files changed on disk and the current
739 * context is not valid anymore.
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.
746 KMOD_EXPORT
int kmod_validate_resources(struct kmod_ctx
*ctx
)
751 if (ctx
== NULL
|| ctx
->config
== NULL
)
752 return KMOD_RESOURCES_MUST_RECREATE
;
754 kmod_list_foreach(l
, ctx
->config
->paths
) {
755 struct kmod_config_path
*cf
= l
->data
;
757 if (is_cache_invalid(cf
->path
, cf
->stamp
))
758 return KMOD_RESOURCES_MUST_RECREATE
;
761 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
764 if (ctx
->indexes
[i
] == NULL
)
767 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
770 if (is_cache_invalid(path
, ctx
->indexes_stamp
[i
]))
771 return KMOD_RESOURCES_MUST_RELOAD
;
774 return KMOD_RESOURCES_OK
;
778 * kmod_load_resources:
779 * @ctx: kmod library context
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.
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.
790 * Returns: 0 on success or < 0 otherwise.
792 KMOD_EXPORT
int kmod_load_resources(struct kmod_ctx
*ctx
)
799 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
802 if (ctx
->indexes
[i
] != NULL
) {
803 INFO(ctx
, "Index %s already loaded\n",
808 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
810 ctx
->indexes
[i
] = index_mm_open(ctx
, path
,
811 &ctx
->indexes_stamp
[i
]);
812 if (ctx
->indexes
[i
] == NULL
)
819 kmod_unload_resources(ctx
);
824 * kmod_unload_resources:
825 * @ctx: kmod library context
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.
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().
836 * Returns: 0 on success or < 0 otherwise.
838 KMOD_EXPORT
void kmod_unload_resources(struct kmod_ctx
*ctx
)
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;
856 * @ctx: kmod library context
857 * @type: index to dump
858 * @fd: file descriptor to dump index to
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
864 * Returns: 0 on success or < 0 otherwise.
866 KMOD_EXPORT
int kmod_dump_index(struct kmod_ctx
*ctx
, enum kmod_index type
,
872 if (type
< 0 || type
>= _KMOD_INDEX_MODULES_SIZE
)
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
);
881 struct index_file
*idx
;
883 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
884 index_files
[type
].fn
);
886 DBG(ctx
, "file=%s\n", fn
);
888 idx
= index_file_open(fn
);
892 index_dump(idx
, fd
, index_files
[type
].prefix
);
893 index_file_close(idx
);
899 const struct kmod_config
*kmod_get_config(const struct kmod_ctx
*ctx
)