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/util.h>
38 #include "libkmod-internal.h"
39 #include "libkmod-index.h"
41 #define KMOD_HASH_SIZE (256)
42 #define KMOD_LRU_MAX (128)
43 #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1
47 * @short_description: libkmod context
49 * The context contains the default values for the library user,
50 * and is passed to all library operations.
53 static struct _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
= ""},
63 static const char *default_config_paths
[] = {
64 SYSCONFDIR
"/modprobe.d",
73 * Opaque object representing the library context.
78 void (*log_fn
)(void *data
,
79 int priority
, const char *file
, int line
,
80 const char *fn
, const char *format
, va_list args
);
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
];
90 void kmod_log(const struct kmod_ctx
*ctx
,
91 int priority
, const char *file
, int line
, const char *fn
,
92 const char *format
, ...)
96 if (ctx
->log_fn
== NULL
)
99 va_start(args
, format
);
100 ctx
->log_fn(ctx
->log_data
, priority
, file
, line
, fn
, format
, args
);
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
)
115 priname
= "EMERGENCY";
121 priname
= "CRITICAL";
139 snprintf(buf
, sizeof(buf
), "L:%d", priority
);
142 fprintf(fp
, "libkmod: %s %s:%d %s: ", priname
, file
, line
, fn
);
144 fprintf(fp
, "libkmod: %s: ", fn
);
146 vfprintf(fp
, format
, args
);
149 const char *kmod_get_dirname(const struct kmod_ctx
*ctx
)
156 * @ctx: kmod library context
158 * Retrieve stored data pointer from library context. This might be useful
159 * to access from callbacks.
161 * Returns: stored userdata
163 KMOD_EXPORT
void *kmod_get_userdata(const struct kmod_ctx
*ctx
)
167 return (void *)ctx
->userdata
;
172 * @ctx: kmod library context
173 * @userdata: data pointer
175 * Store custom @userdata in the library context.
177 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, const void *userdata
)
181 ctx
->userdata
= userdata
;
184 static int log_priority(const char *priority
)
189 prio
= strtol(priority
, &endptr
, 10);
190 if (endptr
[0] == '\0' || isspace(endptr
[0]))
192 if (strncmp(priority
, "err", 3) == 0)
194 if (strncmp(priority
, "info", 4) == 0)
196 if (strncmp(priority
, "debug", 5) == 0)
201 static const char *dirname_default_prefix
= "/lib/modules";
203 static char *get_kernel_release(const char *dirname
)
209 return path_make_absolute_cwd(dirname
);
214 if (asprintf(&p
, "%s/%s", dirname_default_prefix
, u
.release
) < 0)
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.
233 * Create kmod library context. This reads the kmod configuration
234 * and fills in the default values.
236 * The initial refcount is 1, and needs to be decremented to
237 * release the resources of the kmod library context.
239 * Returns: a new kmod library context
241 KMOD_EXPORT
struct kmod_ctx
*kmod_new(const char *dirname
,
242 const char * const *config_paths
)
245 struct kmod_ctx
*ctx
;
248 ctx
= calloc(1, sizeof(struct kmod_ctx
));
253 ctx
->log_fn
= log_filep
;
254 ctx
->log_data
= stderr
;
255 ctx
->log_priority
= LOG_ERR
;
257 ctx
->dirname
= get_kernel_release(dirname
);
259 /* environment overwrites config */
260 env
= secure_getenv("KMOD_LOG");
262 kmod_set_log_priority(ctx
, log_priority(env
));
264 if (config_paths
== NULL
)
265 config_paths
= default_config_paths
;
266 err
= kmod_config_new(ctx
, &ctx
->config
, config_paths
);
268 ERR(ctx
, "could not create config\n");
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");
278 INFO(ctx
, "ctx %p created\n", ctx
);
279 DBG(ctx
, "log_priority=%d\n", ctx
->log_priority
);
284 free(ctx
->modules_by_name
);
292 * @ctx: kmod library context
294 * Take a reference of the kmod library context.
296 * Returns: the passed kmod library context
298 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
308 * @ctx: kmod library context
310 * Drop a reference of the kmod library context. If the refcount
311 * reaches zero, the resources of the context will be released.
313 * Returns: the passed kmod library context or NULL if it's freed
315 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
320 if (--ctx
->refcount
> 0)
323 INFO(ctx
, "context %p released\n", ctx
);
325 kmod_unload_resources(ctx
);
326 hash_free(ctx
->modules_by_name
);
329 kmod_config_free(ctx
->config
);
337 * @ctx: kmod library context
338 * @log_fn: function to be called for logging messages
339 * @data: data to pass to log function
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.
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
),
354 ctx
->log_fn
= log_fn
;
355 ctx
->log_data
= (void *)data
;
356 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
360 * kmod_get_log_priority:
361 * @ctx: kmod library context
363 * Returns: the current logging priority
365 KMOD_EXPORT
int kmod_get_log_priority(const struct kmod_ctx
*ctx
)
369 return ctx
->log_priority
;
373 * kmod_set_log_priority:
374 * @ctx: kmod library context
375 * @priority: the new logging priority
377 * Set the current logging priority. The value controls which messages
380 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
384 ctx
->log_priority
= priority
;
387 struct kmod_module
*kmod_pool_get_module(struct kmod_ctx
*ctx
,
390 struct kmod_module
*mod
;
392 mod
= hash_find(ctx
->modules_by_name
, key
);
394 DBG(ctx
, "get module name='%s' found=%p\n", key
, mod
);
399 void kmod_pool_add_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
402 DBG(ctx
, "add %p key='%s'\n", mod
, key
);
404 hash_add(ctx
->modules_by_name
, key
, mod
);
407 void kmod_pool_del_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
410 DBG(ctx
, "del %p key='%s'\n", mod
, key
);
412 hash_del(ctx
->modules_by_name
, key
);
415 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx
*ctx
,
416 enum kmod_index index_number
,
418 struct kmod_list
**list
)
421 struct index_file
*idx
;
422 struct index_value
*realnames
, *realname
;
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
],
432 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
433 index_files
[index_number
].fn
);
435 DBG(ctx
, "file=%s name=%s\n", fn
, name
);
437 idx
= index_file_open(fn
);
441 realnames
= index_searchwild(idx
, name
);
442 index_file_close(idx
);
445 for (realname
= realnames
; realname
; realname
= realname
->next
) {
446 struct kmod_module
*mod
;
448 err
= kmod_module_new_from_alias(ctx
, name
, realname
->value
, &mod
);
450 ERR(ctx
, "Could not create module for alias=%s realname=%s: %s\n",
451 name
, realname
->value
, strerror(-err
));
455 *list
= kmod_list_append(*list
, mod
);
459 index_values_free(realnames
);
463 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
464 index_values_free(realnames
);
469 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx
*ctx
, const char *name
,
470 struct kmod_list
**list
)
472 if (!strstartswith(name
, "symbol:"))
475 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_SYMBOL
,
479 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx
*ctx
, const char *name
,
480 struct kmod_list
**list
)
482 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_ALIAS
,
486 int kmod_lookup_alias_from_builtin_file(struct kmod_ctx
*ctx
, const char *name
,
487 struct kmod_list
**list
)
492 assert(*list
== NULL
);
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
,
498 line
= index_mm_search(ctx
->indexes
[KMOD_INDEX_MODULES_BUILTIN
],
501 struct index_file
*idx
;
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
);
508 idx
= index_file_open(fn
);
510 DBG(ctx
, "could not open builtin file '%s'\n", fn
);
514 line
= index_search(idx
, name
);
515 index_file_close(idx
);
519 struct kmod_module
*mod
;
521 err
= kmod_module_new_from_name(ctx
, name
, &mod
);
523 ERR(ctx
, "Could not create module from name %s: %s\n",
524 name
, strerror(-err
));
528 kmod_module_set_builtin(mod
, true);
529 *list
= kmod_list_append(*list
, mod
);
539 char *kmod_search_moddep(struct kmod_ctx
*ctx
, const char *name
)
541 struct index_file
*idx
;
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
],
552 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
553 index_files
[KMOD_INDEX_MODULES_DEP
].fn
);
555 DBG(ctx
, "file=%s modname=%s\n", fn
, name
);
557 idx
= index_file_open(fn
);
559 DBG(ctx
, "could not open moddep file '%s'\n", fn
);
563 line
= index_search(idx
, name
);
564 index_file_close(idx
);
569 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx
*ctx
, const char *name
,
570 struct kmod_list
**list
)
576 * Module names do not contain ':'. Return early if we know it will
579 if (strchr(name
, ':'))
582 line
= kmod_search_moddep(ctx
, name
);
584 struct kmod_module
*mod
;
586 n
= kmod_module_new_from_name(ctx
, name
, &mod
);
588 ERR(ctx
, "Could not create module from name %s: %s\n",
593 *list
= kmod_list_append(*list
, mod
);
594 kmod_module_parse_depline(mod
, line
);
603 int kmod_lookup_alias_from_config(struct kmod_ctx
*ctx
, const char *name
,
604 struct kmod_list
**list
)
606 struct kmod_config
*config
= ctx
->config
;
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
);
614 if (fnmatch(aliasname
, name
, 0) == 0) {
615 struct kmod_module
*mod
;
617 err
= kmod_module_new_from_alias(ctx
, aliasname
,
620 ERR(ctx
, "Could not create module for alias=%s modname=%s: %s\n",
621 name
, modname
, strerror(-err
));
625 *list
= kmod_list_append(*list
, mod
);
633 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
637 int kmod_lookup_alias_from_commands(struct kmod_ctx
*ctx
, const char *name
,
638 struct kmod_list
**list
)
640 struct kmod_config
*config
= ctx
->config
;
641 struct kmod_list
*l
, *node
;
644 kmod_list_foreach(l
, config
->install_commands
) {
645 const char *modname
= kmod_command_get_modname(l
);
647 if (streq(modname
, name
)) {
648 const char *cmd
= kmod_command_get_command(l
);
649 struct kmod_module
*mod
;
651 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
653 ERR(ctx
, "Could not create module from name %s: %s\n",
654 modname
, strerror(-err
));
658 node
= kmod_list_append(*list
, mod
);
660 ERR(ctx
, "out of memory\n");
667 kmod_module_set_install_commands(mod
, cmd
);
670 * match only the first one, like modprobe from
671 * module-init-tools does
680 kmod_list_foreach(l
, config
->remove_commands
) {
681 const char *modname
= kmod_command_get_modname(l
);
683 if (streq(modname
, name
)) {
684 const char *cmd
= kmod_command_get_command(l
);
685 struct kmod_module
*mod
;
687 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
689 ERR(ctx
, "Could not create module from name %s: %s\n",
690 modname
, strerror(-err
));
694 node
= kmod_list_append(*list
, mod
);
696 ERR(ctx
, "out of memory\n");
703 kmod_module_set_remove_commands(mod
, cmd
);
706 * match only the first one, like modprobe from
707 * module-init-tools does
716 void kmod_set_modules_visited(struct kmod_ctx
*ctx
, bool visited
)
718 struct hash_iter iter
;
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
);
726 void kmod_set_modules_required(struct kmod_ctx
*ctx
, bool required
)
728 struct hash_iter iter
;
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
);
736 static bool is_cache_invalid(const char *path
, unsigned long long stamp
)
740 if (stat(path
, &st
) < 0)
743 if (stamp
!= stat_mstamp(&st
))
750 * kmod_validate_resources:
751 * @ctx: kmod library context
753 * Check if indexes and configuration files changed on disk and the current
754 * context is not valid anymore.
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.
761 KMOD_EXPORT
int kmod_validate_resources(struct kmod_ctx
*ctx
)
766 if (ctx
== NULL
|| ctx
->config
== NULL
)
767 return KMOD_RESOURCES_MUST_RECREATE
;
769 kmod_list_foreach(l
, ctx
->config
->paths
) {
770 struct kmod_config_path
*cf
= l
->data
;
772 if (is_cache_invalid(cf
->path
, cf
->stamp
))
773 return KMOD_RESOURCES_MUST_RECREATE
;
776 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
779 if (ctx
->indexes
[i
] == NULL
)
782 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
785 if (is_cache_invalid(path
, ctx
->indexes_stamp
[i
]))
786 return KMOD_RESOURCES_MUST_RELOAD
;
789 return KMOD_RESOURCES_OK
;
793 * kmod_load_resources:
794 * @ctx: kmod library context
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.
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.
805 * Returns: 0 on success or < 0 otherwise.
807 KMOD_EXPORT
int kmod_load_resources(struct kmod_ctx
*ctx
)
814 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
817 if (ctx
->indexes
[i
] != NULL
) {
818 INFO(ctx
, "Index %s already loaded\n",
823 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
825 ctx
->indexes
[i
] = index_mm_open(ctx
, path
,
826 &ctx
->indexes_stamp
[i
]);
827 if (ctx
->indexes
[i
] == NULL
)
834 kmod_unload_resources(ctx
);
839 * kmod_unload_resources:
840 * @ctx: kmod library context
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.
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().
851 * Returns: 0 on success or < 0 otherwise.
853 KMOD_EXPORT
void kmod_unload_resources(struct kmod_ctx
*ctx
)
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;
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
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
883 * Returns: 0 on success or < 0 otherwise.
885 KMOD_EXPORT
int kmod_dump_index(struct kmod_ctx
*ctx
, enum kmod_index type
,
891 if (type
< 0 || type
>= _KMOD_INDEX_MODULES_SIZE
)
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
);
900 struct index_file
*idx
;
902 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
903 index_files
[type
].fn
);
905 DBG(ctx
, "file=%s\n", fn
);
907 idx
= index_file_open(fn
);
911 index_dump(idx
, fd
, index_files
[type
].prefix
);
912 index_file_close(idx
);
918 const struct kmod_config
*kmod_get_config(const struct kmod_ctx
*ctx
)