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
31 #include <sys/utsname.h>
35 #include "libkmod-private.h"
36 #include "libkmod-index.h"
38 #define KMOD_HASH_SIZE (256)
39 #define KMOD_LRU_MAX (128)
40 #define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_SYMBOL + 1
44 * @short_description: libkmod context
46 * The context contains the default values for the library user,
47 * and is passed to all library operations.
50 static struct _index_files
{
54 [KMOD_INDEX_MODULES_DEP
] = { .fn
= "modules.dep", .prefix
= "" },
55 [KMOD_INDEX_MODULES_ALIAS
] = { .fn
= "modules.alias", .prefix
= "alias " },
56 [KMOD_INDEX_MODULES_SYMBOL
] = { .fn
= "modules.symbols", .prefix
= "alias "},
59 static const char *default_config_paths
[] = {
61 SYSCONFDIR
"/modprobe.d",
62 ROOTPREFIX
"/lib/modprobe.d",
69 * Opaque object representing the library context.
74 void (*log_fn
)(void *data
,
75 int priority
, const char *file
, int line
,
76 const char *fn
, const char *format
, va_list args
);
80 struct kmod_config
*config
;
81 struct hash
*modules_by_name
;
82 struct index_mm
*indexes
[_KMOD_INDEX_MODULES_SIZE
];
83 unsigned long long indexes_stamp
[_KMOD_INDEX_MODULES_SIZE
];
86 void kmod_log(const struct kmod_ctx
*ctx
,
87 int priority
, const char *file
, int line
, const char *fn
,
88 const char *format
, ...)
92 if (ctx
->log_fn
== NULL
)
95 va_start(args
, format
);
96 ctx
->log_fn(ctx
->log_data
, priority
, file
, line
, fn
, format
, args
);
100 static void log_filep(void *data
,
101 int priority
, const char *file
, int line
,
102 const char *fn
, const char *format
, va_list args
)
105 fprintf(fp
, "libkmod: %s: ", fn
);
106 vfprintf(fp
, format
, args
);
109 const char *kmod_get_dirname(const struct kmod_ctx
*ctx
)
116 * @ctx: kmod library context
118 * Retrieve stored data pointer from library context. This might be useful
119 * to access from callbacks.
121 * Returns: stored userdata
123 KMOD_EXPORT
void *kmod_get_userdata(const struct kmod_ctx
*ctx
)
127 return (void *)ctx
->userdata
;
132 * @ctx: kmod library context
133 * @userdata: data pointer
135 * Store custom @userdata in the library context.
137 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, const void *userdata
)
141 ctx
->userdata
= userdata
;
144 static int log_priority(const char *priority
)
149 prio
= strtol(priority
, &endptr
, 10);
150 if (endptr
[0] == '\0' || isspace(endptr
[0]))
152 if (strncmp(priority
, "err", 3) == 0)
154 if (strncmp(priority
, "info", 4) == 0)
156 if (strncmp(priority
, "debug", 5) == 0)
161 static const char *dirname_default_prefix
= ROOTPREFIX
"/lib/modules";
163 static char *get_kernel_release(const char *dirname
)
169 return path_make_absolute_cwd(dirname
);
174 if (asprintf(&p
, "%s/%s", dirname_default_prefix
, u
.release
) < 0)
182 * @dirname: what to consider as linux module's directory, if NULL
183 * defaults to $rootprefix/lib/modules/`uname -r`. If it's relative,
184 * it's treated as relative to current the current working
185 * directory. Otherwise, give an absolute dirname.
186 * @config_paths: ordered array of paths (directories or files) where
187 * to load from user-defined configuration parameters such as
188 * alias, blacklists, commands (install, remove). If
189 * NULL defaults to /run/modprobe.d, /etc/modprobe.d and
190 * $rootprefix/lib/modprobe.d. Give an empty vector if
191 * configuration should not be read. This array must be null
194 * Create kmod library context. This reads the kmod configuration
195 * and fills in the default values.
197 * The initial refcount is 1, and needs to be decremented to
198 * release the resources of the kmod library context.
200 * Returns: a new kmod library context
202 KMOD_EXPORT
struct kmod_ctx
*kmod_new(const char *dirname
,
203 const char * const *config_paths
)
206 struct kmod_ctx
*ctx
;
209 ctx
= calloc(1, sizeof(struct kmod_ctx
));
214 ctx
->log_fn
= log_filep
;
215 ctx
->log_data
= stderr
;
216 ctx
->log_priority
= LOG_ERR
;
218 ctx
->dirname
= get_kernel_release(dirname
);
220 /* environment overwrites config */
221 env
= getenv("KMOD_LOG");
223 kmod_set_log_priority(ctx
, log_priority(env
));
225 if (config_paths
== NULL
)
226 config_paths
= default_config_paths
;
227 err
= kmod_config_new(ctx
, &ctx
->config
, config_paths
);
229 ERR(ctx
, "could not create config\n");
233 ctx
->modules_by_name
= hash_new(KMOD_HASH_SIZE
, NULL
);
234 if (ctx
->modules_by_name
== NULL
) {
235 ERR(ctx
, "could not create by-name hash\n");
239 INFO(ctx
, "ctx %p created\n", ctx
);
240 DBG(ctx
, "log_priority=%d\n", ctx
->log_priority
);
245 free(ctx
->modules_by_name
);
253 * @ctx: kmod library context
255 * Take a reference of the kmod library context.
257 * Returns: the passed kmod library context
259 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
269 * @ctx: kmod library context
271 * Drop a reference of the kmod library context. If the refcount
272 * reaches zero, the resources of the context will be released.
274 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
279 if (--ctx
->refcount
> 0)
282 INFO(ctx
, "context %p released\n", ctx
);
284 kmod_unload_resources(ctx
);
285 hash_free(ctx
->modules_by_name
);
288 kmod_config_free(ctx
->config
);
296 * @ctx: kmod library context
297 * @log_fn: function to be called for logging messages
298 * @data: data to pass to log function
300 * The built-in logging writes to stderr. It can be
301 * overridden by a custom function, to plug log messages
302 * into the user's logging functionality.
304 KMOD_EXPORT
void kmod_set_log_fn(struct kmod_ctx
*ctx
,
305 void (*log_fn
)(void *data
,
306 int priority
, const char *file
,
307 int line
, const char *fn
,
308 const char *format
, va_list args
),
313 ctx
->log_fn
= log_fn
;
314 ctx
->log_data
= (void *)data
;
315 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
319 * kmod_get_log_priority:
320 * @ctx: kmod library context
322 * Returns: the current logging priority
324 KMOD_EXPORT
int kmod_get_log_priority(const struct kmod_ctx
*ctx
)
328 return ctx
->log_priority
;
332 * kmod_set_log_priority:
333 * @ctx: kmod library context
334 * @priority: the new logging priority
336 * Set the current logging priority. The value controls which messages
339 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
343 ctx
->log_priority
= priority
;
346 struct kmod_module
*kmod_pool_get_module(struct kmod_ctx
*ctx
,
349 struct kmod_module
*mod
;
351 mod
= hash_find(ctx
->modules_by_name
, key
);
353 DBG(ctx
, "get module name='%s' found=%p\n", key
, mod
);
358 void kmod_pool_add_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
361 DBG(ctx
, "add %p key='%s'\n", mod
, key
);
363 hash_add(ctx
->modules_by_name
, key
, mod
);
366 void kmod_pool_del_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
369 DBG(ctx
, "del %p key='%s'\n", mod
, key
);
371 hash_del(ctx
->modules_by_name
, key
);
374 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx
*ctx
,
375 enum kmod_index index_number
,
377 struct kmod_list
**list
)
380 struct index_file
*idx
;
381 struct index_value
*realnames
, *realname
;
383 if (ctx
->indexes
[index_number
] != NULL
) {
384 DBG(ctx
, "use mmaped index '%s' for name=%s\n",
385 index_files
[index_number
].fn
, name
);
386 realnames
= index_mm_searchwild(ctx
->indexes
[index_number
],
391 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
392 index_files
[index_number
].fn
);
394 DBG(ctx
, "file=%s name=%s\n", fn
, name
);
396 idx
= index_file_open(fn
);
400 realnames
= index_searchwild(idx
, name
);
401 index_file_close(idx
);
404 for (realname
= realnames
; realname
; realname
= realname
->next
) {
405 struct kmod_module
*mod
;
407 err
= kmod_module_new_from_alias(ctx
, name
, realname
->value
, &mod
);
409 ERR(ctx
, "%s\n", strerror(-err
));
413 *list
= kmod_list_append(*list
, mod
);
417 index_values_free(realnames
);
421 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
426 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx
*ctx
, const char *name
,
427 struct kmod_list
**list
)
429 if (!strstartswith(name
, "symbol:"))
432 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_SYMBOL
,
436 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx
*ctx
, const char *name
,
437 struct kmod_list
**list
)
439 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_MODULES_ALIAS
,
443 char *kmod_search_moddep(struct kmod_ctx
*ctx
, const char *name
)
445 struct index_file
*idx
;
449 if (ctx
->indexes
[KMOD_INDEX_MODULES_DEP
]) {
450 DBG(ctx
, "use mmaped index '%s' modname=%s\n",
451 index_files
[KMOD_INDEX_MODULES_DEP
].fn
, name
);
452 return index_mm_search(ctx
->indexes
[KMOD_INDEX_MODULES_DEP
],
456 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
457 index_files
[KMOD_INDEX_MODULES_DEP
].fn
);
459 DBG(ctx
, "file=%s modname=%s\n", fn
, name
);
461 idx
= index_file_open(fn
);
463 ERR(ctx
, "could not open moddep file '%s'\n", fn
);
467 line
= index_search(idx
, name
);
468 index_file_close(idx
);
473 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx
*ctx
, const char *name
,
474 struct kmod_list
**list
)
480 * Module names do not contain ':'. Return early if we know it will
483 if (strchr(name
, ':'))
486 line
= kmod_search_moddep(ctx
, name
);
488 struct kmod_module
*mod
;
490 n
= kmod_module_new_from_name(ctx
, name
, &mod
);
492 ERR(ctx
, "%s\n", strerror(-n
));
496 *list
= kmod_list_append(*list
, mod
);
497 kmod_module_parse_depline(mod
, line
);
506 int kmod_lookup_alias_from_config(struct kmod_ctx
*ctx
, const char *name
,
507 struct kmod_list
**list
)
509 struct kmod_config
*config
= ctx
->config
;
513 kmod_list_foreach(l
, config
->aliases
) {
514 const char *aliasname
= kmod_alias_get_name(l
);
515 const char *modname
= kmod_alias_get_modname(l
);
517 if (fnmatch(aliasname
, name
, 0) == 0) {
518 struct kmod_module
*mod
;
520 err
= kmod_module_new_from_alias(ctx
, aliasname
,
523 ERR(ctx
, "%s\n", strerror(-err
));
527 *list
= kmod_list_append(*list
, mod
);
535 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
539 int kmod_lookup_alias_from_commands(struct kmod_ctx
*ctx
, const char *name
,
540 struct kmod_list
**list
)
542 struct kmod_config
*config
= ctx
->config
;
543 struct kmod_list
*l
, *node
;
546 kmod_list_foreach(l
, config
->install_commands
) {
547 const char *modname
= kmod_command_get_modname(l
);
549 if (streq(modname
, name
)) {
550 const char *cmd
= kmod_command_get_command(l
);
551 struct kmod_module
*mod
;
553 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
555 ERR(ctx
, "%s\n", strerror(-err
));
559 node
= kmod_list_append(*list
, mod
);
561 ERR(ctx
, "out of memory\n");
568 kmod_module_set_install_commands(mod
, cmd
);
571 * match only the first one, like modprobe from
572 * module-init-tools does
581 kmod_list_foreach(l
, config
->remove_commands
) {
582 const char *modname
= kmod_command_get_modname(l
);
584 if (streq(modname
, name
)) {
585 const char *cmd
= kmod_command_get_command(l
);
586 struct kmod_module
*mod
;
588 err
= kmod_module_new_from_name(ctx
, modname
, &mod
);
590 ERR(ctx
, "%s\n", strerror(-err
));
594 node
= kmod_list_append(*list
, mod
);
596 ERR(ctx
, "out of memory\n");
603 kmod_module_set_remove_commands(mod
, cmd
);
606 * match only the first one, like modprobe from
607 * module-init-tools does
616 static bool is_cache_invalid(const char *path
, unsigned long long stamp
)
620 if (stat(path
, &st
) < 0)
623 if (stamp
!= ts_usec(&st
.st_mtim
))
630 * kmod_validate_resources:
631 * @ctx: kmod library context
633 * Check if indexes and configuration files changed on disk and the current
634 * context is not valid anymore.
636 * Returns: KMOD_RESOURCES_OK if resources are still valid,
637 * KMOD_RESOURCES_MUST_RELOAD if it's sufficient to call
638 * kmod_unload_resources() and kmod_load_resources() or
639 * KMOD_RESOURCES_MUST_RECREATE if @ctx must be re-created.
641 KMOD_EXPORT
int kmod_validate_resources(struct kmod_ctx
*ctx
)
646 if (ctx
== NULL
|| ctx
->config
== NULL
)
647 return KMOD_RESOURCES_MUST_RECREATE
;
649 kmod_list_foreach(l
, ctx
->config
->paths
) {
650 struct kmod_config_path
*cf
= l
->data
;
652 if (is_cache_invalid(cf
->path
, cf
->stamp
))
653 return KMOD_RESOURCES_MUST_RECREATE
;
656 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
659 if (ctx
->indexes
[i
] == NULL
)
662 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
665 if (is_cache_invalid(path
, ctx
->indexes_stamp
[i
]))
666 return KMOD_RESOURCES_MUST_RELOAD
;
669 return KMOD_RESOURCES_OK
;
673 * kmod_load_resources:
674 * @ctx: kmod library context
676 * Load indexes and keep them open in @ctx. This way it's faster to lookup
677 * information within the indexes. If this function is not called before a
678 * search, the necessary index is always opened and closed.
680 * If user will do more than one or two lookups, insertions, deletions, most
681 * likely it's good to call this function first. Particularly in a daemon like
682 * udev that on bootup issues hundreds of calls to lookup the index, calling
683 * this function will speedup the searches.
685 * Returns: 0 on success or < 0 otherwise.
687 KMOD_EXPORT
int kmod_load_resources(struct kmod_ctx
*ctx
)
694 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
697 if (ctx
->indexes
[i
] != NULL
) {
698 INFO(ctx
, "Index %s already loaded\n",
703 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
705 ctx
->indexes
[i
] = index_mm_open(ctx
, path
, true,
706 &ctx
->indexes_stamp
[i
]);
707 if (ctx
->indexes
[i
] == NULL
)
714 kmod_unload_resources(ctx
);
719 * kmod_unload_resources:
720 * @ctx: kmod library context
722 * Unload all the indexes. This will free the resources to maintain the index
723 * open and all subsequent searches will need to open and close the index.
725 * User is free to call kmod_load_resources() and kmod_unload_resources() as
726 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
727 * knows that when starting up it will lookup a lot of modules, it could call
728 * kmod_load_resources() and after the first burst of searches is gone, it
729 * could free the resources by calling kmod_unload_resources().
731 * Returns: 0 on success or < 0 otherwise.
733 KMOD_EXPORT
void kmod_unload_resources(struct kmod_ctx
*ctx
)
740 for (i
= 0; i
< _KMOD_INDEX_MODULES_SIZE
; i
++) {
741 if (ctx
->indexes
[i
] != NULL
) {
742 index_mm_close(ctx
->indexes
[i
]);
743 ctx
->indexes
[i
] = NULL
;
744 ctx
->indexes_stamp
[i
] = 0;
751 * @ctx: kmod library context
752 * @type: index to dump
753 * @fd: file descriptor to dump index to
755 * Dump index to file descriptor
757 * Returns: 0 on success or < 0 otherwise.
759 KMOD_EXPORT
int kmod_dump_index(struct kmod_ctx
*ctx
, enum kmod_index type
,
765 if (type
< 0 || type
>= _KMOD_INDEX_MODULES_SIZE
)
768 if (ctx
->indexes
[type
] != NULL
) {
769 DBG(ctx
, "use mmaped index '%s'\n", index_files
[type
].fn
);
770 index_mm_dump(ctx
->indexes
[type
], fd
,
771 index_files
[type
].prefix
);
774 struct index_file
*idx
;
776 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
777 index_files
[type
].fn
);
779 DBG(ctx
, "file=%s\n", fn
);
781 idx
= index_file_open(fn
);
785 index_dump(idx
, fd
, index_files
[type
].prefix
);
786 index_file_close(idx
);
792 const struct kmod_list
*kmod_get_blacklists(const struct kmod_ctx
*ctx
)
794 return ctx
->config
->blacklists
;
797 const struct kmod_list
*kmod_get_options(const struct kmod_ctx
*ctx
)
799 return ctx
->config
->options
;
802 const struct kmod_list
*kmod_get_install_commands(const struct kmod_ctx
*ctx
)
804 return ctx
->config
->install_commands
;
807 const struct kmod_list
*kmod_get_remove_commands(const struct kmod_ctx
*ctx
)
809 return ctx
->config
->remove_commands
;
812 const struct kmod_list
*kmod_get_softdeps(const struct kmod_ctx
*ctx
)
814 return ctx
->config
->softdeps
;
817 const struct kmod_list
*kmod_get_aliases(const struct kmod_ctx
*ctx
)
819 return ctx
->config
->aliases
;