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>
36 #include <sys/syscall.h>
41 #ifdef HAVE_LINUX_MODULE_H
42 #include <linux/module.h>
46 #include "libkmod-private.h"
49 * SECTION:libkmod-module
50 * @short_description: operate on kernel modules
56 * Opaque object representing a module.
63 struct kmod_list
*dep
;
65 const char *install_commands
; /* owned by kmod_config */
66 const char *remove_commands
; /* owned by kmod_config */
67 char *alias
; /* only set if this module was created from an alias */
68 struct kmod_file
*file
;
74 bool install_commands
: 1;
75 bool remove_commands
: 1;
79 * private field used by kmod_module_get_probe_list() to detect
85 * set by kmod_module_get_probe_list: indicates for probe_insert()
86 * whether the module's command and softdep should be ignored
91 * if module was created by searching the modules.builtin file, this
92 * is set. There's nothing much useful one can do with such a
93 * "module", except knowing it's builtin.
98 static inline const char *path_join(const char *path
, size_t prefixlen
,
106 pathlen
= strlen(path
);
107 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
110 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
114 static inline bool module_is_inkernel(struct kmod_module
*mod
)
116 int state
= kmod_module_get_initstate(mod
);
118 if (state
== KMOD_MODULE_LIVE
||
119 state
== KMOD_MODULE_BUILTIN
)
125 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
127 struct kmod_ctx
*ctx
= mod
->ctx
;
128 struct kmod_list
*list
= NULL
;
137 assert(mod
->dep
== NULL
);
138 mod
->init
.dep
= true;
140 p
= strchr(line
, ':');
145 dirname
= kmod_get_dirname(mod
->ctx
);
146 dirnamelen
= strlen(dirname
);
147 if (dirnamelen
+ 2 >= PATH_MAX
)
150 memcpy(buf
, dirname
, dirnamelen
);
151 buf
[dirnamelen
] = '/';
153 buf
[dirnamelen
] = '\0';
155 if (mod
->path
== NULL
) {
156 const char *str
= path_join(line
, dirnamelen
, buf
);
159 mod
->path
= strdup(str
);
160 if (mod
->path
== NULL
)
165 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
166 p
= strtok_r(NULL
, " \t", &saveptr
)) {
167 struct kmod_module
*depmod
;
170 path
= path_join(p
, dirnamelen
, buf
);
172 ERR(ctx
, "could not join path '%s' and '%s'.\n",
177 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
179 ERR(ctx
, "ctx=%p path=%s error=%s\n",
180 ctx
, path
, strerror(-err
));
184 DBG(ctx
, "add dep: %s\n", path
);
186 list
= kmod_list_prepend(list
, depmod
);
190 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
197 kmod_module_unref_list(list
);
198 mod
->init
.dep
= false;
202 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
204 mod
->visited
= visited
;
207 void kmod_module_set_builtin(struct kmod_module
*mod
, bool builtin
)
209 mod
->builtin
= builtin
;
213 * Memory layout with alias:
215 * struct kmod_module {
220 * name <----------' | |
221 * alias <-----------' |
222 * name\alias <--------'
224 * Memory layout without alias:
226 * struct kmod_module {
228 * alias -----|----> NULL
231 * name <----------'-'
233 * @key is "name\alias" or "name" (in which case alias == NULL)
235 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
236 const char *name
, size_t namelen
,
237 const char *alias
, size_t aliaslen
,
238 struct kmod_module
**mod
)
240 struct kmod_module
*m
;
243 m
= kmod_pool_get_module(ctx
, key
);
245 *mod
= kmod_module_ref(m
);
252 keylen
= namelen
+ aliaslen
+ 1;
254 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
260 memset(m
, 0, sizeof(*m
));
262 m
->ctx
= kmod_ref(ctx
);
263 m
->name
= (char *)m
+ sizeof(*m
);
264 memcpy(m
->name
, key
, keylen
+ 1);
266 m
->hashkey
= m
->name
;
269 m
->name
[namelen
] = '\0';
270 m
->alias
= m
->name
+ namelen
+ 1;
271 m
->hashkey
= m
->name
+ keylen
+ 1;
272 memcpy(m
->hashkey
, key
, keylen
+ 1);
276 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
283 * kmod_module_new_from_name:
284 * @ctx: kmod library context
285 * @name: name of the module
286 * @mod: where to save the created struct kmod_module
288 * Create a new struct kmod_module using the module name. @name can not be an
289 * alias, file name or anything else; it must be a module name. There's no
290 * check if the module exists in the system.
292 * This function is also used internally by many others that return a new
293 * struct kmod_module or a new list of modules.
295 * The initial refcount is 1, and needs to be decremented to release the
296 * resources of the kmod_module. Since libkmod keeps track of all
297 * kmod_modules created, they are all released upon @ctx destruction too. Do
298 * not unref @ctx before all the desired operations with the returned
299 * kmod_module are done.
301 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
302 * module name or if memory allocation failed.
304 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
306 struct kmod_module
**mod
)
309 char name_norm
[PATH_MAX
];
311 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
314 modname_normalize(name
, name_norm
, &namelen
);
316 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
319 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
320 const char *name
, struct kmod_module
**mod
)
324 size_t namelen
= strlen(name
);
325 size_t aliaslen
= strlen(alias
);
327 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
328 return -ENAMETOOLONG
;
330 memcpy(key
, name
, namelen
);
331 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
334 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
342 * kmod_module_new_from_path:
343 * @ctx: kmod library context
344 * @path: path where to find the given module
345 * @mod: where to save the created struct kmod_module
347 * Create a new struct kmod_module using the module path. @path must be an
348 * existent file with in the filesystem and must be accessible to libkmod.
350 * The initial refcount is 1, and needs to be decremented to release the
351 * resources of the kmod_module. Since libkmod keeps track of all
352 * kmod_modules created, they are all released upon @ctx destruction too. Do
353 * not unref @ctx before all the desired operations with the returned
354 * kmod_module are done.
356 * If @path is relative, it's treated as relative to the current working
357 * directory. Otherwise, give an absolute path.
359 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
360 * it's not a valid file for a kmod_module or if memory allocation failed.
362 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
364 struct kmod_module
**mod
)
366 struct kmod_module
*m
;
373 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
376 abspath
= path_make_absolute_cwd(path
);
377 if (abspath
== NULL
) {
378 DBG(ctx
, "no absolute path for %s\n", path
);
382 err
= stat(abspath
, &st
);
385 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
390 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
391 DBG(ctx
, "could not get modname from path %s\n", path
);
396 m
= kmod_pool_get_module(ctx
, name
);
400 else if (streq(m
->path
, abspath
))
403 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
404 name
, abspath
, m
->path
);
409 *mod
= kmod_module_ref(m
);
413 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
427 * Drop a reference of the kmod module. If the refcount reaches zero, its
428 * resources are released.
430 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
431 * returns the passed @mod with its refcount decremented.
433 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
438 if (--mod
->refcount
> 0)
441 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
443 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
444 kmod_module_unref_list(mod
->dep
);
447 kmod_file_unref(mod
->file
);
449 kmod_unref(mod
->ctx
);
460 * Take a reference of the kmod module, incrementing its refcount.
462 * Returns: the passed @module with its refcount incremented.
464 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
474 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
478 if (*(_list) != NULL) \
483 * kmod_module_new_from_lookup:
484 * @ctx: kmod library context
485 * @given_alias: alias to look for
486 * @list: an empty list where to save the list of modules matching
489 * Create a new list of kmod modules using an alias or module name and lookup
490 * libkmod's configuration files and indexes in order to find the module.
491 * Once it's found in one of the places, it stops searching and create the
492 * list of modules that is saved in @list.
494 * The search order is: 1. aliases in configuration file; 2. module names in
495 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
496 * in modules.alias index.
498 * The initial refcount is 1, and needs to be decremented to release the
499 * resources of the kmod_module. The returned @list must be released by
500 * calling kmod_module_unref_list(). Since libkmod keeps track of all
501 * kmod_modules created, they are all released upon @ctx destruction too. Do
502 * not unref @ctx before all the desired operations with the returned list are
505 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
506 * methods failed, which is basically due to memory allocation fail. If module
507 * is not found, it still returns 0, but @list is an empty list.
509 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
510 const char *given_alias
,
511 struct kmod_list
**list
)
514 char alias
[PATH_MAX
];
516 if (ctx
== NULL
|| given_alias
== NULL
)
519 if (list
== NULL
|| *list
!= NULL
) {
520 ERR(ctx
, "An empty list is needed to create lookup\n");
524 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
525 DBG(ctx
, "invalid alias: %s\n", given_alias
);
529 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
531 /* Aliases from config file override all the others */
532 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
533 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
535 DBG(ctx
, "lookup modules.dep %s\n", alias
);
536 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
537 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
539 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
540 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
541 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
543 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
544 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
545 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
547 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
548 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
549 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
551 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
552 err
= kmod_lookup_alias_from_builtin_file(ctx
, alias
, list
);
553 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
556 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
559 DBG(ctx
, "Failed to lookup %s\n", alias
);
560 kmod_module_unref_list(*list
);
564 #undef CHECK_ERR_AND_FINISH
567 * kmod_module_unref_list:
568 * @list: list of kmod modules
570 * Drop a reference of each kmod module in @list and releases the resources
571 * taken by the list itself.
575 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
577 for (; list
!= NULL
; list
= kmod_list_remove(list
))
578 kmod_module_unref(list
->data
);
584 * kmod_module_get_filtered_blacklist:
585 * @ctx: kmod library context
586 * @input: list of kmod_module to be filtered with blacklist
587 * @output: where to save the new list
589 * This function should not be used. Use kmod_module_apply_filter instead.
591 * Given a list @input, this function filter it out with config's blacklist
592 * and save it in @output.
594 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
597 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
598 const struct kmod_list
*input
,
599 struct kmod_list
**output
)
601 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
604 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
606 if (!mod
->init
.dep
) {
608 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
613 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
624 * kmod_module_get_dependencies:
627 * Search the modules.dep index to find the dependencies of the given @mod.
628 * The result is cached in @mod, so subsequent calls to this function will
629 * return the already searched list of modules.
631 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
632 * that can be released by calling kmod_module_unref_list().
634 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
636 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
641 module_get_dependencies_noref(mod
);
643 kmod_list_foreach(l
, mod
->dep
) {
644 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
646 kmod_module_unref(l
->data
);
656 ERR(mod
->ctx
, "out of memory\n");
657 kmod_module_unref_list(list_new
);
662 * kmod_module_get_module:
663 * @entry: an entry in a list of kmod modules.
665 * Get the kmod module of this @entry in the list, increasing its refcount.
666 * After it's used, unref it. Since the refcount is incremented upon return,
667 * you still have to call kmod_module_unref_list() to release the list of kmod
670 * Returns: NULL on failure or the kmod_module contained in this list entry
671 * with its refcount incremented.
673 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
678 return kmod_module_ref(entry
->data
);
682 * kmod_module_get_name:
685 * Get the name of this kmod module. Name is always available, independently
686 * if it was created by kmod_module_new_from_name() or another function and
687 * it's always normalized (dashes are replaced with underscores).
689 * Returns: the name of this kmod module.
691 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
700 * kmod_module_get_path:
703 * Get the path of this kmod module. If this kmod module was not created by
704 * path, it can search the modules.dep index in order to find out the module
705 * under context's dirname.
707 * Returns: the path of this kmod module or NULL if such information is not
710 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
717 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
719 if (mod
->path
!= NULL
)
725 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
729 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
736 extern long delete_module(const char *name
, unsigned int flags
);
739 * kmod_module_remove_module:
741 * @flags: flags to pass to Linux kernel when removing the module
743 * Remove a module from Linux kernel.
745 * Returns: 0 on success or < 0 on failure.
747 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
755 /* Filter out other flags */
756 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
758 err
= delete_module(mod
->name
, flags
);
761 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
767 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
770 * kmod_module_insert_module:
772 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
773 * behavior of this function.
774 * @options: module's options to pass to Linux Kernel.
776 * Insert a module in Linux kernel. It opens the file pointed by @mod,
777 * mmap'ing it and passing to kernel.
779 * Returns: 0 on success or < 0 on failure. If module is already loaded it
782 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
789 struct kmod_file
*file
;
790 struct kmod_elf
*elf
= NULL
;
792 const char *args
= options
? options
: "";
797 path
= kmod_module_get_path(mod
);
799 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
803 file
= kmod_file_open(mod
->ctx
, path
);
809 if (kmod_file_get_direct(file
)) {
810 unsigned int kernel_flags
= 0;
812 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
)
813 kernel_flags
|= MODULE_INIT_IGNORE_VERMAGIC
;
814 if (flags
& KMOD_INSERT_FORCE_MODVERSION
)
815 kernel_flags
|= MODULE_INIT_IGNORE_MODVERSIONS
;
817 err
= finit_module(kmod_file_get_fd(file
), args
, kernel_flags
);
818 if (err
== 0 || errno
!= ENOSYS
)
822 size
= kmod_file_get_size(file
);
823 mem
= kmod_file_get_contents(file
);
825 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
826 elf
= kmod_elf_new(mem
, size
);
832 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
833 err
= kmod_elf_strip_section(elf
, "__versions");
835 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
838 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
839 err
= kmod_elf_strip_vermagic(elf
);
841 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
844 mem
= kmod_elf_get_memory(elf
);
847 err
= init_module(mem
, size
, args
);
851 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
857 kmod_file_unref(file
);
862 static bool module_is_blacklisted(struct kmod_module
*mod
)
864 struct kmod_ctx
*ctx
= mod
->ctx
;
865 const struct kmod_config
*config
= kmod_get_config(ctx
);
866 const struct kmod_list
*bl
= config
->blacklists
;
867 const struct kmod_list
*l
;
869 kmod_list_foreach(l
, bl
) {
870 const char *modname
= kmod_blacklist_get_modname(l
);
872 if (streq(modname
, mod
->name
))
880 * kmod_module_apply_filter
881 * @ctx: kmod library context
882 * @filter_type: bitmask to filter modules on
883 * @input: list of kmod_module to be filtered
884 * @output: where to save the new list
886 * Given a list @input, this function filter it out by the filter mask
887 * and save it in @output.
889 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
892 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
893 enum kmod_filter filter_type
,
894 const struct kmod_list
*input
,
895 struct kmod_list
**output
)
897 const struct kmod_list
*li
;
899 if (ctx
== NULL
|| output
== NULL
)
906 kmod_list_foreach(li
, input
) {
907 struct kmod_module
*mod
= li
->data
;
908 struct kmod_list
*node
;
910 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
911 module_is_blacklisted(mod
))
914 if ((filter_type
& KMOD_FILTER_BUILTIN
) && mod
->builtin
)
917 node
= kmod_list_append(*output
, mod
);
922 kmod_module_ref(mod
);
928 kmod_module_unref_list(*output
);
933 static int command_do(struct kmod_module
*mod
, const char *type
,
936 const char *modname
= kmod_module_get_name(mod
);
939 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
941 setenv("MODPROBE_MODULE", modname
, 1);
943 unsetenv("MODPROBE_MODULE");
945 if (err
== -1 || WEXITSTATUS(err
)) {
946 ERR(mod
->ctx
, "Error running %s command for %s\n",
949 err
= -WEXITSTATUS(err
);
955 struct probe_insert_cb
{
956 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
960 static int module_do_install_commands(struct kmod_module
*mod
,
962 struct probe_insert_cb
*cb
)
964 const char *command
= kmod_module_get_install_commands(mod
);
967 size_t cmdlen
, options_len
, varlen
;
974 options_len
= strlen(options
);
975 cmdlen
= strlen(command
);
976 varlen
= sizeof("$CMDLINE_OPTS") - 1;
978 cmd
= memdup(command
, cmdlen
+ 1);
982 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
983 size_t prefixlen
= p
- cmd
;
984 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
985 size_t slen
= cmdlen
- varlen
+ options_len
;
986 char *suffix
= p
+ varlen
;
987 char *s
= malloc(slen
+ 1);
992 memcpy(s
, cmd
, p
- cmd
);
993 memcpy(s
+ prefixlen
, options
, options_len
);
994 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
1002 if (cb
->run_install
!= NULL
)
1003 err
= cb
->run_install(mod
, cmd
, cb
->data
);
1005 err
= command_do(mod
, "install", cmd
);
1012 static char *module_options_concat(const char *opt
, const char *xopt
)
1014 // TODO: we might need to check if xopt overrides options on opt
1015 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
1016 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1019 if (optlen
== 0 && xoptlen
== 0)
1022 r
= malloc(optlen
+ xoptlen
+ 2);
1025 memcpy(r
, opt
, optlen
);
1031 memcpy(r
+ optlen
, xopt
, xoptlen
);
1033 r
[optlen
+ xoptlen
] = '\0';
1038 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1040 struct kmod_list
**list
);
1043 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1044 struct kmod_list
**list
)
1046 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1049 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1051 ERR(mod
->ctx
, "could not get softdep: %s\n",
1056 kmod_list_foreach(l
, pre
) {
1057 struct kmod_module
*m
= l
->data
;
1058 err
= __kmod_module_get_probe_list(m
, false, list
);
1063 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1065 kmod_module_unref(mod
);
1070 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1072 kmod_list_foreach(l
, post
) {
1073 struct kmod_module
*m
= l
->data
;
1074 err
= __kmod_module_get_probe_list(m
, false, list
);
1080 kmod_module_unref_list(pre
);
1081 kmod_module_unref_list(post
);
1087 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1089 struct kmod_list
**list
)
1091 struct kmod_list
*dep
, *l
;
1095 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1099 mod
->visited
= true;
1101 dep
= kmod_module_get_dependencies(mod
);
1102 kmod_list_foreach(l
, dep
) {
1103 struct kmod_module
*m
= l
->data
;
1104 err
= __kmod_module_fill_softdep(m
, list
);
1110 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1112 kmod_module_unref(mod
);
1117 mod
->ignorecmd
= true;
1119 err
= __kmod_module_fill_softdep(mod
, list
);
1122 kmod_module_unref_list(dep
);
1126 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1128 struct kmod_list
**list
)
1132 assert(mod
!= NULL
);
1133 assert(list
!= NULL
&& *list
== NULL
);
1136 * Make sure we don't get screwed by previous calls to this function
1138 kmod_set_modules_visited(mod
->ctx
, false);
1140 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1142 kmod_module_unref_list(*list
);
1150 * kmod_module_probe_insert_module:
1152 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1153 * behavior of this function.
1154 * @extra_options: module's options to pass to Linux Kernel. It applies only
1155 * to @mod, not to its dependencies.
1156 * @run_install: function to run when @mod is backed by an install command.
1157 * @data: data to give back to @run_install callback
1158 * @print_action: function to call with the action being taken (install or
1159 * insmod). It's useful for tools like modprobe when running with verbose
1160 * output or in dry-run mode.
1162 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1163 * install commands and applying blacklist.
1165 * If @run_install is NULL, this function will fork and exec by calling
1166 * system(3). Don't pass a NULL argument in @run_install if your binary is
1167 * setuid/setgid (see warning in system(3)). If you need control over the
1168 * execution of an install command, give a callback function instead.
1170 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1173 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1174 unsigned int flags
, const char *extra_options
,
1175 int (*run_install
)(struct kmod_module
*m
,
1176 const char *cmd
, void *data
),
1178 void (*print_action
)(struct kmod_module
*m
,
1180 const char *options
))
1182 struct kmod_list
*list
= NULL
, *l
;
1183 struct probe_insert_cb cb
;
1189 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1190 && module_is_inkernel(mod
)) {
1191 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1198 * Ugly assignement + check. We need to check if we were told to check
1199 * blacklist and also return the reason why we failed.
1200 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1201 * module is an alias, so we also need to check it
1203 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1204 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1205 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1206 if (module_is_blacklisted(mod
))
1210 err
= kmod_module_get_probe_list(mod
,
1211 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1215 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1216 struct kmod_list
*filtered
= NULL
;
1218 err
= kmod_module_apply_filter(mod
->ctx
,
1219 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1223 kmod_module_unref_list(list
);
1224 if (filtered
== NULL
)
1225 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1230 cb
.run_install
= run_install
;
1231 cb
.data
= (void *) data
;
1233 kmod_list_foreach(l
, list
) {
1234 struct kmod_module
*m
= l
->data
;
1235 const char *moptions
= kmod_module_get_options(m
);
1236 const char *cmd
= kmod_module_get_install_commands(m
);
1239 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1240 && module_is_inkernel(m
)) {
1241 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1247 options
= module_options_concat(moptions
,
1248 m
== mod
? extra_options
: NULL
);
1250 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1251 if (print_action
!= NULL
)
1252 print_action(m
, true, options
?: "");
1254 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1255 err
= module_do_install_commands(m
, options
,
1258 if (print_action
!= NULL
)
1259 print_action(m
, false, options
?: "");
1261 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1262 err
= kmod_module_insert_module(m
, flags
,
1270 * Treat "already loaded" error. If we were told to stop on
1271 * already loaded and the module being loaded is not a softdep
1272 * or dep, bail out. Otherwise, just ignore and continue.
1274 * We need to check here because of race conditions. We
1275 * checked first if module was already loaded but it may have
1276 * been loaded between the check and the moment we try to
1279 if (err
== -EEXIST
&& m
== mod
&&
1280 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1289 kmod_module_unref_list(list
);
1294 * kmod_module_get_options:
1297 * Get options of this kmod module. Options come from the configuration file
1298 * and are cached in @mod. The first call to this function will search for
1299 * this module in configuration and subsequent calls return the cached string.
1301 * Returns: a string with all the options separated by spaces. This string is
1302 * owned by @mod, do not free it.
1304 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1309 if (!mod
->init
.options
) {
1311 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1312 const struct kmod_list
*l
;
1313 const struct kmod_config
*config
;
1317 config
= kmod_get_config(mod
->ctx
);
1319 kmod_list_foreach(l
, config
->options
) {
1320 const char *modname
= kmod_option_get_modname(l
);
1325 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1326 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1327 streq(modname
, mod
->alias
))))
1330 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1331 str
= kmod_option_get_options(l
);
1336 tmp
= realloc(opts
, optslen
+ len
+ 2);
1345 opts
[optslen
] = ' ';
1349 memcpy(opts
+ optslen
, str
, len
);
1351 opts
[optslen
] = '\0';
1354 m
->init
.options
= true;
1358 return mod
->options
;
1361 ERR(mod
->ctx
, "out of memory\n");
1366 * kmod_module_get_install_commands:
1369 * Get install commands for this kmod module. Install commands come from the
1370 * configuration file and are cached in @mod. The first call to this function
1371 * will search for this module in configuration and subsequent calls return
1372 * the cached string. The install commands are returned as they were in the
1373 * configuration, concatenated by ';'. No other processing is made in this
1376 * Returns: a string with all install commands separated by semicolons. This
1377 * string is owned by @mod, do not free it.
1379 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1384 if (!mod
->init
.install_commands
) {
1386 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1387 const struct kmod_list
*l
;
1388 const struct kmod_config
*config
;
1390 config
= kmod_get_config(mod
->ctx
);
1392 kmod_list_foreach(l
, config
->install_commands
) {
1393 const char *modname
= kmod_command_get_modname(l
);
1395 if (fnmatch(modname
, mod
->name
, 0) != 0)
1398 m
->install_commands
= kmod_command_get_command(l
);
1401 * find only the first command, as modprobe from
1402 * module-init-tools does
1407 m
->init
.install_commands
= true;
1410 return mod
->install_commands
;
1413 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1415 mod
->init
.install_commands
= true;
1416 mod
->install_commands
= cmd
;
1419 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1421 struct kmod_list
*ret
= NULL
;
1424 for (i
= 0; i
< count
; i
++) {
1425 const char *depname
= array
[i
];
1426 struct kmod_list
*lst
= NULL
;
1429 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1431 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1433 } else if (lst
!= NULL
)
1434 ret
= kmod_list_append_list(ret
, lst
);
1440 * kmod_module_get_softdeps:
1442 * @pre: where to save the list of preceding soft dependencies.
1443 * @post: where to save the list of post soft dependencies.
1445 * Get soft dependencies for this kmod module. Soft dependencies come
1446 * from configuration file and are not cached in @mod because it may include
1447 * dependency cycles that would make we leak kmod_module. Any call
1448 * to this function will search for this module in configuration, allocate a
1449 * list and return the result.
1451 * Both @pre and @post are newly created list of kmod_module and
1452 * should be unreferenced with kmod_module_unref_list().
1454 * Returns: 0 on success or < 0 otherwise.
1456 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1457 struct kmod_list
**pre
,
1458 struct kmod_list
**post
)
1460 const struct kmod_list
*l
;
1461 const struct kmod_config
*config
;
1463 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1466 assert(*pre
== NULL
);
1467 assert(*post
== NULL
);
1469 config
= kmod_get_config(mod
->ctx
);
1471 kmod_list_foreach(l
, config
->softdeps
) {
1472 const char *modname
= kmod_softdep_get_name(l
);
1473 const char * const *array
;
1476 if (fnmatch(modname
, mod
->name
, 0) != 0)
1479 array
= kmod_softdep_get_pre(l
, &count
);
1480 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1481 array
= kmod_softdep_get_post(l
, &count
);
1482 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1485 * find only the first command, as modprobe from
1486 * module-init-tools does
1495 * kmod_module_get_remove_commands:
1498 * Get remove commands for this kmod module. Remove commands come from the
1499 * configuration file and are cached in @mod. The first call to this function
1500 * will search for this module in configuration and subsequent calls return
1501 * the cached string. The remove commands are returned as they were in the
1502 * configuration, concatenated by ';'. No other processing is made in this
1505 * Returns: a string with all remove commands separated by semicolons. This
1506 * string is owned by @mod, do not free it.
1508 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1513 if (!mod
->init
.remove_commands
) {
1515 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1516 const struct kmod_list
*l
;
1517 const struct kmod_config
*config
;
1519 config
= kmod_get_config(mod
->ctx
);
1521 kmod_list_foreach(l
, config
->remove_commands
) {
1522 const char *modname
= kmod_command_get_modname(l
);
1524 if (fnmatch(modname
, mod
->name
, 0) != 0)
1527 m
->remove_commands
= kmod_command_get_command(l
);
1530 * find only the first command, as modprobe from
1531 * module-init-tools does
1536 m
->init
.remove_commands
= true;
1539 return mod
->remove_commands
;
1542 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1544 mod
->init
.remove_commands
= true;
1545 mod
->remove_commands
= cmd
;
1549 * SECTION:libkmod-loaded
1550 * @short_description: currently loaded modules
1552 * Information about currently loaded modules, as reported by Linux kernel.
1553 * These information are not cached by libkmod and are always read from /sys
1554 * and /proc/modules.
1558 * kmod_module_new_from_loaded:
1559 * @ctx: kmod library context
1560 * @list: where to save the list of loaded modules
1562 * Create a new list of kmod modules with all modules currently loaded in
1563 * kernel. It uses /proc/modules to get the names of loaded modules and to
1564 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1565 * They are put in @list in no particular order.
1567 * The initial refcount is 1, and needs to be decremented to release the
1568 * resources of the kmod_module. The returned @list must be released by
1569 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1570 * kmod_modules created, they are all released upon @ctx destruction too. Do
1571 * not unref @ctx before all the desired operations with the returned list are
1574 * Returns: 0 on success or < 0 on error.
1576 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1577 struct kmod_list
**list
)
1579 struct kmod_list
*l
= NULL
;
1583 if (ctx
== NULL
|| list
== NULL
)
1586 fp
= fopen("/proc/modules", "re");
1589 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1593 while (fgets(line
, sizeof(line
), fp
)) {
1594 struct kmod_module
*m
;
1595 struct kmod_list
*node
;
1597 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1599 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1601 ERR(ctx
, "could not get module from name '%s': %s\n",
1602 name
, strerror(-err
));
1606 node
= kmod_list_append(l
, m
);
1610 ERR(ctx
, "out of memory\n");
1611 kmod_module_unref(m
);
1622 * kmod_module_initstate_str:
1623 * @state: the state as returned by kmod_module_get_initstate()
1625 * Translate a initstate to a string.
1627 * Returns: the string associated to the @state. This string is statically
1628 * allocated, do not free it.
1630 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1633 case KMOD_MODULE_BUILTIN
:
1635 case KMOD_MODULE_LIVE
:
1637 case KMOD_MODULE_COMING
:
1639 case KMOD_MODULE_GOING
:
1647 * kmod_module_get_initstate:
1650 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1653 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1655 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1657 char path
[PATH_MAX
], buf
[32];
1658 int fd
, err
, pathlen
;
1664 return KMOD_MODULE_BUILTIN
;
1666 pathlen
= snprintf(path
, sizeof(path
),
1667 "/sys/module/%s/initstate", mod
->name
);
1668 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1672 DBG(mod
->ctx
, "could not open '%s': %s\n",
1673 path
, strerror(-err
));
1675 if (pathlen
> (int)sizeof("/initstate") - 1) {
1677 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1678 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1679 return KMOD_MODULE_BUILTIN
;
1682 DBG(mod
->ctx
, "could not open '%s': %s\n",
1683 path
, strerror(-err
));
1687 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1690 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1691 path
, strerror(-err
));
1695 if (streq(buf
, "live\n"))
1696 return KMOD_MODULE_LIVE
;
1697 else if (streq(buf
, "coming\n"))
1698 return KMOD_MODULE_COMING
;
1699 else if (streq(buf
, "going\n"))
1700 return KMOD_MODULE_GOING
;
1702 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1707 * kmod_module_get_size:
1710 * Get the size of this kmod module as returned by Linux kernel. If supported,
1711 * the size is read from the coresize attribute in /sys/module. For older
1712 * kernels, this falls back on /proc/modules and searches for the specified
1713 * module to get its size.
1715 * Returns: the size of this kmod module.
1717 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1722 long size
= -ENOENT
;
1728 /* try to open the module dir in /sys. If this fails, don't
1729 * bother trying to find the size as we know the module isn't
1732 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1733 dfd
= open(line
, O_RDONLY
);
1737 /* available as of linux 3.3.x */
1738 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1740 if (read_str_long(cfd
, &size
, 10) < 0)
1741 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1746 /* fall back on parsing /proc/modules */
1747 fp
= fopen("/proc/modules", "re");
1751 "could not open /proc/modules: %s\n", strerror(errno
));
1755 while (fgets(line
, sizeof(line
), fp
)) {
1756 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1760 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1763 tok
= strtok_r(NULL
, " \t", &saveptr
);
1766 "invalid line format at /proc/modules:%d\n", lineno
);
1770 value
= strtol(tok
, &endptr
, 10);
1771 if (endptr
== tok
|| *endptr
!= '\0') {
1773 "invalid line format at /proc/modules:%d\n", lineno
);
1788 * kmod_module_get_refcnt:
1791 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1794 * Returns: 0 on success or < 0 on failure.
1796 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1798 char path
[PATH_MAX
];
1805 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1806 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1809 DBG(mod
->ctx
, "could not open '%s': %s\n",
1810 path
, strerror(errno
));
1814 err
= read_str_long(fd
, &refcnt
, 10);
1817 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1818 path
, strerror(-err
));
1826 * kmod_module_get_holders:
1829 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1830 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1832 * Returns: a new list of kmod modules on success or NULL on failure.
1834 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1836 char dname
[PATH_MAX
];
1837 struct kmod_list
*list
= NULL
;
1840 if (mod
== NULL
|| mod
->ctx
== NULL
)
1843 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1847 ERR(mod
->ctx
, "could not open '%s': %s\n",
1848 dname
, strerror(errno
));
1853 struct dirent de
, *entp
;
1854 struct kmod_module
*holder
;
1855 struct kmod_list
*l
;
1858 err
= readdir_r(d
, &de
, &entp
);
1860 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1861 mod
->name
, strerror(-err
));
1868 if (de
.d_name
[0] == '.') {
1869 if (de
.d_name
[1] == '\0' ||
1870 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1874 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1876 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1877 de
.d_name
, strerror(-err
));
1881 l
= kmod_list_append(list
, holder
);
1885 ERR(mod
->ctx
, "out of memory\n");
1886 kmod_module_unref(holder
);
1896 kmod_module_unref_list(list
);
1900 struct kmod_module_section
{
1901 unsigned long address
;
1905 static void kmod_module_section_free(struct kmod_module_section
*section
)
1911 * kmod_module_get_sections:
1914 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1915 * structure contained in this list is internal to libkmod and their fields
1916 * can be obtained by calling kmod_module_section_get_name() and
1917 * kmod_module_section_get_address().
1919 * After use, free the @list by calling kmod_module_section_free_list().
1921 * Returns: a new list of kmod module sections on success or NULL on failure.
1923 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1925 char dname
[PATH_MAX
];
1926 struct kmod_list
*list
= NULL
;
1933 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1937 ERR(mod
->ctx
, "could not open '%s': %s\n",
1938 dname
, strerror(errno
));
1945 struct dirent de
, *entp
;
1946 struct kmod_module_section
*section
;
1947 struct kmod_list
*l
;
1948 unsigned long address
;
1952 err
= readdir_r(d
, &de
, &entp
);
1954 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1955 mod
->name
, strerror(-err
));
1959 if (de
.d_name
[0] == '.') {
1960 if (de
.d_name
[1] == '\0' ||
1961 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1965 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
1967 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1972 err
= read_str_ulong(fd
, &address
, 16);
1975 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1980 namesz
= strlen(de
.d_name
) + 1;
1981 section
= malloc(sizeof(*section
) + namesz
);
1983 if (section
== NULL
) {
1984 ERR(mod
->ctx
, "out of memory\n");
1988 section
->address
= address
;
1989 memcpy(section
->name
, de
.d_name
, namesz
);
1991 l
= kmod_list_append(list
, section
);
1995 ERR(mod
->ctx
, "out of memory\n");
2006 kmod_module_unref_list(list
);
2011 * kmod_module_section_get_module_name:
2012 * @entry: a list entry representing a kmod module section
2014 * Get the name of a kmod module section.
2016 * After use, free the @list by calling kmod_module_section_free_list().
2018 * Returns: the name of this kmod module section on success or NULL on
2019 * failure. The string is owned by the section, do not free it.
2021 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2023 struct kmod_module_section
*section
;
2028 section
= entry
->data
;
2029 return section
->name
;
2033 * kmod_module_section_get_address:
2034 * @entry: a list entry representing a kmod module section
2036 * Get the address of a kmod module section.
2038 * After use, free the @list by calling kmod_module_section_free_list().
2040 * Returns: the address of this kmod module section on success or ULONG_MAX
2043 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2045 struct kmod_module_section
*section
;
2048 return (unsigned long)-1;
2050 section
= entry
->data
;
2051 return section
->address
;
2055 * kmod_module_section_free_list:
2056 * @list: kmod module section list
2058 * Release the resources taken by @list
2060 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2063 kmod_module_section_free(list
->data
);
2064 list
= kmod_list_remove(list
);
2068 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2070 if (mod
->file
== NULL
) {
2071 const char *path
= kmod_module_get_path(mod
);
2078 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2080 if (mod
->file
== NULL
)
2084 return kmod_file_get_elf(mod
->file
);
2087 struct kmod_module_info
{
2092 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2094 struct kmod_module_info
*info
;
2096 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2100 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2102 memcpy(info
->key
, key
, keylen
);
2103 info
->key
[keylen
] = '\0';
2104 memcpy(info
->value
, value
, valuelen
);
2105 info
->value
[valuelen
] = '\0';
2109 static void kmod_module_info_free(struct kmod_module_info
*info
)
2114 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2116 struct kmod_module_info
*info
;
2117 struct kmod_list
*n
;
2119 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2122 n
= kmod_list_append(*list
, info
);
2126 kmod_module_info_free(info
);
2131 * kmod_module_get_info:
2133 * @list: where to return list of module information. Use
2134 * kmod_module_info_get_key() and
2135 * kmod_module_info_get_value(). Release this list with
2136 * kmod_module_info_free_list()
2138 * Get a list of entries in ELF section ".modinfo", these contain
2139 * alias, license, depends, vermagic and other keys with respective
2140 * values. If the module is signed (CONFIG_MODULE_SIG), information
2141 * about the module signature is included as well: signer,
2142 * sig_key and sig_hashalgo.
2144 * After use, free the @list by calling kmod_module_info_free_list().
2146 * Returns: 0 on success or < 0 otherwise.
2148 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2150 struct kmod_elf
*elf
;
2152 int i
, count
, ret
= -ENOMEM
;
2153 struct kmod_signature_info sig_info
;
2155 if (mod
== NULL
|| list
== NULL
)
2158 assert(*list
== NULL
);
2160 elf
= kmod_module_get_elf(mod
);
2164 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2168 for (i
= 0; i
< count
; i
++) {
2169 struct kmod_list
*n
;
2170 const char *key
, *value
;
2171 size_t keylen
, valuelen
;
2174 value
= strchr(key
, '=');
2175 if (value
== NULL
) {
2176 keylen
= strlen(key
);
2180 keylen
= value
- key
;
2182 valuelen
= strlen(value
);
2185 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2190 if (kmod_module_signature_info(mod
->file
, &sig_info
)) {
2191 struct kmod_list
*n
;
2194 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2195 sig_info
.signer
, sig_info
.signer_len
);
2200 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2201 key_hex
= malloc(sig_info
.key_id_len
* 3);
2202 if (key_hex
== NULL
)
2204 for (i
= 0; i
< (int)sig_info
.key_id_len
; i
++) {
2205 sprintf(key_hex
+ i
* 3, "%02X",
2206 (unsigned char)sig_info
.key_id
[i
]);
2207 if (i
< (int)sig_info
.key_id_len
- 1)
2208 key_hex
[i
* 3 + 2] = ':';
2210 n
= kmod_module_info_append(list
, "sig_key", strlen("sig_key"),
2211 key_hex
, sig_info
.key_id_len
* 3 - 1);
2217 n
= kmod_module_info_append(list
,
2218 "sig_hashalgo", strlen("sig_hashalgo"),
2219 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2225 * Omit sig_info.id_type and sig_info.algo for now, as these
2226 * are currently constant.
2233 kmod_module_info_free_list(*list
);
2241 * kmod_module_info_get_key:
2242 * @entry: a list entry representing a kmod module info
2244 * Get the key of a kmod module info.
2246 * Returns: the key of this kmod module info on success or NULL on
2247 * failure. The string is owned by the info, do not free it.
2249 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2251 struct kmod_module_info
*info
;
2261 * kmod_module_info_get_value:
2262 * @entry: a list entry representing a kmod module info
2264 * Get the value of a kmod module info.
2266 * Returns: the value of this kmod module info on success or NULL on
2267 * failure. The string is owned by the info, do not free it.
2269 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2271 struct kmod_module_info
*info
;
2281 * kmod_module_info_free_list:
2282 * @list: kmod module info list
2284 * Release the resources taken by @list
2286 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2289 kmod_module_info_free(list
->data
);
2290 list
= kmod_list_remove(list
);
2294 struct kmod_module_version
{
2299 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2301 struct kmod_module_version
*mv
;
2302 size_t symbollen
= strlen(symbol
) + 1;
2304 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2309 memcpy(mv
->symbol
, symbol
, symbollen
);
2313 static void kmod_module_version_free(struct kmod_module_version
*version
)
2319 * kmod_module_get_versions:
2321 * @list: where to return list of module versions. Use
2322 * kmod_module_version_get_symbol() and
2323 * kmod_module_version_get_crc(). Release this list with
2324 * kmod_module_versions_free_list()
2326 * Get a list of entries in ELF section "__versions".
2328 * After use, free the @list by calling kmod_module_versions_free_list().
2330 * Returns: 0 on success or < 0 otherwise.
2332 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2334 struct kmod_elf
*elf
;
2335 struct kmod_modversion
*versions
;
2336 int i
, count
, ret
= 0;
2338 if (mod
== NULL
|| list
== NULL
)
2341 assert(*list
== NULL
);
2343 elf
= kmod_module_get_elf(mod
);
2347 count
= kmod_elf_get_modversions(elf
, &versions
);
2351 for (i
= 0; i
< count
; i
++) {
2352 struct kmod_module_version
*mv
;
2353 struct kmod_list
*n
;
2355 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2358 kmod_module_versions_free_list(*list
);
2363 n
= kmod_list_append(*list
, mv
);
2367 kmod_module_version_free(mv
);
2368 kmod_module_versions_free_list(*list
);
2382 * kmod_module_version_get_symbol:
2383 * @entry: a list entry representing a kmod module versions
2385 * Get the symbol of a kmod module versions.
2387 * Returns: the symbol of this kmod module versions on success or NULL
2388 * on failure. The string is owned by the versions, do not free it.
2390 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2392 struct kmod_module_version
*version
;
2397 version
= entry
->data
;
2398 return version
->symbol
;
2402 * kmod_module_version_get_crc:
2403 * @entry: a list entry representing a kmod module version
2405 * Get the crc of a kmod module version.
2407 * Returns: the crc of this kmod module version on success or NULL on
2408 * failure. The string is owned by the version, do not free it.
2410 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2412 struct kmod_module_version
*version
;
2417 version
= entry
->data
;
2418 return version
->crc
;
2422 * kmod_module_versions_free_list:
2423 * @list: kmod module versions list
2425 * Release the resources taken by @list
2427 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2430 kmod_module_version_free(list
->data
);
2431 list
= kmod_list_remove(list
);
2435 struct kmod_module_symbol
{
2440 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2442 struct kmod_module_symbol
*mv
;
2443 size_t symbollen
= strlen(symbol
) + 1;
2445 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2450 memcpy(mv
->symbol
, symbol
, symbollen
);
2454 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2460 * kmod_module_get_symbols:
2462 * @list: where to return list of module symbols. Use
2463 * kmod_module_symbol_get_symbol() and
2464 * kmod_module_symbol_get_crc(). Release this list with
2465 * kmod_module_symbols_free_list()
2467 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2469 * After use, free the @list by calling kmod_module_symbols_free_list().
2471 * Returns: 0 on success or < 0 otherwise.
2473 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2475 struct kmod_elf
*elf
;
2476 struct kmod_modversion
*symbols
;
2477 int i
, count
, ret
= 0;
2479 if (mod
== NULL
|| list
== NULL
)
2482 assert(*list
== NULL
);
2484 elf
= kmod_module_get_elf(mod
);
2488 count
= kmod_elf_get_symbols(elf
, &symbols
);
2492 for (i
= 0; i
< count
; i
++) {
2493 struct kmod_module_symbol
*mv
;
2494 struct kmod_list
*n
;
2496 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2499 kmod_module_symbols_free_list(*list
);
2504 n
= kmod_list_append(*list
, mv
);
2508 kmod_module_symbol_free(mv
);
2509 kmod_module_symbols_free_list(*list
);
2523 * kmod_module_symbol_get_symbol:
2524 * @entry: a list entry representing a kmod module symbols
2526 * Get the symbol of a kmod module symbols.
2528 * Returns: the symbol of this kmod module symbols on success or NULL
2529 * on failure. The string is owned by the symbols, do not free it.
2531 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2533 struct kmod_module_symbol
*symbol
;
2538 symbol
= entry
->data
;
2539 return symbol
->symbol
;
2543 * kmod_module_symbol_get_crc:
2544 * @entry: a list entry representing a kmod module symbol
2546 * Get the crc of a kmod module symbol.
2548 * Returns: the crc of this kmod module symbol on success or NULL on
2549 * failure. The string is owned by the symbol, do not free it.
2551 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2553 struct kmod_module_symbol
*symbol
;
2558 symbol
= entry
->data
;
2563 * kmod_module_symbols_free_list:
2564 * @list: kmod module symbols list
2566 * Release the resources taken by @list
2568 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2571 kmod_module_symbol_free(list
->data
);
2572 list
= kmod_list_remove(list
);
2576 struct kmod_module_dependency_symbol
{
2582 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2584 struct kmod_module_dependency_symbol
*mv
;
2585 size_t symbollen
= strlen(symbol
) + 1;
2587 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2593 memcpy(mv
->symbol
, symbol
, symbollen
);
2597 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2599 free(dependency_symbol
);
2603 * kmod_module_get_dependency_symbols:
2605 * @list: where to return list of module dependency_symbols. Use
2606 * kmod_module_dependency_symbol_get_symbol() and
2607 * kmod_module_dependency_symbol_get_crc(). Release this list with
2608 * kmod_module_dependency_symbols_free_list()
2610 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2612 * After use, free the @list by calling
2613 * kmod_module_dependency_symbols_free_list().
2615 * Returns: 0 on success or < 0 otherwise.
2617 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2619 struct kmod_elf
*elf
;
2620 struct kmod_modversion
*symbols
;
2621 int i
, count
, ret
= 0;
2623 if (mod
== NULL
|| list
== NULL
)
2626 assert(*list
== NULL
);
2628 elf
= kmod_module_get_elf(mod
);
2632 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2636 for (i
= 0; i
< count
; i
++) {
2637 struct kmod_module_dependency_symbol
*mv
;
2638 struct kmod_list
*n
;
2640 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2645 kmod_module_dependency_symbols_free_list(*list
);
2650 n
= kmod_list_append(*list
, mv
);
2654 kmod_module_dependency_symbol_free(mv
);
2655 kmod_module_dependency_symbols_free_list(*list
);
2669 * kmod_module_dependency_symbol_get_symbol:
2670 * @entry: a list entry representing a kmod module dependency_symbols
2672 * Get the dependency symbol of a kmod module
2674 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2675 * on failure. The string is owned by the dependency_symbols, do not free it.
2677 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2679 struct kmod_module_dependency_symbol
*dependency_symbol
;
2684 dependency_symbol
= entry
->data
;
2685 return dependency_symbol
->symbol
;
2689 * kmod_module_dependency_symbol_get_crc:
2690 * @entry: a list entry representing a kmod module dependency_symbol
2692 * Get the crc of a kmod module dependency_symbol.
2694 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2695 * failure. The string is owned by the dependency_symbol, do not free it.
2697 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2699 struct kmod_module_dependency_symbol
*dependency_symbol
;
2704 dependency_symbol
= entry
->data
;
2705 return dependency_symbol
->crc
;
2709 * kmod_module_dependency_symbol_get_bind:
2710 * @entry: a list entry representing a kmod module dependency_symbol
2712 * Get the bind type of a kmod module dependency_symbol.
2714 * Returns: the bind of this kmod module dependency_symbol on success
2715 * or < 0 on failure.
2717 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2719 struct kmod_module_dependency_symbol
*dependency_symbol
;
2724 dependency_symbol
= entry
->data
;
2725 return dependency_symbol
->bind
;
2729 * kmod_module_dependency_symbols_free_list:
2730 * @list: kmod module dependency_symbols list
2732 * Release the resources taken by @list
2734 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2737 kmod_module_dependency_symbol_free(list
->data
);
2738 list
= kmod_list_remove(list
);