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, see <http://www.gnu.org/licenses/>.
35 #include <sys/syscall.h>
36 #include <sys/types.h>
38 #ifdef HAVE_LINUX_MODULE_H
39 #include <linux/module.h>
42 #include <shared/util.h>
45 #include "libkmod-internal.h"
48 * SECTION:libkmod-module
49 * @short_description: operate on kernel modules
55 * Opaque object representing a module.
62 struct kmod_list
*dep
;
64 const char *install_commands
; /* owned by kmod_config */
65 const char *remove_commands
; /* owned by kmod_config */
66 char *alias
; /* only set if this module was created from an alias */
67 struct kmod_file
*file
;
73 bool install_commands
: 1;
74 bool remove_commands
: 1;
78 * private field used by kmod_module_get_probe_list() to detect
84 * set by kmod_module_get_probe_list: indicates for probe_insert()
85 * whether the module's command and softdep should be ignored
90 * set by kmod_module_get_probe_list: indicates whether this is the
91 * module the user asked for or its dependency, or whether this
97 * if module was created by searching the modules.builtin file, this
98 * is set. There's nothing much useful one can do with such a
99 * "module", except knowing it's builtin.
104 static inline const char *path_join(const char *path
, size_t prefixlen
,
112 pathlen
= strlen(path
);
113 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
116 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
120 static inline bool module_is_inkernel(struct kmod_module
*mod
)
122 int state
= kmod_module_get_initstate(mod
);
124 if (state
== KMOD_MODULE_LIVE
||
125 state
== KMOD_MODULE_BUILTIN
)
131 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
133 struct kmod_ctx
*ctx
= mod
->ctx
;
134 struct kmod_list
*list
= NULL
;
143 assert(mod
->dep
== NULL
);
144 mod
->init
.dep
= true;
146 p
= strchr(line
, ':');
151 dirname
= kmod_get_dirname(mod
->ctx
);
152 dirnamelen
= strlen(dirname
);
153 if (dirnamelen
+ 2 >= PATH_MAX
)
156 memcpy(buf
, dirname
, dirnamelen
);
157 buf
[dirnamelen
] = '/';
159 buf
[dirnamelen
] = '\0';
161 if (mod
->path
== NULL
) {
162 const char *str
= path_join(line
, dirnamelen
, buf
);
165 mod
->path
= strdup(str
);
166 if (mod
->path
== NULL
)
171 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
172 p
= strtok_r(NULL
, " \t", &saveptr
)) {
173 struct kmod_module
*depmod
;
176 path
= path_join(p
, dirnamelen
, buf
);
178 ERR(ctx
, "could not join path '%s' and '%s'.\n",
183 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
185 ERR(ctx
, "ctx=%p path=%s error=%s\n",
186 ctx
, path
, strerror(-err
));
190 DBG(ctx
, "add dep: %s\n", path
);
192 list
= kmod_list_prepend(list
, depmod
);
196 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
203 kmod_module_unref_list(list
);
204 mod
->init
.dep
= false;
208 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
210 mod
->visited
= visited
;
213 void kmod_module_set_builtin(struct kmod_module
*mod
, bool builtin
)
215 mod
->builtin
= builtin
;
218 void kmod_module_set_required(struct kmod_module
*mod
, bool required
)
220 mod
->required
= required
;
224 * Memory layout with alias:
226 * struct kmod_module {
231 * name <----------' | |
232 * alias <-----------' |
233 * name\alias <--------'
235 * Memory layout without alias:
237 * struct kmod_module {
239 * alias -----|----> NULL
242 * name <----------'-'
244 * @key is "name\alias" or "name" (in which case alias == NULL)
246 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
247 const char *name
, size_t namelen
,
248 const char *alias
, size_t aliaslen
,
249 struct kmod_module
**mod
)
251 struct kmod_module
*m
;
254 m
= kmod_pool_get_module(ctx
, key
);
256 *mod
= kmod_module_ref(m
);
263 keylen
= namelen
+ aliaslen
+ 1;
265 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
269 memset(m
, 0, sizeof(*m
));
271 m
->ctx
= kmod_ref(ctx
);
272 m
->name
= (char *)m
+ sizeof(*m
);
273 memcpy(m
->name
, key
, keylen
+ 1);
275 m
->hashkey
= m
->name
;
278 m
->name
[namelen
] = '\0';
279 m
->alias
= m
->name
+ namelen
+ 1;
280 m
->hashkey
= m
->name
+ keylen
+ 1;
281 memcpy(m
->hashkey
, key
, keylen
+ 1);
285 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
292 * kmod_module_new_from_name:
293 * @ctx: kmod library context
294 * @name: name of the module
295 * @mod: where to save the created struct kmod_module
297 * Create a new struct kmod_module using the module name. @name can not be an
298 * alias, file name or anything else; it must be a module name. There's no
299 * check if the module exists in the system.
301 * This function is also used internally by many others that return a new
302 * struct kmod_module or a new list of modules.
304 * The initial refcount is 1, and needs to be decremented to release the
305 * resources of the kmod_module. Since libkmod keeps track of all
306 * kmod_modules created, they are all released upon @ctx destruction too. Do
307 * not unref @ctx before all the desired operations with the returned
308 * kmod_module are done.
310 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
311 * module name or if memory allocation failed.
313 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
315 struct kmod_module
**mod
)
318 char name_norm
[PATH_MAX
];
320 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
323 modname_normalize(name
, name_norm
, &namelen
);
325 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
328 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
329 const char *name
, struct kmod_module
**mod
)
333 size_t namelen
= strlen(name
);
334 size_t aliaslen
= strlen(alias
);
336 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
337 return -ENAMETOOLONG
;
339 memcpy(key
, name
, namelen
);
340 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
343 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
351 * kmod_module_new_from_path:
352 * @ctx: kmod library context
353 * @path: path where to find the given module
354 * @mod: where to save the created struct kmod_module
356 * Create a new struct kmod_module using the module path. @path must be an
357 * existent file with in the filesystem and must be accessible to libkmod.
359 * The initial refcount is 1, and needs to be decremented to release the
360 * resources of the kmod_module. Since libkmod keeps track of all
361 * kmod_modules created, they are all released upon @ctx destruction too. Do
362 * not unref @ctx before all the desired operations with the returned
363 * kmod_module are done.
365 * If @path is relative, it's treated as relative to the current working
366 * directory. Otherwise, give an absolute path.
368 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
369 * it's not a valid file for a kmod_module or if memory allocation failed.
371 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
373 struct kmod_module
**mod
)
375 struct kmod_module
*m
;
382 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
385 abspath
= path_make_absolute_cwd(path
);
386 if (abspath
== NULL
) {
387 DBG(ctx
, "no absolute path for %s\n", path
);
391 err
= stat(abspath
, &st
);
394 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
399 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
400 DBG(ctx
, "could not get modname from path %s\n", path
);
405 m
= kmod_pool_get_module(ctx
, name
);
409 else if (streq(m
->path
, abspath
))
412 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
413 name
, abspath
, m
->path
);
418 *mod
= kmod_module_ref(m
);
422 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
438 * Drop a reference of the kmod module. If the refcount reaches zero, its
439 * resources are released.
441 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
442 * returns the passed @mod with its refcount decremented.
444 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
449 if (--mod
->refcount
> 0)
452 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
454 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
455 kmod_module_unref_list(mod
->dep
);
458 kmod_file_unref(mod
->file
);
460 kmod_unref(mod
->ctx
);
471 * Take a reference of the kmod module, incrementing its refcount.
473 * Returns: the passed @module with its refcount incremented.
475 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
485 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
489 if (*(_list) != NULL) \
494 * kmod_module_new_from_lookup:
495 * @ctx: kmod library context
496 * @given_alias: alias to look for
497 * @list: an empty list where to save the list of modules matching
500 * Create a new list of kmod modules using an alias or module name and lookup
501 * libkmod's configuration files and indexes in order to find the module.
502 * Once it's found in one of the places, it stops searching and create the
503 * list of modules that is saved in @list.
505 * The search order is: 1. aliases in configuration file; 2. module names in
506 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
507 * in modules.alias index.
509 * The initial refcount is 1, and needs to be decremented to release the
510 * resources of the kmod_module. The returned @list must be released by
511 * calling kmod_module_unref_list(). Since libkmod keeps track of all
512 * kmod_modules created, they are all released upon @ctx destruction too. Do
513 * not unref @ctx before all the desired operations with the returned list are
516 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
517 * methods failed, which is basically due to memory allocation fail. If module
518 * is not found, it still returns 0, but @list is an empty list.
520 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
521 const char *given_alias
,
522 struct kmod_list
**list
)
525 char alias
[PATH_MAX
];
527 if (ctx
== NULL
|| given_alias
== NULL
)
530 if (list
== NULL
|| *list
!= NULL
) {
531 ERR(ctx
, "An empty list is needed to create lookup\n");
535 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
536 DBG(ctx
, "invalid alias: %s\n", given_alias
);
540 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
542 /* Aliases from config file override all the others */
543 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
544 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
546 DBG(ctx
, "lookup modules.dep %s\n", alias
);
547 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
548 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
550 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
551 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
552 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
554 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
555 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
556 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
558 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
559 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
560 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
562 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
563 err
= kmod_lookup_alias_from_builtin_file(ctx
, alias
, list
);
564 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
567 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
570 DBG(ctx
, "Failed to lookup %s\n", alias
);
571 kmod_module_unref_list(*list
);
575 #undef CHECK_ERR_AND_FINISH
578 * kmod_module_unref_list:
579 * @list: list of kmod modules
581 * Drop a reference of each kmod module in @list and releases the resources
582 * taken by the list itself.
586 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
588 for (; list
!= NULL
; list
= kmod_list_remove(list
))
589 kmod_module_unref(list
->data
);
595 * kmod_module_get_filtered_blacklist:
596 * @ctx: kmod library context
597 * @input: list of kmod_module to be filtered with blacklist
598 * @output: where to save the new list
600 * This function should not be used. Use kmod_module_apply_filter instead.
602 * Given a list @input, this function filter it out with config's blacklist
603 * and save it in @output.
605 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
608 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
609 const struct kmod_list
*input
,
610 struct kmod_list
**output
)
612 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
615 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
617 if (!mod
->init
.dep
) {
619 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
624 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
635 * kmod_module_get_dependencies:
638 * Search the modules.dep index to find the dependencies of the given @mod.
639 * The result is cached in @mod, so subsequent calls to this function will
640 * return the already searched list of modules.
642 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
643 * that can be released by calling kmod_module_unref_list().
645 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
647 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
652 module_get_dependencies_noref(mod
);
654 kmod_list_foreach(l
, mod
->dep
) {
655 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
657 kmod_module_unref(l
->data
);
667 ERR(mod
->ctx
, "out of memory\n");
668 kmod_module_unref_list(list_new
);
673 * kmod_module_get_module:
674 * @entry: an entry in a list of kmod modules.
676 * Get the kmod module of this @entry in the list, increasing its refcount.
677 * After it's used, unref it. Since the refcount is incremented upon return,
678 * you still have to call kmod_module_unref_list() to release the list of kmod
681 * Returns: NULL on failure or the kmod_module contained in this list entry
682 * with its refcount incremented.
684 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
689 return kmod_module_ref(entry
->data
);
693 * kmod_module_get_name:
696 * Get the name of this kmod module. Name is always available, independently
697 * if it was created by kmod_module_new_from_name() or another function and
698 * it's always normalized (dashes are replaced with underscores).
700 * Returns: the name of this kmod module.
702 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
711 * kmod_module_get_path:
714 * Get the path of this kmod module. If this kmod module was not created by
715 * path, it can search the modules.dep index in order to find out the module
716 * under context's dirname.
718 * Returns: the path of this kmod module or NULL if such information is not
721 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
728 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
730 if (mod
->path
!= NULL
)
736 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
740 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
747 extern long delete_module(const char *name
, unsigned int flags
);
750 * kmod_module_remove_module:
752 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
753 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
754 * use by a kernel subsystem or other process;
755 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
758 * Remove a module from Linux kernel.
760 * Returns: 0 on success or < 0 on failure.
762 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
770 /* Filter out other flags and force ONONBLOCK */
771 flags
&= KMOD_REMOVE_FORCE
;
772 flags
|= KMOD_REMOVE_NOWAIT
;
774 err
= delete_module(mod
->name
, flags
);
777 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
783 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
786 * kmod_module_insert_module:
788 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
789 * behavior of this function, valid flags are
790 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
791 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
792 * @options: module's options to pass to Linux Kernel.
794 * Insert a module in Linux kernel. It opens the file pointed by @mod,
795 * mmap'ing it and passing to kernel.
797 * Returns: 0 on success or < 0 on failure. If module is already loaded it
800 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
807 struct kmod_elf
*elf
;
809 const char *args
= options
? options
: "";
814 path
= kmod_module_get_path(mod
);
816 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
820 mod
->file
= kmod_file_open(mod
->ctx
, path
);
821 if (mod
->file
== NULL
) {
826 if (kmod_file_get_direct(mod
->file
)) {
827 unsigned int kernel_flags
= 0;
829 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
)
830 kernel_flags
|= MODULE_INIT_IGNORE_VERMAGIC
;
831 if (flags
& KMOD_INSERT_FORCE_MODVERSION
)
832 kernel_flags
|= MODULE_INIT_IGNORE_MODVERSIONS
;
834 err
= finit_module(kmod_file_get_fd(mod
->file
), args
, kernel_flags
);
835 if (err
== 0 || errno
!= ENOSYS
)
839 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
840 elf
= kmod_file_get_elf(mod
->file
);
846 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
847 err
= kmod_elf_strip_section(elf
, "__versions");
849 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
852 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
853 err
= kmod_elf_strip_vermagic(elf
);
855 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
858 mem
= kmod_elf_get_memory(elf
);
860 mem
= kmod_file_get_contents(mod
->file
);
862 size
= kmod_file_get_size(mod
->file
);
864 err
= init_module(mem
, size
, args
);
868 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
873 static bool module_is_blacklisted(struct kmod_module
*mod
)
875 struct kmod_ctx
*ctx
= mod
->ctx
;
876 const struct kmod_config
*config
= kmod_get_config(ctx
);
877 const struct kmod_list
*bl
= config
->blacklists
;
878 const struct kmod_list
*l
;
880 kmod_list_foreach(l
, bl
) {
881 const char *modname
= kmod_blacklist_get_modname(l
);
883 if (streq(modname
, mod
->name
))
891 * kmod_module_apply_filter
892 * @ctx: kmod library context
893 * @filter_type: bitmask to filter modules out, valid types are
894 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
895 * KMOD_FILTER_BUILTIN: filter builtin modules out.
896 * @input: list of kmod_module to be filtered
897 * @output: where to save the new list
899 * Given a list @input, this function filter it out by the filter mask
900 * and save it in @output.
902 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
905 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
906 enum kmod_filter filter_type
,
907 const struct kmod_list
*input
,
908 struct kmod_list
**output
)
910 const struct kmod_list
*li
;
912 if (ctx
== NULL
|| output
== NULL
)
919 kmod_list_foreach(li
, input
) {
920 struct kmod_module
*mod
= li
->data
;
921 struct kmod_list
*node
;
923 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
924 module_is_blacklisted(mod
))
927 if ((filter_type
& KMOD_FILTER_BUILTIN
) && mod
->builtin
)
930 node
= kmod_list_append(*output
, mod
);
935 kmod_module_ref(mod
);
941 kmod_module_unref_list(*output
);
946 static int command_do(struct kmod_module
*mod
, const char *type
,
949 const char *modname
= kmod_module_get_name(mod
);
952 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
954 setenv("MODPROBE_MODULE", modname
, 1);
956 unsetenv("MODPROBE_MODULE");
958 if (err
== -1 || WEXITSTATUS(err
)) {
959 ERR(mod
->ctx
, "Error running %s command for %s\n",
962 err
= -WEXITSTATUS(err
);
968 struct probe_insert_cb
{
969 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
973 static int module_do_install_commands(struct kmod_module
*mod
,
975 struct probe_insert_cb
*cb
)
977 const char *command
= kmod_module_get_install_commands(mod
);
979 _cleanup_free_
char *cmd
;
981 size_t cmdlen
, options_len
, varlen
;
988 options_len
= strlen(options
);
989 cmdlen
= strlen(command
);
990 varlen
= sizeof("$CMDLINE_OPTS") - 1;
992 cmd
= memdup(command
, cmdlen
+ 1);
996 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
997 size_t prefixlen
= p
- cmd
;
998 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
999 size_t slen
= cmdlen
- varlen
+ options_len
;
1000 char *suffix
= p
+ varlen
;
1001 char *s
= malloc(slen
+ 1);
1005 memcpy(s
, cmd
, p
- cmd
);
1006 memcpy(s
+ prefixlen
, options
, options_len
);
1007 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
1015 if (cb
->run_install
!= NULL
)
1016 err
= cb
->run_install(mod
, cmd
, cb
->data
);
1018 err
= command_do(mod
, "install", cmd
);
1023 static char *module_options_concat(const char *opt
, const char *xopt
)
1025 // TODO: we might need to check if xopt overrides options on opt
1026 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
1027 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1030 if (optlen
== 0 && xoptlen
== 0)
1033 r
= malloc(optlen
+ xoptlen
+ 2);
1036 memcpy(r
, opt
, optlen
);
1042 memcpy(r
+ optlen
, xopt
, xoptlen
);
1044 r
[optlen
+ xoptlen
] = '\0';
1049 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, 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, 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
,
1102 struct kmod_list
**list
)
1104 struct kmod_list
*dep
, *l
;
1108 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1112 mod
->visited
= true;
1114 dep
= kmod_module_get_dependencies(mod
);
1117 * Called from kmod_module_probe_insert_module(); set the
1118 * ->required flag on mod and all its dependencies before
1119 * they are possibly visited through some softdeps.
1121 mod
->required
= true;
1122 kmod_list_foreach(l
, dep
) {
1123 struct kmod_module
*m
= l
->data
;
1128 kmod_list_foreach(l
, dep
) {
1129 struct kmod_module
*m
= l
->data
;
1130 err
= __kmod_module_fill_softdep(m
, list
);
1136 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1138 kmod_module_unref(mod
);
1143 mod
->ignorecmd
= true;
1145 err
= __kmod_module_fill_softdep(mod
, list
);
1148 kmod_module_unref_list(dep
);
1152 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1154 struct kmod_list
**list
)
1158 assert(mod
!= NULL
);
1159 assert(list
!= NULL
&& *list
== NULL
);
1162 * Make sure we don't get screwed by previous calls to this function
1164 kmod_set_modules_visited(mod
->ctx
, false);
1165 kmod_set_modules_required(mod
->ctx
, false);
1167 err
= __kmod_module_get_probe_list(mod
, true, ignorecmd
, list
);
1169 kmod_module_unref_list(*list
);
1177 * kmod_module_probe_insert_module:
1179 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1180 * behavior of this function, valid flags are
1181 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1182 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1183 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1184 * commands and softdeps configured in the system;
1185 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1186 * live in kernel or not;
1187 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1188 * associated callback function;
1189 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1190 * and the module is already live in kernel, the function will fail if this
1191 * flag is specified;
1192 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1193 * filter to this module and its dependencies. If any of the dependencies (or
1194 * the module) is blacklisted, the probe will fail, unless the blacklisted
1195 * module is already live in kernel;
1196 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1197 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1198 * alias and is blacklisted.
1199 * @extra_options: module's options to pass to Linux Kernel. It applies only
1200 * to @mod, not to its dependencies.
1201 * @run_install: function to run when @mod is backed by an install command.
1202 * @data: data to give back to @run_install callback
1203 * @print_action: function to call with the action being taken (install or
1204 * insmod). It's useful for tools like modprobe when running with verbose
1205 * output or in dry-run mode.
1207 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1208 * install commands and applying blacklist.
1210 * If @run_install is NULL, this function will fork and exec by calling
1211 * system(3). Don't pass a NULL argument in @run_install if your binary is
1212 * setuid/setgid (see warning in system(3)). If you need control over the
1213 * execution of an install command, give a callback function instead.
1215 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1218 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1219 unsigned int flags
, const char *extra_options
,
1220 int (*run_install
)(struct kmod_module
*m
,
1221 const char *cmd
, void *data
),
1223 void (*print_action
)(struct kmod_module
*m
,
1225 const char *options
))
1227 struct kmod_list
*list
= NULL
, *l
;
1228 struct probe_insert_cb cb
;
1234 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1235 && module_is_inkernel(mod
)) {
1236 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1243 * Ugly assignement + check. We need to check if we were told to check
1244 * blacklist and also return the reason why we failed.
1245 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1246 * module is an alias, so we also need to check it
1248 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1249 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1250 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1251 if (module_is_blacklisted(mod
))
1255 err
= kmod_module_get_probe_list(mod
,
1256 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1260 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1261 struct kmod_list
*filtered
= NULL
;
1263 err
= kmod_module_apply_filter(mod
->ctx
,
1264 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1268 kmod_module_unref_list(list
);
1269 if (filtered
== NULL
)
1270 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1275 cb
.run_install
= run_install
;
1276 cb
.data
= (void *) data
;
1278 kmod_list_foreach(l
, list
) {
1279 struct kmod_module
*m
= l
->data
;
1280 const char *moptions
= kmod_module_get_options(m
);
1281 const char *cmd
= kmod_module_get_install_commands(m
);
1284 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1285 && module_is_inkernel(m
)) {
1286 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1292 options
= module_options_concat(moptions
,
1293 m
== mod
? extra_options
: NULL
);
1295 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1296 if (print_action
!= NULL
)
1297 print_action(m
, true, options
?: "");
1299 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1300 err
= module_do_install_commands(m
, options
,
1303 if (print_action
!= NULL
)
1304 print_action(m
, false, options
?: "");
1306 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1307 err
= kmod_module_insert_module(m
, flags
,
1315 * Treat "already loaded" error. If we were told to stop on
1316 * already loaded and the module being loaded is not a softdep
1317 * or dep, bail out. Otherwise, just ignore and continue.
1319 * We need to check here because of race conditions. We
1320 * checked first if module was already loaded but it may have
1321 * been loaded between the check and the moment we try to
1324 if (err
== -EEXIST
&& m
== mod
&&
1325 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1329 * Ignore errors from softdeps
1331 if (err
== -EEXIST
|| !m
->required
)
1338 kmod_module_unref_list(list
);
1343 * kmod_module_get_options:
1346 * Get options of this kmod module. Options come from the configuration file
1347 * and are cached in @mod. The first call to this function will search for
1348 * this module in configuration and subsequent calls return the cached string.
1350 * Returns: a string with all the options separated by spaces. This string is
1351 * owned by @mod, do not free it.
1353 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1358 if (!mod
->init
.options
) {
1360 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1361 const struct kmod_list
*l
;
1362 const struct kmod_config
*config
;
1366 config
= kmod_get_config(mod
->ctx
);
1368 kmod_list_foreach(l
, config
->options
) {
1369 const char *modname
= kmod_option_get_modname(l
);
1374 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1375 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1376 streq(modname
, mod
->alias
))))
1379 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1380 str
= kmod_option_get_options(l
);
1385 tmp
= realloc(opts
, optslen
+ len
+ 2);
1394 opts
[optslen
] = ' ';
1398 memcpy(opts
+ optslen
, str
, len
);
1400 opts
[optslen
] = '\0';
1403 m
->init
.options
= true;
1407 return mod
->options
;
1410 ERR(mod
->ctx
, "out of memory\n");
1415 * kmod_module_get_install_commands:
1418 * Get install commands for this kmod module. Install commands come from the
1419 * configuration file and are cached in @mod. The first call to this function
1420 * will search for this module in configuration and subsequent calls return
1421 * the cached string. The install commands are returned as they were in the
1422 * configuration, concatenated by ';'. No other processing is made in this
1425 * Returns: a string with all install commands separated by semicolons. This
1426 * string is owned by @mod, do not free it.
1428 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1433 if (!mod
->init
.install_commands
) {
1435 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1436 const struct kmod_list
*l
;
1437 const struct kmod_config
*config
;
1439 config
= kmod_get_config(mod
->ctx
);
1441 kmod_list_foreach(l
, config
->install_commands
) {
1442 const char *modname
= kmod_command_get_modname(l
);
1444 if (fnmatch(modname
, mod
->name
, 0) != 0)
1447 m
->install_commands
= kmod_command_get_command(l
);
1450 * find only the first command, as modprobe from
1451 * module-init-tools does
1456 m
->init
.install_commands
= true;
1459 return mod
->install_commands
;
1462 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1464 mod
->init
.install_commands
= true;
1465 mod
->install_commands
= cmd
;
1468 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1470 struct kmod_list
*ret
= NULL
;
1473 for (i
= 0; i
< count
; i
++) {
1474 const char *depname
= array
[i
];
1475 struct kmod_list
*lst
= NULL
;
1478 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1480 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1482 } else if (lst
!= NULL
)
1483 ret
= kmod_list_append_list(ret
, lst
);
1489 * kmod_module_get_softdeps:
1491 * @pre: where to save the list of preceding soft dependencies.
1492 * @post: where to save the list of post soft dependencies.
1494 * Get soft dependencies for this kmod module. Soft dependencies come
1495 * from configuration file and are not cached in @mod because it may include
1496 * dependency cycles that would make we leak kmod_module. Any call
1497 * to this function will search for this module in configuration, allocate a
1498 * list and return the result.
1500 * Both @pre and @post are newly created list of kmod_module and
1501 * should be unreferenced with kmod_module_unref_list().
1503 * Returns: 0 on success or < 0 otherwise.
1505 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1506 struct kmod_list
**pre
,
1507 struct kmod_list
**post
)
1509 const struct kmod_list
*l
;
1510 const struct kmod_config
*config
;
1512 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1515 assert(*pre
== NULL
);
1516 assert(*post
== NULL
);
1518 config
= kmod_get_config(mod
->ctx
);
1520 kmod_list_foreach(l
, config
->softdeps
) {
1521 const char *modname
= kmod_softdep_get_name(l
);
1522 const char * const *array
;
1525 if (fnmatch(modname
, mod
->name
, 0) != 0)
1528 array
= kmod_softdep_get_pre(l
, &count
);
1529 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1530 array
= kmod_softdep_get_post(l
, &count
);
1531 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1534 * find only the first command, as modprobe from
1535 * module-init-tools does
1544 * kmod_module_get_remove_commands:
1547 * Get remove commands for this kmod module. Remove commands come from the
1548 * configuration file and are cached in @mod. The first call to this function
1549 * will search for this module in configuration and subsequent calls return
1550 * the cached string. The remove commands are returned as they were in the
1551 * configuration, concatenated by ';'. No other processing is made in this
1554 * Returns: a string with all remove commands separated by semicolons. This
1555 * string is owned by @mod, do not free it.
1557 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1562 if (!mod
->init
.remove_commands
) {
1564 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1565 const struct kmod_list
*l
;
1566 const struct kmod_config
*config
;
1568 config
= kmod_get_config(mod
->ctx
);
1570 kmod_list_foreach(l
, config
->remove_commands
) {
1571 const char *modname
= kmod_command_get_modname(l
);
1573 if (fnmatch(modname
, mod
->name
, 0) != 0)
1576 m
->remove_commands
= kmod_command_get_command(l
);
1579 * find only the first command, as modprobe from
1580 * module-init-tools does
1585 m
->init
.remove_commands
= true;
1588 return mod
->remove_commands
;
1591 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1593 mod
->init
.remove_commands
= true;
1594 mod
->remove_commands
= cmd
;
1598 * SECTION:libkmod-loaded
1599 * @short_description: currently loaded modules
1601 * Information about currently loaded modules, as reported by Linux kernel.
1602 * These information are not cached by libkmod and are always read from /sys
1603 * and /proc/modules.
1607 * kmod_module_new_from_loaded:
1608 * @ctx: kmod library context
1609 * @list: where to save the list of loaded modules
1611 * Create a new list of kmod modules with all modules currently loaded in
1612 * kernel. It uses /proc/modules to get the names of loaded modules and to
1613 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1614 * They are put in @list in no particular order.
1616 * The initial refcount is 1, and needs to be decremented to release the
1617 * resources of the kmod_module. The returned @list must be released by
1618 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1619 * kmod_modules created, they are all released upon @ctx destruction too. Do
1620 * not unref @ctx before all the desired operations with the returned list are
1623 * Returns: 0 on success or < 0 on error.
1625 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1626 struct kmod_list
**list
)
1628 struct kmod_list
*l
= NULL
;
1632 if (ctx
== NULL
|| list
== NULL
)
1635 fp
= fopen("/proc/modules", "re");
1638 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1642 while (fgets(line
, sizeof(line
), fp
)) {
1643 struct kmod_module
*m
;
1644 struct kmod_list
*node
;
1646 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1648 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1650 ERR(ctx
, "could not get module from name '%s': %s\n",
1651 name
, strerror(-err
));
1655 node
= kmod_list_append(l
, m
);
1659 ERR(ctx
, "out of memory\n");
1660 kmod_module_unref(m
);
1671 * kmod_module_initstate_str:
1672 * @state: the state as returned by kmod_module_get_initstate()
1674 * Translate a initstate to a string.
1676 * Returns: the string associated to the @state. This string is statically
1677 * allocated, do not free it.
1679 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1682 case KMOD_MODULE_BUILTIN
:
1684 case KMOD_MODULE_LIVE
:
1686 case KMOD_MODULE_COMING
:
1688 case KMOD_MODULE_GOING
:
1696 * kmod_module_get_initstate:
1699 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1702 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1703 * KMOD_MODULE_BUILTIN: module is builtin;
1704 * KMOD_MODULE_LIVE: module is live in kernel;
1705 * KMOD_MODULE_COMING: module is being loaded;
1706 * KMOD_MODULE_GOING: module is being unloaded.
1708 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1710 char path
[PATH_MAX
], buf
[32];
1711 int fd
, err
, pathlen
;
1717 return KMOD_MODULE_BUILTIN
;
1719 pathlen
= snprintf(path
, sizeof(path
),
1720 "/sys/module/%s/initstate", mod
->name
);
1721 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1725 DBG(mod
->ctx
, "could not open '%s': %s\n",
1726 path
, strerror(-err
));
1728 if (pathlen
> (int)sizeof("/initstate") - 1) {
1730 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1731 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1732 return KMOD_MODULE_BUILTIN
;
1735 DBG(mod
->ctx
, "could not open '%s': %s\n",
1736 path
, strerror(-err
));
1740 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1743 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1744 path
, strerror(-err
));
1748 if (streq(buf
, "live\n"))
1749 return KMOD_MODULE_LIVE
;
1750 else if (streq(buf
, "coming\n"))
1751 return KMOD_MODULE_COMING
;
1752 else if (streq(buf
, "going\n"))
1753 return KMOD_MODULE_GOING
;
1755 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1760 * kmod_module_get_size:
1763 * Get the size of this kmod module as returned by Linux kernel. If supported,
1764 * the size is read from the coresize attribute in /sys/module. For older
1765 * kernels, this falls back on /proc/modules and searches for the specified
1766 * module to get its size.
1768 * Returns: the size of this kmod module.
1770 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1775 long size
= -ENOENT
;
1781 /* try to open the module dir in /sys. If this fails, don't
1782 * bother trying to find the size as we know the module isn't
1785 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1786 dfd
= open(line
, O_RDONLY
|O_CLOEXEC
);
1790 /* available as of linux 3.3.x */
1791 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1793 if (read_str_long(cfd
, &size
, 10) < 0)
1794 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1799 /* fall back on parsing /proc/modules */
1800 fp
= fopen("/proc/modules", "re");
1804 "could not open /proc/modules: %s\n", strerror(errno
));
1809 while (fgets(line
, sizeof(line
), fp
)) {
1810 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1814 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1817 tok
= strtok_r(NULL
, " \t", &saveptr
);
1820 "invalid line format at /proc/modules:%d\n", lineno
);
1824 value
= strtol(tok
, &endptr
, 10);
1825 if (endptr
== tok
|| *endptr
!= '\0') {
1827 "invalid line format at /proc/modules:%d\n", lineno
);
1842 * kmod_module_get_refcnt:
1845 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1848 * Returns: 0 on success or < 0 on failure.
1850 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1852 char path
[PATH_MAX
];
1859 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1860 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1863 DBG(mod
->ctx
, "could not open '%s': %s\n",
1864 path
, strerror(errno
));
1868 err
= read_str_long(fd
, &refcnt
, 10);
1871 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1872 path
, strerror(-err
));
1880 * kmod_module_get_holders:
1883 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1884 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1886 * Returns: a new list of kmod modules on success or NULL on failure.
1888 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1890 char dname
[PATH_MAX
];
1891 struct kmod_list
*list
= NULL
;
1892 struct dirent
*dent
;
1895 if (mod
== NULL
|| mod
->ctx
== NULL
)
1898 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1902 ERR(mod
->ctx
, "could not open '%s': %s\n",
1903 dname
, strerror(errno
));
1907 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
1908 struct kmod_module
*holder
;
1909 struct kmod_list
*l
;
1912 if (dent
->d_name
[0] == '.') {
1913 if (dent
->d_name
[1] == '\0' ||
1914 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1918 err
= kmod_module_new_from_name(mod
->ctx
, dent
->d_name
,
1921 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1922 dent
->d_name
, strerror(-err
));
1926 l
= kmod_list_append(list
, holder
);
1930 ERR(mod
->ctx
, "out of memory\n");
1931 kmod_module_unref(holder
);
1941 kmod_module_unref_list(list
);
1945 struct kmod_module_section
{
1946 unsigned long address
;
1950 static void kmod_module_section_free(struct kmod_module_section
*section
)
1956 * kmod_module_get_sections:
1959 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1960 * structure contained in this list is internal to libkmod and their fields
1961 * can be obtained by calling kmod_module_section_get_name() and
1962 * kmod_module_section_get_address().
1964 * After use, free the @list by calling kmod_module_section_free_list().
1966 * Returns: a new list of kmod module sections on success or NULL on failure.
1968 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1970 char dname
[PATH_MAX
];
1971 struct kmod_list
*list
= NULL
;
1972 struct dirent
*dent
;
1979 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1983 ERR(mod
->ctx
, "could not open '%s': %s\n",
1984 dname
, strerror(errno
));
1990 for (dent
= readdir(d
); dent
; dent
= readdir(d
)) {
1991 struct kmod_module_section
*section
;
1992 struct kmod_list
*l
;
1993 unsigned long address
;
1997 if (dent
->d_name
[0] == '.') {
1998 if (dent
->d_name
[1] == '\0' ||
1999 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
2003 fd
= openat(dfd
, dent
->d_name
, O_RDONLY
|O_CLOEXEC
);
2005 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
2006 dname
, dent
->d_name
);
2010 err
= read_str_ulong(fd
, &address
, 16);
2013 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
2014 dname
, dent
->d_name
);
2018 namesz
= strlen(dent
->d_name
) + 1;
2019 section
= malloc(sizeof(*section
) + namesz
);
2021 if (section
== NULL
) {
2022 ERR(mod
->ctx
, "out of memory\n");
2026 section
->address
= address
;
2027 memcpy(section
->name
, dent
->d_name
, namesz
);
2029 l
= kmod_list_append(list
, section
);
2033 ERR(mod
->ctx
, "out of memory\n");
2044 kmod_module_unref_list(list
);
2049 * kmod_module_section_get_module_name:
2050 * @entry: a list entry representing a kmod module section
2052 * Get the name of a kmod module section.
2054 * After use, free the @list by calling kmod_module_section_free_list().
2056 * Returns: the name of this kmod module section on success or NULL on
2057 * failure. The string is owned by the section, do not free it.
2059 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2061 struct kmod_module_section
*section
;
2066 section
= entry
->data
;
2067 return section
->name
;
2071 * kmod_module_section_get_address:
2072 * @entry: a list entry representing a kmod module section
2074 * Get the address of a kmod module section.
2076 * After use, free the @list by calling kmod_module_section_free_list().
2078 * Returns: the address of this kmod module section on success or ULONG_MAX
2081 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2083 struct kmod_module_section
*section
;
2086 return (unsigned long)-1;
2088 section
= entry
->data
;
2089 return section
->address
;
2093 * kmod_module_section_free_list:
2094 * @list: kmod module section list
2096 * Release the resources taken by @list
2098 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2101 kmod_module_section_free(list
->data
);
2102 list
= kmod_list_remove(list
);
2106 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2108 if (mod
->file
== NULL
) {
2109 const char *path
= kmod_module_get_path(mod
);
2116 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2118 if (mod
->file
== NULL
)
2122 return kmod_file_get_elf(mod
->file
);
2125 struct kmod_module_info
{
2130 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2132 struct kmod_module_info
*info
;
2134 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2138 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2140 memcpy(info
->key
, key
, keylen
);
2141 info
->key
[keylen
] = '\0';
2142 memcpy(info
->value
, value
, valuelen
);
2143 info
->value
[valuelen
] = '\0';
2147 static void kmod_module_info_free(struct kmod_module_info
*info
)
2152 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2154 struct kmod_module_info
*info
;
2155 struct kmod_list
*n
;
2157 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2160 n
= kmod_list_append(*list
, info
);
2164 kmod_module_info_free(info
);
2169 * kmod_module_get_info:
2171 * @list: where to return list of module information. Use
2172 * kmod_module_info_get_key() and
2173 * kmod_module_info_get_value(). Release this list with
2174 * kmod_module_info_free_list()
2176 * Get a list of entries in ELF section ".modinfo", these contain
2177 * alias, license, depends, vermagic and other keys with respective
2178 * values. If the module is signed (CONFIG_MODULE_SIG), information
2179 * about the module signature is included as well: signer,
2180 * sig_key and sig_hashalgo.
2182 * After use, free the @list by calling kmod_module_info_free_list().
2184 * Returns: 0 on success or < 0 otherwise.
2186 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2188 struct kmod_elf
*elf
;
2190 int i
, count
, ret
= -ENOMEM
;
2191 struct kmod_signature_info sig_info
;
2193 if (mod
== NULL
|| list
== NULL
)
2196 assert(*list
== NULL
);
2198 elf
= kmod_module_get_elf(mod
);
2202 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2206 for (i
= 0; i
< count
; i
++) {
2207 struct kmod_list
*n
;
2208 const char *key
, *value
;
2209 size_t keylen
, valuelen
;
2212 value
= strchr(key
, '=');
2213 if (value
== NULL
) {
2214 keylen
= strlen(key
);
2218 keylen
= value
- key
;
2220 valuelen
= strlen(value
);
2223 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2228 if (kmod_module_signature_info(mod
->file
, &sig_info
)) {
2229 struct kmod_list
*n
;
2232 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2233 sig_info
.signer
, sig_info
.signer_len
);
2238 /* Display the key id as 01:12:DE:AD:BE:EF:... */
2239 key_hex
= malloc(sig_info
.key_id_len
* 3);
2240 if (key_hex
== NULL
)
2242 for (i
= 0; i
< (int)sig_info
.key_id_len
; i
++) {
2243 sprintf(key_hex
+ i
* 3, "%02X",
2244 (unsigned char)sig_info
.key_id
[i
]);
2245 if (i
< (int)sig_info
.key_id_len
- 1)
2246 key_hex
[i
* 3 + 2] = ':';
2248 n
= kmod_module_info_append(list
, "sig_key", strlen("sig_key"),
2249 key_hex
, sig_info
.key_id_len
* 3 - 1);
2255 n
= kmod_module_info_append(list
,
2256 "sig_hashalgo", strlen("sig_hashalgo"),
2257 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2263 * Omit sig_info.id_type and sig_info.algo for now, as these
2264 * are currently constant.
2271 kmod_module_info_free_list(*list
);
2279 * kmod_module_info_get_key:
2280 * @entry: a list entry representing a kmod module info
2282 * Get the key of a kmod module info.
2284 * Returns: the key of this kmod module info on success or NULL on
2285 * failure. The string is owned by the info, do not free it.
2287 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2289 struct kmod_module_info
*info
;
2299 * kmod_module_info_get_value:
2300 * @entry: a list entry representing a kmod module info
2302 * Get the value of a kmod module info.
2304 * Returns: the value of this kmod module info on success or NULL on
2305 * failure. The string is owned by the info, do not free it.
2307 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2309 struct kmod_module_info
*info
;
2319 * kmod_module_info_free_list:
2320 * @list: kmod module info list
2322 * Release the resources taken by @list
2324 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2327 kmod_module_info_free(list
->data
);
2328 list
= kmod_list_remove(list
);
2332 struct kmod_module_version
{
2337 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2339 struct kmod_module_version
*mv
;
2340 size_t symbollen
= strlen(symbol
) + 1;
2342 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2347 memcpy(mv
->symbol
, symbol
, symbollen
);
2351 static void kmod_module_version_free(struct kmod_module_version
*version
)
2357 * kmod_module_get_versions:
2359 * @list: where to return list of module versions. Use
2360 * kmod_module_version_get_symbol() and
2361 * kmod_module_version_get_crc(). Release this list with
2362 * kmod_module_versions_free_list()
2364 * Get a list of entries in ELF section "__versions".
2366 * After use, free the @list by calling kmod_module_versions_free_list().
2368 * Returns: 0 on success or < 0 otherwise.
2370 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2372 struct kmod_elf
*elf
;
2373 struct kmod_modversion
*versions
;
2374 int i
, count
, ret
= 0;
2376 if (mod
== NULL
|| list
== NULL
)
2379 assert(*list
== NULL
);
2381 elf
= kmod_module_get_elf(mod
);
2385 count
= kmod_elf_get_modversions(elf
, &versions
);
2389 for (i
= 0; i
< count
; i
++) {
2390 struct kmod_module_version
*mv
;
2391 struct kmod_list
*n
;
2393 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2396 kmod_module_versions_free_list(*list
);
2401 n
= kmod_list_append(*list
, mv
);
2405 kmod_module_version_free(mv
);
2406 kmod_module_versions_free_list(*list
);
2420 * kmod_module_version_get_symbol:
2421 * @entry: a list entry representing a kmod module versions
2423 * Get the symbol of a kmod module versions.
2425 * Returns: the symbol of this kmod module versions on success or NULL
2426 * on failure. The string is owned by the versions, do not free it.
2428 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2430 struct kmod_module_version
*version
;
2435 version
= entry
->data
;
2436 return version
->symbol
;
2440 * kmod_module_version_get_crc:
2441 * @entry: a list entry representing a kmod module version
2443 * Get the crc of a kmod module version.
2445 * Returns: the crc of this kmod module version on success or NULL on
2446 * failure. The string is owned by the version, do not free it.
2448 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2450 struct kmod_module_version
*version
;
2455 version
= entry
->data
;
2456 return version
->crc
;
2460 * kmod_module_versions_free_list:
2461 * @list: kmod module versions list
2463 * Release the resources taken by @list
2465 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2468 kmod_module_version_free(list
->data
);
2469 list
= kmod_list_remove(list
);
2473 struct kmod_module_symbol
{
2478 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2480 struct kmod_module_symbol
*mv
;
2481 size_t symbollen
= strlen(symbol
) + 1;
2483 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2488 memcpy(mv
->symbol
, symbol
, symbollen
);
2492 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2498 * kmod_module_get_symbols:
2500 * @list: where to return list of module symbols. Use
2501 * kmod_module_symbol_get_symbol() and
2502 * kmod_module_symbol_get_crc(). Release this list with
2503 * kmod_module_symbols_free_list()
2505 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2507 * After use, free the @list by calling kmod_module_symbols_free_list().
2509 * Returns: 0 on success or < 0 otherwise.
2511 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2513 struct kmod_elf
*elf
;
2514 struct kmod_modversion
*symbols
;
2515 int i
, count
, ret
= 0;
2517 if (mod
== NULL
|| list
== NULL
)
2520 assert(*list
== NULL
);
2522 elf
= kmod_module_get_elf(mod
);
2526 count
= kmod_elf_get_symbols(elf
, &symbols
);
2530 for (i
= 0; i
< count
; i
++) {
2531 struct kmod_module_symbol
*mv
;
2532 struct kmod_list
*n
;
2534 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2537 kmod_module_symbols_free_list(*list
);
2542 n
= kmod_list_append(*list
, mv
);
2546 kmod_module_symbol_free(mv
);
2547 kmod_module_symbols_free_list(*list
);
2561 * kmod_module_symbol_get_symbol:
2562 * @entry: a list entry representing a kmod module symbols
2564 * Get the symbol of a kmod module symbols.
2566 * Returns: the symbol of this kmod module symbols on success or NULL
2567 * on failure. The string is owned by the symbols, do not free it.
2569 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2571 struct kmod_module_symbol
*symbol
;
2576 symbol
= entry
->data
;
2577 return symbol
->symbol
;
2581 * kmod_module_symbol_get_crc:
2582 * @entry: a list entry representing a kmod module symbol
2584 * Get the crc of a kmod module symbol.
2586 * Returns: the crc of this kmod module symbol on success or NULL on
2587 * failure. The string is owned by the symbol, do not free it.
2589 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2591 struct kmod_module_symbol
*symbol
;
2596 symbol
= entry
->data
;
2601 * kmod_module_symbols_free_list:
2602 * @list: kmod module symbols list
2604 * Release the resources taken by @list
2606 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2609 kmod_module_symbol_free(list
->data
);
2610 list
= kmod_list_remove(list
);
2614 struct kmod_module_dependency_symbol
{
2620 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2622 struct kmod_module_dependency_symbol
*mv
;
2623 size_t symbollen
= strlen(symbol
) + 1;
2625 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2631 memcpy(mv
->symbol
, symbol
, symbollen
);
2635 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2637 free(dependency_symbol
);
2641 * kmod_module_get_dependency_symbols:
2643 * @list: where to return list of module dependency_symbols. Use
2644 * kmod_module_dependency_symbol_get_symbol() and
2645 * kmod_module_dependency_symbol_get_crc(). Release this list with
2646 * kmod_module_dependency_symbols_free_list()
2648 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2650 * After use, free the @list by calling
2651 * kmod_module_dependency_symbols_free_list().
2653 * Returns: 0 on success or < 0 otherwise.
2655 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2657 struct kmod_elf
*elf
;
2658 struct kmod_modversion
*symbols
;
2659 int i
, count
, ret
= 0;
2661 if (mod
== NULL
|| list
== NULL
)
2664 assert(*list
== NULL
);
2666 elf
= kmod_module_get_elf(mod
);
2670 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2674 for (i
= 0; i
< count
; i
++) {
2675 struct kmod_module_dependency_symbol
*mv
;
2676 struct kmod_list
*n
;
2678 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2683 kmod_module_dependency_symbols_free_list(*list
);
2688 n
= kmod_list_append(*list
, mv
);
2692 kmod_module_dependency_symbol_free(mv
);
2693 kmod_module_dependency_symbols_free_list(*list
);
2707 * kmod_module_dependency_symbol_get_symbol:
2708 * @entry: a list entry representing a kmod module dependency_symbols
2710 * Get the dependency symbol of a kmod module
2712 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2713 * on failure. The string is owned by the dependency_symbols, do not free it.
2715 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2717 struct kmod_module_dependency_symbol
*dependency_symbol
;
2722 dependency_symbol
= entry
->data
;
2723 return dependency_symbol
->symbol
;
2727 * kmod_module_dependency_symbol_get_crc:
2728 * @entry: a list entry representing a kmod module dependency_symbol
2730 * Get the crc of a kmod module dependency_symbol.
2732 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2733 * failure. The string is owned by the dependency_symbol, do not free it.
2735 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2737 struct kmod_module_dependency_symbol
*dependency_symbol
;
2742 dependency_symbol
= entry
->data
;
2743 return dependency_symbol
->crc
;
2747 * kmod_module_dependency_symbol_get_bind:
2748 * @entry: a list entry representing a kmod module dependency_symbol
2750 * Get the bind type of a kmod module dependency_symbol.
2752 * Returns: the bind of this kmod module dependency_symbol on success
2753 * or < 0 on failure.
2755 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2757 struct kmod_module_dependency_symbol
*dependency_symbol
;
2762 dependency_symbol
= entry
->data
;
2763 return dependency_symbol
->bind
;
2767 * kmod_module_dependency_symbols_free_list:
2768 * @list: kmod module dependency_symbols list
2770 * Release the resources taken by @list
2772 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2775 kmod_module_dependency_symbol_free(list
->data
);
2776 list
= kmod_list_remove(list
);