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-internal.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, valid flags are
742 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
743 * use by a kernel subsystem or other process;
744 * KMOD_REMOVE_NOWAIT: return immediately. It will fail if the module
745 * is in using and KMOD_REMOVE_FORCE is not specified.
746 * If this module is in use by any kernel subsystem or process, not using
747 * this flag will cause the call to block indefinitely, until the module
748 * is not in use anymore. Always use this flag, it's deprecated not using
749 * it and the default behavior might change in future to always set it.
751 * Remove a module from Linux kernel.
753 * Returns: 0 on success or < 0 on failure.
755 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
763 /* Filter out other flags */
764 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
766 err
= delete_module(mod
->name
, flags
);
769 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
775 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
778 * kmod_module_insert_module:
780 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
781 * behavior of this function, valid flags are
782 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
783 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
784 * @options: module's options to pass to Linux Kernel.
786 * Insert a module in Linux kernel. It opens the file pointed by @mod,
787 * mmap'ing it and passing to kernel.
789 * Returns: 0 on success or < 0 on failure. If module is already loaded it
792 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
799 struct kmod_file
*file
;
800 struct kmod_elf
*elf
= NULL
;
802 const char *args
= options
? options
: "";
807 path
= kmod_module_get_path(mod
);
809 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
813 file
= kmod_file_open(mod
->ctx
, path
);
819 if (kmod_file_get_direct(file
)) {
820 unsigned int kernel_flags
= 0;
822 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
)
823 kernel_flags
|= MODULE_INIT_IGNORE_VERMAGIC
;
824 if (flags
& KMOD_INSERT_FORCE_MODVERSION
)
825 kernel_flags
|= MODULE_INIT_IGNORE_MODVERSIONS
;
827 err
= finit_module(kmod_file_get_fd(file
), args
, kernel_flags
);
828 if (err
== 0 || errno
!= ENOSYS
)
832 size
= kmod_file_get_size(file
);
833 mem
= kmod_file_get_contents(file
);
835 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
836 elf
= kmod_elf_new(mem
, size
);
842 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
843 err
= kmod_elf_strip_section(elf
, "__versions");
845 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
848 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
849 err
= kmod_elf_strip_vermagic(elf
);
851 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
854 mem
= kmod_elf_get_memory(elf
);
857 err
= init_module(mem
, size
, args
);
861 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
867 kmod_file_unref(file
);
872 static bool module_is_blacklisted(struct kmod_module
*mod
)
874 struct kmod_ctx
*ctx
= mod
->ctx
;
875 const struct kmod_config
*config
= kmod_get_config(ctx
);
876 const struct kmod_list
*bl
= config
->blacklists
;
877 const struct kmod_list
*l
;
879 kmod_list_foreach(l
, bl
) {
880 const char *modname
= kmod_blacklist_get_modname(l
);
882 if (streq(modname
, mod
->name
))
890 * kmod_module_apply_filter
891 * @ctx: kmod library context
892 * @filter_type: bitmask to filter modules out, valid types are
893 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
894 * KMOD_FILTER_BUILTIN: filter builtin modules out.
895 * @input: list of kmod_module to be filtered
896 * @output: where to save the new list
898 * Given a list @input, this function filter it out by the filter mask
899 * and save it in @output.
901 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
904 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
905 enum kmod_filter filter_type
,
906 const struct kmod_list
*input
,
907 struct kmod_list
**output
)
909 const struct kmod_list
*li
;
911 if (ctx
== NULL
|| output
== NULL
)
918 kmod_list_foreach(li
, input
) {
919 struct kmod_module
*mod
= li
->data
;
920 struct kmod_list
*node
;
922 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
923 module_is_blacklisted(mod
))
926 if ((filter_type
& KMOD_FILTER_BUILTIN
) && mod
->builtin
)
929 node
= kmod_list_append(*output
, mod
);
934 kmod_module_ref(mod
);
940 kmod_module_unref_list(*output
);
945 static int command_do(struct kmod_module
*mod
, const char *type
,
948 const char *modname
= kmod_module_get_name(mod
);
951 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
953 setenv("MODPROBE_MODULE", modname
, 1);
955 unsetenv("MODPROBE_MODULE");
957 if (err
== -1 || WEXITSTATUS(err
)) {
958 ERR(mod
->ctx
, "Error running %s command for %s\n",
961 err
= -WEXITSTATUS(err
);
967 struct probe_insert_cb
{
968 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
972 static int module_do_install_commands(struct kmod_module
*mod
,
974 struct probe_insert_cb
*cb
)
976 const char *command
= kmod_module_get_install_commands(mod
);
979 size_t cmdlen
, options_len
, varlen
;
986 options_len
= strlen(options
);
987 cmdlen
= strlen(command
);
988 varlen
= sizeof("$CMDLINE_OPTS") - 1;
990 cmd
= memdup(command
, cmdlen
+ 1);
994 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
995 size_t prefixlen
= p
- cmd
;
996 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
997 size_t slen
= cmdlen
- varlen
+ options_len
;
998 char *suffix
= p
+ varlen
;
999 char *s
= malloc(slen
+ 1);
1004 memcpy(s
, cmd
, p
- cmd
);
1005 memcpy(s
+ prefixlen
, options
, options_len
);
1006 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
1014 if (cb
->run_install
!= NULL
)
1015 err
= cb
->run_install(mod
, cmd
, cb
->data
);
1017 err
= command_do(mod
, "install", cmd
);
1024 static char *module_options_concat(const char *opt
, const char *xopt
)
1026 // TODO: we might need to check if xopt overrides options on opt
1027 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
1028 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1031 if (optlen
== 0 && xoptlen
== 0)
1034 r
= malloc(optlen
+ xoptlen
+ 2);
1037 memcpy(r
, opt
, optlen
);
1043 memcpy(r
+ optlen
, xopt
, xoptlen
);
1045 r
[optlen
+ xoptlen
] = '\0';
1050 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1052 struct kmod_list
**list
);
1055 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1056 struct kmod_list
**list
)
1058 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1061 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1063 ERR(mod
->ctx
, "could not get softdep: %s\n",
1068 kmod_list_foreach(l
, pre
) {
1069 struct kmod_module
*m
= l
->data
;
1070 err
= __kmod_module_get_probe_list(m
, false, list
);
1075 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1077 kmod_module_unref(mod
);
1082 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1084 kmod_list_foreach(l
, post
) {
1085 struct kmod_module
*m
= l
->data
;
1086 err
= __kmod_module_get_probe_list(m
, false, list
);
1092 kmod_module_unref_list(pre
);
1093 kmod_module_unref_list(post
);
1099 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1101 struct kmod_list
**list
)
1103 struct kmod_list
*dep
, *l
;
1107 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1111 mod
->visited
= true;
1113 dep
= kmod_module_get_dependencies(mod
);
1114 kmod_list_foreach(l
, dep
) {
1115 struct kmod_module
*m
= l
->data
;
1116 err
= __kmod_module_fill_softdep(m
, list
);
1122 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1124 kmod_module_unref(mod
);
1129 mod
->ignorecmd
= true;
1131 err
= __kmod_module_fill_softdep(mod
, list
);
1134 kmod_module_unref_list(dep
);
1138 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1140 struct kmod_list
**list
)
1144 assert(mod
!= NULL
);
1145 assert(list
!= NULL
&& *list
== NULL
);
1148 * Make sure we don't get screwed by previous calls to this function
1150 kmod_set_modules_visited(mod
->ctx
, false);
1152 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1154 kmod_module_unref_list(*list
);
1162 * kmod_module_probe_insert_module:
1164 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1165 * behavior of this function, valid flags are
1166 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1167 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1168 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1169 * commands and softdeps configured in the system;
1170 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1171 * live in kernel or not;
1172 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1173 * associated callback function;
1174 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1175 * and the module is already live in kernel, the function will fail if this
1176 * flag is specified;
1177 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1178 * filter to this module and its dependencies. If any of the dependencies (or
1179 * the module) is blacklisted, the probe will fail, unless the blacklisted
1180 * module is already live in kernel;
1181 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1182 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1183 * alias and is blacklisted.
1184 * @extra_options: module's options to pass to Linux Kernel. It applies only
1185 * to @mod, not to its dependencies.
1186 * @run_install: function to run when @mod is backed by an install command.
1187 * @data: data to give back to @run_install callback
1188 * @print_action: function to call with the action being taken (install or
1189 * insmod). It's useful for tools like modprobe when running with verbose
1190 * output or in dry-run mode.
1192 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1193 * install commands and applying blacklist.
1195 * If @run_install is NULL, this function will fork and exec by calling
1196 * system(3). Don't pass a NULL argument in @run_install if your binary is
1197 * setuid/setgid (see warning in system(3)). If you need control over the
1198 * execution of an install command, give a callback function instead.
1200 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1203 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1204 unsigned int flags
, const char *extra_options
,
1205 int (*run_install
)(struct kmod_module
*m
,
1206 const char *cmd
, void *data
),
1208 void (*print_action
)(struct kmod_module
*m
,
1210 const char *options
))
1212 struct kmod_list
*list
= NULL
, *l
;
1213 struct probe_insert_cb cb
;
1219 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1220 && module_is_inkernel(mod
)) {
1221 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1228 * Ugly assignement + check. We need to check if we were told to check
1229 * blacklist and also return the reason why we failed.
1230 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1231 * module is an alias, so we also need to check it
1233 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1234 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1235 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1236 if (module_is_blacklisted(mod
))
1240 err
= kmod_module_get_probe_list(mod
,
1241 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1245 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1246 struct kmod_list
*filtered
= NULL
;
1248 err
= kmod_module_apply_filter(mod
->ctx
,
1249 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1253 kmod_module_unref_list(list
);
1254 if (filtered
== NULL
)
1255 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1260 cb
.run_install
= run_install
;
1261 cb
.data
= (void *) data
;
1263 kmod_list_foreach(l
, list
) {
1264 struct kmod_module
*m
= l
->data
;
1265 const char *moptions
= kmod_module_get_options(m
);
1266 const char *cmd
= kmod_module_get_install_commands(m
);
1269 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1270 && module_is_inkernel(m
)) {
1271 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1277 options
= module_options_concat(moptions
,
1278 m
== mod
? extra_options
: NULL
);
1280 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1281 if (print_action
!= NULL
)
1282 print_action(m
, true, options
?: "");
1284 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1285 err
= module_do_install_commands(m
, options
,
1288 if (print_action
!= NULL
)
1289 print_action(m
, false, options
?: "");
1291 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1292 err
= kmod_module_insert_module(m
, flags
,
1300 * Treat "already loaded" error. If we were told to stop on
1301 * already loaded and the module being loaded is not a softdep
1302 * or dep, bail out. Otherwise, just ignore and continue.
1304 * We need to check here because of race conditions. We
1305 * checked first if module was already loaded but it may have
1306 * been loaded between the check and the moment we try to
1309 if (err
== -EEXIST
&& m
== mod
&&
1310 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1319 kmod_module_unref_list(list
);
1324 * kmod_module_get_options:
1327 * Get options of this kmod module. Options come from the configuration file
1328 * and are cached in @mod. The first call to this function will search for
1329 * this module in configuration and subsequent calls return the cached string.
1331 * Returns: a string with all the options separated by spaces. This string is
1332 * owned by @mod, do not free it.
1334 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1339 if (!mod
->init
.options
) {
1341 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1342 const struct kmod_list
*l
;
1343 const struct kmod_config
*config
;
1347 config
= kmod_get_config(mod
->ctx
);
1349 kmod_list_foreach(l
, config
->options
) {
1350 const char *modname
= kmod_option_get_modname(l
);
1355 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1356 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1357 streq(modname
, mod
->alias
))))
1360 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1361 str
= kmod_option_get_options(l
);
1366 tmp
= realloc(opts
, optslen
+ len
+ 2);
1375 opts
[optslen
] = ' ';
1379 memcpy(opts
+ optslen
, str
, len
);
1381 opts
[optslen
] = '\0';
1384 m
->init
.options
= true;
1388 return mod
->options
;
1391 ERR(mod
->ctx
, "out of memory\n");
1396 * kmod_module_get_install_commands:
1399 * Get install commands for this kmod module. Install commands come from the
1400 * configuration file and are cached in @mod. The first call to this function
1401 * will search for this module in configuration and subsequent calls return
1402 * the cached string. The install commands are returned as they were in the
1403 * configuration, concatenated by ';'. No other processing is made in this
1406 * Returns: a string with all install commands separated by semicolons. This
1407 * string is owned by @mod, do not free it.
1409 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1414 if (!mod
->init
.install_commands
) {
1416 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1417 const struct kmod_list
*l
;
1418 const struct kmod_config
*config
;
1420 config
= kmod_get_config(mod
->ctx
);
1422 kmod_list_foreach(l
, config
->install_commands
) {
1423 const char *modname
= kmod_command_get_modname(l
);
1425 if (fnmatch(modname
, mod
->name
, 0) != 0)
1428 m
->install_commands
= kmod_command_get_command(l
);
1431 * find only the first command, as modprobe from
1432 * module-init-tools does
1437 m
->init
.install_commands
= true;
1440 return mod
->install_commands
;
1443 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1445 mod
->init
.install_commands
= true;
1446 mod
->install_commands
= cmd
;
1449 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1451 struct kmod_list
*ret
= NULL
;
1454 for (i
= 0; i
< count
; i
++) {
1455 const char *depname
= array
[i
];
1456 struct kmod_list
*lst
= NULL
;
1459 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1461 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1463 } else if (lst
!= NULL
)
1464 ret
= kmod_list_append_list(ret
, lst
);
1470 * kmod_module_get_softdeps:
1472 * @pre: where to save the list of preceding soft dependencies.
1473 * @post: where to save the list of post soft dependencies.
1475 * Get soft dependencies for this kmod module. Soft dependencies come
1476 * from configuration file and are not cached in @mod because it may include
1477 * dependency cycles that would make we leak kmod_module. Any call
1478 * to this function will search for this module in configuration, allocate a
1479 * list and return the result.
1481 * Both @pre and @post are newly created list of kmod_module and
1482 * should be unreferenced with kmod_module_unref_list().
1484 * Returns: 0 on success or < 0 otherwise.
1486 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1487 struct kmod_list
**pre
,
1488 struct kmod_list
**post
)
1490 const struct kmod_list
*l
;
1491 const struct kmod_config
*config
;
1493 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1496 assert(*pre
== NULL
);
1497 assert(*post
== NULL
);
1499 config
= kmod_get_config(mod
->ctx
);
1501 kmod_list_foreach(l
, config
->softdeps
) {
1502 const char *modname
= kmod_softdep_get_name(l
);
1503 const char * const *array
;
1506 if (fnmatch(modname
, mod
->name
, 0) != 0)
1509 array
= kmod_softdep_get_pre(l
, &count
);
1510 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1511 array
= kmod_softdep_get_post(l
, &count
);
1512 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1515 * find only the first command, as modprobe from
1516 * module-init-tools does
1525 * kmod_module_get_remove_commands:
1528 * Get remove commands for this kmod module. Remove commands come from the
1529 * configuration file and are cached in @mod. The first call to this function
1530 * will search for this module in configuration and subsequent calls return
1531 * the cached string. The remove commands are returned as they were in the
1532 * configuration, concatenated by ';'. No other processing is made in this
1535 * Returns: a string with all remove commands separated by semicolons. This
1536 * string is owned by @mod, do not free it.
1538 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1543 if (!mod
->init
.remove_commands
) {
1545 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1546 const struct kmod_list
*l
;
1547 const struct kmod_config
*config
;
1549 config
= kmod_get_config(mod
->ctx
);
1551 kmod_list_foreach(l
, config
->remove_commands
) {
1552 const char *modname
= kmod_command_get_modname(l
);
1554 if (fnmatch(modname
, mod
->name
, 0) != 0)
1557 m
->remove_commands
= kmod_command_get_command(l
);
1560 * find only the first command, as modprobe from
1561 * module-init-tools does
1566 m
->init
.remove_commands
= true;
1569 return mod
->remove_commands
;
1572 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1574 mod
->init
.remove_commands
= true;
1575 mod
->remove_commands
= cmd
;
1579 * SECTION:libkmod-loaded
1580 * @short_description: currently loaded modules
1582 * Information about currently loaded modules, as reported by Linux kernel.
1583 * These information are not cached by libkmod and are always read from /sys
1584 * and /proc/modules.
1588 * kmod_module_new_from_loaded:
1589 * @ctx: kmod library context
1590 * @list: where to save the list of loaded modules
1592 * Create a new list of kmod modules with all modules currently loaded in
1593 * kernel. It uses /proc/modules to get the names of loaded modules and to
1594 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1595 * They are put in @list in no particular order.
1597 * The initial refcount is 1, and needs to be decremented to release the
1598 * resources of the kmod_module. The returned @list must be released by
1599 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1600 * kmod_modules created, they are all released upon @ctx destruction too. Do
1601 * not unref @ctx before all the desired operations with the returned list are
1604 * Returns: 0 on success or < 0 on error.
1606 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1607 struct kmod_list
**list
)
1609 struct kmod_list
*l
= NULL
;
1613 if (ctx
== NULL
|| list
== NULL
)
1616 fp
= fopen("/proc/modules", "re");
1619 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1623 while (fgets(line
, sizeof(line
), fp
)) {
1624 struct kmod_module
*m
;
1625 struct kmod_list
*node
;
1627 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1629 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1631 ERR(ctx
, "could not get module from name '%s': %s\n",
1632 name
, strerror(-err
));
1636 node
= kmod_list_append(l
, m
);
1640 ERR(ctx
, "out of memory\n");
1641 kmod_module_unref(m
);
1652 * kmod_module_initstate_str:
1653 * @state: the state as returned by kmod_module_get_initstate()
1655 * Translate a initstate to a string.
1657 * Returns: the string associated to the @state. This string is statically
1658 * allocated, do not free it.
1660 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1663 case KMOD_MODULE_BUILTIN
:
1665 case KMOD_MODULE_LIVE
:
1667 case KMOD_MODULE_COMING
:
1669 case KMOD_MODULE_GOING
:
1677 * kmod_module_get_initstate:
1680 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1683 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1684 * KMOD_MODULE_BUILTIN: module is builtin;
1685 * KMOD_MODULE_LIVE: module is live in kernel;
1686 * KMOD_MODULE_COMING: module is being loaded;
1687 * KMOD_MODULE_GOING: module is being unloaded.
1689 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1691 char path
[PATH_MAX
], buf
[32];
1692 int fd
, err
, pathlen
;
1698 return KMOD_MODULE_BUILTIN
;
1700 pathlen
= snprintf(path
, sizeof(path
),
1701 "/sys/module/%s/initstate", mod
->name
);
1702 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1706 DBG(mod
->ctx
, "could not open '%s': %s\n",
1707 path
, strerror(-err
));
1709 if (pathlen
> (int)sizeof("/initstate") - 1) {
1711 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1712 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1713 return KMOD_MODULE_BUILTIN
;
1716 DBG(mod
->ctx
, "could not open '%s': %s\n",
1717 path
, strerror(-err
));
1721 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1724 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1725 path
, strerror(-err
));
1729 if (streq(buf
, "live\n"))
1730 return KMOD_MODULE_LIVE
;
1731 else if (streq(buf
, "coming\n"))
1732 return KMOD_MODULE_COMING
;
1733 else if (streq(buf
, "going\n"))
1734 return KMOD_MODULE_GOING
;
1736 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1741 * kmod_module_get_size:
1744 * Get the size of this kmod module as returned by Linux kernel. If supported,
1745 * the size is read from the coresize attribute in /sys/module. For older
1746 * kernels, this falls back on /proc/modules and searches for the specified
1747 * module to get its size.
1749 * Returns: the size of this kmod module.
1751 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1756 long size
= -ENOENT
;
1762 /* try to open the module dir in /sys. If this fails, don't
1763 * bother trying to find the size as we know the module isn't
1766 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1767 dfd
= open(line
, O_RDONLY
);
1771 /* available as of linux 3.3.x */
1772 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1774 if (read_str_long(cfd
, &size
, 10) < 0)
1775 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1780 /* fall back on parsing /proc/modules */
1781 fp
= fopen("/proc/modules", "re");
1785 "could not open /proc/modules: %s\n", strerror(errno
));
1789 while (fgets(line
, sizeof(line
), fp
)) {
1790 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1794 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1797 tok
= strtok_r(NULL
, " \t", &saveptr
);
1800 "invalid line format at /proc/modules:%d\n", lineno
);
1804 value
= strtol(tok
, &endptr
, 10);
1805 if (endptr
== tok
|| *endptr
!= '\0') {
1807 "invalid line format at /proc/modules:%d\n", lineno
);
1822 * kmod_module_get_refcnt:
1825 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1828 * Returns: 0 on success or < 0 on failure.
1830 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1832 char path
[PATH_MAX
];
1839 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1840 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1843 DBG(mod
->ctx
, "could not open '%s': %s\n",
1844 path
, strerror(errno
));
1848 err
= read_str_long(fd
, &refcnt
, 10);
1851 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1852 path
, strerror(-err
));
1860 * kmod_module_get_holders:
1863 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1864 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1866 * Returns: a new list of kmod modules on success or NULL on failure.
1868 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1870 char dname
[PATH_MAX
];
1871 struct kmod_list
*list
= NULL
;
1874 if (mod
== NULL
|| mod
->ctx
== NULL
)
1877 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1881 ERR(mod
->ctx
, "could not open '%s': %s\n",
1882 dname
, strerror(errno
));
1887 struct dirent de
, *entp
;
1888 struct kmod_module
*holder
;
1889 struct kmod_list
*l
;
1892 err
= readdir_r(d
, &de
, &entp
);
1894 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1895 mod
->name
, strerror(-err
));
1902 if (de
.d_name
[0] == '.') {
1903 if (de
.d_name
[1] == '\0' ||
1904 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1908 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1910 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1911 de
.d_name
, strerror(-err
));
1915 l
= kmod_list_append(list
, holder
);
1919 ERR(mod
->ctx
, "out of memory\n");
1920 kmod_module_unref(holder
);
1930 kmod_module_unref_list(list
);
1934 struct kmod_module_section
{
1935 unsigned long address
;
1939 static void kmod_module_section_free(struct kmod_module_section
*section
)
1945 * kmod_module_get_sections:
1948 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1949 * structure contained in this list is internal to libkmod and their fields
1950 * can be obtained by calling kmod_module_section_get_name() and
1951 * kmod_module_section_get_address().
1953 * After use, free the @list by calling kmod_module_section_free_list().
1955 * Returns: a new list of kmod module sections on success or NULL on failure.
1957 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1959 char dname
[PATH_MAX
];
1960 struct kmod_list
*list
= NULL
;
1967 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1971 ERR(mod
->ctx
, "could not open '%s': %s\n",
1972 dname
, strerror(errno
));
1979 struct dirent de
, *entp
;
1980 struct kmod_module_section
*section
;
1981 struct kmod_list
*l
;
1982 unsigned long address
;
1986 err
= readdir_r(d
, &de
, &entp
);
1988 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1989 mod
->name
, strerror(-err
));
1993 if (de
.d_name
[0] == '.') {
1994 if (de
.d_name
[1] == '\0' ||
1995 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1999 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
2001 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
2006 err
= read_str_ulong(fd
, &address
, 16);
2009 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
2014 namesz
= strlen(de
.d_name
) + 1;
2015 section
= malloc(sizeof(*section
) + namesz
);
2017 if (section
== NULL
) {
2018 ERR(mod
->ctx
, "out of memory\n");
2022 section
->address
= address
;
2023 memcpy(section
->name
, de
.d_name
, namesz
);
2025 l
= kmod_list_append(list
, section
);
2029 ERR(mod
->ctx
, "out of memory\n");
2040 kmod_module_unref_list(list
);
2045 * kmod_module_section_get_module_name:
2046 * @entry: a list entry representing a kmod module section
2048 * Get the name of a kmod module section.
2050 * After use, free the @list by calling kmod_module_section_free_list().
2052 * Returns: the name of this kmod module section on success or NULL on
2053 * failure. The string is owned by the section, do not free it.
2055 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2057 struct kmod_module_section
*section
;
2062 section
= entry
->data
;
2063 return section
->name
;
2067 * kmod_module_section_get_address:
2068 * @entry: a list entry representing a kmod module section
2070 * Get the address of a kmod module section.
2072 * After use, free the @list by calling kmod_module_section_free_list().
2074 * Returns: the address of this kmod module section on success or ULONG_MAX
2077 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2079 struct kmod_module_section
*section
;
2082 return (unsigned long)-1;
2084 section
= entry
->data
;
2085 return section
->address
;
2089 * kmod_module_section_free_list:
2090 * @list: kmod module section list
2092 * Release the resources taken by @list
2094 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2097 kmod_module_section_free(list
->data
);
2098 list
= kmod_list_remove(list
);
2102 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2104 if (mod
->file
== NULL
) {
2105 const char *path
= kmod_module_get_path(mod
);
2112 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2114 if (mod
->file
== NULL
)
2118 return kmod_file_get_elf(mod
->file
);
2121 struct kmod_module_info
{
2126 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2128 struct kmod_module_info
*info
;
2130 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2134 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2136 memcpy(info
->key
, key
, keylen
);
2137 info
->key
[keylen
] = '\0';
2138 memcpy(info
->value
, value
, valuelen
);
2139 info
->value
[valuelen
] = '\0';
2143 static void kmod_module_info_free(struct kmod_module_info
*info
)
2148 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2150 struct kmod_module_info
*info
;
2151 struct kmod_list
*n
;
2153 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2156 n
= kmod_list_append(*list
, info
);
2160 kmod_module_info_free(info
);
2165 * kmod_module_get_info:
2167 * @list: where to return list of module information. Use
2168 * kmod_module_info_get_key() and
2169 * kmod_module_info_get_value(). Release this list with
2170 * kmod_module_info_free_list()
2172 * Get a list of entries in ELF section ".modinfo", these contain
2173 * alias, license, depends, vermagic and other keys with respective
2174 * values. If the module is signed (CONFIG_MODULE_SIG), information
2175 * about the module signature is included as well: signer,
2176 * sig_key and sig_hashalgo.
2178 * After use, free the @list by calling kmod_module_info_free_list().
2180 * Returns: 0 on success or < 0 otherwise.
2182 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2184 struct kmod_elf
*elf
;
2186 int i
, count
, ret
= -ENOMEM
;
2187 struct kmod_signature_info sig_info
;
2189 if (mod
== NULL
|| list
== NULL
)
2192 assert(*list
== NULL
);
2194 elf
= kmod_module_get_elf(mod
);
2198 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2202 for (i
= 0; i
< count
; i
++) {
2203 struct kmod_list
*n
;
2204 const char *key
, *value
;
2205 size_t keylen
, valuelen
;
2208 value
= strchr(key
, '=');
2209 if (value
== NULL
) {
2210 keylen
= strlen(key
);
2214 keylen
= value
- key
;
2216 valuelen
= strlen(value
);
2219 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2224 if (kmod_module_signature_info(mod
->file
, &sig_info
)) {
2225 struct kmod_list
*n
;
2228 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2229 sig_info
.signer
, sig_info
.signer_len
);
2234 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2235 key_hex
= malloc(sig_info
.key_id_len
* 3);
2236 if (key_hex
== NULL
)
2238 for (i
= 0; i
< (int)sig_info
.key_id_len
; i
++) {
2239 sprintf(key_hex
+ i
* 3, "%02X",
2240 (unsigned char)sig_info
.key_id
[i
]);
2241 if (i
< (int)sig_info
.key_id_len
- 1)
2242 key_hex
[i
* 3 + 2] = ':';
2244 n
= kmod_module_info_append(list
, "sig_key", strlen("sig_key"),
2245 key_hex
, sig_info
.key_id_len
* 3 - 1);
2251 n
= kmod_module_info_append(list
,
2252 "sig_hashalgo", strlen("sig_hashalgo"),
2253 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2259 * Omit sig_info.id_type and sig_info.algo for now, as these
2260 * are currently constant.
2267 kmod_module_info_free_list(*list
);
2275 * kmod_module_info_get_key:
2276 * @entry: a list entry representing a kmod module info
2278 * Get the key of a kmod module info.
2280 * Returns: the key of this kmod module info on success or NULL on
2281 * failure. The string is owned by the info, do not free it.
2283 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2285 struct kmod_module_info
*info
;
2295 * kmod_module_info_get_value:
2296 * @entry: a list entry representing a kmod module info
2298 * Get the value of a kmod module info.
2300 * Returns: the value of this kmod module info on success or NULL on
2301 * failure. The string is owned by the info, do not free it.
2303 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2305 struct kmod_module_info
*info
;
2315 * kmod_module_info_free_list:
2316 * @list: kmod module info list
2318 * Release the resources taken by @list
2320 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2323 kmod_module_info_free(list
->data
);
2324 list
= kmod_list_remove(list
);
2328 struct kmod_module_version
{
2333 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2335 struct kmod_module_version
*mv
;
2336 size_t symbollen
= strlen(symbol
) + 1;
2338 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2343 memcpy(mv
->symbol
, symbol
, symbollen
);
2347 static void kmod_module_version_free(struct kmod_module_version
*version
)
2353 * kmod_module_get_versions:
2355 * @list: where to return list of module versions. Use
2356 * kmod_module_version_get_symbol() and
2357 * kmod_module_version_get_crc(). Release this list with
2358 * kmod_module_versions_free_list()
2360 * Get a list of entries in ELF section "__versions".
2362 * After use, free the @list by calling kmod_module_versions_free_list().
2364 * Returns: 0 on success or < 0 otherwise.
2366 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2368 struct kmod_elf
*elf
;
2369 struct kmod_modversion
*versions
;
2370 int i
, count
, ret
= 0;
2372 if (mod
== NULL
|| list
== NULL
)
2375 assert(*list
== NULL
);
2377 elf
= kmod_module_get_elf(mod
);
2381 count
= kmod_elf_get_modversions(elf
, &versions
);
2385 for (i
= 0; i
< count
; i
++) {
2386 struct kmod_module_version
*mv
;
2387 struct kmod_list
*n
;
2389 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2392 kmod_module_versions_free_list(*list
);
2397 n
= kmod_list_append(*list
, mv
);
2401 kmod_module_version_free(mv
);
2402 kmod_module_versions_free_list(*list
);
2416 * kmod_module_version_get_symbol:
2417 * @entry: a list entry representing a kmod module versions
2419 * Get the symbol of a kmod module versions.
2421 * Returns: the symbol of this kmod module versions on success or NULL
2422 * on failure. The string is owned by the versions, do not free it.
2424 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2426 struct kmod_module_version
*version
;
2431 version
= entry
->data
;
2432 return version
->symbol
;
2436 * kmod_module_version_get_crc:
2437 * @entry: a list entry representing a kmod module version
2439 * Get the crc of a kmod module version.
2441 * Returns: the crc of this kmod module version on success or NULL on
2442 * failure. The string is owned by the version, do not free it.
2444 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2446 struct kmod_module_version
*version
;
2451 version
= entry
->data
;
2452 return version
->crc
;
2456 * kmod_module_versions_free_list:
2457 * @list: kmod module versions list
2459 * Release the resources taken by @list
2461 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2464 kmod_module_version_free(list
->data
);
2465 list
= kmod_list_remove(list
);
2469 struct kmod_module_symbol
{
2474 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2476 struct kmod_module_symbol
*mv
;
2477 size_t symbollen
= strlen(symbol
) + 1;
2479 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2484 memcpy(mv
->symbol
, symbol
, symbollen
);
2488 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2494 * kmod_module_get_symbols:
2496 * @list: where to return list of module symbols. Use
2497 * kmod_module_symbol_get_symbol() and
2498 * kmod_module_symbol_get_crc(). Release this list with
2499 * kmod_module_symbols_free_list()
2501 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2503 * After use, free the @list by calling kmod_module_symbols_free_list().
2505 * Returns: 0 on success or < 0 otherwise.
2507 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2509 struct kmod_elf
*elf
;
2510 struct kmod_modversion
*symbols
;
2511 int i
, count
, ret
= 0;
2513 if (mod
== NULL
|| list
== NULL
)
2516 assert(*list
== NULL
);
2518 elf
= kmod_module_get_elf(mod
);
2522 count
= kmod_elf_get_symbols(elf
, &symbols
);
2526 for (i
= 0; i
< count
; i
++) {
2527 struct kmod_module_symbol
*mv
;
2528 struct kmod_list
*n
;
2530 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2533 kmod_module_symbols_free_list(*list
);
2538 n
= kmod_list_append(*list
, mv
);
2542 kmod_module_symbol_free(mv
);
2543 kmod_module_symbols_free_list(*list
);
2557 * kmod_module_symbol_get_symbol:
2558 * @entry: a list entry representing a kmod module symbols
2560 * Get the symbol of a kmod module symbols.
2562 * Returns: the symbol of this kmod module symbols on success or NULL
2563 * on failure. The string is owned by the symbols, do not free it.
2565 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2567 struct kmod_module_symbol
*symbol
;
2572 symbol
= entry
->data
;
2573 return symbol
->symbol
;
2577 * kmod_module_symbol_get_crc:
2578 * @entry: a list entry representing a kmod module symbol
2580 * Get the crc of a kmod module symbol.
2582 * Returns: the crc of this kmod module symbol on success or NULL on
2583 * failure. The string is owned by the symbol, do not free it.
2585 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2587 struct kmod_module_symbol
*symbol
;
2592 symbol
= entry
->data
;
2597 * kmod_module_symbols_free_list:
2598 * @list: kmod module symbols list
2600 * Release the resources taken by @list
2602 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2605 kmod_module_symbol_free(list
->data
);
2606 list
= kmod_list_remove(list
);
2610 struct kmod_module_dependency_symbol
{
2616 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2618 struct kmod_module_dependency_symbol
*mv
;
2619 size_t symbollen
= strlen(symbol
) + 1;
2621 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2627 memcpy(mv
->symbol
, symbol
, symbollen
);
2631 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2633 free(dependency_symbol
);
2637 * kmod_module_get_dependency_symbols:
2639 * @list: where to return list of module dependency_symbols. Use
2640 * kmod_module_dependency_symbol_get_symbol() and
2641 * kmod_module_dependency_symbol_get_crc(). Release this list with
2642 * kmod_module_dependency_symbols_free_list()
2644 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2646 * After use, free the @list by calling
2647 * kmod_module_dependency_symbols_free_list().
2649 * Returns: 0 on success or < 0 otherwise.
2651 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2653 struct kmod_elf
*elf
;
2654 struct kmod_modversion
*symbols
;
2655 int i
, count
, ret
= 0;
2657 if (mod
== NULL
|| list
== NULL
)
2660 assert(*list
== NULL
);
2662 elf
= kmod_module_get_elf(mod
);
2666 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2670 for (i
= 0; i
< count
; i
++) {
2671 struct kmod_module_dependency_symbol
*mv
;
2672 struct kmod_list
*n
;
2674 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2679 kmod_module_dependency_symbols_free_list(*list
);
2684 n
= kmod_list_append(*list
, mv
);
2688 kmod_module_dependency_symbol_free(mv
);
2689 kmod_module_dependency_symbols_free_list(*list
);
2703 * kmod_module_dependency_symbol_get_symbol:
2704 * @entry: a list entry representing a kmod module dependency_symbols
2706 * Get the dependency symbol of a kmod module
2708 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2709 * on failure. The string is owned by the dependency_symbols, do not free it.
2711 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2713 struct kmod_module_dependency_symbol
*dependency_symbol
;
2718 dependency_symbol
= entry
->data
;
2719 return dependency_symbol
->symbol
;
2723 * kmod_module_dependency_symbol_get_crc:
2724 * @entry: a list entry representing a kmod module dependency_symbol
2726 * Get the crc of a kmod module dependency_symbol.
2728 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2729 * failure. The string is owned by the dependency_symbol, do not free it.
2731 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2733 struct kmod_module_dependency_symbol
*dependency_symbol
;
2738 dependency_symbol
= entry
->data
;
2739 return dependency_symbol
->crc
;
2743 * kmod_module_dependency_symbol_get_bind:
2744 * @entry: a list entry representing a kmod module dependency_symbol
2746 * Get the bind type of a kmod module dependency_symbol.
2748 * Returns: the bind of this kmod module dependency_symbol on success
2749 * or < 0 on failure.
2751 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2753 struct kmod_module_dependency_symbol
*dependency_symbol
;
2758 dependency_symbol
= entry
->data
;
2759 return dependency_symbol
->bind
;
2763 * kmod_module_dependency_symbols_free_list:
2764 * @list: kmod module dependency_symbols list
2766 * Release the resources taken by @list
2768 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2771 kmod_module_dependency_symbol_free(list
->data
);
2772 list
= kmod_list_remove(list
);