2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 */
63 struct kmod_file
*file
;
69 bool install_commands
: 1;
70 bool remove_commands
: 1;
74 * private field used by kmod_module_get_probe_list() to detect
80 * set by kmod_module_get_probe_list: indicates for probe_insert()
81 * whether the module's command and softdep should be ignored
86 * if module was created by searching the modules.builtin file, this
87 * is set. There's nothing much useful one can do with such a
88 * "module", except knowing it's builtin.
93 static inline const char *path_join(const char *path
, size_t prefixlen
,
101 pathlen
= strlen(path
);
102 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
105 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
109 static inline bool module_is_inkernel(struct kmod_module
*mod
)
111 int state
= kmod_module_get_initstate(mod
);
113 if (state
== KMOD_MODULE_LIVE
||
114 state
== KMOD_MODULE_BUILTIN
)
120 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
122 struct kmod_ctx
*ctx
= mod
->ctx
;
123 struct kmod_list
*list
= NULL
;
132 assert(mod
->dep
== NULL
);
133 mod
->init
.dep
= true;
135 p
= strchr(line
, ':');
140 dirname
= kmod_get_dirname(mod
->ctx
);
141 dirnamelen
= strlen(dirname
);
142 if (dirnamelen
+ 2 >= PATH_MAX
)
145 memcpy(buf
, dirname
, dirnamelen
);
146 buf
[dirnamelen
] = '/';
148 buf
[dirnamelen
] = '\0';
150 if (mod
->path
== NULL
) {
151 const char *str
= path_join(line
, dirnamelen
, buf
);
154 mod
->path
= strdup(str
);
155 if (mod
->path
== NULL
)
160 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
161 p
= strtok_r(NULL
, " \t", &saveptr
)) {
162 struct kmod_module
*depmod
;
165 path
= path_join(p
, dirnamelen
, buf
);
167 ERR(ctx
, "could not join path '%s' and '%s'.\n",
172 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
174 ERR(ctx
, "ctx=%p path=%s error=%s\n",
175 ctx
, path
, strerror(-err
));
179 DBG(ctx
, "add dep: %s\n", path
);
181 list
= kmod_list_prepend(list
, depmod
);
185 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
192 kmod_module_unref_list(list
);
193 mod
->init
.dep
= false;
197 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
199 mod
->visited
= visited
;
202 void kmod_module_set_builtin(struct kmod_module
*mod
, bool builtin
)
204 mod
->builtin
= builtin
;
208 * Memory layout with alias:
210 * struct kmod_module {
215 * name <----------' | |
216 * alias <-----------' |
217 * name\alias <--------'
219 * Memory layout without alias:
221 * struct kmod_module {
223 * alias -----|----> NULL
226 * name <----------'-'
228 * @key is "name\alias" or "name" (in which case alias == NULL)
230 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
231 const char *name
, size_t namelen
,
232 const char *alias
, size_t aliaslen
,
233 struct kmod_module
**mod
)
235 struct kmod_module
*m
;
238 m
= kmod_pool_get_module(ctx
, key
);
240 *mod
= kmod_module_ref(m
);
247 keylen
= namelen
+ aliaslen
+ 1;
249 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
255 memset(m
, 0, sizeof(*m
));
257 m
->ctx
= kmod_ref(ctx
);
258 m
->name
= (char *)m
+ sizeof(*m
);
259 memcpy(m
->name
, key
, keylen
+ 1);
261 m
->hashkey
= m
->name
;
264 m
->name
[namelen
] = '\0';
265 m
->alias
= m
->name
+ namelen
+ 1;
266 m
->hashkey
= m
->name
+ keylen
+ 1;
267 memcpy(m
->hashkey
, key
, keylen
+ 1);
271 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
278 * kmod_module_new_from_name:
279 * @ctx: kmod library context
280 * @name: name of the module
281 * @mod: where to save the created struct kmod_module
283 * Create a new struct kmod_module using the module name. @name can not be an
284 * alias, file name or anything else; it must be a module name. There's no
285 * check if the module exists in the system.
287 * This function is also used internally by many others that return a new
288 * struct kmod_module or a new list of modules.
290 * The initial refcount is 1, and needs to be decremented to release the
291 * resources of the kmod_module. Since libkmod keeps track of all
292 * kmod_modules created, they are all released upon @ctx destruction too. Do
293 * not unref @ctx before all the desired operations with the returned
294 * kmod_module are done.
296 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
297 * module name or if memory allocation failed.
299 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
301 struct kmod_module
**mod
)
304 char name_norm
[PATH_MAX
];
306 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
309 modname_normalize(name
, name_norm
, &namelen
);
311 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
314 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
315 const char *name
, struct kmod_module
**mod
)
319 size_t namelen
= strlen(name
);
320 size_t aliaslen
= strlen(alias
);
322 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
323 return -ENAMETOOLONG
;
325 memcpy(key
, name
, namelen
);
326 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
329 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
337 * kmod_module_new_from_path:
338 * @ctx: kmod library context
339 * @path: path where to find the given module
340 * @mod: where to save the created struct kmod_module
342 * Create a new struct kmod_module using the module path. @path must be an
343 * existent file with in the filesystem and must be accessible to libkmod.
345 * The initial refcount is 1, and needs to be decremented to release the
346 * resources of the kmod_module. Since libkmod keeps track of all
347 * kmod_modules created, they are all released upon @ctx destruction too. Do
348 * not unref @ctx before all the desired operations with the returned
349 * kmod_module are done.
351 * If @path is relative, it's treated as relative to the current working
352 * directory. Otherwise, give an absolute path.
354 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
355 * it's not a valid file for a kmod_module or if memory allocation failed.
357 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
359 struct kmod_module
**mod
)
361 struct kmod_module
*m
;
368 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
371 abspath
= path_make_absolute_cwd(path
);
372 if (abspath
== NULL
) {
373 DBG(ctx
, "no absolute path for %s\n", path
);
377 err
= stat(abspath
, &st
);
380 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
385 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
386 DBG(ctx
, "could not get modname from path %s\n", path
);
391 m
= kmod_pool_get_module(ctx
, name
);
395 else if (streq(m
->path
, abspath
))
398 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
399 name
, abspath
, m
->path
);
404 *mod
= kmod_module_ref(m
);
408 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
422 * Drop a reference of the kmod module. If the refcount reaches zero, its
423 * resources are released.
425 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
426 * returns the passed @mod with its refcount decremented.
428 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
433 if (--mod
->refcount
> 0)
436 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
438 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
439 kmod_module_unref_list(mod
->dep
);
442 kmod_file_unref(mod
->file
);
444 kmod_unref(mod
->ctx
);
455 * Take a reference of the kmod module, incrementing its refcount.
457 * Returns: the passed @module with its refcount incremented.
459 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
469 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
473 if (*(_list) != NULL) \
478 * kmod_module_new_from_lookup:
479 * @ctx: kmod library context
480 * @given_alias: alias to look for
481 * @list: an empty list where to save the list of modules matching
484 * Create a new list of kmod modules using an alias or module name and lookup
485 * libkmod's configuration files and indexes in order to find the module.
486 * Once it's found in one of the places, it stops searching and create the
487 * list of modules that is saved in @list.
489 * The search order is: 1. aliases in configuration file; 2. module names in
490 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
491 * in modules.alias index.
493 * The initial refcount is 1, and needs to be decremented to release the
494 * resources of the kmod_module. The returned @list must be released by
495 * calling kmod_module_unref_list(). Since libkmod keeps track of all
496 * kmod_modules created, they are all released upon @ctx destruction too. Do
497 * not unref @ctx before all the desired operations with the returned list are
500 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
501 * methods failed, which is basically due to memory allocation fail. If module
502 * is not found, it still returns 0, but @list is an empty list.
504 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
505 const char *given_alias
,
506 struct kmod_list
**list
)
509 char alias
[PATH_MAX
];
511 if (ctx
== NULL
|| given_alias
== NULL
)
514 if (list
== NULL
|| *list
!= NULL
) {
515 ERR(ctx
, "An empty list is needed to create lookup\n");
519 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
520 DBG(ctx
, "invalid alias: %s\n", given_alias
);
524 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
526 /* Aliases from config file override all the others */
527 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
528 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
530 DBG(ctx
, "lookup modules.dep %s\n", alias
);
531 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
532 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
534 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
535 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
536 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
538 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
539 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
540 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
542 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
543 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
544 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
546 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
547 err
= kmod_lookup_alias_from_builtin_file(ctx
, alias
, list
);
548 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
551 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
554 DBG(ctx
, "Failed to lookup %s\n", alias
);
555 kmod_module_unref_list(*list
);
559 #undef CHECK_ERR_AND_FINISH
562 * kmod_module_unref_list:
563 * @list: list of kmod modules
565 * Drop a reference of each kmod module in @list and releases the resources
566 * taken by the list itself.
568 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
569 * returns the passed @mod with its refcount decremented.
571 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
573 for (; list
!= NULL
; list
= kmod_list_remove(list
))
574 kmod_module_unref(list
->data
);
580 * kmod_module_get_filtered_blacklist:
581 * @ctx: kmod library context
582 * @input: list of kmod_module to be filtered with blacklist
583 * @output: where to save the new list
585 * This function should not be used. Use kmod_module_apply_filter instead.
587 * Given a list @input, this function filter it out with config's blacklist
588 * and save it in @output.
590 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
593 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
594 const struct kmod_list
*input
,
595 struct kmod_list
**output
)
597 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
600 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
602 if (!mod
->init
.dep
) {
604 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
609 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
620 * kmod_module_get_dependencies:
623 * Search the modules.dep index to find the dependencies of the given @mod.
624 * The result is cached in @mod, so subsequent calls to this function will
625 * return the already searched list of modules.
627 * Returns: NULL on failure or if there are any dependencies. Otherwise it
628 * returns a list of kmod modules that can be released by calling
629 * kmod_module_unref_list().
631 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
633 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
638 module_get_dependencies_noref(mod
);
640 kmod_list_foreach(l
, mod
->dep
) {
641 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
643 kmod_module_unref(l
->data
);
653 ERR(mod
->ctx
, "out of memory\n");
654 kmod_module_unref_list(list_new
);
659 * kmod_module_get_module:
660 * @entry: an entry in a list of kmod modules.
662 * Get the kmod module of this @entry in the list, increasing its refcount.
663 * After it's used, unref it. Since the refcount is incremented upon return,
664 * you still have to call kmod_module_unref_list() to release the list of kmod
667 * Returns: NULL on failure or the kmod_module contained in this list entry
668 * with its refcount incremented.
670 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
675 return kmod_module_ref(entry
->data
);
679 * kmod_module_get_name:
682 * Get the name of this kmod module. Name is always available, independently
683 * if it was created by kmod_module_new_from_name() or another function and
684 * it's always normalized (dashes are replaced with underscores).
686 * Returns: the name of this kmod module.
688 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
697 * kmod_module_get_path:
700 * Get the path of this kmod module. If this kmod module was not created by
701 * path, it can search the modules.dep index in order to find out the module
702 * under context's dirname.
704 * Returns: the path of this kmod module or NULL if such information is not
707 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
714 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
716 if (mod
->path
!= NULL
)
722 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
726 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
733 extern long delete_module(const char *name
, unsigned int flags
);
736 * kmod_module_remove_module:
738 * @flags: flags to pass to Linux kernel when removing the module
740 * Remove a module from Linux kernel.
742 * Returns: 0 on success or < 0 on failure.
744 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
752 /* Filter out other flags */
753 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
755 err
= delete_module(mod
->name
, flags
);
758 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
764 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
767 * kmod_module_insert_module:
769 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
770 * behavior of this function.
771 * @options: module's options to pass to Linux Kernel.
773 * Insert a module in Linux kernel. It opens the file pointed by @mod,
774 * mmap'ing it and passing to kernel.
776 * Returns: 0 on success or < 0 on failure. If module is already loaded it
779 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
786 struct kmod_file
*file
;
787 struct kmod_elf
*elf
= NULL
;
789 const char *args
= options
? options
: "";
794 path
= kmod_module_get_path(mod
);
796 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
800 file
= kmod_file_open(mod
->ctx
, path
);
806 size
= kmod_file_get_size(file
);
807 mem
= kmod_file_get_contents(file
);
809 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
810 elf
= kmod_elf_new(mem
, size
);
816 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
817 err
= kmod_elf_strip_section(elf
, "__versions");
819 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
822 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
823 err
= kmod_elf_strip_vermagic(elf
);
825 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
828 mem
= kmod_elf_get_memory(elf
);
831 err
= init_module(mem
, size
, args
);
834 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
840 kmod_file_unref(file
);
845 static bool module_is_blacklisted(struct kmod_module
*mod
)
847 struct kmod_ctx
*ctx
= mod
->ctx
;
848 const struct kmod_config
*config
= kmod_get_config(ctx
);
849 const struct kmod_list
*bl
= config
->blacklists
;
850 const struct kmod_list
*l
;
852 kmod_list_foreach(l
, bl
) {
853 const char *modname
= kmod_blacklist_get_modname(l
);
855 if (streq(modname
, mod
->name
))
863 * kmod_module_apply_filter
864 * @ctx: kmod library context
865 * @filter_type: bitmask to filter modules on
866 * @input: list of kmod_module to be filtered
867 * @output: where to save the new list
869 * Given a list @input, this function filter it out by the filter mask
870 * and save it in @output.
872 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
875 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
876 enum kmod_filter filter_type
,
877 const struct kmod_list
*input
,
878 struct kmod_list
**output
)
880 const struct kmod_list
*li
;
882 if (ctx
== NULL
|| output
== NULL
)
889 kmod_list_foreach(li
, input
) {
890 struct kmod_module
*mod
= li
->data
;
891 struct kmod_list
*node
;
893 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
894 module_is_blacklisted(mod
))
897 if ((filter_type
& KMOD_FILTER_BUILTIN
) && mod
->builtin
)
900 node
= kmod_list_append(*output
, mod
);
905 kmod_module_ref(mod
);
911 kmod_module_unref_list(*output
);
916 static int command_do(struct kmod_module
*mod
, const char *type
,
919 const char *modname
= kmod_module_get_name(mod
);
922 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
924 setenv("MODPROBE_MODULE", modname
, 1);
926 unsetenv("MODPROBE_MODULE");
928 if (err
== -1 || WEXITSTATUS(err
)) {
929 ERR(mod
->ctx
, "Error running %s command for %s\n",
932 err
= -WEXITSTATUS(err
);
938 struct probe_insert_cb
{
939 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
943 static int module_do_install_commands(struct kmod_module
*mod
,
945 struct probe_insert_cb
*cb
)
947 const char *command
= kmod_module_get_install_commands(mod
);
950 size_t cmdlen
, options_len
, varlen
;
957 options_len
= strlen(options
);
958 cmdlen
= strlen(command
);
959 varlen
= sizeof("$CMDLINE_OPTS") - 1;
961 cmd
= memdup(command
, cmdlen
+ 1);
965 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
966 size_t prefixlen
= p
- cmd
;
967 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
968 size_t slen
= cmdlen
- varlen
+ options_len
;
969 char *suffix
= p
+ varlen
;
970 char *s
= malloc(slen
+ 1);
975 memcpy(s
, cmd
, p
- cmd
);
976 memcpy(s
+ prefixlen
, options
, options_len
);
977 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
985 if (cb
->run_install
!= NULL
)
986 err
= cb
->run_install(mod
, cmd
, cb
->data
);
988 err
= command_do(mod
, "install", cmd
);
995 static char *module_options_concat(const char *opt
, const char *xopt
)
997 // TODO: we might need to check if xopt overrides options on opt
998 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
999 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1002 if (optlen
== 0 && xoptlen
== 0)
1005 r
= malloc(optlen
+ xoptlen
+ 2);
1008 memcpy(r
, opt
, optlen
);
1014 memcpy(r
+ optlen
, xopt
, xoptlen
);
1016 r
[optlen
+ xoptlen
] = '\0';
1021 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1023 struct kmod_list
**list
);
1026 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1027 struct kmod_list
**list
)
1029 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1032 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1034 ERR(mod
->ctx
, "could not get softdep: %s\n",
1039 kmod_list_foreach(l
, pre
) {
1040 struct kmod_module
*m
= l
->data
;
1041 err
= __kmod_module_get_probe_list(m
, false, list
);
1046 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1048 kmod_module_unref(mod
);
1053 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1055 kmod_list_foreach(l
, post
) {
1056 struct kmod_module
*m
= l
->data
;
1057 err
= __kmod_module_get_probe_list(m
, false, list
);
1063 kmod_module_unref_list(pre
);
1064 kmod_module_unref_list(post
);
1070 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1072 struct kmod_list
**list
)
1074 struct kmod_list
*dep
, *l
;
1078 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1082 mod
->visited
= true;
1084 dep
= kmod_module_get_dependencies(mod
);
1085 kmod_list_foreach(l
, dep
) {
1086 struct kmod_module
*m
= l
->data
;
1087 err
= __kmod_module_fill_softdep(m
, list
);
1093 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1095 kmod_module_unref(mod
);
1100 mod
->ignorecmd
= true;
1102 err
= __kmod_module_fill_softdep(mod
, list
);
1105 kmod_module_unref_list(dep
);
1109 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1111 struct kmod_list
**list
)
1115 assert(mod
!= NULL
);
1116 assert(list
!= NULL
&& *list
== NULL
);
1119 * Make sure we don't get screwed by previous calls to this function
1121 kmod_set_modules_visited(mod
->ctx
, false);
1123 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1125 kmod_module_unref_list(*list
);
1133 * kmod_module_probe_insert_module:
1135 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1136 * behavior of this function.
1137 * @extra_options: module's options to pass to Linux Kernel. It applies only
1138 * to @mod, not to its dependencies.
1139 * @run_install: function to run when @mod is backed by an install command.
1140 * @data: data to give back to @run_install callback
1141 * @print_action: function to call with the action being taken (install or
1142 * insmod). It's useful for tools like modprobe when running with verbose
1143 * output or in dry-run mode.
1145 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1146 * install commands and applying blacklist.
1148 * If @run_install is NULL, this function will fork and exec by calling
1149 * system(3). Don't pass a NULL argument in @run_install if your binary is
1150 * setuid/setgid (see warning in system(3)). If you need control over the
1151 * execution of an install command, give a callback function instead.
1153 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1156 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1157 unsigned int flags
, const char *extra_options
,
1158 int (*run_install
)(struct kmod_module
*m
,
1159 const char *cmd
, void *data
),
1161 void (*print_action
)(struct kmod_module
*m
,
1163 const char *options
))
1165 struct kmod_list
*list
= NULL
, *l
;
1166 struct probe_insert_cb cb
;
1172 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1173 && module_is_inkernel(mod
)) {
1174 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1181 * Ugly assignement + check. We need to check if we were told to check
1182 * blacklist and also return the reason why we failed.
1183 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1184 * module is an alias, so we also need to check it
1186 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1187 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1188 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1189 if (module_is_blacklisted(mod
))
1193 err
= kmod_module_get_probe_list(mod
,
1194 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1198 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1199 struct kmod_list
*filtered
= NULL
;
1201 err
= kmod_module_apply_filter(mod
->ctx
,
1202 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1206 kmod_module_unref_list(list
);
1207 if (filtered
== NULL
)
1208 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1213 cb
.run_install
= run_install
;
1214 cb
.data
= (void *) data
;
1216 kmod_list_foreach(l
, list
) {
1217 struct kmod_module
*m
= l
->data
;
1218 const char *moptions
= kmod_module_get_options(m
);
1219 const char *cmd
= kmod_module_get_install_commands(m
);
1222 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1223 && module_is_inkernel(m
)) {
1224 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1230 options
= module_options_concat(moptions
,
1231 m
== mod
? extra_options
: NULL
);
1233 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1234 if (print_action
!= NULL
)
1235 print_action(m
, true, options
?: "");
1237 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1238 err
= module_do_install_commands(m
, options
,
1241 if (print_action
!= NULL
)
1242 print_action(m
, false, options
?: "");
1244 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1245 err
= kmod_module_insert_module(m
, flags
,
1253 * Treat "already loaded" error. If we were told to stop on
1254 * already loaded and the module being loaded is not a softdep
1255 * or dep, bail out. Otherwise, just ignore and continue.
1257 * We need to check here because of race conditions. We
1258 * checked first if module was already loaded but it may have
1259 * been loaded between the check and the moment we try to
1262 if (err
== -EEXIST
&& m
== mod
&&
1263 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1272 kmod_module_unref_list(list
);
1277 * kmod_module_get_options:
1280 * Get options of this kmod module. Options come from the configuration file
1281 * and are cached in @mod. The first call to this function will search for
1282 * this module in configuration and subsequent calls return the cached string.
1284 * Returns: a string with all the options separated by spaces. This string is
1285 * owned by @mod, do not free it.
1287 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1292 if (!mod
->init
.options
) {
1294 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1295 const struct kmod_list
*l
;
1296 const struct kmod_config
*config
;
1300 config
= kmod_get_config(mod
->ctx
);
1302 kmod_list_foreach(l
, config
->options
) {
1303 const char *modname
= kmod_option_get_modname(l
);
1308 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1309 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1310 streq(modname
, mod
->alias
))))
1313 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1314 str
= kmod_option_get_options(l
);
1319 tmp
= realloc(opts
, optslen
+ len
+ 2);
1328 opts
[optslen
] = ' ';
1332 memcpy(opts
+ optslen
, str
, len
);
1334 opts
[optslen
] = '\0';
1337 m
->init
.options
= true;
1341 return mod
->options
;
1344 ERR(mod
->ctx
, "out of memory\n");
1349 * kmod_module_get_install_commands:
1352 * Get install commands for this kmod module. Install commands come from the
1353 * configuration file and are cached in @mod. The first call to this function
1354 * will search for this module in configuration and subsequent calls return
1355 * the cached string. The install commands are returned as they were in the
1356 * configuration, concatenated by ';'. No other processing is made in this
1359 * Returns: a string with all install commands separated by semicolons. This
1360 * string is owned by @mod, do not free it.
1362 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1367 if (!mod
->init
.install_commands
) {
1369 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1370 const struct kmod_list
*l
;
1371 const struct kmod_config
*config
;
1373 config
= kmod_get_config(mod
->ctx
);
1375 kmod_list_foreach(l
, config
->install_commands
) {
1376 const char *modname
= kmod_command_get_modname(l
);
1378 if (fnmatch(modname
, mod
->name
, 0) != 0)
1381 m
->install_commands
= kmod_command_get_command(l
);
1384 * find only the first command, as modprobe from
1385 * module-init-tools does
1390 m
->init
.install_commands
= true;
1393 return mod
->install_commands
;
1396 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1398 mod
->init
.install_commands
= true;
1399 mod
->install_commands
= cmd
;
1402 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1404 struct kmod_list
*ret
= NULL
;
1407 for (i
= 0; i
< count
; i
++) {
1408 const char *depname
= array
[i
];
1409 struct kmod_list
*lst
= NULL
;
1412 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1414 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1416 } else if (lst
!= NULL
)
1417 ret
= kmod_list_append_list(ret
, lst
);
1423 * kmod_module_get_softdeps:
1425 * @pre: where to save the list of preceding soft dependencies.
1426 * @post: where to save the list of post soft dependencies.
1428 * Get soft dependencies for this kmod module. Soft dependencies come
1429 * from configuration file and are not cached in @mod because it may include
1430 * dependency cycles that would make we leak kmod_module. Any call
1431 * to this function will search for this module in configuration, allocate a
1432 * list and return the result.
1434 * Both @pre and @post are newly created list of kmod_module and
1435 * should be unreferenced with kmod_module_unref_list().
1437 * Returns: 0 on success or < 0 otherwise.
1439 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1440 struct kmod_list
**pre
,
1441 struct kmod_list
**post
)
1443 const struct kmod_list
*l
;
1444 const struct kmod_config
*config
;
1446 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1449 assert(*pre
== NULL
);
1450 assert(*post
== NULL
);
1452 config
= kmod_get_config(mod
->ctx
);
1454 kmod_list_foreach(l
, config
->softdeps
) {
1455 const char *modname
= kmod_softdep_get_name(l
);
1456 const char * const *array
;
1459 if (fnmatch(modname
, mod
->name
, 0) != 0)
1462 array
= kmod_softdep_get_pre(l
, &count
);
1463 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1464 array
= kmod_softdep_get_post(l
, &count
);
1465 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1468 * find only the first command, as modprobe from
1469 * module-init-tools does
1478 * kmod_module_get_remove_commands:
1481 * Get remove commands for this kmod module. Remove commands come from the
1482 * configuration file and are cached in @mod. The first call to this function
1483 * will search for this module in configuration and subsequent calls return
1484 * the cached string. The remove commands are returned as they were in the
1485 * configuration, concatenated by ';'. No other processing is made in this
1488 * Returns: a string with all remove commands separated by semicolons. This
1489 * string is owned by @mod, do not free it.
1491 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1496 if (!mod
->init
.remove_commands
) {
1498 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1499 const struct kmod_list
*l
;
1500 const struct kmod_config
*config
;
1502 config
= kmod_get_config(mod
->ctx
);
1504 kmod_list_foreach(l
, config
->remove_commands
) {
1505 const char *modname
= kmod_command_get_modname(l
);
1507 if (fnmatch(modname
, mod
->name
, 0) != 0)
1510 m
->remove_commands
= kmod_command_get_command(l
);
1513 * find only the first command, as modprobe from
1514 * module-init-tools does
1519 m
->init
.remove_commands
= true;
1522 return mod
->remove_commands
;
1525 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1527 mod
->init
.remove_commands
= true;
1528 mod
->remove_commands
= cmd
;
1532 * SECTION:libkmod-loaded
1533 * @short_description: currently loaded modules
1535 * Information about currently loaded modules, as reported by Linux kernel.
1536 * These information are not cached by libkmod and are always read from /sys
1537 * and /proc/modules.
1541 * kmod_module_new_from_loaded:
1542 * @ctx: kmod library context
1543 * @list: where to save the list of loaded modules
1545 * Create a new list of kmod modules with all modules currently loaded in
1546 * kernel. It uses /proc/modules to get the names of loaded modules and to
1547 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1548 * They are put are put in @list in no particular order.
1550 * The initial refcount is 1, and needs to be decremented to release the
1551 * resources of the kmod_module. The returned @list must be released by
1552 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1553 * kmod_modules created, they are all released upon @ctx destruction too. Do
1554 * not unref @ctx before all the desired operations with the returned list are
1557 * Returns: 0 on success or < 0 on error.
1559 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1560 struct kmod_list
**list
)
1562 struct kmod_list
*l
= NULL
;
1566 if (ctx
== NULL
|| list
== NULL
)
1569 fp
= fopen("/proc/modules", "re");
1572 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1576 while (fgets(line
, sizeof(line
), fp
)) {
1577 struct kmod_module
*m
;
1578 struct kmod_list
*node
;
1580 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1582 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1584 ERR(ctx
, "could not get module from name '%s': %s\n",
1585 name
, strerror(-err
));
1589 node
= kmod_list_append(l
, m
);
1593 ERR(ctx
, "out of memory\n");
1594 kmod_module_unref(m
);
1605 * kmod_module_initstate_str:
1606 * @state: the state as returned by kmod_module_get_initstate()
1608 * Translate a initstate to a string.
1610 * Returns: the string associated to the @state. This string is statically
1611 * allocated, do not free it.
1613 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1616 case KMOD_MODULE_BUILTIN
:
1618 case KMOD_MODULE_LIVE
:
1620 case KMOD_MODULE_COMING
:
1622 case KMOD_MODULE_GOING
:
1630 * kmod_module_get_initstate:
1633 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1636 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1638 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1640 char path
[PATH_MAX
], buf
[32];
1641 int fd
, err
, pathlen
;
1647 return KMOD_MODULE_BUILTIN
;
1649 pathlen
= snprintf(path
, sizeof(path
),
1650 "/sys/module/%s/initstate", mod
->name
);
1651 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1655 DBG(mod
->ctx
, "could not open '%s': %s\n",
1656 path
, strerror(-err
));
1658 if (pathlen
> (int)sizeof("/initstate") - 1) {
1660 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1661 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1662 return KMOD_MODULE_BUILTIN
;
1665 DBG(mod
->ctx
, "could not open '%s': %s\n",
1666 path
, strerror(-err
));
1670 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1673 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1674 path
, strerror(-err
));
1678 if (streq(buf
, "live\n"))
1679 return KMOD_MODULE_LIVE
;
1680 else if (streq(buf
, "coming\n"))
1681 return KMOD_MODULE_COMING
;
1682 else if (streq(buf
, "going\n"))
1683 return KMOD_MODULE_GOING
;
1685 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1690 * kmod_module_get_size:
1693 * Get the size of this kmod module as returned by Linux kernel. If supported,
1694 * the size is read from the coresize attribute in /sys/module. For older
1695 * kernels, this falls back on /proc/modules and searches for the specified
1696 * module to get its size.
1698 * Returns: the size of this kmod module.
1700 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1705 long size
= -ENOENT
;
1711 /* try to open the module dir in /sys. If this fails, don't
1712 * bother trying to find the size as we know the module isn't
1715 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1716 dfd
= open(line
, O_RDONLY
);
1720 /* available as of linux 3.3.x */
1721 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1723 if (read_str_long(cfd
, &size
, 10) < 0)
1724 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1729 /* fall back on parsing /proc/modules */
1730 fp
= fopen("/proc/modules", "re");
1734 "could not open /proc/modules: %s\n", strerror(errno
));
1738 while (fgets(line
, sizeof(line
), fp
)) {
1739 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1743 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1746 tok
= strtok_r(NULL
, " \t", &saveptr
);
1749 "invalid line format at /proc/modules:%d\n", lineno
);
1753 value
= strtol(tok
, &endptr
, 10);
1754 if (endptr
== tok
|| *endptr
!= '\0') {
1756 "invalid line format at /proc/modules:%d\n", lineno
);
1771 * kmod_module_get_refcnt:
1774 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1777 * Returns: 0 on success or < 0 on failure.
1779 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1781 char path
[PATH_MAX
];
1788 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1789 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1792 DBG(mod
->ctx
, "could not open '%s': %s\n",
1793 path
, strerror(errno
));
1797 err
= read_str_long(fd
, &refcnt
, 10);
1800 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1801 path
, strerror(-err
));
1809 * kmod_module_get_holders:
1812 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1813 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1815 * Returns: a new list of kmod modules on success or NULL on failure.
1817 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1819 char dname
[PATH_MAX
];
1820 struct kmod_list
*list
= NULL
;
1826 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1830 ERR(mod
->ctx
, "could not open '%s': %s\n",
1831 dname
, strerror(errno
));
1836 struct dirent de
, *entp
;
1837 struct kmod_module
*holder
;
1838 struct kmod_list
*l
;
1841 err
= readdir_r(d
, &de
, &entp
);
1843 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1844 mod
->name
, strerror(-err
));
1851 if (de
.d_name
[0] == '.') {
1852 if (de
.d_name
[1] == '\0' ||
1853 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1857 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1859 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1860 de
.d_name
, strerror(-err
));
1864 l
= kmod_list_append(list
, holder
);
1868 ERR(mod
->ctx
, "out of memory\n");
1869 kmod_module_unref(holder
);
1879 kmod_module_unref_list(list
);
1883 struct kmod_module_section
{
1884 unsigned long address
;
1888 static void kmod_module_section_free(struct kmod_module_section
*section
)
1894 * kmod_module_get_sections:
1897 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1898 * structure contained in this list is internal to libkmod and their fields
1899 * can be obtained by calling kmod_module_section_get_name() and
1900 * kmod_module_section_get_address().
1902 * After use, free the @list by calling kmod_module_section_free_list().
1904 * Returns: a new list of kmod module sections on success or NULL on failure.
1906 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1908 char dname
[PATH_MAX
];
1909 struct kmod_list
*list
= NULL
;
1916 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1920 ERR(mod
->ctx
, "could not open '%s': %s\n",
1921 dname
, strerror(errno
));
1928 struct dirent de
, *entp
;
1929 struct kmod_module_section
*section
;
1930 struct kmod_list
*l
;
1931 unsigned long address
;
1935 err
= readdir_r(d
, &de
, &entp
);
1937 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1938 mod
->name
, strerror(-err
));
1942 if (de
.d_name
[0] == '.') {
1943 if (de
.d_name
[1] == '\0' ||
1944 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1948 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
1950 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1955 err
= read_str_ulong(fd
, &address
, 16);
1958 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1963 namesz
= strlen(de
.d_name
) + 1;
1964 section
= malloc(sizeof(*section
) + namesz
);
1966 if (section
== NULL
) {
1967 ERR(mod
->ctx
, "out of memory\n");
1971 section
->address
= address
;
1972 memcpy(section
->name
, de
.d_name
, namesz
);
1974 l
= kmod_list_append(list
, section
);
1978 ERR(mod
->ctx
, "out of memory\n");
1989 kmod_module_unref_list(list
);
1994 * kmod_module_section_get_module_name:
1995 * @entry: a list entry representing a kmod module section
1997 * Get the name of a kmod module section.
1999 * After use, free the @list by calling kmod_module_section_free_list().
2001 * Returns: the name of this kmod module section on success or NULL on
2002 * failure. The string is owned by the section, do not free it.
2004 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2006 struct kmod_module_section
*section
;
2011 section
= entry
->data
;
2012 return section
->name
;
2016 * kmod_module_section_get_address:
2017 * @entry: a list entry representing a kmod module section
2019 * Get the address of a kmod module section.
2021 * After use, free the @list by calling kmod_module_section_free_list().
2023 * Returns: the address of this kmod module section on success or ULONG_MAX
2026 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2028 struct kmod_module_section
*section
;
2031 return (unsigned long)-1;
2033 section
= entry
->data
;
2034 return section
->address
;
2038 * kmod_module_section_free_list:
2039 * @list: kmod module section list
2041 * Release the resources taken by @list
2043 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2046 kmod_module_section_free(list
->data
);
2047 list
= kmod_list_remove(list
);
2051 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2053 if (mod
->file
== NULL
) {
2054 const char *path
= kmod_module_get_path(mod
);
2061 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2063 if (mod
->file
== NULL
)
2067 return kmod_file_get_elf(mod
->file
);
2070 struct kmod_module_info
{
2075 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2077 struct kmod_module_info
*info
;
2079 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2083 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2085 memcpy(info
->key
, key
, keylen
);
2086 info
->key
[keylen
] = '\0';
2087 memcpy(info
->value
, value
, valuelen
);
2088 info
->value
[valuelen
] = '\0';
2092 static void kmod_module_info_free(struct kmod_module_info
*info
)
2097 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2099 struct kmod_module_info
*info
;
2100 struct kmod_list
*n
;
2102 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2105 n
= kmod_list_append(*list
, info
);
2109 kmod_module_info_free(info
);
2114 * kmod_module_get_info:
2116 * @list: where to return list of module information. Use
2117 * kmod_module_info_get_key() and
2118 * kmod_module_info_get_value(). Release this list with
2119 * kmod_module_info_free_list()
2121 * Get a list of entries in ELF section ".modinfo", these contain
2122 * alias, license, depends, vermagic and other keys with respective
2123 * values. If the module is signed (CONFIG_MODULE_SIG), information
2124 * about the module signature is included as well: signer,
2125 * sig_key and sig_hashalgo.
2127 * After use, free the @list by calling kmod_module_info_free_list().
2129 * Returns: 0 on success or < 0 otherwise.
2131 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2133 struct kmod_elf
*elf
;
2135 int i
, count
, ret
= -ENOMEM
;
2136 struct kmod_signature_info sig_info
;
2138 if (mod
== NULL
|| list
== NULL
)
2141 assert(*list
== NULL
);
2143 elf
= kmod_module_get_elf(mod
);
2147 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2151 for (i
= 0; i
< count
; i
++) {
2152 struct kmod_list
*n
;
2153 const char *key
, *value
;
2154 size_t keylen
, valuelen
;
2157 value
= strchr(key
, '=');
2158 if (value
== NULL
) {
2159 keylen
= strlen(key
);
2162 keylen
= value
- key
;
2164 valuelen
= strlen(value
);
2167 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2172 if (kmod_module_signature_info(mod
->file
, &sig_info
)) {
2173 struct kmod_list
*n
;
2176 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2177 sig_info
.signer
, sig_info
.signer_len
);
2182 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2183 key_hex
= malloc(sig_info
.key_id_len
* 3);
2184 if (key_hex
== NULL
)
2186 for (i
= 0; i
< (int)sig_info
.key_id_len
; i
++) {
2187 sprintf(key_hex
+ i
* 3, "%02X",
2188 (unsigned char)sig_info
.key_id
[i
]);
2189 if (i
< (int)sig_info
.key_id_len
- 1)
2190 key_hex
[i
* 3 + 2] = ':';
2192 n
= kmod_module_info_append(list
, "sig_key", strlen("sig_key"),
2193 key_hex
, sig_info
.key_id_len
* 3 - 1);
2199 n
= kmod_module_info_append(list
,
2200 "sig_hashalgo", strlen("sig_hashalgo"),
2201 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2207 * Omit sig_info.id_type and sig_info.algo for now, as these
2208 * are currently constant.
2215 kmod_module_info_free_list(*list
);
2223 * kmod_module_info_get_key:
2224 * @entry: a list entry representing a kmod module info
2226 * Get the key of a kmod module info.
2228 * Returns: the key of this kmod module info on success or NULL on
2229 * failure. The string is owned by the info, do not free it.
2231 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2233 struct kmod_module_info
*info
;
2243 * kmod_module_info_get_value:
2244 * @entry: a list entry representing a kmod module info
2246 * Get the value of a kmod module info.
2248 * Returns: the value of this kmod module info on success or NULL on
2249 * failure. The string is owned by the info, do not free it.
2251 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2253 struct kmod_module_info
*info
;
2263 * kmod_module_info_free_list:
2264 * @list: kmod module info list
2266 * Release the resources taken by @list
2268 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2271 kmod_module_info_free(list
->data
);
2272 list
= kmod_list_remove(list
);
2276 struct kmod_module_version
{
2281 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2283 struct kmod_module_version
*mv
;
2284 size_t symbollen
= strlen(symbol
) + 1;
2286 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2291 memcpy(mv
->symbol
, symbol
, symbollen
);
2295 static void kmod_module_version_free(struct kmod_module_version
*version
)
2301 * kmod_module_get_versions:
2303 * @list: where to return list of module versions. Use
2304 * kmod_module_version_get_symbol() and
2305 * kmod_module_version_get_crc(). Release this list with
2306 * kmod_module_versions_free_list()
2308 * Get a list of entries in ELF section "__versions".
2310 * After use, free the @list by calling kmod_module_versions_free_list().
2312 * Returns: 0 on success or < 0 otherwise.
2314 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2316 struct kmod_elf
*elf
;
2317 struct kmod_modversion
*versions
;
2318 int i
, count
, ret
= 0;
2320 if (mod
== NULL
|| list
== NULL
)
2323 assert(*list
== NULL
);
2325 elf
= kmod_module_get_elf(mod
);
2329 count
= kmod_elf_get_modversions(elf
, &versions
);
2333 for (i
= 0; i
< count
; i
++) {
2334 struct kmod_module_version
*mv
;
2335 struct kmod_list
*n
;
2337 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2340 kmod_module_versions_free_list(*list
);
2345 n
= kmod_list_append(*list
, mv
);
2349 kmod_module_version_free(mv
);
2350 kmod_module_versions_free_list(*list
);
2364 * kmod_module_versions_get_symbol:
2365 * @entry: a list entry representing a kmod module versions
2367 * Get the symbol of a kmod module versions.
2369 * Returns: the symbol of this kmod module versions on success or NULL
2370 * on failure. The string is owned by the versions, do not free it.
2372 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2374 struct kmod_module_version
*version
;
2379 version
= entry
->data
;
2380 return version
->symbol
;
2384 * kmod_module_version_get_crc:
2385 * @entry: a list entry representing a kmod module version
2387 * Get the crc of a kmod module version.
2389 * Returns: the crc of this kmod module version on success or NULL on
2390 * failure. The string is owned by the version, do not free it.
2392 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2394 struct kmod_module_version
*version
;
2399 version
= entry
->data
;
2400 return version
->crc
;
2404 * kmod_module_versions_free_list:
2405 * @list: kmod module versions list
2407 * Release the resources taken by @list
2409 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2412 kmod_module_version_free(list
->data
);
2413 list
= kmod_list_remove(list
);
2417 struct kmod_module_symbol
{
2422 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2424 struct kmod_module_symbol
*mv
;
2425 size_t symbollen
= strlen(symbol
) + 1;
2427 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2432 memcpy(mv
->symbol
, symbol
, symbollen
);
2436 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2442 * kmod_module_get_symbols:
2444 * @list: where to return list of module symbols. Use
2445 * kmod_module_symbol_get_symbol() and
2446 * kmod_module_symbol_get_crc(). Release this list with
2447 * kmod_module_symbols_free_list()
2449 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2451 * After use, free the @list by calling kmod_module_symbols_free_list().
2453 * Returns: 0 on success or < 0 otherwise.
2455 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2457 struct kmod_elf
*elf
;
2458 struct kmod_modversion
*symbols
;
2459 int i
, count
, ret
= 0;
2461 if (mod
== NULL
|| list
== NULL
)
2464 assert(*list
== NULL
);
2466 elf
= kmod_module_get_elf(mod
);
2470 count
= kmod_elf_get_symbols(elf
, &symbols
);
2474 for (i
= 0; i
< count
; i
++) {
2475 struct kmod_module_symbol
*mv
;
2476 struct kmod_list
*n
;
2478 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2481 kmod_module_symbols_free_list(*list
);
2486 n
= kmod_list_append(*list
, mv
);
2490 kmod_module_symbol_free(mv
);
2491 kmod_module_symbols_free_list(*list
);
2505 * kmod_module_symbol_get_symbol:
2506 * @entry: a list entry representing a kmod module symbols
2508 * Get the symbol of a kmod module symbols.
2510 * Returns: the symbol of this kmod module symbols on success or NULL
2511 * on failure. The string is owned by the symbols, do not free it.
2513 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2515 struct kmod_module_symbol
*symbol
;
2520 symbol
= entry
->data
;
2521 return symbol
->symbol
;
2525 * kmod_module_symbol_get_crc:
2526 * @entry: a list entry representing a kmod module symbol
2528 * Get the crc of a kmod module symbol.
2530 * Returns: the crc of this kmod module symbol on success or NULL on
2531 * failure. The string is owned by the symbol, do not free it.
2533 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2535 struct kmod_module_symbol
*symbol
;
2540 symbol
= entry
->data
;
2545 * kmod_module_symbols_free_list:
2546 * @list: kmod module symbols list
2548 * Release the resources taken by @list
2550 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2553 kmod_module_symbol_free(list
->data
);
2554 list
= kmod_list_remove(list
);
2558 struct kmod_module_dependency_symbol
{
2564 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2566 struct kmod_module_dependency_symbol
*mv
;
2567 size_t symbollen
= strlen(symbol
) + 1;
2569 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2575 memcpy(mv
->symbol
, symbol
, symbollen
);
2579 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2581 free(dependency_symbol
);
2585 * kmod_module_get_dependency_symbols:
2587 * @list: where to return list of module dependency_symbols. Use
2588 * kmod_module_dependency_symbol_get_symbol() and
2589 * kmod_module_dependency_symbol_get_crc(). Release this list with
2590 * kmod_module_dependency_symbols_free_list()
2592 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2594 * After use, free the @list by calling
2595 * kmod_module_dependency_symbols_free_list().
2597 * Returns: 0 on success or < 0 otherwise.
2599 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2601 struct kmod_elf
*elf
;
2602 struct kmod_modversion
*symbols
;
2603 int i
, count
, ret
= 0;
2605 if (mod
== NULL
|| list
== NULL
)
2608 assert(*list
== NULL
);
2610 elf
= kmod_module_get_elf(mod
);
2614 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2618 for (i
= 0; i
< count
; i
++) {
2619 struct kmod_module_dependency_symbol
*mv
;
2620 struct kmod_list
*n
;
2622 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2627 kmod_module_dependency_symbols_free_list(*list
);
2632 n
= kmod_list_append(*list
, mv
);
2636 kmod_module_dependency_symbol_free(mv
);
2637 kmod_module_dependency_symbols_free_list(*list
);
2651 * kmod_module_dependency_symbol_get_symbol:
2652 * @entry: a list entry representing a kmod module dependency_symbols
2654 * Get the dependency symbol of a kmod module
2656 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2657 * on failure. The string is owned by the dependency_symbols, do not free it.
2659 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2661 struct kmod_module_dependency_symbol
*dependency_symbol
;
2666 dependency_symbol
= entry
->data
;
2667 return dependency_symbol
->symbol
;
2671 * kmod_module_dependency_symbol_get_crc:
2672 * @entry: a list entry representing a kmod module dependency_symbol
2674 * Get the crc of a kmod module dependency_symbol.
2676 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2677 * failure. The string is owned by the dependency_symbol, do not free it.
2679 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2681 struct kmod_module_dependency_symbol
*dependency_symbol
;
2686 dependency_symbol
= entry
->data
;
2687 return dependency_symbol
->crc
;
2691 * kmod_module_dependency_symbol_get_bind:
2692 * @entry: a list entry representing a kmod module dependency_symbol
2694 * Get the bind type of a kmod module dependency_symbol.
2696 * Returns: the bind of this kmod module dependency_symbol on success
2697 * or < 0 on failure.
2699 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2701 struct kmod_module_dependency_symbol
*dependency_symbol
;
2706 dependency_symbol
= entry
->data
;
2707 return dependency_symbol
->bind
;
2711 * kmod_module_dependency_symbols_free_list:
2712 * @list: kmod module dependency_symbols list
2714 * Release the resources taken by @list
2716 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2719 kmod_module_dependency_symbol_free(list
->data
);
2720 list
= kmod_list_remove(list
);