]>
git.ipfire.org Git - thirdparty/kmod.git/blob - libkmod/libkmod.c
a94980973578384cbbe5b9e329a49586699f1383
2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011 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>
34 #include "libkmod-private.h"
35 #include "libkmod-index.h"
37 #define KMOD_HASH_SIZE (256)
38 #define KMOD_LRU_MAX (128)
42 * @short_description: libkmod context
44 * The context contains the default values for the library user,
45 * and is passed to all library operations.
55 static const char* index_files
[] = {
56 [KMOD_INDEX_DEP
] = "modules.dep",
57 [KMOD_INDEX_ALIAS
] = "modules.alias",
58 [KMOD_INDEX_SYMBOL
] = "modules.symbols",
61 static const char *default_config_paths
[] = {
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 kmod_hash
*modules_by_name
;
84 struct index_mm
*indexes
[_KMOD_INDEX_LAST
];
87 void kmod_log(const struct kmod_ctx
*ctx
,
88 int priority
, const char *file
, int line
, const char *fn
,
89 const char *format
, ...)
93 if (ctx
->log_fn
== NULL
)
96 va_start(args
, format
);
97 ctx
->log_fn(ctx
->log_data
, priority
, file
, line
, fn
, format
, args
);
101 static void log_filep(void *data
,
102 int priority
, const char *file
, int line
,
103 const char *fn
, const char *format
, va_list args
)
106 fprintf(fp
, "libkmod: %s: ", fn
);
107 vfprintf(fp
, format
, args
);
110 const char *kmod_get_dirname(const struct kmod_ctx
*ctx
)
117 * @ctx: kmod library context
119 * Retrieve stored data pointer from library context. This might be useful
120 * to access from callbacks.
122 * Returns: stored userdata
124 KMOD_EXPORT
void *kmod_get_userdata(const struct kmod_ctx
*ctx
)
128 return (void *)ctx
->userdata
;
133 * @ctx: kmod library context
134 * @userdata: data pointer
136 * Store custom @userdata in the library context.
138 KMOD_EXPORT
void kmod_set_userdata(struct kmod_ctx
*ctx
, const void *userdata
)
142 ctx
->userdata
= userdata
;
145 static int log_priority(const char *priority
)
150 prio
= strtol(priority
, &endptr
, 10);
151 if (endptr
[0] == '\0' || isspace(endptr
[0]))
153 if (strncmp(priority
, "err", 3) == 0)
155 if (strncmp(priority
, "info", 4) == 0)
157 if (strncmp(priority
, "debug", 5) == 0)
162 static const char *dirname_default_prefix
= "/lib/modules";
164 static char *get_kernel_release(const char *dirname
)
170 return strdup(dirname
);
175 if (asprintf(&p
, "%s/%s", dirname_default_prefix
, u
.release
) < 0)
184 * Create kmod library context. This reads the kmod configuration
185 * and fills in the default values.
187 * The initial refcount is 1, and needs to be decremented to
188 * release the resources of the kmod library context.
190 * @dirname: what to consider as linux module's directory, if NULL
191 * defaults to /lib/modules/`uname -r`.
192 * @config_paths: ordered array of paths (directories or files) where
193 * to load from user-defined configuration parameters such as
194 * alias, blacklists, commands (install, remove). If
195 * NULL defaults to /run/modprobe.d, /etc/modprobe.d and
196 * /lib/modprobe.d. Give an empty vector if configuration should
197 * not be read. This array must be null terminated.
199 * Returns: a new kmod library context
201 KMOD_EXPORT
struct kmod_ctx
*kmod_new(const char *dirname
,
202 const char * const *config_paths
)
205 struct kmod_ctx
*ctx
;
208 ctx
= calloc(1, sizeof(struct kmod_ctx
));
213 ctx
->log_fn
= log_filep
;
214 ctx
->log_data
= stderr
;
215 ctx
->log_priority
= LOG_ERR
;
217 ctx
->dirname
= get_kernel_release(dirname
);
219 /* environment overwrites config */
220 env
= getenv("KMOD_LOG");
222 kmod_set_log_priority(ctx
, log_priority(env
));
224 if (config_paths
== NULL
)
225 config_paths
= default_config_paths
;
226 err
= kmod_config_new(ctx
, &ctx
->config
, config_paths
);
228 ERR(ctx
, "could not create config\n");
232 ctx
->modules_by_name
= kmod_hash_new(KMOD_HASH_SIZE
, NULL
);
233 if (ctx
->modules_by_name
== NULL
) {
234 ERR(ctx
, "could not create by-name hash\n");
238 INFO(ctx
, "ctx %p created\n", ctx
);
239 DBG(ctx
, "log_priority=%d\n", ctx
->log_priority
);
244 free(ctx
->modules_by_name
);
252 * @ctx: kmod library context
254 * Take a reference of the kmod library context.
256 * Returns: the passed kmod library context
258 KMOD_EXPORT
struct kmod_ctx
*kmod_ref(struct kmod_ctx
*ctx
)
268 * @ctx: kmod library context
270 * Drop a reference of the kmod library context. If the refcount
271 * reaches zero, the resources of the context will be released.
273 KMOD_EXPORT
struct kmod_ctx
*kmod_unref(struct kmod_ctx
*ctx
)
278 if (--ctx
->refcount
> 0)
281 INFO(ctx
, "context %p released\n", ctx
);
283 kmod_unload_resources(ctx
);
284 kmod_hash_free(ctx
->modules_by_name
);
287 kmod_config_free(ctx
->config
);
295 * @ctx: kmod library context
296 * @log_fn: function to be called for logging messages
298 * The built-in logging writes to stderr. It can be
299 * overridden by a custom function, to plug log messages
300 * into the user's logging functionality.
302 KMOD_EXPORT
void kmod_set_log_fn(struct kmod_ctx
*ctx
,
303 void (*log_fn
)(void *data
,
304 int priority
, const char *file
,
305 int line
, const char *fn
,
306 const char *format
, va_list args
),
311 ctx
->log_fn
= log_fn
;
312 ctx
->log_data
= (void *)data
;
313 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
317 * kmod_get_log_priority:
318 * @ctx: kmod library context
320 * Returns: the current logging priority
322 KMOD_EXPORT
int kmod_get_log_priority(const struct kmod_ctx
*ctx
)
326 return ctx
->log_priority
;
330 * kmod_set_log_priority:
331 * @ctx: kmod library context
332 * @priority: the new logging priority
334 * Set the current logging priority. The value controls which messages
337 KMOD_EXPORT
void kmod_set_log_priority(struct kmod_ctx
*ctx
, int priority
)
341 ctx
->log_priority
= priority
;
344 struct kmod_module
*kmod_pool_get_module(struct kmod_ctx
*ctx
,
347 struct kmod_module
*mod
;
349 mod
= kmod_hash_find(ctx
->modules_by_name
, key
);
351 DBG(ctx
, "get module name='%s' found=%p\n", key
, mod
);
356 void kmod_pool_add_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
359 DBG(ctx
, "add %p key='%s'\n", mod
, key
);
361 kmod_hash_add(ctx
->modules_by_name
, key
, mod
);
364 void kmod_pool_del_module(struct kmod_ctx
*ctx
, struct kmod_module
*mod
,
367 DBG(ctx
, "del %p key='%s'\n", mod
, key
);
369 kmod_hash_del(ctx
->modules_by_name
, key
);
372 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx
*ctx
,
373 enum kmod_index index_number
,
375 struct kmod_list
**list
)
378 struct index_file
*idx
;
379 struct index_value
*realnames
, *realname
;
381 if (ctx
->indexes
[index_number
] != NULL
) {
382 DBG(ctx
, "use mmaped index '%s' for name=%s\n",
383 index_files
[index_number
], name
);
384 realnames
= index_mm_searchwild(ctx
->indexes
[index_number
],
389 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
390 index_files
[index_number
]);
392 DBG(ctx
, "file=%s name=%s\n", fn
, name
);
394 idx
= index_file_open(fn
);
398 realnames
= index_searchwild(idx
, name
);
399 index_file_close(idx
);
402 for (realname
= realnames
; realname
; realname
= realnames
->next
) {
403 struct kmod_module
*mod
;
405 err
= kmod_module_new_from_alias(ctx
, name
, realname
->value
, &mod
);
407 ERR(ctx
, "%s\n", strerror(-err
));
411 *list
= kmod_list_append(*list
, mod
);
415 index_values_free(realnames
);
419 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
424 int kmod_lookup_alias_from_symbols_file(struct kmod_ctx
*ctx
, const char *name
,
425 struct kmod_list
**list
)
427 if (!startswith(name
, "symbol:"))
430 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_SYMBOL
, name
,
434 int kmod_lookup_alias_from_aliases_file(struct kmod_ctx
*ctx
, const char *name
,
435 struct kmod_list
**list
)
437 return kmod_lookup_alias_from_alias_bin(ctx
, KMOD_INDEX_ALIAS
, name
,
441 char *kmod_search_moddep(struct kmod_ctx
*ctx
, const char *name
)
443 struct index_file
*idx
;
447 if (ctx
->indexes
[KMOD_INDEX_DEP
]) {
448 DBG(ctx
, "use mmaped index '%s' modname=%s\n",
449 index_files
[KMOD_INDEX_DEP
], name
);
450 return index_mm_search(ctx
->indexes
[KMOD_INDEX_DEP
], name
);
453 snprintf(fn
, sizeof(fn
), "%s/%s.bin", ctx
->dirname
,
454 index_files
[KMOD_INDEX_DEP
]);
456 DBG(ctx
, "file=%s modname=%s\n", fn
, name
);
458 idx
= index_file_open(fn
);
460 ERR(ctx
, "Could not open moddep file '%s'\n", fn
);
464 line
= index_search(idx
, name
);
465 index_file_close(idx
);
470 int kmod_lookup_alias_from_moddep_file(struct kmod_ctx
*ctx
, const char *name
,
471 struct kmod_list
**list
)
477 * Module names do not contain ':'. Return early if we know it will
480 if (strchr(name
, ':'))
483 line
= kmod_search_moddep(ctx
, name
);
485 struct kmod_module
*mod
;
487 n
= kmod_module_new_from_name(ctx
, name
, &mod
);
489 ERR(ctx
, "%s\n", strerror(-n
));
493 *list
= kmod_list_append(*list
, mod
);
494 kmod_module_parse_depline(mod
, line
);
503 int kmod_lookup_alias_from_config(struct kmod_ctx
*ctx
, const char *name
,
504 struct kmod_list
**list
)
506 struct kmod_config
*config
= ctx
->config
;
510 kmod_list_foreach(l
, config
->aliases
) {
511 const char *aliasname
= kmod_alias_get_name(l
);
512 const char *modname
= kmod_alias_get_modname(l
);
514 if (fnmatch(aliasname
, name
, 0) == 0) {
515 struct kmod_module
*mod
;
517 err
= kmod_module_new_from_alias(ctx
, aliasname
,
520 ERR(ctx
, "%s\n", strerror(-err
));
524 *list
= kmod_list_append(*list
, mod
);
532 *list
= kmod_list_remove_n_latest(*list
, nmatch
);
537 * kmod_module_get_filtered_blacklist:
538 * @ctx: kmod library context
539 * @input: list of kmod_module to be filtered with blacklist
540 * @output: where to save the new list
542 * Given a list @input, this function filter it out with config's blacklist
543 * ans save it in @output.
545 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
548 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
549 const struct kmod_list
*input
,
550 struct kmod_list
**output
)
552 const struct kmod_config
*config
;
553 const struct kmod_list
*li
;
555 if (ctx
== NULL
|| output
== NULL
)
562 config
= ctx
->config
;
563 kmod_list_foreach(li
, input
) {
564 struct kmod_module
*mod
= li
->data
;
565 const struct kmod_list
*lb
;
566 struct kmod_list
*node
;
567 bool filtered
= false;
568 kmod_list_foreach(lb
, config
->blacklists
) {
569 const char *name
= lb
->data
;
570 if (streq(name
, kmod_module_get_name(mod
))) {
578 node
= kmod_list_append(*output
, mod
);
582 kmod_module_ref(mod
);
587 kmod_module_unref_list(*output
);
593 * kmod_load_resources:
594 * @ctx: kmod library context
596 * Load indexes and keep them open in @ctx. This way it's faster to lookup
597 * information within the indexes. If this function is not called before a
598 * search, the necessary index is always opened and closed.
600 * If user will do more than one or two lookups, insertions, deletions, most
601 * likely it's good to call this function first. Particularly in a daemon like
602 * udev that on bootup issues hundreds of calls to lookup the index, calling
603 * this function will speedup the searches.
605 * Returns: 0 on success or < 0 otherwise.
607 KMOD_EXPORT
int kmod_load_resources(struct kmod_ctx
*ctx
)
614 for (i
= 0; i
< ARRAY_SIZE(index_files
); i
++) {
617 if (ctx
->indexes
[i
] == NULL
) {
618 INFO(ctx
, "Index %s already loaded\n", index_files
[i
]);
622 snprintf(path
, sizeof(path
), "%s/%s.bin", ctx
->dirname
,
624 ctx
->indexes
[i
] = index_mm_open(ctx
, path
, true);
625 if (ctx
->indexes
[i
] == NULL
)
632 kmod_unload_resources(ctx
);
637 * kmod_unload_resources:
638 * @ctx: kmod library context
640 * Unload all the indexes. This will free the resources to maintain the index
641 * open and all subsequent searches will need to open and close the index.
643 * User is free to call kmod_load_resources() and kmod_unload_resources() as
644 * many times as wanted during the lifecycle of @ctx. For example, if a daemon
645 * knows that when starting up it will lookup a lot of modules, it could call
646 * kmod_load_resources() and after the first burst of searches is gone, it
647 * could free the resources by calling kmod_unload_resources().
649 * Returns: 0 on success or < 0 otherwise.
651 KMOD_EXPORT
void kmod_unload_resources(struct kmod_ctx
*ctx
)
658 for (i
= 0; i
< ARRAY_SIZE(index_files
); i
++) {
659 if (ctx
->indexes
[i
] != NULL
) {
660 index_mm_close(ctx
->indexes
[i
]);
661 ctx
->indexes
[i
] = NULL
;
666 const struct kmod_list
*kmod_get_options(const struct kmod_ctx
*ctx
)
668 return ctx
->config
->options
;
671 const struct kmod_list
*kmod_get_install_commands(const struct kmod_ctx
*ctx
)
673 return ctx
->config
->install_commands
;
676 const struct kmod_list
*kmod_get_remove_commands(const struct kmod_ctx
*ctx
)
678 return ctx
->config
->remove_commands
;