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
34 #include <sys/types.h>
41 #include "libkmod-private.h"
44 * SECTION:libkmod-module
45 * @short_description: operate on kernel modules
51 * Opaque object representing a module.
58 struct kmod_list
*dep
;
60 const char *install_commands
; /* owned by kmod_config */
61 const char *remove_commands
; /* owned by kmod_config */
62 char *alias
; /* only set if this module was created from an alias */
68 bool install_commands
: 1;
69 bool remove_commands
: 1;
73 * private field used by kmod_module_get_probe_list() to detect
79 * set by kmod_module_get_probe_list: indicates for probe_insert()
80 * whether the module's command and softdep should be ignored
85 static inline const char *path_join(const char *path
, size_t prefixlen
,
93 pathlen
= strlen(path
);
94 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
97 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
101 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
103 struct kmod_ctx
*ctx
= mod
->ctx
;
104 struct kmod_list
*list
= NULL
;
113 assert(mod
->dep
== NULL
);
114 mod
->init
.dep
= true;
116 p
= strchr(line
, ':');
121 dirname
= kmod_get_dirname(mod
->ctx
);
122 dirnamelen
= strlen(dirname
);
123 if (dirnamelen
+ 2 >= PATH_MAX
)
126 memcpy(buf
, dirname
, dirnamelen
);
127 buf
[dirnamelen
] = '/';
129 buf
[dirnamelen
] = '\0';
131 if (mod
->path
== NULL
) {
132 const char *str
= path_join(line
, dirnamelen
, buf
);
135 mod
->path
= strdup(str
);
136 if (mod
->path
== NULL
)
141 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
142 p
= strtok_r(NULL
, " \t", &saveptr
)) {
143 struct kmod_module
*depmod
;
146 path
= path_join(p
, dirnamelen
, buf
);
148 ERR(ctx
, "could not join path '%s' and '%s'.\n",
153 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
155 ERR(ctx
, "ctx=%p path=%s error=%s\n",
156 ctx
, path
, strerror(-err
));
160 DBG(ctx
, "add dep: %s\n", path
);
162 list
= kmod_list_prepend(list
, depmod
);
166 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
173 kmod_module_unref_list(list
);
174 mod
->init
.dep
= false;
178 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
180 mod
->visited
= visited
;
184 * Memory layout with alias:
186 * struct kmod_module {
191 * name <----------' | |
192 * alias <-----------' |
193 * name\alias <--------'
195 * Memory layout without alias:
197 * struct kmod_module {
199 * alias -----|----> NULL
202 * name <----------'-'
204 * @key is "name\alias" or "name" (in which case alias == NULL)
206 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
207 const char *name
, size_t namelen
,
208 const char *alias
, size_t aliaslen
,
209 struct kmod_module
**mod
)
211 struct kmod_module
*m
;
214 m
= kmod_pool_get_module(ctx
, key
);
216 *mod
= kmod_module_ref(m
);
223 keylen
= namelen
+ aliaslen
+ 1;
225 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
231 memset(m
, 0, sizeof(*m
));
233 m
->ctx
= kmod_ref(ctx
);
234 m
->name
= (char *)m
+ sizeof(*m
);
235 memcpy(m
->name
, key
, keylen
+ 1);
237 m
->hashkey
= m
->name
;
240 m
->name
[namelen
] = '\0';
241 m
->alias
= m
->name
+ namelen
+ 1;
242 m
->hashkey
= m
->name
+ keylen
+ 1;
243 memcpy(m
->hashkey
, key
, keylen
+ 1);
247 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
254 * kmod_module_new_from_name:
255 * @ctx: kmod library context
256 * @name: name of the module
257 * @mod: where to save the created struct kmod_module
259 * Create a new struct kmod_module using the module name. @name can not be an
260 * alias, file name or anything else; it must be a module name. There's no
261 * check if the module does exists in the system.
263 * This function is also used internally by many others that return a new
264 * struct kmod_module or a new list of modules.
266 * The initial refcount is 1, and needs to be decremented to release the
267 * resources of the kmod_module. Since libkmod keeps track of all
268 * kmod_modules created, they are all released upon @ctx destruction too. Do
269 * not unref @ctx before all the desired operations with the returned
270 * kmod_module are done.
272 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
273 * module name or if memory allocation failed.
275 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
277 struct kmod_module
**mod
)
280 char name_norm
[PATH_MAX
];
282 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
285 modname_normalize(name
, name_norm
, &namelen
);
287 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
290 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
291 const char *name
, struct kmod_module
**mod
)
295 size_t namelen
= strlen(name
);
296 size_t aliaslen
= strlen(alias
);
298 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
299 return -ENAMETOOLONG
;
301 memcpy(key
, name
, namelen
);
302 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
305 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
313 * kmod_module_new_from_path:
314 * @ctx: kmod library context
315 * @path: path where to find the given module
316 * @mod: where to save the created struct kmod_module
318 * Create a new struct kmod_module using the module path. @path must be an
319 * existent file with in the filesystem and must be accessible to libkmod.
321 * The initial refcount is 1, and needs to be decremented to release the
322 * resources of the kmod_module. Since libkmod keeps track of all
323 * kmod_modules created, they are all released upon @ctx destruction too. Do
324 * not unref @ctx before all the desired operations with the returned
325 * kmod_module are done.
327 * If @path is relative, it's treated as relative to the current working
328 * directory. Otherwise, give an absolute path.
330 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
331 * it's not a valid file for a kmod_module or if memory allocation failed.
333 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
335 struct kmod_module
**mod
)
337 struct kmod_module
*m
;
344 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
347 abspath
= path_make_absolute_cwd(path
);
348 if (abspath
== NULL
) {
349 DBG(ctx
, "no absolute path for %s\n", path
);
353 err
= stat(abspath
, &st
);
356 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
361 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
362 DBG(ctx
, "could not get modname from path %s\n", path
);
367 m
= kmod_pool_get_module(ctx
, name
);
371 else if (streq(m
->path
, abspath
))
374 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
375 name
, abspath
, m
->path
);
380 *mod
= kmod_module_ref(m
);
384 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
398 * Drop a reference of the kmod module. If the refcount reaches zero, its
399 * resources are released.
401 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
402 * returns the passed @mod with its refcount decremented.
404 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
409 if (--mod
->refcount
> 0)
412 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
414 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
415 kmod_module_unref_list(mod
->dep
);
416 kmod_unref(mod
->ctx
);
427 * Take a reference of the kmod module, incrementing its refcount.
429 * Returns: the passed @module with its refcount incremented.
431 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
441 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
445 if (*(_list) != NULL) \
450 * kmod_module_new_from_lookup:
451 * @ctx: kmod library context
452 * @given_alias: alias to look for
453 * @list: an empty list where to save the list of modules matching
456 * Create a new list of kmod modules using an alias or module name and lookup
457 * libkmod's configuration files and indexes in order to find the module.
458 * Once it's found in one of the places, it stops searching and create the
459 * list of modules that is saved in @list.
461 * The search order is: 1. aliases in configuration file; 2. module names in
462 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
463 * in modules.alias index.
465 * The initial refcount is 1, and needs to be decremented to release the
466 * resources of the kmod_module. The returned @list must be released by
467 * calling kmod_module_unref_list(). Since libkmod keeps track of all
468 * kmod_modules created, they are all released upon @ctx destruction too. Do
469 * not unref @ctx before all the desired operations with the returned list are
472 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
473 * methods failed, which is basically due to memory allocation fail. If module
474 * is not found, it still returns 0, but @list is an empty list.
476 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
477 const char *given_alias
,
478 struct kmod_list
**list
)
481 char alias
[PATH_MAX
];
483 if (ctx
== NULL
|| given_alias
== NULL
)
486 if (list
== NULL
|| *list
!= NULL
) {
487 ERR(ctx
, "An empty list is needed to create lookup\n");
491 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
492 DBG(ctx
, "invalid alias: %s\n", given_alias
);
496 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
498 /* Aliases from config file override all the others */
499 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
500 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
502 DBG(ctx
, "lookup modules.dep %s\n", alias
);
503 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
504 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
506 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
507 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
508 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
510 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
511 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
512 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
514 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
515 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
516 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
519 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
522 DBG(ctx
, "Failed to lookup %s\n", alias
);
523 kmod_module_unref_list(*list
);
527 #undef CHECK_ERR_AND_FINISH
530 * kmod_module_unref_list:
531 * @list: list of kmod modules
533 * Drop a reference of each kmod module in @list and releases the resources
534 * taken by the list itself.
536 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
537 * returns the passed @mod with its refcount decremented.
539 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
541 for (; list
!= NULL
; list
= kmod_list_remove(list
))
542 kmod_module_unref(list
->data
);
548 * kmod_module_get_filtered_blacklist:
549 * @ctx: kmod library context
550 * @input: list of kmod_module to be filtered with blacklist
551 * @output: where to save the new list
553 * Given a list @input, this function filter it out with config's blacklist
554 * ans save it in @output.
556 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
559 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
560 const struct kmod_list
*input
,
561 struct kmod_list
**output
)
563 const struct kmod_list
*li
;
564 const struct kmod_list
*blacklist
;
566 if (ctx
== NULL
|| output
== NULL
)
573 blacklist
= kmod_get_blacklists(ctx
);
574 kmod_list_foreach(li
, input
) {
575 struct kmod_module
*mod
= li
->data
;
576 const struct kmod_list
*lb
;
577 struct kmod_list
*node
;
578 bool filtered
= false;
580 kmod_list_foreach(lb
, blacklist
) {
581 const char *name
= lb
->data
;
583 if (streq(name
, mod
->name
)) {
592 node
= kmod_list_append(*output
, mod
);
597 kmod_module_ref(mod
);
603 kmod_module_unref_list(*output
);
608 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
610 if (!mod
->init
.dep
) {
612 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
617 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
628 * kmod_module_get_dependencies:
631 * Search the modules.dep index to find the dependencies of the given @mod.
632 * The result is cached in @mod, so subsequent calls to this function will
633 * return the already searched list of modules.
635 * Returns: NULL on failure or if there are any dependencies. Otherwise it
636 * returns a list of kmod modules that can be released by calling
637 * kmod_module_unref_list().
639 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
641 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
646 module_get_dependencies_noref(mod
);
648 kmod_list_foreach(l
, mod
->dep
) {
649 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
651 kmod_module_unref(l
->data
);
661 ERR(mod
->ctx
, "out of memory\n");
662 kmod_module_unref_list(list_new
);
667 * kmod_module_get_module:
668 * @entry: an entry in a list of kmod modules.
670 * Get the kmod module of this @entry in the list, increasing its refcount.
671 * After it's used, unref it. Since the refcount is incremented upon return,
672 * you still have to call kmod_module_unref_list() to release the list of kmod
675 * Returns: NULL on failure or the kmod_module contained in this list entry
676 * with its refcount incremented.
678 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
683 return kmod_module_ref(entry
->data
);
687 * kmod_module_get_name:
690 * Get the name of this kmod module. Name is always available, independently
691 * if it was created by kmod_module_new_from_name() or another function and
692 * it's always normalized (dashes are replaced with underscores).
694 * Returns: the name of this kmod module.
696 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
705 * kmod_module_get_path:
708 * Get the path of this kmod module. If this kmod module was not created by
709 * path, it can search the modules.dep index in order to find out the module
710 * under context's dirname.
712 * Returns: the path of this kmod module or NULL if such information is not
715 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
722 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
724 if (mod
->path
!= NULL
)
730 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
734 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
741 extern long delete_module(const char *name
, unsigned int flags
);
744 * kmod_module_remove_module:
746 * @flags: flags to pass to Linux kernel when removing the module
748 * Remove a module from Linux kernel.
750 * Returns: 0 on success or < 0 on failure.
752 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
760 /* Filter out other flags */
761 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
763 err
= delete_module(mod
->name
, flags
);
766 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
772 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
775 * kmod_module_insert_module:
777 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
778 * behavior of this function.
779 * @options: module's options to pass to Linux Kernel.
781 * Insert a module in Linux kernel. It opens the file pointed by @mod,
782 * mmap'ing it and passing to kernel.
784 * Returns: 0 on success or < 0 on failure. If module is already loaded it
787 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
794 struct kmod_file
*file
;
795 struct kmod_elf
*elf
= NULL
;
797 const char *args
= options
? options
: "";
802 path
= kmod_module_get_path(mod
);
804 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
808 file
= kmod_file_open(mod
->ctx
, path
);
814 size
= kmod_file_get_size(file
);
815 mem
= kmod_file_get_contents(file
);
817 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
818 elf
= kmod_elf_new(mem
, size
);
824 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
825 err
= kmod_elf_strip_section(elf
, "__versions");
827 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
830 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
831 err
= kmod_elf_strip_vermagic(elf
);
833 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
836 mem
= kmod_elf_get_memory(elf
);
839 err
= init_module(mem
, size
, args
);
842 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
848 kmod_file_unref(file
);
853 static bool module_is_blacklisted(struct kmod_module
*mod
)
855 struct kmod_ctx
*ctx
= mod
->ctx
;
856 const struct kmod_list
*bl
= kmod_get_blacklists(ctx
);
857 const struct kmod_list
*l
;
859 kmod_list_foreach(l
, bl
) {
860 const char *modname
= kmod_blacklist_get_modname(l
);
862 if (streq(modname
, mod
->name
))
869 static int command_do(struct kmod_module
*mod
, const char *type
,
872 const char *modname
= kmod_module_get_name(mod
);
875 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
877 setenv("MODPROBE_MODULE", modname
, 1);
879 unsetenv("MODPROBE_MODULE");
881 if (err
== -1 || WEXITSTATUS(err
)) {
882 ERR(mod
->ctx
, "Error running %s command for %s\n",
885 err
= -WEXITSTATUS(err
);
891 struct probe_insert_cb
{
892 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
896 static int module_do_install_commands(struct kmod_module
*mod
,
898 struct probe_insert_cb
*cb
)
900 const char *command
= kmod_module_get_install_commands(mod
);
903 size_t cmdlen
, options_len
, varlen
;
910 options_len
= strlen(options
);
911 cmdlen
= strlen(command
);
912 varlen
= sizeof("$CMDLINE_OPTS") - 1;
914 cmd
= memdup(command
, cmdlen
+ 1);
918 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
919 size_t prefixlen
= p
- cmd
;
920 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
921 size_t slen
= cmdlen
- varlen
+ options_len
;
922 char *suffix
= p
+ varlen
;
923 char *s
= malloc(slen
+ 1);
928 memcpy(s
, cmd
, p
- cmd
);
929 memcpy(s
+ prefixlen
, options
, options_len
);
930 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
938 if (cb
->run_install
!= NULL
)
939 err
= cb
->run_install(mod
, cmd
, cb
->data
);
941 err
= command_do(mod
, "install", cmd
);
948 static char *module_options_concat(const char *opt
, const char *xopt
)
950 // TODO: we might need to check if xopt overrides options on opt
951 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
952 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
955 if (optlen
== 0 && xoptlen
== 0)
958 r
= malloc(optlen
+ xoptlen
+ 2);
961 memcpy(r
, opt
, optlen
);
967 memcpy(r
+ optlen
, xopt
, xoptlen
);
969 r
[optlen
+ xoptlen
] = '\0';
974 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
976 struct kmod_list
**list
);
979 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
980 struct kmod_list
**list
)
982 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
985 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
987 ERR(mod
->ctx
, "could not get softdep: %s", strerror(-err
));
991 kmod_list_foreach(l
, pre
) {
992 struct kmod_module
*m
= l
->data
;
993 err
= __kmod_module_get_probe_list(m
, false, list
);
998 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1000 kmod_module_unref(mod
);
1005 mod
->visited
= true;
1006 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1008 kmod_list_foreach(l
, post
) {
1009 struct kmod_module
*m
= l
->data
;
1010 err
= __kmod_module_get_probe_list(m
, false, list
);
1016 kmod_module_unref_list(pre
);
1017 kmod_module_unref_list(post
);
1023 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1025 struct kmod_list
**list
)
1027 struct kmod_list
*dep
, *l
;
1031 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1036 dep
= kmod_module_get_dependencies(mod
);
1037 kmod_list_foreach(l
, dep
) {
1038 struct kmod_module
*m
= l
->data
;
1039 err
= __kmod_module_fill_softdep(m
, list
);
1045 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1047 kmod_module_unref(mod
);
1052 mod
->ignorecmd
= true;
1054 err
= __kmod_module_fill_softdep(mod
, list
);
1057 kmod_module_unref_list(dep
);
1061 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1063 struct kmod_list
**list
)
1067 assert(mod
!= NULL
);
1068 assert(list
!= NULL
&& *list
== NULL
);
1071 * Make sure we don't get screwed by previous calls to this function
1073 kmod_set_modules_visited(mod
->ctx
, false);
1075 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1077 kmod_module_unref_list(*list
);
1085 * kmod_module_probe_insert_module:
1087 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1088 * behavior of this function.
1089 * @extra_options: module's options to pass to Linux Kernel. It applies only
1090 * to @mod, not to its dependencies.
1091 * @run_install: function to run when @mod is backed by an install command.
1092 * @data: data to give back to @run_install callback
1094 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1095 * install commands and applying blacklist.
1097 * If @run_install is NULL, and the flag KMOD_PROBE_STOP_ON_COMMANDS is not
1098 * given, this function will fork and exec by calling system(3). Don't pass a
1099 * NULL argument in @run_install if your binary is setuid/setgid (see warning
1100 * in system(3)). If you need control over the execution of an install
1101 * command, give a callback function instead.
1103 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1106 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1107 unsigned int flags
, const char *extra_options
,
1108 int (*run_install
)(struct kmod_module
*m
,
1109 const char *cmd
, void *data
),
1112 struct kmod_list
*list
= NULL
, *l
;
1113 struct probe_insert_cb cb
;
1119 err
= flags
& (KMOD_PROBE_APPLY_BLACKLIST
|
1120 KMOD_PROBE_APPLY_BLACKLIST_ALL
);
1122 if (module_is_blacklisted(mod
))
1126 err
= kmod_module_get_probe_list(mod
,
1127 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1131 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1132 struct kmod_list
*filtered
= NULL
;
1134 err
= kmod_module_get_filtered_blacklist(mod
->ctx
,
1139 kmod_module_unref_list(list
);
1140 if (filtered
== NULL
)
1141 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1146 cb
.run_install
= run_install
;
1147 cb
.data
= (void *) data
;
1149 kmod_list_foreach(l
, list
) {
1150 struct kmod_module
*m
= l
->data
;
1151 const char *moptions
= kmod_module_get_options(m
);
1152 const char *cmd
= kmod_module_get_install_commands(m
);
1153 char *options
= module_options_concat(moptions
,
1154 m
== mod
? extra_options
: NULL
);
1156 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1157 if (flags
& KMOD_PROBE_STOP_ON_COMMAND
) {
1158 DBG(mod
->ctx
, "Stopping on '%s': "
1159 "install command\n", m
->name
);
1160 err
= KMOD_PROBE_STOP_ON_COMMAND
;
1164 err
= module_do_install_commands(m
, options
, &cb
);
1166 int state
= kmod_module_get_initstate(m
);
1168 if (state
== KMOD_MODULE_LIVE
||
1169 state
== KMOD_MODULE_COMING
||
1170 state
== KMOD_MODULE_BUILTIN
) {
1171 if (m
== mod
&& (flags
& KMOD_PROBE_STOP_ON_ALREADY_LOADED
)) {
1172 err
= KMOD_PROBE_STOP_ON_ALREADY_LOADED
;
1176 DBG(mod
->ctx
, "Ignoring '%s': "
1177 "module already loaded\n", m
->name
);
1181 err
= kmod_module_insert_module(m
, flags
, options
);
1187 * Treat "already loaded" error. If we were told to stop on
1188 * already loaded and the module being loaded is not a
1189 * softdep, bail out. Otherwise, just ignore and continue.
1191 * We need to check here because of race conditions. We
1192 * checked first if module was already loaded but it may have
1193 * been loaded between the check and the moment we try to
1196 if (err
== -EEXIST
&& m
== mod
&&
1197 (flags
& KMOD_PROBE_STOP_ON_ALREADY_LOADED
)) {
1198 err
= KMOD_PROBE_STOP_ON_ALREADY_LOADED
;
1202 if (err
< 0 && err
!= -EEXIST
)
1206 kmod_module_unref_list(list
);
1211 * kmod_module_get_options:
1214 * Get options of this kmod module. Options come from the configuration file
1215 * and are cached in @mod. The first call to this function will search for
1216 * this module in configuration and subsequent calls return the cached string.
1218 * Returns: a string with all the options separated by spaces. This string is
1219 * owned by @mod, do not free it.
1221 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1226 if (!mod
->init
.options
) {
1228 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1229 const struct kmod_list
*l
, *ctx_options
;
1233 ctx_options
= kmod_get_options(mod
->ctx
);
1235 kmod_list_foreach(l
, ctx_options
) {
1236 const char *modname
= kmod_option_get_modname(l
);
1241 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1242 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1243 streq(modname
, mod
->alias
))))
1246 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1247 str
= kmod_option_get_options(l
);
1252 tmp
= realloc(opts
, optslen
+ len
+ 2);
1261 opts
[optslen
] = ' ';
1265 memcpy(opts
+ optslen
, str
, len
);
1267 opts
[optslen
] = '\0';
1270 m
->init
.options
= true;
1274 return mod
->options
;
1277 ERR(mod
->ctx
, "out of memory\n");
1282 * kmod_module_get_install_commands:
1285 * Get install commands for this kmod module. Install commands come from the
1286 * configuration file and are cached in @mod. The first call to this function
1287 * will search for this module in configuration and subsequent calls return
1288 * the cached string. The install commands are returned as they were in the
1289 * configuration, concatenated by ';'. No other processing is made in this
1292 * Returns: a string with all install commands separated by semicolons. This
1293 * string is owned by @mod, do not free it.
1295 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1300 if (!mod
->init
.install_commands
) {
1302 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1303 const struct kmod_list
*l
, *ctx_install_commands
;
1305 ctx_install_commands
= kmod_get_install_commands(mod
->ctx
);
1307 kmod_list_foreach(l
, ctx_install_commands
) {
1308 const char *modname
= kmod_command_get_modname(l
);
1310 if (fnmatch(modname
, mod
->name
, 0) != 0)
1313 m
->install_commands
= kmod_command_get_command(l
);
1316 * find only the first command, as modprobe from
1317 * module-init-tools does
1322 m
->init
.install_commands
= true;
1325 return mod
->install_commands
;
1328 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1330 mod
->init
.install_commands
= true;
1331 mod
->install_commands
= cmd
;
1334 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1336 struct kmod_list
*ret
= NULL
;
1339 for (i
= 0; i
< count
; i
++) {
1340 const char *depname
= array
[i
];
1341 struct kmod_list
*lst
= NULL
;
1344 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1346 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1348 } else if (lst
!= NULL
)
1349 ret
= kmod_list_append_list(ret
, lst
);
1355 * kmod_module_get_softdeps:
1357 * @pre: where to save the list of preceding soft dependencies.
1358 * @post: where to save the list of post soft dependencies.
1360 * Get soft dependencies for this kmod module. Soft dependencies come
1361 * from configuration file and are not cached in @mod because it may include
1362 * dependency cycles that would make we leak kmod_module. Any call
1363 * to this function will search for this module in configuration, allocate a
1364 * list and return the result.
1366 * Both @pre and @post are newly created list of kmod_module and
1367 * should be unreferenced with kmod_module_unref_list().
1369 * Returns: 0 on success or < 0 otherwise.
1371 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1372 struct kmod_list
**pre
,
1373 struct kmod_list
**post
)
1375 const struct kmod_list
*l
, *ctx_softdeps
;
1377 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1380 assert(*pre
== NULL
);
1381 assert(*post
== NULL
);
1383 ctx_softdeps
= kmod_get_softdeps(mod
->ctx
);
1385 kmod_list_foreach(l
, ctx_softdeps
) {
1386 const char *modname
= kmod_softdep_get_name(l
);
1387 const char * const *array
;
1390 if (fnmatch(modname
, mod
->name
, 0) != 0)
1393 array
= kmod_softdep_get_pre(l
, &count
);
1394 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1395 array
= kmod_softdep_get_post(l
, &count
);
1396 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1399 * find only the first command, as modprobe from
1400 * module-init-tools does
1409 * kmod_module_get_remove_commands:
1412 * Get remove commands for this kmod module. Remove commands come from the
1413 * configuration file and are cached in @mod. The first call to this function
1414 * will search for this module in configuration and subsequent calls return
1415 * the cached string. The remove commands are returned as they were in the
1416 * configuration, concatenated by ';'. No other processing is made in this
1419 * Returns: a string with all remove commands separated by semicolons. This
1420 * string is owned by @mod, do not free it.
1422 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1427 if (!mod
->init
.remove_commands
) {
1429 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1430 const struct kmod_list
*l
, *ctx_remove_commands
;
1432 ctx_remove_commands
= kmod_get_remove_commands(mod
->ctx
);
1434 kmod_list_foreach(l
, ctx_remove_commands
) {
1435 const char *modname
= kmod_command_get_modname(l
);
1437 if (fnmatch(modname
, mod
->name
, 0) != 0)
1440 m
->remove_commands
= kmod_command_get_command(l
);
1443 * find only the first command, as modprobe from
1444 * module-init-tools does
1449 m
->init
.remove_commands
= true;
1452 return mod
->remove_commands
;
1455 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1457 mod
->init
.remove_commands
= true;
1458 mod
->remove_commands
= cmd
;
1462 * SECTION:libkmod-loaded
1463 * @short_description: currently loaded modules
1465 * Information about currently loaded modules, as reported by Linux kernel.
1466 * These information are not cached by libkmod and are always read from /sys
1467 * and /proc/modules.
1471 * kmod_module_new_from_loaded:
1472 * @ctx: kmod library context
1473 * @list: where to save the list of loaded modules
1475 * Create a new list of kmod modules with all modules currently loaded in
1476 * kernel. It uses /proc/modules to get the names of loaded modules and to
1477 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1478 * They are put are put in @list in no particular order.
1480 * The initial refcount is 1, and needs to be decremented to release the
1481 * resources of the kmod_module. The returned @list must be released by
1482 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1483 * kmod_modules created, they are all released upon @ctx destruction too. Do
1484 * not unref @ctx before all the desired operations with the returned list are
1487 * Returns: 0 on success or < 0 on error.
1489 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1490 struct kmod_list
**list
)
1492 struct kmod_list
*l
= NULL
;
1496 if (ctx
== NULL
|| list
== NULL
)
1499 fp
= fopen("/proc/modules", "re");
1502 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1506 while (fgets(line
, sizeof(line
), fp
)) {
1507 struct kmod_module
*m
;
1508 struct kmod_list
*node
;
1510 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1512 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1514 ERR(ctx
, "could not get module from name '%s': %s\n",
1515 name
, strerror(-err
));
1519 node
= kmod_list_append(l
, m
);
1523 ERR(ctx
, "out of memory\n");
1524 kmod_module_unref(m
);
1535 * kmod_module_initstate_str:
1536 * @state: the state as returned by kmod_module_get_initstate()
1538 * Translate a initstate to a string.
1540 * Returns: the string associated to the @state. This string is statically
1541 * allocated, do not free it.
1543 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1546 case KMOD_MODULE_BUILTIN
:
1548 case KMOD_MODULE_LIVE
:
1550 case KMOD_MODULE_COMING
:
1552 case KMOD_MODULE_GOING
:
1560 * kmod_module_get_initstate:
1563 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1566 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1568 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1570 char path
[PATH_MAX
], buf
[32];
1571 int fd
, err
, pathlen
;
1576 pathlen
= snprintf(path
, sizeof(path
),
1577 "/sys/module/%s/initstate", mod
->name
);
1578 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1582 DBG(mod
->ctx
, "could not open '%s': %s\n",
1583 path
, strerror(-err
));
1585 if (pathlen
> (int)sizeof("/initstate") - 1) {
1587 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1588 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1589 return KMOD_MODULE_BUILTIN
;
1592 DBG(mod
->ctx
, "could not open '%s': %s\n",
1593 path
, strerror(-err
));
1597 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1600 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1601 path
, strerror(-err
));
1605 if (streq(buf
, "live\n"))
1606 return KMOD_MODULE_LIVE
;
1607 else if (streq(buf
, "coming\n"))
1608 return KMOD_MODULE_COMING
;
1609 else if (streq(buf
, "going\n"))
1610 return KMOD_MODULE_GOING
;
1612 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1617 * kmod_module_get_size:
1620 * Get the size of this kmod module as returned by Linux kernel. It reads the
1621 * file /proc/modules to search for this module and get its size.
1623 * Returns: the size of this kmod module.
1625 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1627 // FIXME TODO: this should be available from /sys/module/foo
1631 long size
= -ENOENT
;
1636 fp
= fopen("/proc/modules", "re");
1640 "could not open /proc/modules: %s\n", strerror(errno
));
1644 while (fgets(line
, sizeof(line
), fp
)) {
1645 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1649 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1652 tok
= strtok_r(NULL
, " \t", &saveptr
);
1655 "invalid line format at /proc/modules:%d\n", lineno
);
1659 value
= strtol(tok
, &endptr
, 10);
1660 if (endptr
== tok
|| *endptr
!= '\0') {
1662 "invalid line format at /proc/modules:%d\n", lineno
);
1674 * kmod_module_get_refcnt:
1677 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1680 * Returns: 0 on success or < 0 on failure.
1682 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1684 char path
[PATH_MAX
];
1691 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1692 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1695 ERR(mod
->ctx
, "could not open '%s': %s\n",
1696 path
, strerror(errno
));
1700 err
= read_str_long(fd
, &refcnt
, 10);
1703 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1704 path
, strerror(-err
));
1712 * kmod_module_get_holders:
1715 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1716 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1718 * Returns: a new list of kmod modules on success or NULL on failure.
1720 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1722 char dname
[PATH_MAX
];
1723 struct kmod_list
*list
= NULL
;
1729 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1733 ERR(mod
->ctx
, "could not open '%s': %s\n",
1734 dname
, strerror(errno
));
1739 struct dirent de
, *entp
;
1740 struct kmod_module
*holder
;
1741 struct kmod_list
*l
;
1744 err
= readdir_r(d
, &de
, &entp
);
1746 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1747 mod
->name
, strerror(-err
));
1754 if (de
.d_name
[0] == '.') {
1755 if (de
.d_name
[1] == '\0' ||
1756 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1760 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1762 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1763 de
.d_name
, strerror(-err
));
1767 l
= kmod_list_append(list
, holder
);
1771 ERR(mod
->ctx
, "out of memory\n");
1772 kmod_module_unref(holder
);
1782 kmod_module_unref_list(list
);
1786 struct kmod_module_section
{
1787 unsigned long address
;
1791 static void kmod_module_section_free(struct kmod_module_section
*section
)
1797 * kmod_module_get_sections:
1800 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1801 * structure contained in this list is internal to libkmod and their fields
1802 * can be obtained by calling kmod_module_section_get_name() and
1803 * kmod_module_section_get_address().
1805 * After use, free the @list by calling kmod_module_section_free_list().
1807 * Returns: a new list of kmod module sections on success or NULL on failure.
1809 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1811 char dname
[PATH_MAX
];
1812 struct kmod_list
*list
= NULL
;
1819 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1823 ERR(mod
->ctx
, "could not open '%s': %s\n",
1824 dname
, strerror(errno
));
1831 struct dirent de
, *entp
;
1832 struct kmod_module_section
*section
;
1833 struct kmod_list
*l
;
1834 unsigned long address
;
1838 err
= readdir_r(d
, &de
, &entp
);
1840 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1841 mod
->name
, strerror(-err
));
1845 if (de
.d_name
[0] == '.') {
1846 if (de
.d_name
[1] == '\0' ||
1847 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1851 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
1853 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1858 err
= read_str_ulong(fd
, &address
, 16);
1861 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1866 namesz
= strlen(de
.d_name
) + 1;
1867 section
= malloc(sizeof(*section
) + namesz
);
1869 if (section
== NULL
) {
1870 ERR(mod
->ctx
, "out of memory\n");
1874 section
->address
= address
;
1875 memcpy(section
->name
, de
.d_name
, namesz
);
1877 l
= kmod_list_append(list
, section
);
1881 ERR(mod
->ctx
, "out of memory\n");
1892 kmod_module_unref_list(list
);
1897 * kmod_module_section_get_module_name:
1898 * @entry: a list entry representing a kmod module section
1900 * Get the name of a kmod module section.
1902 * After use, free the @list by calling kmod_module_section_free_list().
1904 * Returns: the name of this kmod module section on success or NULL on
1905 * failure. The string is owned by the section, do not free it.
1907 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1909 struct kmod_module_section
*section
;
1914 section
= entry
->data
;
1915 return section
->name
;
1919 * kmod_module_section_get_address:
1920 * @entry: a list entry representing a kmod module section
1922 * Get the address of a kmod module section.
1924 * After use, free the @list by calling kmod_module_section_free_list().
1926 * Returns: the address of this kmod module section on success or ULONG_MAX
1929 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1931 struct kmod_module_section
*section
;
1934 return (unsigned long)-1;
1936 section
= entry
->data
;
1937 return section
->address
;
1941 * kmod_module_section_free_list:
1942 * @list: kmod module section list
1944 * Release the resources taken by @list
1946 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
1949 kmod_module_section_free(list
->data
);
1950 list
= kmod_list_remove(list
);
1954 struct kmod_module_info
{
1959 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
1961 struct kmod_module_info
*info
;
1963 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
1967 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
1969 memcpy(info
->key
, key
, keylen
);
1970 info
->key
[keylen
] = '\0';
1971 memcpy(info
->value
, value
, valuelen
);
1972 info
->value
[valuelen
] = '\0';
1976 static void kmod_module_info_free(struct kmod_module_info
*info
)
1982 * kmod_module_get_info:
1984 * @list: where to return list of module information. Use
1985 * kmod_module_info_get_key() and
1986 * kmod_module_info_get_value(). Release this list with
1987 * kmod_module_info_free_list()
1989 * Get a list of entries in ELF section ".modinfo", these contain
1990 * alias, license, depends, vermagic and other keys with respective
1993 * After use, free the @list by calling kmod_module_info_free_list().
1995 * Returns: 0 on success or < 0 otherwise.
1997 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
1999 struct kmod_file
*file
;
2000 struct kmod_elf
*elf
;
2005 int i
, count
, ret
= 0;
2007 if (mod
== NULL
|| list
== NULL
)
2010 assert(*list
== NULL
);
2012 path
= kmod_module_get_path(mod
);
2016 file
= kmod_file_open(mod
->ctx
, path
);
2020 size
= kmod_file_get_size(file
);
2021 mem
= kmod_file_get_contents(file
);
2023 elf
= kmod_elf_new(mem
, size
);
2026 goto elf_open_error
;
2029 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2032 goto get_strings_error
;
2035 for (i
= 0; i
< count
; i
++) {
2036 struct kmod_module_info
*info
;
2037 struct kmod_list
*n
;
2038 const char *key
, *value
;
2039 size_t keylen
, valuelen
;
2042 value
= strchr(key
, '=');
2043 if (value
== NULL
) {
2044 keylen
= strlen(key
);
2047 keylen
= value
- key
;
2049 valuelen
= strlen(value
);
2052 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2055 kmod_module_info_free_list(*list
);
2060 n
= kmod_list_append(*list
, info
);
2064 kmod_module_info_free(info
);
2065 kmod_module_info_free_list(*list
);
2076 kmod_elf_unref(elf
);
2078 kmod_file_unref(file
);
2084 * kmod_module_info_get_key:
2085 * @entry: a list entry representing a kmod module info
2087 * Get the key of a kmod module info.
2089 * Returns: the key of this kmod module info on success or NULL on
2090 * failure. The string is owned by the info, do not free it.
2092 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2094 struct kmod_module_info
*info
;
2104 * kmod_module_info_get_value:
2105 * @entry: a list entry representing a kmod module info
2107 * Get the value of a kmod module info.
2109 * Returns: the value of this kmod module info on success or NULL on
2110 * failure. The string is owned by the info, do not free it.
2112 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2114 struct kmod_module_info
*info
;
2124 * kmod_module_info_free_list:
2125 * @list: kmod module info list
2127 * Release the resources taken by @list
2129 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2132 kmod_module_info_free(list
->data
);
2133 list
= kmod_list_remove(list
);
2137 struct kmod_module_version
{
2142 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2144 struct kmod_module_version
*mv
;
2145 size_t symbollen
= strlen(symbol
) + 1;
2147 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2152 memcpy(mv
->symbol
, symbol
, symbollen
);
2156 static void kmod_module_version_free(struct kmod_module_version
*version
)
2162 * kmod_module_get_versions:
2164 * @list: where to return list of module versions. Use
2165 * kmod_module_version_get_symbol() and
2166 * kmod_module_version_get_crc(). Release this list with
2167 * kmod_module_versions_free_list()
2169 * Get a list of entries in ELF section "__versions".
2171 * After use, free the @list by calling kmod_module_versions_free_list().
2173 * Returns: 0 on success or < 0 otherwise.
2175 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2177 struct kmod_file
*file
;
2178 struct kmod_elf
*elf
;
2181 struct kmod_modversion
*versions
;
2183 int i
, count
, ret
= 0;
2185 if (mod
== NULL
|| list
== NULL
)
2188 assert(*list
== NULL
);
2190 path
= kmod_module_get_path(mod
);
2194 file
= kmod_file_open(mod
->ctx
, path
);
2198 size
= kmod_file_get_size(file
);
2199 mem
= kmod_file_get_contents(file
);
2201 elf
= kmod_elf_new(mem
, size
);
2204 goto elf_open_error
;
2207 count
= kmod_elf_get_modversions(elf
, &versions
);
2210 goto get_strings_error
;
2213 for (i
= 0; i
< count
; i
++) {
2214 struct kmod_module_version
*mv
;
2215 struct kmod_list
*n
;
2217 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2220 kmod_module_versions_free_list(*list
);
2225 n
= kmod_list_append(*list
, mv
);
2229 kmod_module_version_free(mv
);
2230 kmod_module_versions_free_list(*list
);
2241 kmod_elf_unref(elf
);
2243 kmod_file_unref(file
);
2249 * kmod_module_versions_get_symbol:
2250 * @entry: a list entry representing a kmod module versions
2252 * Get the symbol of a kmod module versions.
2254 * Returns: the symbol of this kmod module versions on success or NULL
2255 * on failure. The string is owned by the versions, do not free it.
2257 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2259 struct kmod_module_version
*version
;
2264 version
= entry
->data
;
2265 return version
->symbol
;
2269 * kmod_module_version_get_crc:
2270 * @entry: a list entry representing a kmod module version
2272 * Get the crc of a kmod module version.
2274 * Returns: the crc of this kmod module version on success or NULL on
2275 * failure. The string is owned by the version, do not free it.
2277 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2279 struct kmod_module_version
*version
;
2284 version
= entry
->data
;
2285 return version
->crc
;
2289 * kmod_module_versions_free_list:
2290 * @list: kmod module versions list
2292 * Release the resources taken by @list
2294 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2297 kmod_module_version_free(list
->data
);
2298 list
= kmod_list_remove(list
);
2302 struct kmod_module_symbol
{
2307 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2309 struct kmod_module_symbol
*mv
;
2310 size_t symbollen
= strlen(symbol
) + 1;
2312 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2317 memcpy(mv
->symbol
, symbol
, symbollen
);
2321 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2327 * kmod_module_get_symbols:
2329 * @list: where to return list of module symbols. Use
2330 * kmod_module_symbol_get_symbol() and
2331 * kmod_module_symbol_get_crc(). Release this list with
2332 * kmod_module_symbols_free_list()
2334 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2336 * After use, free the @list by calling kmod_module_symbols_free_list().
2338 * Returns: 0 on success or < 0 otherwise.
2340 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2342 struct kmod_file
*file
;
2343 struct kmod_elf
*elf
;
2346 struct kmod_modversion
*symbols
;
2348 int i
, count
, ret
= 0;
2350 if (mod
== NULL
|| list
== NULL
)
2353 assert(*list
== NULL
);
2355 path
= kmod_module_get_path(mod
);
2359 file
= kmod_file_open(mod
->ctx
, path
);
2363 size
= kmod_file_get_size(file
);
2364 mem
= kmod_file_get_contents(file
);
2366 elf
= kmod_elf_new(mem
, size
);
2369 goto elf_open_error
;
2372 count
= kmod_elf_get_symbols(elf
, &symbols
);
2375 goto get_strings_error
;
2378 for (i
= 0; i
< count
; i
++) {
2379 struct kmod_module_symbol
*mv
;
2380 struct kmod_list
*n
;
2382 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2385 kmod_module_symbols_free_list(*list
);
2390 n
= kmod_list_append(*list
, mv
);
2394 kmod_module_symbol_free(mv
);
2395 kmod_module_symbols_free_list(*list
);
2406 kmod_elf_unref(elf
);
2408 kmod_file_unref(file
);
2414 * kmod_module_symbol_get_symbol:
2415 * @entry: a list entry representing a kmod module symbols
2417 * Get the symbol of a kmod module symbols.
2419 * Returns: the symbol of this kmod module symbols on success or NULL
2420 * on failure. The string is owned by the symbols, do not free it.
2422 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2424 struct kmod_module_symbol
*symbol
;
2429 symbol
= entry
->data
;
2430 return symbol
->symbol
;
2434 * kmod_module_symbol_get_crc:
2435 * @entry: a list entry representing a kmod module symbol
2437 * Get the crc of a kmod module symbol.
2439 * Returns: the crc of this kmod module symbol on success or NULL on
2440 * failure. The string is owned by the symbol, do not free it.
2442 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2444 struct kmod_module_symbol
*symbol
;
2449 symbol
= entry
->data
;
2454 * kmod_module_symbols_free_list:
2455 * @list: kmod module symbols list
2457 * Release the resources taken by @list
2459 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2462 kmod_module_symbol_free(list
->data
);
2463 list
= kmod_list_remove(list
);
2467 struct kmod_module_dependency_symbol
{
2473 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2475 struct kmod_module_dependency_symbol
*mv
;
2476 size_t symbollen
= strlen(symbol
) + 1;
2478 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2484 memcpy(mv
->symbol
, symbol
, symbollen
);
2488 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2490 free(dependency_symbol
);
2494 * kmod_module_get_dependency_symbols:
2496 * @list: where to return list of module dependency_symbols. Use
2497 * kmod_module_dependency_symbol_get_symbol() and
2498 * kmod_module_dependency_symbol_get_crc(). Release this list with
2499 * kmod_module_dependency_symbols_free_list()
2501 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2503 * After use, free the @list by calling
2504 * kmod_module_dependency_symbols_free_list().
2506 * Returns: 0 on success or < 0 otherwise.
2508 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2510 struct kmod_file
*file
;
2511 struct kmod_elf
*elf
;
2514 struct kmod_modversion
*symbols
;
2516 int i
, count
, ret
= 0;
2518 if (mod
== NULL
|| list
== NULL
)
2521 assert(*list
== NULL
);
2523 path
= kmod_module_get_path(mod
);
2527 file
= kmod_file_open(mod
->ctx
, path
);
2531 size
= kmod_file_get_size(file
);
2532 mem
= kmod_file_get_contents(file
);
2534 elf
= kmod_elf_new(mem
, size
);
2537 goto elf_open_error
;
2540 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2543 goto get_strings_error
;
2546 for (i
= 0; i
< count
; i
++) {
2547 struct kmod_module_dependency_symbol
*mv
;
2548 struct kmod_list
*n
;
2550 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2555 kmod_module_dependency_symbols_free_list(*list
);
2560 n
= kmod_list_append(*list
, mv
);
2564 kmod_module_dependency_symbol_free(mv
);
2565 kmod_module_dependency_symbols_free_list(*list
);
2576 kmod_elf_unref(elf
);
2578 kmod_file_unref(file
);
2584 * kmod_module_dependency_symbol_get_symbol:
2585 * @entry: a list entry representing a kmod module dependency_symbols
2587 * Get the dependency symbol of a kmod module
2589 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2590 * on failure. The string is owned by the dependency_symbols, do not free it.
2592 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2594 struct kmod_module_dependency_symbol
*dependency_symbol
;
2599 dependency_symbol
= entry
->data
;
2600 return dependency_symbol
->symbol
;
2604 * kmod_module_dependency_symbol_get_crc:
2605 * @entry: a list entry representing a kmod module dependency_symbol
2607 * Get the crc of a kmod module dependency_symbol.
2609 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2610 * failure. The string is owned by the dependency_symbol, do not free it.
2612 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2614 struct kmod_module_dependency_symbol
*dependency_symbol
;
2619 dependency_symbol
= entry
->data
;
2620 return dependency_symbol
->crc
;
2624 * kmod_module_dependency_symbol_get_bind:
2625 * @entry: a list entry representing a kmod module dependency_symbol
2627 * Get the bind type of a kmod module dependency_symbol.
2629 * Returns: the bind of this kmod module dependency_symbol on success
2630 * or < 0 on failure.
2632 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2634 struct kmod_module_dependency_symbol
*dependency_symbol
;
2639 dependency_symbol
= entry
->data
;
2640 return dependency_symbol
->bind
;
2644 * kmod_module_dependency_symbols_free_list:
2645 * @list: kmod module dependency_symbols list
2647 * Release the resources taken by @list
2649 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2652 kmod_module_dependency_symbol_free(list
->data
);
2653 list
= kmod_list_remove(list
);