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, see <http://www.gnu.org/licenses/>.
35 #include <sys/syscall.h>
36 #include <sys/types.h>
38 #ifdef HAVE_LINUX_MODULE_H
39 #include <linux/module.h>
42 #include <shared/util.h>
45 #include "libkmod-internal.h"
48 * SECTION:libkmod-module
49 * @short_description: operate on kernel modules
52 enum kmod_module_builtin {
53 KMOD_MODULE_BUILTIN_UNKNOWN,
54 KMOD_MODULE_BUILTIN_NO,
55 KMOD_MODULE_BUILTIN_YES,
61 * Opaque object representing a module.
68 struct kmod_list *dep;
70 const char *install_commands; /* owned by kmod_config */
71 const char *remove_commands; /* owned by kmod_config */
72 char *alias; /* only set if this module was created from an alias */
73 struct kmod_file *file;
79 bool install_commands : 1;
80 bool remove_commands : 1;
84 * mark if module is builtin, i.e. it's present on modules.builtin
85 * file. This is set as soon as it is needed or as soon as we know
86 * about it, i.e. the module was created from builtin lookup.
88 enum kmod_module_builtin builtin;
91 * private field used by kmod_module_get_probe_list() to detect
97 * set by kmod_module_get_probe_list: indicates for probe_insert()
98 * whether the module's command and softdep should be ignored
103 * set by kmod_module_get_probe_list: indicates whether this is the
104 * module the user asked for or its dependency, or whether this
110 static inline const char *path_join(const char *path, size_t prefixlen,
118 pathlen = strlen(path);
119 if (prefixlen + pathlen + 1 >= PATH_MAX)
122 memcpy(buf + prefixlen, path, pathlen + 1);
126 static inline bool module_is_inkernel(struct kmod_module *mod)
128 int state = kmod_module_get_initstate(mod);
130 if (state == KMOD_MODULE_LIVE ||
131 state == KMOD_MODULE_BUILTIN)
137 int kmod_module_parse_depline(struct kmod_module *mod, char *line)
139 struct kmod_ctx *ctx = mod->ctx;
140 struct kmod_list *list = NULL;
149 assert(mod->dep == NULL);
150 mod->init.dep = true;
152 p = strchr(line, ':');
157 dirname = kmod_get_dirname(mod->ctx);
158 dirnamelen = strlen(dirname);
159 if (dirnamelen + 2 >= PATH_MAX)
162 memcpy(buf, dirname, dirnamelen);
163 buf[dirnamelen] = '/';
165 buf[dirnamelen] = '\0';
167 if (mod->path == NULL) {
168 const char *str = path_join(line, dirnamelen, buf);
171 mod->path = strdup(str);
172 if (mod->path == NULL)
177 for (p = strtok_r(p, " \t", &saveptr); p != NULL;
178 p = strtok_r(NULL, " \t", &saveptr)) {
179 struct kmod_module *depmod = NULL;
182 path = path_join(p, dirnamelen, buf);
184 ERR(ctx, "could not join path '%s' and '%s'.\n",
189 err = kmod_module_new_from_path(ctx, path, &depmod);
191 ERR(ctx, "ctx=%p path=%s error=%s\n",
192 ctx, path, strerror(-err));
196 DBG(ctx, "add dep: %s\n", path);
198 list = kmod_list_prepend(list, depmod);
202 DBG(ctx, "%d dependencies for %s\n", n, mod->name);
209 kmod_module_unref_list(list);
210 mod->init.dep = false;
214 void kmod_module_set_visited(struct kmod_module *mod, bool visited)
216 mod->visited = visited;
219 void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
222 builtin ? KMOD_MODULE_BUILTIN_YES : KMOD_MODULE_BUILTIN_NO;
225 void kmod_module_set_required(struct kmod_module *mod, bool required)
227 mod->required = required;
230 bool kmod_module_is_builtin(struct kmod_module *mod)
232 if (mod->builtin == KMOD_MODULE_BUILTIN_UNKNOWN) {
233 kmod_module_set_builtin(mod,
234 kmod_lookup_alias_is_builtin(mod->ctx, mod->name));
237 return mod->builtin == KMOD_MODULE_BUILTIN_YES;
240 * Memory layout with alias:
242 * struct kmod_module {
247 * name <----------' | |
248 * alias <-----------' |
249 * name\alias <--------'
251 * Memory layout without alias:
253 * struct kmod_module {
255 * alias -----|----> NULL
258 * name <----------'-'
260 * @key is "name\alias" or "name" (in which case alias == NULL)
262 static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
263 const char *name, size_t namelen,
264 const char *alias, size_t aliaslen,
265 struct kmod_module **mod)
267 struct kmod_module *m;
270 m = kmod_pool_get_module(ctx, key);
272 *mod = kmod_module_ref(m);
279 keylen = namelen + aliaslen + 1;
281 m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
285 memset(m, 0, sizeof(*m));
287 m->ctx = kmod_ref(ctx);
288 m->name = (char *)m + sizeof(*m);
289 memcpy(m->name, key, keylen + 1);
291 m->hashkey = m->name;
294 m->name[namelen] = '\0';
295 m->alias = m->name + namelen + 1;
296 m->hashkey = m->name + keylen + 1;
297 memcpy(m->hashkey, key, keylen + 1);
301 kmod_pool_add_module(ctx, m, m->hashkey);
308 * kmod_module_new_from_name:
309 * @ctx: kmod library context
310 * @name: name of the module
311 * @mod: where to save the created struct kmod_module
313 * Create a new struct kmod_module using the module name. @name can not be an
314 * alias, file name or anything else; it must be a module name. There's no
315 * check if the module exists in the system.
317 * This function is also used internally by many others that return a new
318 * struct kmod_module or a new list of modules.
320 * The initial refcount is 1, and needs to be decremented to release the
321 * resources of the kmod_module. Since libkmod keeps track of all
322 * kmod_modules created, they are all released upon @ctx destruction too. Do
323 * not unref @ctx before all the desired operations with the returned
324 * kmod_module are done.
326 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
327 * module name or if memory allocation failed.
329 KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
331 struct kmod_module **mod)
334 char name_norm[PATH_MAX];
336 if (ctx == NULL || name == NULL || mod == NULL)
339 modname_normalize(name, name_norm, &namelen);
341 return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
344 int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
345 const char *name, struct kmod_module **mod)
349 size_t namelen = strlen(name);
350 size_t aliaslen = strlen(alias);
352 if (namelen + aliaslen + 2 > PATH_MAX)
353 return -ENAMETOOLONG;
355 memcpy(key, name, namelen);
356 memcpy(key + namelen + 1, alias, aliaslen + 1);
359 err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
367 * kmod_module_new_from_path:
368 * @ctx: kmod library context
369 * @path: path where to find the given module
370 * @mod: where to save the created struct kmod_module
372 * Create a new struct kmod_module using the module path. @path must be an
373 * existent file with in the filesystem and must be accessible to libkmod.
375 * The initial refcount is 1, and needs to be decremented to release the
376 * resources of the kmod_module. Since libkmod keeps track of all
377 * kmod_modules created, they are all released upon @ctx destruction too. Do
378 * not unref @ctx before all the desired operations with the returned
379 * kmod_module are done.
381 * If @path is relative, it's treated as relative to the current working
382 * directory. Otherwise, give an absolute path.
384 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
385 * it's not a valid file for a kmod_module or if memory allocation failed.
387 KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
389 struct kmod_module **mod)
391 struct kmod_module *m;
398 if (ctx == NULL || path == NULL || mod == NULL)
401 abspath = path_make_absolute_cwd(path);
402 if (abspath == NULL) {
403 DBG(ctx, "no absolute path for %s\n", path);
407 err = stat(abspath, &st);
410 DBG(ctx, "stat %s: %s\n", path, strerror(errno));
415 if (path_to_modname(path, name, &namelen) == NULL) {
416 DBG(ctx, "could not get modname from path %s\n", path);
421 m = kmod_pool_get_module(ctx, name);
425 else if (streq(m->path, abspath))
428 ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
429 name, abspath, m->path);
434 *mod = kmod_module_ref(m);
438 err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
454 * Drop a reference of the kmod module. If the refcount reaches zero, its
455 * resources are released.
457 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
458 * returns the passed @mod with its refcount decremented.
460 KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
465 if (--mod->refcount > 0)
468 DBG(mod->ctx, "kmod_module %p released\n", mod);
470 kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
471 kmod_module_unref_list(mod->dep);
474 kmod_file_unref(mod->file);
476 kmod_unref(mod->ctx);
487 * Take a reference of the kmod module, incrementing its refcount.
489 * Returns: the passed @module with its refcount incremented.
491 KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
501 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
505 if (*(_list) != NULL) \
510 * kmod_module_new_from_lookup:
511 * @ctx: kmod library context
512 * @given_alias: alias to look for
513 * @list: an empty list where to save the list of modules matching
516 * Create a new list of kmod modules using an alias or module name and lookup
517 * libkmod's configuration files and indexes in order to find the module.
518 * Once it's found in one of the places, it stops searching and create the
519 * list of modules that is saved in @list.
521 * The search order is: 1. aliases in configuration file; 2. module names in
522 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
523 * in modules.alias index.
525 * The initial refcount is 1, and needs to be decremented to release the
526 * resources of the kmod_module. The returned @list must be released by
527 * calling kmod_module_unref_list(). Since libkmod keeps track of all
528 * kmod_modules created, they are all released upon @ctx destruction too. Do
529 * not unref @ctx before all the desired operations with the returned list are
532 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
533 * methods failed, which is basically due to memory allocation fail. If module
534 * is not found, it still returns 0, but @list is an empty list.
536 KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
537 const char *given_alias,
538 struct kmod_list **list)
541 char alias[PATH_MAX];
543 if (ctx == NULL || given_alias == NULL)
546 if (list == NULL || *list != NULL) {
547 ERR(ctx, "An empty list is needed to create lookup\n");
551 if (alias_normalize(given_alias, alias, NULL) < 0) {
552 DBG(ctx, "invalid alias: %s\n", given_alias);
556 DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
558 /* Aliases from config file override all the others */
559 err = kmod_lookup_alias_from_config(ctx, alias, list);
560 CHECK_ERR_AND_FINISH(err, fail, list, finish);
562 DBG(ctx, "lookup modules.dep %s\n", alias);
563 err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
564 CHECK_ERR_AND_FINISH(err, fail, list, finish);
566 DBG(ctx, "lookup modules.symbols %s\n", alias);
567 err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
568 CHECK_ERR_AND_FINISH(err, fail, list, finish);
570 DBG(ctx, "lookup install and remove commands %s\n", alias);
571 err = kmod_lookup_alias_from_commands(ctx, alias, list);
572 CHECK_ERR_AND_FINISH(err, fail, list, finish);
574 DBG(ctx, "lookup modules.aliases %s\n", alias);
575 err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
576 CHECK_ERR_AND_FINISH(err, fail, list, finish);
578 DBG(ctx, "lookup modules.builtin %s\n", alias);
579 err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
580 CHECK_ERR_AND_FINISH(err, fail, list, finish);
583 DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
586 DBG(ctx, "Failed to lookup %s\n", alias);
587 kmod_module_unref_list(*list);
591 #undef CHECK_ERR_AND_FINISH
594 * kmod_module_unref_list:
595 * @list: list of kmod modules
597 * Drop a reference of each kmod module in @list and releases the resources
598 * taken by the list itself.
602 KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
604 for (; list != NULL; list = kmod_list_remove(list))
605 kmod_module_unref(list->data);
611 * kmod_module_get_filtered_blacklist:
612 * @ctx: kmod library context
613 * @input: list of kmod_module to be filtered with blacklist
614 * @output: where to save the new list
616 * This function should not be used. Use kmod_module_apply_filter instead.
618 * Given a list @input, this function filter it out with config's blacklist
619 * and save it in @output.
621 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
624 KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
625 const struct kmod_list *input,
626 struct kmod_list **output)
628 return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
631 static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
633 if (!mod->init.dep) {
635 char *line = kmod_search_moddep(mod->ctx, mod->name);
640 kmod_module_parse_depline((struct kmod_module *)mod, line);
651 * kmod_module_get_dependencies:
654 * Search the modules.dep index to find the dependencies of the given @mod.
655 * The result is cached in @mod, so subsequent calls to this function will
656 * return the already searched list of modules.
658 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
659 * that can be released by calling kmod_module_unref_list().
661 KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
663 struct kmod_list *l, *l_new, *list_new = NULL;
668 module_get_dependencies_noref(mod);
670 kmod_list_foreach(l, mod->dep) {
671 l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
673 kmod_module_unref(l->data);
683 ERR(mod->ctx, "out of memory\n");
684 kmod_module_unref_list(list_new);
689 * kmod_module_get_module:
690 * @entry: an entry in a list of kmod modules.
692 * Get the kmod module of this @entry in the list, increasing its refcount.
693 * After it's used, unref it. Since the refcount is incremented upon return,
694 * you still have to call kmod_module_unref_list() to release the list of kmod
697 * Returns: NULL on failure or the kmod_module contained in this list entry
698 * with its refcount incremented.
700 KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
705 return kmod_module_ref(entry->data);
709 * kmod_module_get_name:
712 * Get the name of this kmod module. Name is always available, independently
713 * if it was created by kmod_module_new_from_name() or another function and
714 * it's always normalized (dashes are replaced with underscores).
716 * Returns: the name of this kmod module.
718 KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
727 * kmod_module_get_path:
730 * Get the path of this kmod module. If this kmod module was not created by
731 * path, it can search the modules.dep index in order to find out the module
732 * under context's dirname.
734 * Returns: the path of this kmod module or NULL if such information is not
737 KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
744 DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
746 if (mod->path != NULL)
752 line = kmod_search_moddep(mod->ctx, mod->name);
756 kmod_module_parse_depline((struct kmod_module *) mod, line);
763 extern long delete_module(const char *name, unsigned int flags);
766 * kmod_module_remove_module:
768 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
769 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
770 * use by a kernel subsystem or other process;
771 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
774 * Remove a module from Linux kernel.
776 * Returns: 0 on success or < 0 on failure.
778 KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
786 /* Filter out other flags and force ONONBLOCK */
787 flags &= KMOD_REMOVE_FORCE;
788 flags |= KMOD_REMOVE_NOWAIT;
790 err = delete_module(mod->name, flags);
793 ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
799 extern long init_module(const void *mem, unsigned long len, const char *args);
802 * kmod_module_insert_module:
804 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
805 * behavior of this function, valid flags are
806 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
807 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
808 * @options: module's options to pass to Linux Kernel.
810 * Insert a module in Linux kernel. It opens the file pointed by @mod,
811 * mmap'ing it and passing to kernel.
813 * Returns: 0 on success or < 0 on failure. If module is already loaded it
816 KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
823 struct kmod_elf *elf;
825 const char *args = options ? options : "";
830 path = kmod_module_get_path(mod);
832 ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
837 mod->file = kmod_file_open(mod->ctx, path);
838 if (mod->file == NULL) {
844 if (kmod_file_get_direct(mod->file)) {
845 unsigned int kernel_flags = 0;
847 if (flags & KMOD_INSERT_FORCE_VERMAGIC)
848 kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
849 if (flags & KMOD_INSERT_FORCE_MODVERSION)
850 kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
852 err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
853 if (err == 0 || errno != ENOSYS)
857 if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
858 elf = kmod_file_get_elf(mod->file);
864 if (flags & KMOD_INSERT_FORCE_MODVERSION) {
865 err = kmod_elf_strip_section(elf, "__versions");
867 INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
870 if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
871 err = kmod_elf_strip_vermagic(elf);
873 INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
876 mem = kmod_elf_get_memory(elf);
878 mem = kmod_file_get_contents(mod->file);
880 size = kmod_file_get_size(mod->file);
882 err = init_module(mem, size, args);
886 INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
891 static bool module_is_blacklisted(struct kmod_module *mod)
893 struct kmod_ctx *ctx = mod->ctx;
894 const struct kmod_config *config = kmod_get_config(ctx);
895 const struct kmod_list *bl = config->blacklists;
896 const struct kmod_list *l;
898 kmod_list_foreach(l, bl) {
899 const char *modname = kmod_blacklist_get_modname(l);
901 if (streq(modname, mod->name))
909 * kmod_module_apply_filter
910 * @ctx: kmod library context
911 * @filter_type: bitmask to filter modules out, valid types are
912 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
913 * KMOD_FILTER_BUILTIN: filter builtin modules out.
914 * @input: list of kmod_module to be filtered
915 * @output: where to save the new list
917 * Given a list @input, this function filter it out by the filter mask
918 * and save it in @output.
920 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
923 KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
924 enum kmod_filter filter_type,
925 const struct kmod_list *input,
926 struct kmod_list **output)
928 const struct kmod_list *li;
930 if (ctx == NULL || output == NULL)
937 kmod_list_foreach(li, input) {
938 struct kmod_module *mod = li->data;
939 struct kmod_list *node;
941 if ((filter_type & KMOD_FILTER_BLACKLIST) &&
942 module_is_blacklisted(mod))
945 if ((filter_type & KMOD_FILTER_BUILTIN)
946 && kmod_module_is_builtin(mod))
949 node = kmod_list_append(*output, mod);
954 kmod_module_ref(mod);
960 kmod_module_unref_list(*output);
965 static int command_do(struct kmod_module *mod, const char *type,
968 const char *modname = kmod_module_get_name(mod);
971 DBG(mod->ctx, "%s %s\n", type, cmd);
973 setenv("MODPROBE_MODULE", modname, 1);
975 unsetenv("MODPROBE_MODULE");
977 if (err == -1 || WEXITSTATUS(err)) {
978 ERR(mod->ctx, "Error running %s command for %s\n",
981 err = -WEXITSTATUS(err);
987 struct probe_insert_cb {
988 int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
992 static int module_do_install_commands(struct kmod_module *mod,
994 struct probe_insert_cb *cb)
996 const char *command = kmod_module_get_install_commands(mod);
998 _cleanup_free_ char *cmd;
1000 size_t cmdlen, options_len, varlen;
1004 if (options == NULL)
1007 options_len = strlen(options);
1008 cmdlen = strlen(command);
1009 varlen = sizeof("$CMDLINE_OPTS") - 1;
1011 cmd = memdup(command, cmdlen + 1);
1015 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
1016 size_t prefixlen = p - cmd;
1017 size_t suffixlen = cmdlen - prefixlen - varlen;
1018 size_t slen = cmdlen - varlen + options_len;
1019 char *suffix = p + varlen;
1020 char *s = malloc(slen + 1);
1024 memcpy(s, cmd, p - cmd);
1025 memcpy(s + prefixlen, options, options_len);
1026 memcpy(s + prefixlen + options_len, suffix, suffixlen);
1034 if (cb->run_install != NULL)
1035 err = cb->run_install(mod, cmd, cb->data);
1037 err = command_do(mod, "install", cmd);
1042 static char *module_options_concat(const char *opt, const char *xopt)
1044 // TODO: we might need to check if xopt overrides options on opt
1045 size_t optlen = opt == NULL ? 0 : strlen(opt);
1046 size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
1049 if (optlen == 0 && xoptlen == 0)
1052 r = malloc(optlen + xoptlen + 2);
1055 memcpy(r, opt, optlen);
1061 memcpy(r + optlen, xopt, xoptlen);
1063 r[optlen + xoptlen] = '\0';
1068 static int __kmod_module_get_probe_list(struct kmod_module *mod,
1071 struct kmod_list **list);
1074 static int __kmod_module_fill_softdep(struct kmod_module *mod,
1075 struct kmod_list **list)
1077 struct kmod_list *pre = NULL, *post = NULL, *l;
1080 err = kmod_module_get_softdeps(mod, &pre, &post);
1082 ERR(mod->ctx, "could not get softdep: %s\n",
1087 kmod_list_foreach(l, pre) {
1088 struct kmod_module *m = l->data;
1089 err = __kmod_module_get_probe_list(m, false, false, list);
1094 l = kmod_list_append(*list, kmod_module_ref(mod));
1096 kmod_module_unref(mod);
1101 mod->ignorecmd = (pre != NULL || post != NULL);
1103 kmod_list_foreach(l, post) {
1104 struct kmod_module *m = l->data;
1105 err = __kmod_module_get_probe_list(m, false, false, list);
1111 kmod_module_unref_list(pre);
1112 kmod_module_unref_list(post);
1118 static int __kmod_module_get_probe_list(struct kmod_module *mod,
1121 struct kmod_list **list)
1123 struct kmod_list *dep, *l;
1127 DBG(mod->ctx, "Ignore module '%s': already visited\n",
1131 mod->visited = true;
1133 dep = kmod_module_get_dependencies(mod);
1136 * Called from kmod_module_probe_insert_module(); set the
1137 * ->required flag on mod and all its dependencies before
1138 * they are possibly visited through some softdeps.
1140 mod->required = true;
1141 kmod_list_foreach(l, dep) {
1142 struct kmod_module *m = l->data;
1147 kmod_list_foreach(l, dep) {
1148 struct kmod_module *m = l->data;
1149 err = __kmod_module_fill_softdep(m, list);
1155 l = kmod_list_append(*list, kmod_module_ref(mod));
1157 kmod_module_unref(mod);
1162 mod->ignorecmd = true;
1164 err = __kmod_module_fill_softdep(mod, list);
1167 kmod_module_unref_list(dep);
1171 static int kmod_module_get_probe_list(struct kmod_module *mod,
1173 struct kmod_list **list)
1177 assert(mod != NULL);
1178 assert(list != NULL && *list == NULL);
1181 * Make sure we don't get screwed by previous calls to this function
1183 kmod_set_modules_visited(mod->ctx, false);
1184 kmod_set_modules_required(mod->ctx, false);
1186 err = __kmod_module_get_probe_list(mod, true, ignorecmd, list);
1188 kmod_module_unref_list(*list);
1196 * kmod_module_probe_insert_module:
1198 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1199 * behavior of this function, valid flags are
1200 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1201 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1202 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1203 * commands and softdeps configured in the system;
1204 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1205 * live in kernel or not;
1206 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1207 * associated callback function;
1208 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1209 * and the module is already live in kernel, the function will fail if this
1210 * flag is specified;
1211 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1212 * filter to this module and its dependencies. If any of the dependencies (or
1213 * the module) is blacklisted, the probe will fail, unless the blacklisted
1214 * module is already live in kernel;
1215 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1216 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1217 * alias and is blacklisted.
1218 * @extra_options: module's options to pass to Linux Kernel. It applies only
1219 * to @mod, not to its dependencies.
1220 * @run_install: function to run when @mod is backed by an install command.
1221 * @data: data to give back to @run_install callback
1222 * @print_action: function to call with the action being taken (install or
1223 * insmod). It's useful for tools like modprobe when running with verbose
1224 * output or in dry-run mode.
1226 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1227 * install commands and applying blacklist.
1229 * If @run_install is NULL, this function will fork and exec by calling
1230 * system(3). Don't pass a NULL argument in @run_install if your binary is
1231 * setuid/setgid (see warning in system(3)). If you need control over the
1232 * execution of an install command, give a callback function instead.
1234 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1237 KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1238 unsigned int flags, const char *extra_options,
1239 int (*run_install)(struct kmod_module *m,
1240 const char *cmd, void *data),
1242 void (*print_action)(struct kmod_module *m,
1244 const char *options))
1246 struct kmod_list *list = NULL, *l;
1247 struct probe_insert_cb cb;
1253 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1254 && module_is_inkernel(mod)) {
1255 if (flags & KMOD_PROBE_FAIL_ON_LOADED)
1262 * Ugly assignement + check. We need to check if we were told to check
1263 * blacklist and also return the reason why we failed.
1264 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1265 * module is an alias, so we also need to check it
1267 if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
1268 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
1269 || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
1270 if (module_is_blacklisted(mod))
1274 err = kmod_module_get_probe_list(mod,
1275 !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
1279 if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1280 struct kmod_list *filtered = NULL;
1282 err = kmod_module_apply_filter(mod->ctx,
1283 KMOD_FILTER_BLACKLIST, list, &filtered);
1287 kmod_module_unref_list(list);
1288 if (filtered == NULL)
1289 return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1294 cb.run_install = run_install;
1295 cb.data = (void *) data;
1297 kmod_list_foreach(l, list) {
1298 struct kmod_module *m = l->data;
1299 const char *moptions = kmod_module_get_options(m);
1300 const char *cmd = kmod_module_get_install_commands(m);
1303 if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1304 && module_is_inkernel(m)) {
1305 DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
1311 options = module_options_concat(moptions,
1312 m == mod ? extra_options : NULL);
1314 if (cmd != NULL && !m->ignorecmd) {
1315 if (print_action != NULL)
1316 print_action(m, true, options ?: "");
1318 if (!(flags & KMOD_PROBE_DRY_RUN))
1319 err = module_do_install_commands(m, options,
1322 if (print_action != NULL)
1323 print_action(m, false, options ?: "");
1325 if (!(flags & KMOD_PROBE_DRY_RUN))
1326 err = kmod_module_insert_module(m, flags,
1334 * Treat "already loaded" error. If we were told to stop on
1335 * already loaded and the module being loaded is not a softdep
1336 * or dep, bail out. Otherwise, just ignore and continue.
1338 * We need to check here because of race conditions. We
1339 * checked first if module was already loaded but it may have
1340 * been loaded between the check and the moment we try to
1343 if (err == -EEXIST && m == mod &&
1344 (flags & KMOD_PROBE_FAIL_ON_LOADED))
1348 * Ignore errors from softdeps
1350 if (err == -EEXIST || !m->required)
1357 kmod_module_unref_list(list);
1362 * kmod_module_get_options:
1365 * Get options of this kmod module. Options come from the configuration file
1366 * and are cached in @mod. The first call to this function will search for
1367 * this module in configuration and subsequent calls return the cached string.
1369 * Returns: a string with all the options separated by spaces. This string is
1370 * owned by @mod, do not free it.
1372 KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1377 if (!mod->init.options) {
1379 struct kmod_module *m = (struct kmod_module *)mod;
1380 const struct kmod_list *l;
1381 const struct kmod_config *config;
1385 config = kmod_get_config(mod->ctx);
1387 kmod_list_foreach(l, config->options) {
1388 const char *modname = kmod_option_get_modname(l);
1393 DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1394 if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1395 streq(modname, mod->alias))))
1398 DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1399 str = kmod_option_get_options(l);
1404 tmp = realloc(opts, optslen + len + 2);
1413 opts[optslen] = ' ';
1417 memcpy(opts + optslen, str, len);
1419 opts[optslen] = '\0';
1422 m->init.options = true;
1426 return mod->options;
1429 ERR(mod->ctx, "out of memory\n");
1434 * kmod_module_get_install_commands:
1437 * Get install commands for this kmod module. Install commands come from the
1438 * configuration file and are cached in @mod. The first call to this function
1439 * will search for this module in configuration and subsequent calls return
1440 * the cached string. The install commands are returned as they were in the
1441 * configuration, concatenated by ';'. No other processing is made in this
1444 * Returns: a string with all install commands separated by semicolons. This
1445 * string is owned by @mod, do not free it.
1447 KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1452 if (!mod->init.install_commands) {
1454 struct kmod_module *m = (struct kmod_module *)mod;
1455 const struct kmod_list *l;
1456 const struct kmod_config *config;
1458 config = kmod_get_config(mod->ctx);
1460 kmod_list_foreach(l, config->install_commands) {
1461 const char *modname = kmod_command_get_modname(l);
1463 if (fnmatch(modname, mod->name, 0) != 0)
1466 m->install_commands = kmod_command_get_command(l);
1469 * find only the first command, as modprobe from
1470 * module-init-tools does
1475 m->init.install_commands = true;
1478 return mod->install_commands;
1481 void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1483 mod->init.install_commands = true;
1484 mod->install_commands = cmd;
1487 static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1489 struct kmod_list *ret = NULL;
1492 for (i = 0; i < count; i++) {
1493 const char *depname = array[i];
1494 struct kmod_list *lst = NULL;
1497 err = kmod_module_new_from_lookup(ctx, depname, &lst);
1499 ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1501 } else if (lst != NULL)
1502 ret = kmod_list_append_list(ret, lst);
1508 * kmod_module_get_softdeps:
1510 * @pre: where to save the list of preceding soft dependencies.
1511 * @post: where to save the list of post soft dependencies.
1513 * Get soft dependencies for this kmod module. Soft dependencies come
1514 * from configuration file and are not cached in @mod because it may include
1515 * dependency cycles that would make we leak kmod_module. Any call
1516 * to this function will search for this module in configuration, allocate a
1517 * list and return the result.
1519 * Both @pre and @post are newly created list of kmod_module and
1520 * should be unreferenced with kmod_module_unref_list().
1522 * Returns: 0 on success or < 0 otherwise.
1524 KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1525 struct kmod_list **pre,
1526 struct kmod_list **post)
1528 const struct kmod_list *l;
1529 const struct kmod_config *config;
1531 if (mod == NULL || pre == NULL || post == NULL)
1534 assert(*pre == NULL);
1535 assert(*post == NULL);
1537 config = kmod_get_config(mod->ctx);
1539 kmod_list_foreach(l, config->softdeps) {
1540 const char *modname = kmod_softdep_get_name(l);
1541 const char * const *array;
1544 if (fnmatch(modname, mod->name, 0) != 0)
1547 array = kmod_softdep_get_pre(l, &count);
1548 *pre = lookup_softdep(mod->ctx, array, count);
1549 array = kmod_softdep_get_post(l, &count);
1550 *post = lookup_softdep(mod->ctx, array, count);
1553 * find only the first command, as modprobe from
1554 * module-init-tools does
1563 * kmod_module_get_remove_commands:
1566 * Get remove commands for this kmod module. Remove commands come from the
1567 * configuration file and are cached in @mod. The first call to this function
1568 * will search for this module in configuration and subsequent calls return
1569 * the cached string. The remove commands are returned as they were in the
1570 * configuration, concatenated by ';'. No other processing is made in this
1573 * Returns: a string with all remove commands separated by semicolons. This
1574 * string is owned by @mod, do not free it.
1576 KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1581 if (!mod->init.remove_commands) {
1583 struct kmod_module *m = (struct kmod_module *)mod;
1584 const struct kmod_list *l;
1585 const struct kmod_config *config;
1587 config = kmod_get_config(mod->ctx);
1589 kmod_list_foreach(l, config->remove_commands) {
1590 const char *modname = kmod_command_get_modname(l);
1592 if (fnmatch(modname, mod->name, 0) != 0)
1595 m->remove_commands = kmod_command_get_command(l);
1598 * find only the first command, as modprobe from
1599 * module-init-tools does
1604 m->init.remove_commands = true;
1607 return mod->remove_commands;
1610 void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1612 mod->init.remove_commands = true;
1613 mod->remove_commands = cmd;
1617 * SECTION:libkmod-loaded
1618 * @short_description: currently loaded modules
1620 * Information about currently loaded modules, as reported by Linux kernel.
1621 * These information are not cached by libkmod and are always read from /sys
1622 * and /proc/modules.
1626 * kmod_module_new_from_loaded:
1627 * @ctx: kmod library context
1628 * @list: where to save the list of loaded modules
1630 * Create a new list of kmod modules with all modules currently loaded in
1631 * kernel. It uses /proc/modules to get the names of loaded modules and to
1632 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1633 * They are put in @list in no particular order.
1635 * The initial refcount is 1, and needs to be decremented to release the
1636 * resources of the kmod_module. The returned @list must be released by
1637 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1638 * kmod_modules created, they are all released upon @ctx destruction too. Do
1639 * not unref @ctx before all the desired operations with the returned list are
1642 * Returns: 0 on success or < 0 on error.
1644 KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1645 struct kmod_list **list)
1647 struct kmod_list *l = NULL;
1651 if (ctx == NULL || list == NULL)
1654 fp = fopen("/proc/modules", "re");
1657 ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1661 while (fgets(line, sizeof(line), fp)) {
1662 struct kmod_module *m;
1663 struct kmod_list *node;
1665 size_t len = strlen(line);
1666 char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1668 err = kmod_module_new_from_name(ctx, name, &m);
1670 ERR(ctx, "could not get module from name '%s': %s\n",
1671 name, strerror(-err));
1675 node = kmod_list_append(l, m);
1679 ERR(ctx, "out of memory\n");
1680 kmod_module_unref(m);
1683 while (line[len - 1] != '\n' && fgets(line, sizeof(line), fp))
1694 * kmod_module_initstate_str:
1695 * @state: the state as returned by kmod_module_get_initstate()
1697 * Translate a initstate to a string.
1699 * Returns: the string associated to the @state. This string is statically
1700 * allocated, do not free it.
1702 KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1705 case KMOD_MODULE_BUILTIN:
1707 case KMOD_MODULE_LIVE:
1709 case KMOD_MODULE_COMING:
1711 case KMOD_MODULE_GOING:
1719 * kmod_module_get_initstate:
1722 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1725 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1726 * KMOD_MODULE_BUILTIN: module is builtin;
1727 * KMOD_MODULE_LIVE: module is live in kernel;
1728 * KMOD_MODULE_COMING: module is being loaded;
1729 * KMOD_MODULE_GOING: module is being unloaded.
1731 KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1733 char path[PATH_MAX], buf[32];
1734 int fd, err, pathlen;
1739 /* remove const: this can only change internal state */
1740 if (kmod_module_is_builtin((struct kmod_module *)mod))
1741 return KMOD_MODULE_BUILTIN;
1743 pathlen = snprintf(path, sizeof(path),
1744 "/sys/module/%s/initstate", mod->name);
1745 fd = open(path, O_RDONLY|O_CLOEXEC);
1749 DBG(mod->ctx, "could not open '%s': %s\n",
1750 path, strerror(-err));
1752 if (pathlen > (int)sizeof("/initstate") - 1) {
1754 path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1755 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1756 return KMOD_MODULE_COMING;
1759 DBG(mod->ctx, "could not open '%s': %s\n",
1760 path, strerror(-err));
1764 err = read_str_safe(fd, buf, sizeof(buf));
1767 ERR(mod->ctx, "could not read from '%s': %s\n",
1768 path, strerror(-err));
1772 if (streq(buf, "live\n"))
1773 return KMOD_MODULE_LIVE;
1774 else if (streq(buf, "coming\n"))
1775 return KMOD_MODULE_COMING;
1776 else if (streq(buf, "going\n"))
1777 return KMOD_MODULE_GOING;
1779 ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1784 * kmod_module_get_size:
1787 * Get the size of this kmod module as returned by Linux kernel. If supported,
1788 * the size is read from the coresize attribute in /sys/module. For older
1789 * kernels, this falls back on /proc/modules and searches for the specified
1790 * module to get its size.
1792 * Returns: the size of this kmod module.
1794 KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1799 long size = -ENOENT;
1805 /* try to open the module dir in /sys. If this fails, don't
1806 * bother trying to find the size as we know the module isn't
1809 snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
1810 dfd = open(line, O_RDONLY|O_CLOEXEC);
1814 /* available as of linux 3.3.x */
1815 cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
1817 if (read_str_long(cfd, &size, 10) < 0)
1818 ERR(mod->ctx, "failed to read coresize from %s\n", line);
1823 /* fall back on parsing /proc/modules */
1824 fp = fopen("/proc/modules", "re");
1828 "could not open /proc/modules: %s\n", strerror(errno));
1833 while (fgets(line, sizeof(line), fp)) {
1834 size_t len = strlen(line);
1835 char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1839 if (tok == NULL || !streq(tok, mod->name))
1842 tok = strtok_r(NULL, " \t", &saveptr);
1845 "invalid line format at /proc/modules:%d\n", lineno);
1849 value = strtol(tok, &endptr, 10);
1850 if (endptr == tok || *endptr != '\0') {
1852 "invalid line format at /proc/modules:%d\n", lineno);
1859 while (line[len - 1] != '\n' && fgets(line, sizeof(line), fp))
1870 * kmod_module_get_refcnt:
1873 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1876 * Returns: the reference count on success or < 0 on failure.
1878 KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1880 char path[PATH_MAX];
1887 snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
1888 fd = open(path, O_RDONLY|O_CLOEXEC);
1891 DBG(mod->ctx, "could not open '%s': %s\n",
1892 path, strerror(errno));
1896 err = read_str_long(fd, &refcnt, 10);
1899 ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1900 path, strerror(-err));
1908 * kmod_module_get_holders:
1911 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1912 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1914 * Returns: a new list of kmod modules on success or NULL on failure.
1916 KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1918 char dname[PATH_MAX];
1919 struct kmod_list *list = NULL;
1920 struct dirent *dent;
1923 if (mod == NULL || mod->ctx == NULL)
1926 snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1930 ERR(mod->ctx, "could not open '%s': %s\n",
1931 dname, strerror(errno));
1935 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
1936 struct kmod_module *holder;
1937 struct kmod_list *l;
1940 if (dent->d_name[0] == '.') {
1941 if (dent->d_name[1] == '\0' ||
1942 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
1946 err = kmod_module_new_from_name(mod->ctx, dent->d_name,
1949 ERR(mod->ctx, "could not create module for '%s': %s\n",
1950 dent->d_name, strerror(-err));
1954 l = kmod_list_append(list, holder);
1958 ERR(mod->ctx, "out of memory\n");
1959 kmod_module_unref(holder);
1969 kmod_module_unref_list(list);
1973 struct kmod_module_section {
1974 unsigned long address;
1978 static void kmod_module_section_free(struct kmod_module_section *section)
1984 * kmod_module_get_sections:
1987 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1988 * structure contained in this list is internal to libkmod and their fields
1989 * can be obtained by calling kmod_module_section_get_name() and
1990 * kmod_module_section_get_address().
1992 * After use, free the @list by calling kmod_module_section_free_list().
1994 * Returns: a new list of kmod module sections on success or NULL on failure.
1996 KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1998 char dname[PATH_MAX];
1999 struct kmod_list *list = NULL;
2000 struct dirent *dent;
2007 snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
2011 ERR(mod->ctx, "could not open '%s': %s\n",
2012 dname, strerror(errno));
2018 for (dent = readdir(d); dent; dent = readdir(d)) {
2019 struct kmod_module_section *section;
2020 struct kmod_list *l;
2021 unsigned long address;
2025 if (dent->d_name[0] == '.') {
2026 if (dent->d_name[1] == '\0' ||
2027 (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
2031 fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
2033 ERR(mod->ctx, "could not open '%s/%s': %m\n",
2034 dname, dent->d_name);
2038 err = read_str_ulong(fd, &address, 16);
2041 ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
2042 dname, dent->d_name);
2046 namesz = strlen(dent->d_name) + 1;
2047 section = malloc(sizeof(*section) + namesz);
2049 if (section == NULL) {
2050 ERR(mod->ctx, "out of memory\n");
2054 section->address = address;
2055 memcpy(section->name, dent->d_name, namesz);
2057 l = kmod_list_append(list, section);
2061 ERR(mod->ctx, "out of memory\n");
2072 kmod_module_unref_list(list);
2077 * kmod_module_section_get_module_name:
2078 * @entry: a list entry representing a kmod module section
2080 * Get the name of a kmod module section.
2082 * After use, free the @list by calling kmod_module_section_free_list().
2084 * Returns: the name of this kmod module section on success or NULL on
2085 * failure. The string is owned by the section, do not free it.
2087 KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
2089 struct kmod_module_section *section;
2094 section = entry->data;
2095 return section->name;
2099 * kmod_module_section_get_address:
2100 * @entry: a list entry representing a kmod module section
2102 * Get the address of a kmod module section.
2104 * After use, free the @list by calling kmod_module_section_free_list().
2106 * Returns: the address of this kmod module section on success or ULONG_MAX
2109 KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
2111 struct kmod_module_section *section;
2114 return (unsigned long)-1;
2116 section = entry->data;
2117 return section->address;
2121 * kmod_module_section_free_list:
2122 * @list: kmod module section list
2124 * Release the resources taken by @list
2126 KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
2129 kmod_module_section_free(list->data);
2130 list = kmod_list_remove(list);
2134 static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
2136 if (mod->file == NULL) {
2137 const char *path = kmod_module_get_path(mod);
2144 ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
2146 if (mod->file == NULL)
2150 return kmod_file_get_elf(mod->file);
2153 struct kmod_module_info {
2158 static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
2160 struct kmod_module_info *info;
2162 info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
2166 info->key = (char *)info + sizeof(struct kmod_module_info)
2168 memcpy(info->key, key, keylen);
2169 info->key[keylen] = '\0';
2170 memcpy(info->value, value, valuelen);
2171 info->value[valuelen] = '\0';
2175 static void kmod_module_info_free(struct kmod_module_info *info)
2180 static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
2182 struct kmod_module_info *info;
2183 struct kmod_list *n;
2185 info = kmod_module_info_new(key, keylen, value, valuelen);
2188 n = kmod_list_append(*list, info);
2192 kmod_module_info_free(info);
2196 static char *kmod_module_hex_to_str(const char *hex, size_t len)
2201 const size_t line_limit = 20;
2204 str_len = len * 3; /* XX: or XX\0 */
2205 str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */
2207 str = malloc(str_len);
2211 for (i = 0, j = 0; i < (int)len; i++) {
2212 j += sprintf(str + j, "%02X", (unsigned char)hex[i]);
2213 if (i < (int)len - 1) {
2216 if ((i + 1) % line_limit == 0)
2217 j += sprintf(str + j, "\n\t\t");
2223 static struct kmod_list *kmod_module_info_append_hex(struct kmod_list **list,
2230 struct kmod_list *n;
2233 /* Display as 01:12:DE:AD:BE:EF:... */
2234 hex = kmod_module_hex_to_str(value, valuelen);
2237 n = kmod_module_info_append(list, key, keylen, hex, strlen(hex));
2242 n = kmod_module_info_append(list, key, keylen, NULL, 0);
2254 * kmod_module_get_info:
2256 * @list: where to return list of module information. Use
2257 * kmod_module_info_get_key() and
2258 * kmod_module_info_get_value(). Release this list with
2259 * kmod_module_info_free_list()
2261 * Get a list of entries in ELF section ".modinfo", these contain
2262 * alias, license, depends, vermagic and other keys with respective
2263 * values. If the module is signed (CONFIG_MODULE_SIG), information
2264 * about the module signature is included as well: signer,
2265 * sig_key and sig_hashalgo.
2267 * After use, free the @list by calling kmod_module_info_free_list().
2269 * Returns: 0 on success or < 0 otherwise.
2271 KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2273 struct kmod_elf *elf;
2275 int i, count, ret = -ENOMEM;
2276 struct kmod_signature_info sig_info;
2278 if (mod == NULL || list == NULL)
2281 assert(*list == NULL);
2283 elf = kmod_module_get_elf(mod);
2287 count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2291 for (i = 0; i < count; i++) {
2292 struct kmod_list *n;
2293 const char *key, *value;
2294 size_t keylen, valuelen;
2297 value = strchr(key, '=');
2298 if (value == NULL) {
2299 keylen = strlen(key);
2303 keylen = value - key;
2305 valuelen = strlen(value);
2308 n = kmod_module_info_append(list, key, keylen, value, valuelen);
2313 if (kmod_module_signature_info(mod->file, &sig_info)) {
2314 struct kmod_list *n;
2316 n = kmod_module_info_append(list, "sig_id", strlen("sig_id"),
2317 sig_info.id_type, strlen(sig_info.id_type));
2322 n = kmod_module_info_append(list, "signer", strlen("signer"),
2323 sig_info.signer, sig_info.signer_len);
2329 n = kmod_module_info_append_hex(list, "sig_key", strlen("sig_key"),
2331 sig_info.key_id_len);
2336 n = kmod_module_info_append(list,
2337 "sig_hashalgo", strlen("sig_hashalgo"),
2338 sig_info.hash_algo, strlen(sig_info.hash_algo));
2344 * Omit sig_info.algo for now, as these
2345 * are currently constant.
2347 n = kmod_module_info_append_hex(list, "signature",
2348 strlen("signature"),
2361 kmod_module_info_free_list(*list);
2369 * kmod_module_info_get_key:
2370 * @entry: a list entry representing a kmod module info
2372 * Get the key of a kmod module info.
2374 * Returns: the key of this kmod module info on success or NULL on
2375 * failure. The string is owned by the info, do not free it.
2377 KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2379 struct kmod_module_info *info;
2389 * kmod_module_info_get_value:
2390 * @entry: a list entry representing a kmod module info
2392 * Get the value of a kmod module info.
2394 * Returns: the value of this kmod module info on success or NULL on
2395 * failure. The string is owned by the info, do not free it.
2397 KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2399 struct kmod_module_info *info;
2409 * kmod_module_info_free_list:
2410 * @list: kmod module info list
2412 * Release the resources taken by @list
2414 KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2417 kmod_module_info_free(list->data);
2418 list = kmod_list_remove(list);
2422 struct kmod_module_version {
2427 static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2429 struct kmod_module_version *mv;
2430 size_t symbollen = strlen(symbol) + 1;
2432 mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2437 memcpy(mv->symbol, symbol, symbollen);
2441 static void kmod_module_version_free(struct kmod_module_version *version)
2447 * kmod_module_get_versions:
2449 * @list: where to return list of module versions. Use
2450 * kmod_module_version_get_symbol() and
2451 * kmod_module_version_get_crc(). Release this list with
2452 * kmod_module_versions_free_list()
2454 * Get a list of entries in ELF section "__versions".
2456 * After use, free the @list by calling kmod_module_versions_free_list().
2458 * Returns: 0 on success or < 0 otherwise.
2460 KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2462 struct kmod_elf *elf;
2463 struct kmod_modversion *versions;
2464 int i, count, ret = 0;
2466 if (mod == NULL || list == NULL)
2469 assert(*list == NULL);
2471 elf = kmod_module_get_elf(mod);
2475 count = kmod_elf_get_modversions(elf, &versions);
2479 for (i = 0; i < count; i++) {
2480 struct kmod_module_version *mv;
2481 struct kmod_list *n;
2483 mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2486 kmod_module_versions_free_list(*list);
2491 n = kmod_list_append(*list, mv);
2495 kmod_module_version_free(mv);
2496 kmod_module_versions_free_list(*list);
2510 * kmod_module_version_get_symbol:
2511 * @entry: a list entry representing a kmod module versions
2513 * Get the symbol of a kmod module versions.
2515 * Returns: the symbol of this kmod module versions on success or NULL
2516 * on failure. The string is owned by the versions, do not free it.
2518 KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2520 struct kmod_module_version *version;
2522 if (entry == NULL || entry->data == NULL)
2525 version = entry->data;
2526 return version->symbol;
2530 * kmod_module_version_get_crc:
2531 * @entry: a list entry representing a kmod module version
2533 * Get the crc of a kmod module version.
2535 * Returns: the crc of this kmod module version if available, otherwise default to 0.
2537 KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2539 struct kmod_module_version *version;
2541 if (entry == NULL || entry->data == NULL)
2544 version = entry->data;
2545 return version->crc;
2549 * kmod_module_versions_free_list:
2550 * @list: kmod module versions list
2552 * Release the resources taken by @list
2554 KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2557 kmod_module_version_free(list->data);
2558 list = kmod_list_remove(list);
2562 struct kmod_module_symbol {
2567 static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2569 struct kmod_module_symbol *mv;
2570 size_t symbollen = strlen(symbol) + 1;
2572 mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2577 memcpy(mv->symbol, symbol, symbollen);
2581 static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2587 * kmod_module_get_symbols:
2589 * @list: where to return list of module symbols. Use
2590 * kmod_module_symbol_get_symbol() and
2591 * kmod_module_symbol_get_crc(). Release this list with
2592 * kmod_module_symbols_free_list()
2594 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2596 * After use, free the @list by calling kmod_module_symbols_free_list().
2598 * Returns: 0 on success or < 0 otherwise.
2600 KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2602 struct kmod_elf *elf;
2603 struct kmod_modversion *symbols;
2604 int i, count, ret = 0;
2606 if (mod == NULL || list == NULL)
2609 assert(*list == NULL);
2611 elf = kmod_module_get_elf(mod);
2615 count = kmod_elf_get_symbols(elf, &symbols);
2619 for (i = 0; i < count; i++) {
2620 struct kmod_module_symbol *mv;
2621 struct kmod_list *n;
2623 mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2626 kmod_module_symbols_free_list(*list);
2631 n = kmod_list_append(*list, mv);
2635 kmod_module_symbol_free(mv);
2636 kmod_module_symbols_free_list(*list);
2650 * kmod_module_symbol_get_symbol:
2651 * @entry: a list entry representing a kmod module symbols
2653 * Get the symbol of a kmod module symbols.
2655 * Returns: the symbol of this kmod module symbols on success or NULL
2656 * on failure. The string is owned by the symbols, do not free it.
2658 KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2660 struct kmod_module_symbol *symbol;
2662 if (entry == NULL || entry->data == NULL)
2665 symbol = entry->data;
2666 return symbol->symbol;
2670 * kmod_module_symbol_get_crc:
2671 * @entry: a list entry representing a kmod module symbol
2673 * Get the crc of a kmod module symbol.
2675 * Returns: the crc of this kmod module symbol if available, otherwise default to 0.
2677 KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2679 struct kmod_module_symbol *symbol;
2681 if (entry == NULL || entry->data == NULL)
2684 symbol = entry->data;
2689 * kmod_module_symbols_free_list:
2690 * @list: kmod module symbols list
2692 * Release the resources taken by @list
2694 KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2697 kmod_module_symbol_free(list->data);
2698 list = kmod_list_remove(list);
2702 struct kmod_module_dependency_symbol {
2708 static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2710 struct kmod_module_dependency_symbol *mv;
2711 size_t symbollen = strlen(symbol) + 1;
2713 mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2719 memcpy(mv->symbol, symbol, symbollen);
2723 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2725 free(dependency_symbol);
2729 * kmod_module_get_dependency_symbols:
2731 * @list: where to return list of module dependency_symbols. Use
2732 * kmod_module_dependency_symbol_get_symbol() and
2733 * kmod_module_dependency_symbol_get_crc(). Release this list with
2734 * kmod_module_dependency_symbols_free_list()
2736 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2738 * After use, free the @list by calling
2739 * kmod_module_dependency_symbols_free_list().
2741 * Returns: 0 on success or < 0 otherwise.
2743 KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2745 struct kmod_elf *elf;
2746 struct kmod_modversion *symbols;
2747 int i, count, ret = 0;
2749 if (mod == NULL || list == NULL)
2752 assert(*list == NULL);
2754 elf = kmod_module_get_elf(mod);
2758 count = kmod_elf_get_dependency_symbols(elf, &symbols);
2762 for (i = 0; i < count; i++) {
2763 struct kmod_module_dependency_symbol *mv;
2764 struct kmod_list *n;
2766 mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2771 kmod_module_dependency_symbols_free_list(*list);
2776 n = kmod_list_append(*list, mv);
2780 kmod_module_dependency_symbol_free(mv);
2781 kmod_module_dependency_symbols_free_list(*list);
2795 * kmod_module_dependency_symbol_get_symbol:
2796 * @entry: a list entry representing a kmod module dependency_symbols
2798 * Get the dependency symbol of a kmod module
2800 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2801 * on failure. The string is owned by the dependency_symbols, do not free it.
2803 KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2805 struct kmod_module_dependency_symbol *dependency_symbol;
2807 if (entry == NULL || entry->data == NULL)
2810 dependency_symbol = entry->data;
2811 return dependency_symbol->symbol;
2815 * kmod_module_dependency_symbol_get_crc:
2816 * @entry: a list entry representing a kmod module dependency_symbol
2818 * Get the crc of a kmod module dependency_symbol.
2820 * Returns: the crc of this kmod module dependency_symbol if available, otherwise default to 0.
2822 KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2824 struct kmod_module_dependency_symbol *dependency_symbol;
2826 if (entry == NULL || entry->data == NULL)
2829 dependency_symbol = entry->data;
2830 return dependency_symbol->crc;
2834 * kmod_module_dependency_symbol_get_bind:
2835 * @entry: a list entry representing a kmod module dependency_symbol
2837 * Get the bind type of a kmod module dependency_symbol.
2839 * Returns: the bind of this kmod module dependency_symbol on success
2840 * or < 0 on failure.
2842 KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2844 struct kmod_module_dependency_symbol *dependency_symbol;
2846 if (entry == NULL || entry->data == NULL)
2849 dependency_symbol = entry->data;
2850 return dependency_symbol->bind;
2854 * kmod_module_dependency_symbols_free_list:
2855 * @list: kmod module dependency_symbols list
2857 * Release the resources taken by @list
2859 KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2862 kmod_module_dependency_symbol_free(list->data);
2863 list = kmod_list_remove(list);