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));
258 memset(m
, 0, sizeof(*m
));
260 m
->ctx
= kmod_ref(ctx
);
261 m
->name
= (char *)m
+ sizeof(*m
);
262 memcpy(m
->name
, key
, keylen
+ 1);
264 m
->hashkey
= m
->name
;
267 m
->name
[namelen
] = '\0';
268 m
->alias
= m
->name
+ namelen
+ 1;
269 m
->hashkey
= m
->name
+ keylen
+ 1;
270 memcpy(m
->hashkey
, key
, keylen
+ 1);
274 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
281 * kmod_module_new_from_name:
282 * @ctx: kmod library context
283 * @name: name of the module
284 * @mod: where to save the created struct kmod_module
286 * Create a new struct kmod_module using the module name. @name can not be an
287 * alias, file name or anything else; it must be a module name. There's no
288 * check if the module exists in the system.
290 * This function is also used internally by many others that return a new
291 * struct kmod_module or a new list of modules.
293 * The initial refcount is 1, and needs to be decremented to release the
294 * resources of the kmod_module. Since libkmod keeps track of all
295 * kmod_modules created, they are all released upon @ctx destruction too. Do
296 * not unref @ctx before all the desired operations with the returned
297 * kmod_module are done.
299 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
300 * module name or if memory allocation failed.
302 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
304 struct kmod_module
**mod
)
307 char name_norm
[PATH_MAX
];
309 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
312 modname_normalize(name
, name_norm
, &namelen
);
314 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
317 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
318 const char *name
, struct kmod_module
**mod
)
322 size_t namelen
= strlen(name
);
323 size_t aliaslen
= strlen(alias
);
325 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
326 return -ENAMETOOLONG
;
328 memcpy(key
, name
, namelen
);
329 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
332 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
340 * kmod_module_new_from_path:
341 * @ctx: kmod library context
342 * @path: path where to find the given module
343 * @mod: where to save the created struct kmod_module
345 * Create a new struct kmod_module using the module path. @path must be an
346 * existent file with in the filesystem and must be accessible to libkmod.
348 * The initial refcount is 1, and needs to be decremented to release the
349 * resources of the kmod_module. Since libkmod keeps track of all
350 * kmod_modules created, they are all released upon @ctx destruction too. Do
351 * not unref @ctx before all the desired operations with the returned
352 * kmod_module are done.
354 * If @path is relative, it's treated as relative to the current working
355 * directory. Otherwise, give an absolute path.
357 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
358 * it's not a valid file for a kmod_module or if memory allocation failed.
360 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
362 struct kmod_module
**mod
)
364 struct kmod_module
*m
;
371 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
374 abspath
= path_make_absolute_cwd(path
);
375 if (abspath
== NULL
) {
376 DBG(ctx
, "no absolute path for %s\n", path
);
380 err
= stat(abspath
, &st
);
383 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
388 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
389 DBG(ctx
, "could not get modname from path %s\n", path
);
394 m
= kmod_pool_get_module(ctx
, name
);
398 else if (streq(m
->path
, abspath
))
401 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
402 name
, abspath
, m
->path
);
407 *mod
= kmod_module_ref(m
);
411 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
425 * Drop a reference of the kmod module. If the refcount reaches zero, its
426 * resources are released.
428 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
429 * returns the passed @mod with its refcount decremented.
431 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
436 if (--mod
->refcount
> 0)
439 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
441 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
442 kmod_module_unref_list(mod
->dep
);
445 kmod_file_unref(mod
->file
);
447 kmod_unref(mod
->ctx
);
458 * Take a reference of the kmod module, incrementing its refcount.
460 * Returns: the passed @module with its refcount incremented.
462 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
472 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
476 if (*(_list) != NULL) \
481 * kmod_module_new_from_lookup:
482 * @ctx: kmod library context
483 * @given_alias: alias to look for
484 * @list: an empty list where to save the list of modules matching
487 * Create a new list of kmod modules using an alias or module name and lookup
488 * libkmod's configuration files and indexes in order to find the module.
489 * Once it's found in one of the places, it stops searching and create the
490 * list of modules that is saved in @list.
492 * The search order is: 1. aliases in configuration file; 2. module names in
493 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
494 * in modules.alias index.
496 * The initial refcount is 1, and needs to be decremented to release the
497 * resources of the kmod_module. The returned @list must be released by
498 * calling kmod_module_unref_list(). Since libkmod keeps track of all
499 * kmod_modules created, they are all released upon @ctx destruction too. Do
500 * not unref @ctx before all the desired operations with the returned list are
503 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
504 * methods failed, which is basically due to memory allocation fail. If module
505 * is not found, it still returns 0, but @list is an empty list.
507 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
508 const char *given_alias
,
509 struct kmod_list
**list
)
512 char alias
[PATH_MAX
];
514 if (ctx
== NULL
|| given_alias
== NULL
)
517 if (list
== NULL
|| *list
!= NULL
) {
518 ERR(ctx
, "An empty list is needed to create lookup\n");
522 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
523 DBG(ctx
, "invalid alias: %s\n", given_alias
);
527 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
529 /* Aliases from config file override all the others */
530 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
531 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
533 DBG(ctx
, "lookup modules.dep %s\n", alias
);
534 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
535 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
537 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
538 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
539 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
541 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
542 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
543 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
545 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
546 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
547 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
549 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
550 err
= kmod_lookup_alias_from_builtin_file(ctx
, alias
, list
);
551 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
554 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
557 DBG(ctx
, "Failed to lookup %s\n", alias
);
558 kmod_module_unref_list(*list
);
562 #undef CHECK_ERR_AND_FINISH
565 * kmod_module_unref_list:
566 * @list: list of kmod modules
568 * Drop a reference of each kmod module in @list and releases the resources
569 * taken by the list itself.
573 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
575 for (; list
!= NULL
; list
= kmod_list_remove(list
))
576 kmod_module_unref(list
->data
);
582 * kmod_module_get_filtered_blacklist:
583 * @ctx: kmod library context
584 * @input: list of kmod_module to be filtered with blacklist
585 * @output: where to save the new list
587 * This function should not be used. Use kmod_module_apply_filter instead.
589 * Given a list @input, this function filter it out with config's blacklist
590 * and save it in @output.
592 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
595 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
596 const struct kmod_list
*input
,
597 struct kmod_list
**output
)
599 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
602 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
604 if (!mod
->init
.dep
) {
606 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
611 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
622 * kmod_module_get_dependencies:
625 * Search the modules.dep index to find the dependencies of the given @mod.
626 * The result is cached in @mod, so subsequent calls to this function will
627 * return the already searched list of modules.
629 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
630 * that can be released by calling kmod_module_unref_list().
632 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
634 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
639 module_get_dependencies_noref(mod
);
641 kmod_list_foreach(l
, mod
->dep
) {
642 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
644 kmod_module_unref(l
->data
);
654 ERR(mod
->ctx
, "out of memory\n");
655 kmod_module_unref_list(list_new
);
660 * kmod_module_get_module:
661 * @entry: an entry in a list of kmod modules.
663 * Get the kmod module of this @entry in the list, increasing its refcount.
664 * After it's used, unref it. Since the refcount is incremented upon return,
665 * you still have to call kmod_module_unref_list() to release the list of kmod
668 * Returns: NULL on failure or the kmod_module contained in this list entry
669 * with its refcount incremented.
671 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
676 return kmod_module_ref(entry
->data
);
680 * kmod_module_get_name:
683 * Get the name of this kmod module. Name is always available, independently
684 * if it was created by kmod_module_new_from_name() or another function and
685 * it's always normalized (dashes are replaced with underscores).
687 * Returns: the name of this kmod module.
689 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
698 * kmod_module_get_path:
701 * Get the path of this kmod module. If this kmod module was not created by
702 * path, it can search the modules.dep index in order to find out the module
703 * under context's dirname.
705 * Returns: the path of this kmod module or NULL if such information is not
708 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
715 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
717 if (mod
->path
!= NULL
)
723 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
727 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
734 extern long delete_module(const char *name
, unsigned int flags
);
737 * kmod_module_remove_module:
739 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
740 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
741 * use by a kernel subsystem or other process;
742 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
745 * Remove a module from Linux kernel.
747 * Returns: 0 on success or < 0 on failure.
749 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
757 /* Filter out other flags and force ONONBLOCK */
758 flags
&= KMOD_REMOVE_FORCE
;
759 flags
|= KMOD_REMOVE_NOWAIT
;
761 err
= delete_module(mod
->name
, flags
);
764 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
770 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
773 * kmod_module_insert_module:
775 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
776 * behavior of this function, valid flags are
777 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
778 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
779 * @options: module's options to pass to Linux Kernel.
781 * Insert a module in Linux kernel. It opens the file pointed by @mod,
782 * mmap'ing it and passing to kernel.
784 * Returns: 0 on success or < 0 on failure. If module is already loaded it
787 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
794 struct kmod_file
*file
;
795 struct kmod_elf
*elf
= NULL
;
797 const char *args
= options
? options
: "";
802 path
= kmod_module_get_path(mod
);
804 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
808 file
= kmod_file_open(mod
->ctx
, path
);
814 if (kmod_file_get_direct(file
)) {
815 unsigned int kernel_flags
= 0;
817 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
)
818 kernel_flags
|= MODULE_INIT_IGNORE_VERMAGIC
;
819 if (flags
& KMOD_INSERT_FORCE_MODVERSION
)
820 kernel_flags
|= MODULE_INIT_IGNORE_MODVERSIONS
;
822 err
= finit_module(kmod_file_get_fd(file
), args
, kernel_flags
);
823 if (err
== 0 || errno
!= ENOSYS
)
827 size
= kmod_file_get_size(file
);
828 mem
= kmod_file_get_contents(file
);
830 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
831 elf
= kmod_elf_new(mem
, size
);
837 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
838 err
= kmod_elf_strip_section(elf
, "__versions");
840 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
843 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
844 err
= kmod_elf_strip_vermagic(elf
);
846 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
849 mem
= kmod_elf_get_memory(elf
);
852 err
= init_module(mem
, size
, args
);
856 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
862 kmod_file_unref(file
);
867 static bool module_is_blacklisted(struct kmod_module
*mod
)
869 struct kmod_ctx
*ctx
= mod
->ctx
;
870 const struct kmod_config
*config
= kmod_get_config(ctx
);
871 const struct kmod_list
*bl
= config
->blacklists
;
872 const struct kmod_list
*l
;
874 kmod_list_foreach(l
, bl
) {
875 const char *modname
= kmod_blacklist_get_modname(l
);
877 if (streq(modname
, mod
->name
))
885 * kmod_module_apply_filter
886 * @ctx: kmod library context
887 * @filter_type: bitmask to filter modules out, valid types are
888 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
889 * KMOD_FILTER_BUILTIN: filter builtin modules out.
890 * @input: list of kmod_module to be filtered
891 * @output: where to save the new list
893 * Given a list @input, this function filter it out by the filter mask
894 * and save it in @output.
896 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
899 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
900 enum kmod_filter filter_type
,
901 const struct kmod_list
*input
,
902 struct kmod_list
**output
)
904 const struct kmod_list
*li
;
906 if (ctx
== NULL
|| output
== NULL
)
913 kmod_list_foreach(li
, input
) {
914 struct kmod_module
*mod
= li
->data
;
915 struct kmod_list
*node
;
917 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
918 module_is_blacklisted(mod
))
921 if ((filter_type
& KMOD_FILTER_BUILTIN
) && mod
->builtin
)
924 node
= kmod_list_append(*output
, mod
);
929 kmod_module_ref(mod
);
935 kmod_module_unref_list(*output
);
940 static int command_do(struct kmod_module
*mod
, const char *type
,
943 const char *modname
= kmod_module_get_name(mod
);
946 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
948 setenv("MODPROBE_MODULE", modname
, 1);
950 unsetenv("MODPROBE_MODULE");
952 if (err
== -1 || WEXITSTATUS(err
)) {
953 ERR(mod
->ctx
, "Error running %s command for %s\n",
956 err
= -WEXITSTATUS(err
);
962 struct probe_insert_cb
{
963 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
967 static int module_do_install_commands(struct kmod_module
*mod
,
969 struct probe_insert_cb
*cb
)
971 const char *command
= kmod_module_get_install_commands(mod
);
973 _cleanup_free_
char *cmd
;
975 size_t cmdlen
, options_len
, varlen
;
982 options_len
= strlen(options
);
983 cmdlen
= strlen(command
);
984 varlen
= sizeof("$CMDLINE_OPTS") - 1;
986 cmd
= memdup(command
, cmdlen
+ 1);
990 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
991 size_t prefixlen
= p
- cmd
;
992 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
993 size_t slen
= cmdlen
- varlen
+ options_len
;
994 char *suffix
= p
+ varlen
;
995 char *s
= malloc(slen
+ 1);
999 memcpy(s
, cmd
, p
- cmd
);
1000 memcpy(s
+ prefixlen
, options
, options_len
);
1001 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
1009 if (cb
->run_install
!= NULL
)
1010 err
= cb
->run_install(mod
, cmd
, cb
->data
);
1012 err
= command_do(mod
, "install", cmd
);
1017 static char *module_options_concat(const char *opt
, const char *xopt
)
1019 // TODO: we might need to check if xopt overrides options on opt
1020 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
1021 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1024 if (optlen
== 0 && xoptlen
== 0)
1027 r
= malloc(optlen
+ xoptlen
+ 2);
1030 memcpy(r
, opt
, optlen
);
1036 memcpy(r
+ optlen
, xopt
, xoptlen
);
1038 r
[optlen
+ xoptlen
] = '\0';
1043 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1045 struct kmod_list
**list
);
1048 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1049 struct kmod_list
**list
)
1051 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1054 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1056 ERR(mod
->ctx
, "could not get softdep: %s\n",
1061 kmod_list_foreach(l
, pre
) {
1062 struct kmod_module
*m
= l
->data
;
1063 err
= __kmod_module_get_probe_list(m
, false, list
);
1068 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1070 kmod_module_unref(mod
);
1075 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1077 kmod_list_foreach(l
, post
) {
1078 struct kmod_module
*m
= l
->data
;
1079 err
= __kmod_module_get_probe_list(m
, false, list
);
1085 kmod_module_unref_list(pre
);
1086 kmod_module_unref_list(post
);
1092 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1094 struct kmod_list
**list
)
1096 struct kmod_list
*dep
, *l
;
1100 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1104 mod
->visited
= true;
1106 dep
= kmod_module_get_dependencies(mod
);
1107 kmod_list_foreach(l
, dep
) {
1108 struct kmod_module
*m
= l
->data
;
1109 err
= __kmod_module_fill_softdep(m
, list
);
1115 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1117 kmod_module_unref(mod
);
1122 mod
->ignorecmd
= true;
1124 err
= __kmod_module_fill_softdep(mod
, list
);
1127 kmod_module_unref_list(dep
);
1131 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1133 struct kmod_list
**list
)
1137 assert(mod
!= NULL
);
1138 assert(list
!= NULL
&& *list
== NULL
);
1141 * Make sure we don't get screwed by previous calls to this function
1143 kmod_set_modules_visited(mod
->ctx
, false);
1145 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1147 kmod_module_unref_list(*list
);
1155 * kmod_module_probe_insert_module:
1157 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1158 * behavior of this function, valid flags are
1159 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1160 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1161 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1162 * commands and softdeps configured in the system;
1163 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1164 * live in kernel or not;
1165 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1166 * associated callback function;
1167 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1168 * and the module is already live in kernel, the function will fail if this
1169 * flag is specified;
1170 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1171 * filter to this module and its dependencies. If any of the dependencies (or
1172 * the module) is blacklisted, the probe will fail, unless the blacklisted
1173 * module is already live in kernel;
1174 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1175 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1176 * alias and is blacklisted.
1177 * @extra_options: module's options to pass to Linux Kernel. It applies only
1178 * to @mod, not to its dependencies.
1179 * @run_install: function to run when @mod is backed by an install command.
1180 * @data: data to give back to @run_install callback
1181 * @print_action: function to call with the action being taken (install or
1182 * insmod). It's useful for tools like modprobe when running with verbose
1183 * output or in dry-run mode.
1185 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1186 * install commands and applying blacklist.
1188 * If @run_install is NULL, this function will fork and exec by calling
1189 * system(3). Don't pass a NULL argument in @run_install if your binary is
1190 * setuid/setgid (see warning in system(3)). If you need control over the
1191 * execution of an install command, give a callback function instead.
1193 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1196 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1197 unsigned int flags
, const char *extra_options
,
1198 int (*run_install
)(struct kmod_module
*m
,
1199 const char *cmd
, void *data
),
1201 void (*print_action
)(struct kmod_module
*m
,
1203 const char *options
))
1205 struct kmod_list
*list
= NULL
, *l
;
1206 struct probe_insert_cb cb
;
1212 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1213 && module_is_inkernel(mod
)) {
1214 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1221 * Ugly assignement + check. We need to check if we were told to check
1222 * blacklist and also return the reason why we failed.
1223 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1224 * module is an alias, so we also need to check it
1226 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1227 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1228 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1229 if (module_is_blacklisted(mod
))
1233 err
= kmod_module_get_probe_list(mod
,
1234 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1238 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1239 struct kmod_list
*filtered
= NULL
;
1241 err
= kmod_module_apply_filter(mod
->ctx
,
1242 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1246 kmod_module_unref_list(list
);
1247 if (filtered
== NULL
)
1248 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1253 cb
.run_install
= run_install
;
1254 cb
.data
= (void *) data
;
1256 kmod_list_foreach(l
, list
) {
1257 struct kmod_module
*m
= l
->data
;
1258 const char *moptions
= kmod_module_get_options(m
);
1259 const char *cmd
= kmod_module_get_install_commands(m
);
1262 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1263 && module_is_inkernel(m
)) {
1264 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1270 options
= module_options_concat(moptions
,
1271 m
== mod
? extra_options
: NULL
);
1273 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1274 if (print_action
!= NULL
)
1275 print_action(m
, true, options
?: "");
1277 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1278 err
= module_do_install_commands(m
, options
,
1281 if (print_action
!= NULL
)
1282 print_action(m
, false, options
?: "");
1284 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1285 err
= kmod_module_insert_module(m
, flags
,
1293 * Treat "already loaded" error. If we were told to stop on
1294 * already loaded and the module being loaded is not a softdep
1295 * or dep, bail out. Otherwise, just ignore and continue.
1297 * We need to check here because of race conditions. We
1298 * checked first if module was already loaded but it may have
1299 * been loaded between the check and the moment we try to
1302 if (err
== -EEXIST
&& m
== mod
&&
1303 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1312 kmod_module_unref_list(list
);
1317 * kmod_module_get_options:
1320 * Get options of this kmod module. Options come from the configuration file
1321 * and are cached in @mod. The first call to this function will search for
1322 * this module in configuration and subsequent calls return the cached string.
1324 * Returns: a string with all the options separated by spaces. This string is
1325 * owned by @mod, do not free it.
1327 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1332 if (!mod
->init
.options
) {
1334 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1335 const struct kmod_list
*l
;
1336 const struct kmod_config
*config
;
1340 config
= kmod_get_config(mod
->ctx
);
1342 kmod_list_foreach(l
, config
->options
) {
1343 const char *modname
= kmod_option_get_modname(l
);
1348 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1349 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1350 streq(modname
, mod
->alias
))))
1353 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1354 str
= kmod_option_get_options(l
);
1359 tmp
= realloc(opts
, optslen
+ len
+ 2);
1368 opts
[optslen
] = ' ';
1372 memcpy(opts
+ optslen
, str
, len
);
1374 opts
[optslen
] = '\0';
1377 m
->init
.options
= true;
1381 return mod
->options
;
1384 ERR(mod
->ctx
, "out of memory\n");
1389 * kmod_module_get_install_commands:
1392 * Get install commands for this kmod module. Install commands come from the
1393 * configuration file and are cached in @mod. The first call to this function
1394 * will search for this module in configuration and subsequent calls return
1395 * the cached string. The install commands are returned as they were in the
1396 * configuration, concatenated by ';'. No other processing is made in this
1399 * Returns: a string with all install commands separated by semicolons. This
1400 * string is owned by @mod, do not free it.
1402 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1407 if (!mod
->init
.install_commands
) {
1409 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1410 const struct kmod_list
*l
;
1411 const struct kmod_config
*config
;
1413 config
= kmod_get_config(mod
->ctx
);
1415 kmod_list_foreach(l
, config
->install_commands
) {
1416 const char *modname
= kmod_command_get_modname(l
);
1418 if (fnmatch(modname
, mod
->name
, 0) != 0)
1421 m
->install_commands
= kmod_command_get_command(l
);
1424 * find only the first command, as modprobe from
1425 * module-init-tools does
1430 m
->init
.install_commands
= true;
1433 return mod
->install_commands
;
1436 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1438 mod
->init
.install_commands
= true;
1439 mod
->install_commands
= cmd
;
1442 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1444 struct kmod_list
*ret
= NULL
;
1447 for (i
= 0; i
< count
; i
++) {
1448 const char *depname
= array
[i
];
1449 struct kmod_list
*lst
= NULL
;
1452 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1454 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1456 } else if (lst
!= NULL
)
1457 ret
= kmod_list_append_list(ret
, lst
);
1463 * kmod_module_get_softdeps:
1465 * @pre: where to save the list of preceding soft dependencies.
1466 * @post: where to save the list of post soft dependencies.
1468 * Get soft dependencies for this kmod module. Soft dependencies come
1469 * from configuration file and are not cached in @mod because it may include
1470 * dependency cycles that would make we leak kmod_module. Any call
1471 * to this function will search for this module in configuration, allocate a
1472 * list and return the result.
1474 * Both @pre and @post are newly created list of kmod_module and
1475 * should be unreferenced with kmod_module_unref_list().
1477 * Returns: 0 on success or < 0 otherwise.
1479 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1480 struct kmod_list
**pre
,
1481 struct kmod_list
**post
)
1483 const struct kmod_list
*l
;
1484 const struct kmod_config
*config
;
1486 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1489 assert(*pre
== NULL
);
1490 assert(*post
== NULL
);
1492 config
= kmod_get_config(mod
->ctx
);
1494 kmod_list_foreach(l
, config
->softdeps
) {
1495 const char *modname
= kmod_softdep_get_name(l
);
1496 const char * const *array
;
1499 if (fnmatch(modname
, mod
->name
, 0) != 0)
1502 array
= kmod_softdep_get_pre(l
, &count
);
1503 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1504 array
= kmod_softdep_get_post(l
, &count
);
1505 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1508 * find only the first command, as modprobe from
1509 * module-init-tools does
1518 * kmod_module_get_remove_commands:
1521 * Get remove commands for this kmod module. Remove commands come from the
1522 * configuration file and are cached in @mod. The first call to this function
1523 * will search for this module in configuration and subsequent calls return
1524 * the cached string. The remove commands are returned as they were in the
1525 * configuration, concatenated by ';'. No other processing is made in this
1528 * Returns: a string with all remove commands separated by semicolons. This
1529 * string is owned by @mod, do not free it.
1531 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1536 if (!mod
->init
.remove_commands
) {
1538 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1539 const struct kmod_list
*l
;
1540 const struct kmod_config
*config
;
1542 config
= kmod_get_config(mod
->ctx
);
1544 kmod_list_foreach(l
, config
->remove_commands
) {
1545 const char *modname
= kmod_command_get_modname(l
);
1547 if (fnmatch(modname
, mod
->name
, 0) != 0)
1550 m
->remove_commands
= kmod_command_get_command(l
);
1553 * find only the first command, as modprobe from
1554 * module-init-tools does
1559 m
->init
.remove_commands
= true;
1562 return mod
->remove_commands
;
1565 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1567 mod
->init
.remove_commands
= true;
1568 mod
->remove_commands
= cmd
;
1572 * SECTION:libkmod-loaded
1573 * @short_description: currently loaded modules
1575 * Information about currently loaded modules, as reported by Linux kernel.
1576 * These information are not cached by libkmod and are always read from /sys
1577 * and /proc/modules.
1581 * kmod_module_new_from_loaded:
1582 * @ctx: kmod library context
1583 * @list: where to save the list of loaded modules
1585 * Create a new list of kmod modules with all modules currently loaded in
1586 * kernel. It uses /proc/modules to get the names of loaded modules and to
1587 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1588 * They are put in @list in no particular order.
1590 * The initial refcount is 1, and needs to be decremented to release the
1591 * resources of the kmod_module. The returned @list must be released by
1592 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1593 * kmod_modules created, they are all released upon @ctx destruction too. Do
1594 * not unref @ctx before all the desired operations with the returned list are
1597 * Returns: 0 on success or < 0 on error.
1599 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1600 struct kmod_list
**list
)
1602 struct kmod_list
*l
= NULL
;
1606 if (ctx
== NULL
|| list
== NULL
)
1609 fp
= fopen("/proc/modules", "re");
1612 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1616 while (fgets(line
, sizeof(line
), fp
)) {
1617 struct kmod_module
*m
;
1618 struct kmod_list
*node
;
1620 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1622 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1624 ERR(ctx
, "could not get module from name '%s': %s\n",
1625 name
, strerror(-err
));
1629 node
= kmod_list_append(l
, m
);
1633 ERR(ctx
, "out of memory\n");
1634 kmod_module_unref(m
);
1645 * kmod_module_initstate_str:
1646 * @state: the state as returned by kmod_module_get_initstate()
1648 * Translate a initstate to a string.
1650 * Returns: the string associated to the @state. This string is statically
1651 * allocated, do not free it.
1653 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1656 case KMOD_MODULE_BUILTIN
:
1658 case KMOD_MODULE_LIVE
:
1660 case KMOD_MODULE_COMING
:
1662 case KMOD_MODULE_GOING
:
1670 * kmod_module_get_initstate:
1673 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1676 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1677 * KMOD_MODULE_BUILTIN: module is builtin;
1678 * KMOD_MODULE_LIVE: module is live in kernel;
1679 * KMOD_MODULE_COMING: module is being loaded;
1680 * KMOD_MODULE_GOING: module is being unloaded.
1682 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1684 char path
[PATH_MAX
], buf
[32];
1685 int fd
, err
, pathlen
;
1691 return KMOD_MODULE_BUILTIN
;
1693 pathlen
= snprintf(path
, sizeof(path
),
1694 "/sys/module/%s/initstate", mod
->name
);
1695 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1699 DBG(mod
->ctx
, "could not open '%s': %s\n",
1700 path
, strerror(-err
));
1702 if (pathlen
> (int)sizeof("/initstate") - 1) {
1704 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1705 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1706 return KMOD_MODULE_BUILTIN
;
1709 DBG(mod
->ctx
, "could not open '%s': %s\n",
1710 path
, strerror(-err
));
1714 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1717 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1718 path
, strerror(-err
));
1722 if (streq(buf
, "live\n"))
1723 return KMOD_MODULE_LIVE
;
1724 else if (streq(buf
, "coming\n"))
1725 return KMOD_MODULE_COMING
;
1726 else if (streq(buf
, "going\n"))
1727 return KMOD_MODULE_GOING
;
1729 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1734 * kmod_module_get_size:
1737 * Get the size of this kmod module as returned by Linux kernel. If supported,
1738 * the size is read from the coresize attribute in /sys/module. For older
1739 * kernels, this falls back on /proc/modules and searches for the specified
1740 * module to get its size.
1742 * Returns: the size of this kmod module.
1744 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1749 long size
= -ENOENT
;
1755 /* try to open the module dir in /sys. If this fails, don't
1756 * bother trying to find the size as we know the module isn't
1759 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1760 dfd
= open(line
, O_RDONLY
);
1764 /* available as of linux 3.3.x */
1765 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1767 if (read_str_long(cfd
, &size
, 10) < 0)
1768 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1773 /* fall back on parsing /proc/modules */
1774 fp
= fopen("/proc/modules", "re");
1778 "could not open /proc/modules: %s\n", strerror(errno
));
1782 while (fgets(line
, sizeof(line
), fp
)) {
1783 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1787 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1790 tok
= strtok_r(NULL
, " \t", &saveptr
);
1793 "invalid line format at /proc/modules:%d\n", lineno
);
1797 value
= strtol(tok
, &endptr
, 10);
1798 if (endptr
== tok
|| *endptr
!= '\0') {
1800 "invalid line format at /proc/modules:%d\n", lineno
);
1815 * kmod_module_get_refcnt:
1818 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1821 * Returns: 0 on success or < 0 on failure.
1823 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1825 char path
[PATH_MAX
];
1832 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1833 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1836 DBG(mod
->ctx
, "could not open '%s': %s\n",
1837 path
, strerror(errno
));
1841 err
= read_str_long(fd
, &refcnt
, 10);
1844 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1845 path
, strerror(-err
));
1853 * kmod_module_get_holders:
1856 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1857 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1859 * Returns: a new list of kmod modules on success or NULL on failure.
1861 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1863 char dname
[PATH_MAX
];
1864 struct kmod_list
*list
= NULL
;
1865 struct dirent
*dent
;
1868 if (mod
== NULL
|| mod
->ctx
== NULL
)
1871 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1875 ERR(mod
->ctx
, "could not open '%s': %s\n",
1876 dname
, strerror(errno
));
1880 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
1881 struct kmod_module
*holder
;
1882 struct kmod_list
*l
;
1885 if (dent
->d_name
[0] == '.') {
1886 if (dent
->d_name
[1] == '\0' ||
1887 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1891 err
= kmod_module_new_from_name(mod
->ctx
, dent
->d_name
,
1894 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1895 dent
->d_name
, strerror(-err
));
1899 l
= kmod_list_append(list
, holder
);
1903 ERR(mod
->ctx
, "out of memory\n");
1904 kmod_module_unref(holder
);
1914 kmod_module_unref_list(list
);
1918 struct kmod_module_section
{
1919 unsigned long address
;
1923 static void kmod_module_section_free(struct kmod_module_section
*section
)
1929 * kmod_module_get_sections:
1932 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1933 * structure contained in this list is internal to libkmod and their fields
1934 * can be obtained by calling kmod_module_section_get_name() and
1935 * kmod_module_section_get_address().
1937 * After use, free the @list by calling kmod_module_section_free_list().
1939 * Returns: a new list of kmod module sections on success or NULL on failure.
1941 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1943 char dname
[PATH_MAX
];
1944 struct kmod_list
*list
= NULL
;
1945 struct dirent
*dent
;
1952 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1956 ERR(mod
->ctx
, "could not open '%s': %s\n",
1957 dname
, strerror(errno
));
1963 for (dent
= readdir(d
); dent
; dent
= readdir(d
)) {
1964 struct kmod_module_section
*section
;
1965 struct kmod_list
*l
;
1966 unsigned long address
;
1970 if (dent
->d_name
[0] == '.') {
1971 if (dent
->d_name
[1] == '\0' ||
1972 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1976 fd
= openat(dfd
, dent
->d_name
, O_RDONLY
|O_CLOEXEC
);
1978 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1979 dname
, dent
->d_name
);
1983 err
= read_str_ulong(fd
, &address
, 16);
1986 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1987 dname
, dent
->d_name
);
1991 namesz
= strlen(dent
->d_name
) + 1;
1992 section
= malloc(sizeof(*section
) + namesz
);
1994 if (section
== NULL
) {
1995 ERR(mod
->ctx
, "out of memory\n");
1999 section
->address
= address
;
2000 memcpy(section
->name
, dent
->d_name
, namesz
);
2002 l
= kmod_list_append(list
, section
);
2006 ERR(mod
->ctx
, "out of memory\n");
2017 kmod_module_unref_list(list
);
2022 * kmod_module_section_get_module_name:
2023 * @entry: a list entry representing a kmod module section
2025 * Get the name of a kmod module section.
2027 * After use, free the @list by calling kmod_module_section_free_list().
2029 * Returns: the name of this kmod module section on success or NULL on
2030 * failure. The string is owned by the section, do not free it.
2032 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2034 struct kmod_module_section
*section
;
2039 section
= entry
->data
;
2040 return section
->name
;
2044 * kmod_module_section_get_address:
2045 * @entry: a list entry representing a kmod module section
2047 * Get the address of a kmod module section.
2049 * After use, free the @list by calling kmod_module_section_free_list().
2051 * Returns: the address of this kmod module section on success or ULONG_MAX
2054 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2056 struct kmod_module_section
*section
;
2059 return (unsigned long)-1;
2061 section
= entry
->data
;
2062 return section
->address
;
2066 * kmod_module_section_free_list:
2067 * @list: kmod module section list
2069 * Release the resources taken by @list
2071 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2074 kmod_module_section_free(list
->data
);
2075 list
= kmod_list_remove(list
);
2079 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2081 if (mod
->file
== NULL
) {
2082 const char *path
= kmod_module_get_path(mod
);
2089 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2091 if (mod
->file
== NULL
)
2095 return kmod_file_get_elf(mod
->file
);
2098 struct kmod_module_info
{
2103 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2105 struct kmod_module_info
*info
;
2107 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2111 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2113 memcpy(info
->key
, key
, keylen
);
2114 info
->key
[keylen
] = '\0';
2115 memcpy(info
->value
, value
, valuelen
);
2116 info
->value
[valuelen
] = '\0';
2120 static void kmod_module_info_free(struct kmod_module_info
*info
)
2125 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2127 struct kmod_module_info
*info
;
2128 struct kmod_list
*n
;
2130 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2133 n
= kmod_list_append(*list
, info
);
2137 kmod_module_info_free(info
);
2142 * kmod_module_get_info:
2144 * @list: where to return list of module information. Use
2145 * kmod_module_info_get_key() and
2146 * kmod_module_info_get_value(). Release this list with
2147 * kmod_module_info_free_list()
2149 * Get a list of entries in ELF section ".modinfo", these contain
2150 * alias, license, depends, vermagic and other keys with respective
2151 * values. If the module is signed (CONFIG_MODULE_SIG), information
2152 * about the module signature is included as well: signer,
2153 * sig_key and sig_hashalgo.
2155 * After use, free the @list by calling kmod_module_info_free_list().
2157 * Returns: 0 on success or < 0 otherwise.
2159 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2161 struct kmod_elf
*elf
;
2163 int i
, count
, ret
= -ENOMEM
;
2164 struct kmod_signature_info sig_info
;
2166 if (mod
== NULL
|| list
== NULL
)
2169 assert(*list
== NULL
);
2171 elf
= kmod_module_get_elf(mod
);
2175 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2179 for (i
= 0; i
< count
; i
++) {
2180 struct kmod_list
*n
;
2181 const char *key
, *value
;
2182 size_t keylen
, valuelen
;
2185 value
= strchr(key
, '=');
2186 if (value
== NULL
) {
2187 keylen
= strlen(key
);
2191 keylen
= value
- key
;
2193 valuelen
= strlen(value
);
2196 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2201 if (kmod_module_signature_info(mod
->file
, &sig_info
)) {
2202 struct kmod_list
*n
;
2205 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2206 sig_info
.signer
, sig_info
.signer_len
);
2211 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2212 key_hex
= malloc(sig_info
.key_id_len
* 3);
2213 if (key_hex
== NULL
)
2215 for (i
= 0; i
< (int)sig_info
.key_id_len
; i
++) {
2216 sprintf(key_hex
+ i
* 3, "%02X",
2217 (unsigned char)sig_info
.key_id
[i
]);
2218 if (i
< (int)sig_info
.key_id_len
- 1)
2219 key_hex
[i
* 3 + 2] = ':';
2221 n
= kmod_module_info_append(list
, "sig_key", strlen("sig_key"),
2222 key_hex
, sig_info
.key_id_len
* 3 - 1);
2228 n
= kmod_module_info_append(list
,
2229 "sig_hashalgo", strlen("sig_hashalgo"),
2230 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2236 * Omit sig_info.id_type and sig_info.algo for now, as these
2237 * are currently constant.
2244 kmod_module_info_free_list(*list
);
2252 * kmod_module_info_get_key:
2253 * @entry: a list entry representing a kmod module info
2255 * Get the key of a kmod module info.
2257 * Returns: the key of this kmod module info on success or NULL on
2258 * failure. The string is owned by the info, do not free it.
2260 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2262 struct kmod_module_info
*info
;
2272 * kmod_module_info_get_value:
2273 * @entry: a list entry representing a kmod module info
2275 * Get the value of a kmod module info.
2277 * Returns: the value of this kmod module info on success or NULL on
2278 * failure. The string is owned by the info, do not free it.
2280 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2282 struct kmod_module_info
*info
;
2292 * kmod_module_info_free_list:
2293 * @list: kmod module info list
2295 * Release the resources taken by @list
2297 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2300 kmod_module_info_free(list
->data
);
2301 list
= kmod_list_remove(list
);
2305 struct kmod_module_version
{
2310 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2312 struct kmod_module_version
*mv
;
2313 size_t symbollen
= strlen(symbol
) + 1;
2315 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2320 memcpy(mv
->symbol
, symbol
, symbollen
);
2324 static void kmod_module_version_free(struct kmod_module_version
*version
)
2330 * kmod_module_get_versions:
2332 * @list: where to return list of module versions. Use
2333 * kmod_module_version_get_symbol() and
2334 * kmod_module_version_get_crc(). Release this list with
2335 * kmod_module_versions_free_list()
2337 * Get a list of entries in ELF section "__versions".
2339 * After use, free the @list by calling kmod_module_versions_free_list().
2341 * Returns: 0 on success or < 0 otherwise.
2343 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2345 struct kmod_elf
*elf
;
2346 struct kmod_modversion
*versions
;
2347 int i
, count
, ret
= 0;
2349 if (mod
== NULL
|| list
== NULL
)
2352 assert(*list
== NULL
);
2354 elf
= kmod_module_get_elf(mod
);
2358 count
= kmod_elf_get_modversions(elf
, &versions
);
2362 for (i
= 0; i
< count
; i
++) {
2363 struct kmod_module_version
*mv
;
2364 struct kmod_list
*n
;
2366 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2369 kmod_module_versions_free_list(*list
);
2374 n
= kmod_list_append(*list
, mv
);
2378 kmod_module_version_free(mv
);
2379 kmod_module_versions_free_list(*list
);
2393 * kmod_module_version_get_symbol:
2394 * @entry: a list entry representing a kmod module versions
2396 * Get the symbol of a kmod module versions.
2398 * Returns: the symbol of this kmod module versions on success or NULL
2399 * on failure. The string is owned by the versions, do not free it.
2401 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2403 struct kmod_module_version
*version
;
2408 version
= entry
->data
;
2409 return version
->symbol
;
2413 * kmod_module_version_get_crc:
2414 * @entry: a list entry representing a kmod module version
2416 * Get the crc of a kmod module version.
2418 * Returns: the crc of this kmod module version on success or NULL on
2419 * failure. The string is owned by the version, do not free it.
2421 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2423 struct kmod_module_version
*version
;
2428 version
= entry
->data
;
2429 return version
->crc
;
2433 * kmod_module_versions_free_list:
2434 * @list: kmod module versions list
2436 * Release the resources taken by @list
2438 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2441 kmod_module_version_free(list
->data
);
2442 list
= kmod_list_remove(list
);
2446 struct kmod_module_symbol
{
2451 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2453 struct kmod_module_symbol
*mv
;
2454 size_t symbollen
= strlen(symbol
) + 1;
2456 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2461 memcpy(mv
->symbol
, symbol
, symbollen
);
2465 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2471 * kmod_module_get_symbols:
2473 * @list: where to return list of module symbols. Use
2474 * kmod_module_symbol_get_symbol() and
2475 * kmod_module_symbol_get_crc(). Release this list with
2476 * kmod_module_symbols_free_list()
2478 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2480 * After use, free the @list by calling kmod_module_symbols_free_list().
2482 * Returns: 0 on success or < 0 otherwise.
2484 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2486 struct kmod_elf
*elf
;
2487 struct kmod_modversion
*symbols
;
2488 int i
, count
, ret
= 0;
2490 if (mod
== NULL
|| list
== NULL
)
2493 assert(*list
== NULL
);
2495 elf
= kmod_module_get_elf(mod
);
2499 count
= kmod_elf_get_symbols(elf
, &symbols
);
2503 for (i
= 0; i
< count
; i
++) {
2504 struct kmod_module_symbol
*mv
;
2505 struct kmod_list
*n
;
2507 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2510 kmod_module_symbols_free_list(*list
);
2515 n
= kmod_list_append(*list
, mv
);
2519 kmod_module_symbol_free(mv
);
2520 kmod_module_symbols_free_list(*list
);
2534 * kmod_module_symbol_get_symbol:
2535 * @entry: a list entry representing a kmod module symbols
2537 * Get the symbol of a kmod module symbols.
2539 * Returns: the symbol of this kmod module symbols on success or NULL
2540 * on failure. The string is owned by the symbols, do not free it.
2542 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2544 struct kmod_module_symbol
*symbol
;
2549 symbol
= entry
->data
;
2550 return symbol
->symbol
;
2554 * kmod_module_symbol_get_crc:
2555 * @entry: a list entry representing a kmod module symbol
2557 * Get the crc of a kmod module symbol.
2559 * Returns: the crc of this kmod module symbol on success or NULL on
2560 * failure. The string is owned by the symbol, do not free it.
2562 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2564 struct kmod_module_symbol
*symbol
;
2569 symbol
= entry
->data
;
2574 * kmod_module_symbols_free_list:
2575 * @list: kmod module symbols list
2577 * Release the resources taken by @list
2579 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2582 kmod_module_symbol_free(list
->data
);
2583 list
= kmod_list_remove(list
);
2587 struct kmod_module_dependency_symbol
{
2593 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2595 struct kmod_module_dependency_symbol
*mv
;
2596 size_t symbollen
= strlen(symbol
) + 1;
2598 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2604 memcpy(mv
->symbol
, symbol
, symbollen
);
2608 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2610 free(dependency_symbol
);
2614 * kmod_module_get_dependency_symbols:
2616 * @list: where to return list of module dependency_symbols. Use
2617 * kmod_module_dependency_symbol_get_symbol() and
2618 * kmod_module_dependency_symbol_get_crc(). Release this list with
2619 * kmod_module_dependency_symbols_free_list()
2621 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2623 * After use, free the @list by calling
2624 * kmod_module_dependency_symbols_free_list().
2626 * Returns: 0 on success or < 0 otherwise.
2628 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2630 struct kmod_elf
*elf
;
2631 struct kmod_modversion
*symbols
;
2632 int i
, count
, ret
= 0;
2634 if (mod
== NULL
|| list
== NULL
)
2637 assert(*list
== NULL
);
2639 elf
= kmod_module_get_elf(mod
);
2643 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2647 for (i
= 0; i
< count
; i
++) {
2648 struct kmod_module_dependency_symbol
*mv
;
2649 struct kmod_list
*n
;
2651 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2656 kmod_module_dependency_symbols_free_list(*list
);
2661 n
= kmod_list_append(*list
, mv
);
2665 kmod_module_dependency_symbol_free(mv
);
2666 kmod_module_dependency_symbols_free_list(*list
);
2680 * kmod_module_dependency_symbol_get_symbol:
2681 * @entry: a list entry representing a kmod module dependency_symbols
2683 * Get the dependency symbol of a kmod module
2685 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2686 * on failure. The string is owned by the dependency_symbols, do not free it.
2688 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2690 struct kmod_module_dependency_symbol
*dependency_symbol
;
2695 dependency_symbol
= entry
->data
;
2696 return dependency_symbol
->symbol
;
2700 * kmod_module_dependency_symbol_get_crc:
2701 * @entry: a list entry representing a kmod module dependency_symbol
2703 * Get the crc of a kmod module dependency_symbol.
2705 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2706 * failure. The string is owned by the dependency_symbol, do not free it.
2708 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2710 struct kmod_module_dependency_symbol
*dependency_symbol
;
2715 dependency_symbol
= entry
->data
;
2716 return dependency_symbol
->crc
;
2720 * kmod_module_dependency_symbol_get_bind:
2721 * @entry: a list entry representing a kmod module dependency_symbol
2723 * Get the bind type of a kmod module dependency_symbol.
2725 * Returns: the bind of this kmod module dependency_symbol on success
2726 * or < 0 on failure.
2728 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2730 struct kmod_module_dependency_symbol
*dependency_symbol
;
2735 dependency_symbol
= entry
->data
;
2736 return dependency_symbol
->bind
;
2740 * kmod_module_dependency_symbols_free_list:
2741 * @list: kmod module dependency_symbols list
2743 * Release the resources taken by @list
2745 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2748 kmod_module_dependency_symbol_free(list
->data
);
2749 list
= kmod_list_remove(list
);