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
52 enum kmod_module_builtin
{
53 KMOD_MODULE_BUILTIN_UNKNOWN
,
54 KMOD_MODULE_BUILTIN_NO
,
55 KMOD_MODULE_BUILTIN_YES
,
61 * Opaque object representing a module.
68 struct kmod_list
*dep
;
70 const char *install_commands
; /* owned by kmod_config */
71 const char *remove_commands
; /* owned by kmod_config */
72 char *alias
; /* only set if this module was created from an alias */
73 struct kmod_file
*file
;
79 bool install_commands
: 1;
80 bool remove_commands
: 1;
84 * mark if module is builtin, i.e. it's present on modules.builtin
85 * file. This is set as soon as it is needed or as soon as we know
86 * about it, i.e. the module was created from builtin lookup.
88 enum kmod_module_builtin builtin
;
91 * private field used by kmod_module_get_probe_list() to detect
97 * set by kmod_module_get_probe_list: indicates for probe_insert()
98 * whether the module's command and softdep should be ignored
103 * set by kmod_module_get_probe_list: indicates whether this is the
104 * module the user asked for or its dependency, or whether this
110 static inline const char *path_join(const char *path
, size_t prefixlen
,
118 pathlen
= strlen(path
);
119 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
122 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
126 static inline bool module_is_inkernel(struct kmod_module
*mod
)
128 int state
= kmod_module_get_initstate(mod
);
130 if (state
== KMOD_MODULE_LIVE
||
131 state
== KMOD_MODULE_BUILTIN
)
137 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
139 struct kmod_ctx
*ctx
= mod
->ctx
;
140 struct kmod_list
*list
= NULL
;
149 assert(mod
->dep
== NULL
);
150 mod
->init
.dep
= true;
152 p
= strchr(line
, ':');
157 dirname
= kmod_get_dirname(mod
->ctx
);
158 dirnamelen
= strlen(dirname
);
159 if (dirnamelen
+ 2 >= PATH_MAX
)
162 memcpy(buf
, dirname
, dirnamelen
);
163 buf
[dirnamelen
] = '/';
165 buf
[dirnamelen
] = '\0';
167 if (mod
->path
== NULL
) {
168 const char *str
= path_join(line
, dirnamelen
, buf
);
171 mod
->path
= strdup(str
);
172 if (mod
->path
== NULL
)
177 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
178 p
= strtok_r(NULL
, " \t", &saveptr
)) {
179 struct kmod_module
*depmod
= NULL
;
182 path
= path_join(p
, dirnamelen
, buf
);
184 ERR(ctx
, "could not join path '%s' and '%s'.\n",
189 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
191 ERR(ctx
, "ctx=%p path=%s error=%s\n",
192 ctx
, path
, strerror(-err
));
196 DBG(ctx
, "add dep: %s\n", path
);
198 list
= kmod_list_prepend(list
, depmod
);
202 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
209 kmod_module_unref_list(list
);
210 mod
->init
.dep
= false;
214 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
216 mod
->visited
= visited
;
219 void kmod_module_set_builtin(struct kmod_module
*mod
, bool builtin
)
222 builtin
? KMOD_MODULE_BUILTIN_YES
: KMOD_MODULE_BUILTIN_NO
;
225 void kmod_module_set_required(struct kmod_module
*mod
, bool required
)
227 mod
->required
= required
;
230 bool kmod_module_is_builtin(struct kmod_module
*mod
)
232 if (mod
->builtin
== KMOD_MODULE_BUILTIN_UNKNOWN
) {
233 kmod_module_set_builtin(mod
,
234 kmod_lookup_alias_is_builtin(mod
->ctx
, mod
->name
));
237 return mod
->builtin
== KMOD_MODULE_BUILTIN_YES
;
240 * Memory layout with alias:
242 * struct kmod_module {
247 * name <----------' | |
248 * alias <-----------' |
249 * name\alias <--------'
251 * Memory layout without alias:
253 * struct kmod_module {
255 * alias -----|----> NULL
258 * name <----------'-'
260 * @key is "name\alias" or "name" (in which case alias == NULL)
262 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
263 const char *name
, size_t namelen
,
264 const char *alias
, size_t aliaslen
,
265 struct kmod_module
**mod
)
267 struct kmod_module
*m
;
270 m
= kmod_pool_get_module(ctx
, key
);
272 *mod
= kmod_module_ref(m
);
279 keylen
= namelen
+ aliaslen
+ 1;
281 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
285 memset(m
, 0, sizeof(*m
));
287 m
->ctx
= kmod_ref(ctx
);
288 m
->name
= (char *)m
+ sizeof(*m
);
289 memcpy(m
->name
, key
, keylen
+ 1);
291 m
->hashkey
= m
->name
;
294 m
->name
[namelen
] = '\0';
295 m
->alias
= m
->name
+ namelen
+ 1;
296 m
->hashkey
= m
->name
+ keylen
+ 1;
297 memcpy(m
->hashkey
, key
, keylen
+ 1);
301 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
308 * kmod_module_new_from_name:
309 * @ctx: kmod library context
310 * @name: name of the module
311 * @mod: where to save the created struct kmod_module
313 * Create a new struct kmod_module using the module name. @name can not be an
314 * alias, file name or anything else; it must be a module name. There's no
315 * check if the module exists in the system.
317 * This function is also used internally by many others that return a new
318 * struct kmod_module or a new list of modules.
320 * The initial refcount is 1, and needs to be decremented to release the
321 * resources of the kmod_module. Since libkmod keeps track of all
322 * kmod_modules created, they are all released upon @ctx destruction too. Do
323 * not unref @ctx before all the desired operations with the returned
324 * kmod_module are done.
326 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
327 * module name or if memory allocation failed.
329 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
331 struct kmod_module
**mod
)
334 char name_norm
[PATH_MAX
];
336 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
339 modname_normalize(name
, name_norm
, &namelen
);
341 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
344 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
345 const char *name
, struct kmod_module
**mod
)
349 size_t namelen
= strlen(name
);
350 size_t aliaslen
= strlen(alias
);
352 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
353 return -ENAMETOOLONG
;
355 memcpy(key
, name
, namelen
);
356 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
359 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
367 * kmod_module_new_from_path:
368 * @ctx: kmod library context
369 * @path: path where to find the given module
370 * @mod: where to save the created struct kmod_module
372 * Create a new struct kmod_module using the module path. @path must be an
373 * existent file with in the filesystem and must be accessible to libkmod.
375 * The initial refcount is 1, and needs to be decremented to release the
376 * resources of the kmod_module. Since libkmod keeps track of all
377 * kmod_modules created, they are all released upon @ctx destruction too. Do
378 * not unref @ctx before all the desired operations with the returned
379 * kmod_module are done.
381 * If @path is relative, it's treated as relative to the current working
382 * directory. Otherwise, give an absolute path.
384 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
385 * it's not a valid file for a kmod_module or if memory allocation failed.
387 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
389 struct kmod_module
**mod
)
391 struct kmod_module
*m
;
398 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
401 abspath
= path_make_absolute_cwd(path
);
402 if (abspath
== NULL
) {
403 DBG(ctx
, "no absolute path for %s\n", path
);
407 err
= stat(abspath
, &st
);
410 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
415 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
416 DBG(ctx
, "could not get modname from path %s\n", path
);
421 m
= kmod_pool_get_module(ctx
, name
);
425 else if (streq(m
->path
, abspath
))
428 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
429 name
, abspath
, m
->path
);
434 *mod
= kmod_module_ref(m
);
438 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
454 * Drop a reference of the kmod module. If the refcount reaches zero, its
455 * resources are released.
457 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
458 * returns the passed @mod with its refcount decremented.
460 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
465 if (--mod
->refcount
> 0)
468 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
470 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
471 kmod_module_unref_list(mod
->dep
);
474 kmod_file_unref(mod
->file
);
476 kmod_unref(mod
->ctx
);
487 * Take a reference of the kmod module, incrementing its refcount.
489 * Returns: the passed @module with its refcount incremented.
491 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
501 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
505 if (*(_list) != NULL) \
510 * kmod_module_new_from_lookup:
511 * @ctx: kmod library context
512 * @given_alias: alias to look for
513 * @list: an empty list where to save the list of modules matching
516 * Create a new list of kmod modules using an alias or module name and lookup
517 * libkmod's configuration files and indexes in order to find the module.
518 * Once it's found in one of the places, it stops searching and create the
519 * list of modules that is saved in @list.
521 * The search order is: 1. aliases in configuration file; 2. module names in
522 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
523 * in modules.alias index.
525 * The initial refcount is 1, and needs to be decremented to release the
526 * resources of the kmod_module. The returned @list must be released by
527 * calling kmod_module_unref_list(). Since libkmod keeps track of all
528 * kmod_modules created, they are all released upon @ctx destruction too. Do
529 * not unref @ctx before all the desired operations with the returned list are
532 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
533 * methods failed, which is basically due to memory allocation fail. If module
534 * is not found, it still returns 0, but @list is an empty list.
536 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
537 const char *given_alias
,
538 struct kmod_list
**list
)
541 char alias
[PATH_MAX
];
543 if (ctx
== NULL
|| given_alias
== NULL
)
546 if (list
== NULL
|| *list
!= NULL
) {
547 ERR(ctx
, "An empty list is needed to create lookup\n");
551 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
552 DBG(ctx
, "invalid alias: %s\n", given_alias
);
556 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
558 /* Aliases from config file override all the others */
559 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
560 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
562 DBG(ctx
, "lookup modules.dep %s\n", alias
);
563 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
564 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
566 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
567 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
568 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
570 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
571 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
572 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
574 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
575 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
576 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
578 DBG(ctx
, "lookup modules.builtin.modinfo %s\n", alias
);
579 err
= kmod_lookup_alias_from_kernel_builtin_file(ctx
, alias
, list
);
580 if (err
== -ENOSYS
) {
581 /* Optional index missing, try the old one */
582 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
583 err
= kmod_lookup_alias_from_builtin_file(ctx
, alias
, list
);
585 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
589 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
592 DBG(ctx
, "Failed to lookup %s\n", alias
);
593 kmod_module_unref_list(*list
);
597 #undef CHECK_ERR_AND_FINISH
600 * kmod_module_unref_list:
601 * @list: list of kmod modules
603 * Drop a reference of each kmod module in @list and releases the resources
604 * taken by the list itself.
608 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
610 for (; list
!= NULL
; list
= kmod_list_remove(list
))
611 kmod_module_unref(list
->data
);
617 * kmod_module_get_filtered_blacklist:
618 * @ctx: kmod library context
619 * @input: list of kmod_module to be filtered with blacklist
620 * @output: where to save the new list
622 * This function should not be used. Use kmod_module_apply_filter instead.
624 * Given a list @input, this function filter it out with config's blacklist
625 * and save it in @output.
627 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
630 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
631 const struct kmod_list
*input
,
632 struct kmod_list
**output
)
634 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
637 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
639 if (!mod
->init
.dep
) {
641 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
646 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
657 * kmod_module_get_dependencies:
660 * Search the modules.dep index to find the dependencies of the given @mod.
661 * The result is cached in @mod, so subsequent calls to this function will
662 * return the already searched list of modules.
664 * Returns: NULL on failure. Otherwise it returns a list of kmod modules
665 * that can be released by calling kmod_module_unref_list().
667 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
669 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
674 module_get_dependencies_noref(mod
);
676 kmod_list_foreach(l
, mod
->dep
) {
677 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
679 kmod_module_unref(l
->data
);
689 ERR(mod
->ctx
, "out of memory\n");
690 kmod_module_unref_list(list_new
);
695 * kmod_module_get_module:
696 * @entry: an entry in a list of kmod modules.
698 * Get the kmod module of this @entry in the list, increasing its refcount.
699 * After it's used, unref it. Since the refcount is incremented upon return,
700 * you still have to call kmod_module_unref_list() to release the list of kmod
703 * Returns: NULL on failure or the kmod_module contained in this list entry
704 * with its refcount incremented.
706 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
711 return kmod_module_ref(entry
->data
);
715 * kmod_module_get_name:
718 * Get the name of this kmod module. Name is always available, independently
719 * if it was created by kmod_module_new_from_name() or another function and
720 * it's always normalized (dashes are replaced with underscores).
722 * Returns: the name of this kmod module.
724 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
733 * kmod_module_get_path:
736 * Get the path of this kmod module. If this kmod module was not created by
737 * path, it can search the modules.dep index in order to find out the module
738 * under context's dirname.
740 * Returns: the path of this kmod module or NULL if such information is not
743 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
750 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
752 if (mod
->path
!= NULL
)
758 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
762 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
769 extern long delete_module(const char *name
, unsigned int flags
);
772 * kmod_module_remove_module:
774 * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
775 * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
776 * use by a kernel subsystem or other process;
777 * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
780 * Remove a module from Linux kernel.
782 * Returns: 0 on success or < 0 on failure.
784 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
792 /* Filter out other flags and force ONONBLOCK */
793 flags
&= KMOD_REMOVE_FORCE
;
794 flags
|= KMOD_REMOVE_NOWAIT
;
796 err
= delete_module(mod
->name
, flags
);
799 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
805 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
808 * kmod_module_insert_module:
810 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
811 * behavior of this function, valid flags are
812 * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
813 * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
814 * @options: module's options to pass to Linux Kernel.
816 * Insert a module in Linux kernel. It opens the file pointed by @mod,
817 * mmap'ing it and passing to kernel.
819 * Returns: 0 on success or < 0 on failure. If module is already loaded it
822 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
829 struct kmod_elf
*elf
;
831 const char *args
= options
? options
: "";
836 path
= kmod_module_get_path(mod
);
838 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
843 mod
->file
= kmod_file_open(mod
->ctx
, path
);
844 if (mod
->file
== NULL
) {
850 if (kmod_file_get_direct(mod
->file
)) {
851 unsigned int kernel_flags
= 0;
853 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
)
854 kernel_flags
|= MODULE_INIT_IGNORE_VERMAGIC
;
855 if (flags
& KMOD_INSERT_FORCE_MODVERSION
)
856 kernel_flags
|= MODULE_INIT_IGNORE_MODVERSIONS
;
858 err
= finit_module(kmod_file_get_fd(mod
->file
), args
, kernel_flags
);
859 if (err
== 0 || errno
!= ENOSYS
)
863 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
864 elf
= kmod_file_get_elf(mod
->file
);
870 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
871 err
= kmod_elf_strip_section(elf
, "__versions");
873 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
876 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
877 err
= kmod_elf_strip_vermagic(elf
);
879 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
882 mem
= kmod_elf_get_memory(elf
);
884 mem
= kmod_file_get_contents(mod
->file
);
886 size
= kmod_file_get_size(mod
->file
);
888 err
= init_module(mem
, size
, args
);
892 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
897 static bool module_is_blacklisted(struct kmod_module
*mod
)
899 struct kmod_ctx
*ctx
= mod
->ctx
;
900 const struct kmod_config
*config
= kmod_get_config(ctx
);
901 const struct kmod_list
*bl
= config
->blacklists
;
902 const struct kmod_list
*l
;
904 kmod_list_foreach(l
, bl
) {
905 const char *modname
= kmod_blacklist_get_modname(l
);
907 if (streq(modname
, mod
->name
))
915 * kmod_module_apply_filter
916 * @ctx: kmod library context
917 * @filter_type: bitmask to filter modules out, valid types are
918 * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
919 * KMOD_FILTER_BUILTIN: filter builtin modules out.
920 * @input: list of kmod_module to be filtered
921 * @output: where to save the new list
923 * Given a list @input, this function filter it out by the filter mask
924 * and save it in @output.
926 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
929 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
930 enum kmod_filter filter_type
,
931 const struct kmod_list
*input
,
932 struct kmod_list
**output
)
934 const struct kmod_list
*li
;
936 if (ctx
== NULL
|| output
== NULL
)
943 kmod_list_foreach(li
, input
) {
944 struct kmod_module
*mod
= li
->data
;
945 struct kmod_list
*node
;
947 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
948 module_is_blacklisted(mod
))
951 if ((filter_type
& KMOD_FILTER_BUILTIN
)
952 && kmod_module_is_builtin(mod
))
955 node
= kmod_list_append(*output
, mod
);
960 kmod_module_ref(mod
);
966 kmod_module_unref_list(*output
);
971 static int command_do(struct kmod_module
*mod
, const char *type
,
974 const char *modname
= kmod_module_get_name(mod
);
977 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
979 setenv("MODPROBE_MODULE", modname
, 1);
981 unsetenv("MODPROBE_MODULE");
984 ERR(mod
->ctx
, "Could not run %s command '%s' for module %s: %m\n",
989 if (WEXITSTATUS(err
)) {
990 ERR(mod
->ctx
, "Error running %s command '%s' for module %s: retcode %d\n",
991 type
, cmd
, modname
, WEXITSTATUS(err
));
998 struct probe_insert_cb
{
999 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
1003 static int module_do_install_commands(struct kmod_module
*mod
,
1004 const char *options
,
1005 struct probe_insert_cb
*cb
)
1007 const char *command
= kmod_module_get_install_commands(mod
);
1009 _cleanup_free_
char *cmd
;
1011 size_t cmdlen
, options_len
, varlen
;
1015 if (options
== NULL
)
1018 options_len
= strlen(options
);
1019 cmdlen
= strlen(command
);
1020 varlen
= sizeof("$CMDLINE_OPTS") - 1;
1022 cmd
= memdup(command
, cmdlen
+ 1);
1026 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
1027 size_t prefixlen
= p
- cmd
;
1028 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
1029 size_t slen
= cmdlen
- varlen
+ options_len
;
1030 char *suffix
= p
+ varlen
;
1031 char *s
= malloc(slen
+ 1);
1035 memcpy(s
, cmd
, p
- cmd
);
1036 memcpy(s
+ prefixlen
, options
, options_len
);
1037 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
1045 if (cb
->run_install
!= NULL
)
1046 err
= cb
->run_install(mod
, cmd
, cb
->data
);
1048 err
= command_do(mod
, "install", cmd
);
1053 static char *module_options_concat(const char *opt
, const char *xopt
)
1055 // TODO: we might need to check if xopt overrides options on opt
1056 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
1057 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1060 if (optlen
== 0 && xoptlen
== 0)
1063 r
= malloc(optlen
+ xoptlen
+ 2);
1066 memcpy(r
, opt
, optlen
);
1072 memcpy(r
+ optlen
, xopt
, xoptlen
);
1074 r
[optlen
+ xoptlen
] = '\0';
1079 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1082 struct kmod_list
**list
);
1085 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1086 struct kmod_list
**list
)
1088 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1091 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1093 ERR(mod
->ctx
, "could not get softdep: %s\n",
1098 kmod_list_foreach(l
, pre
) {
1099 struct kmod_module
*m
= l
->data
;
1100 err
= __kmod_module_get_probe_list(m
, false, false, list
);
1105 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1107 kmod_module_unref(mod
);
1112 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1114 kmod_list_foreach(l
, post
) {
1115 struct kmod_module
*m
= l
->data
;
1116 err
= __kmod_module_get_probe_list(m
, false, false, list
);
1122 kmod_module_unref_list(pre
);
1123 kmod_module_unref_list(post
);
1129 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1132 struct kmod_list
**list
)
1134 struct kmod_list
*dep
, *l
;
1138 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1142 mod
->visited
= true;
1144 dep
= kmod_module_get_dependencies(mod
);
1147 * Called from kmod_module_probe_insert_module(); set the
1148 * ->required flag on mod and all its dependencies before
1149 * they are possibly visited through some softdeps.
1151 mod
->required
= true;
1152 kmod_list_foreach(l
, dep
) {
1153 struct kmod_module
*m
= l
->data
;
1158 kmod_list_foreach(l
, dep
) {
1159 struct kmod_module
*m
= l
->data
;
1160 err
= __kmod_module_fill_softdep(m
, list
);
1166 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1168 kmod_module_unref(mod
);
1173 mod
->ignorecmd
= true;
1175 err
= __kmod_module_fill_softdep(mod
, list
);
1178 kmod_module_unref_list(dep
);
1182 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1184 struct kmod_list
**list
)
1188 assert(mod
!= NULL
);
1189 assert(list
!= NULL
&& *list
== NULL
);
1192 * Make sure we don't get screwed by previous calls to this function
1194 kmod_set_modules_visited(mod
->ctx
, false);
1195 kmod_set_modules_required(mod
->ctx
, false);
1197 err
= __kmod_module_get_probe_list(mod
, true, ignorecmd
, list
);
1199 kmod_module_unref_list(*list
);
1207 * kmod_module_probe_insert_module:
1209 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1210 * behavior of this function, valid flags are
1211 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1212 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1213 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1214 * commands and softdeps configured in the system;
1215 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1216 * live in kernel or not;
1217 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1218 * associated callback function;
1219 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1220 * and the module is already live in kernel, the function will fail if this
1221 * flag is specified;
1222 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1223 * filter to this module and its dependencies. If any of the dependencies (or
1224 * the module) is blacklisted, the probe will fail, unless the blacklisted
1225 * module is already live in kernel;
1226 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1227 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1228 * alias and is blacklisted.
1229 * @extra_options: module's options to pass to Linux Kernel. It applies only
1230 * to @mod, not to its dependencies.
1231 * @run_install: function to run when @mod is backed by an install command.
1232 * @data: data to give back to @run_install callback
1233 * @print_action: function to call with the action being taken (install or
1234 * insmod). It's useful for tools like modprobe when running with verbose
1235 * output or in dry-run mode.
1237 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1238 * install commands and applying blacklist.
1240 * If @run_install is NULL, this function will fork and exec by calling
1241 * system(3). Don't pass a NULL argument in @run_install if your binary is
1242 * setuid/setgid (see warning in system(3)). If you need control over the
1243 * execution of an install command, give a callback function instead.
1245 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1248 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1249 unsigned int flags
, const char *extra_options
,
1250 int (*run_install
)(struct kmod_module
*m
,
1251 const char *cmd
, void *data
),
1253 void (*print_action
)(struct kmod_module
*m
,
1255 const char *options
))
1257 struct kmod_list
*list
= NULL
, *l
;
1258 struct probe_insert_cb cb
;
1264 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1265 && module_is_inkernel(mod
)) {
1266 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1273 * Ugly assignement + check. We need to check if we were told to check
1274 * blacklist and also return the reason why we failed.
1275 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1276 * module is an alias, so we also need to check it
1278 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1279 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1280 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1281 if (module_is_blacklisted(mod
))
1285 err
= kmod_module_get_probe_list(mod
,
1286 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1290 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1291 struct kmod_list
*filtered
= NULL
;
1293 err
= kmod_module_apply_filter(mod
->ctx
,
1294 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1298 kmod_module_unref_list(list
);
1299 if (filtered
== NULL
)
1300 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1305 cb
.run_install
= run_install
;
1306 cb
.data
= (void *) data
;
1308 kmod_list_foreach(l
, list
) {
1309 struct kmod_module
*m
= l
->data
;
1310 const char *moptions
= kmod_module_get_options(m
);
1311 const char *cmd
= kmod_module_get_install_commands(m
);
1314 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1315 && module_is_inkernel(m
)) {
1316 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1322 options
= module_options_concat(moptions
,
1323 m
== mod
? extra_options
: NULL
);
1325 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1326 if (print_action
!= NULL
)
1327 print_action(m
, true, options
?: "");
1329 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1330 err
= module_do_install_commands(m
, options
,
1333 if (print_action
!= NULL
)
1334 print_action(m
, false, options
?: "");
1336 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1337 err
= kmod_module_insert_module(m
, flags
,
1345 * Treat "already loaded" error. If we were told to stop on
1346 * already loaded and the module being loaded is not a softdep
1347 * or dep, bail out. Otherwise, just ignore and continue.
1349 * We need to check here because of race conditions. We
1350 * checked first if module was already loaded but it may have
1351 * been loaded between the check and the moment we try to
1354 if (err
== -EEXIST
&& m
== mod
&&
1355 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1359 * Ignore errors from softdeps
1361 if (err
== -EEXIST
|| !m
->required
)
1368 kmod_module_unref_list(list
);
1373 * kmod_module_get_options:
1376 * Get options of this kmod module. Options come from the configuration file
1377 * and are cached in @mod. The first call to this function will search for
1378 * this module in configuration and subsequent calls return the cached string.
1380 * Returns: a string with all the options separated by spaces. This string is
1381 * owned by @mod, do not free it.
1383 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1388 if (!mod
->init
.options
) {
1390 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1391 const struct kmod_list
*l
;
1392 const struct kmod_config
*config
;
1396 config
= kmod_get_config(mod
->ctx
);
1398 kmod_list_foreach(l
, config
->options
) {
1399 const char *modname
= kmod_option_get_modname(l
);
1404 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1405 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1406 streq(modname
, mod
->alias
))))
1409 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1410 str
= kmod_option_get_options(l
);
1415 tmp
= realloc(opts
, optslen
+ len
+ 2);
1424 opts
[optslen
] = ' ';
1428 memcpy(opts
+ optslen
, str
, len
);
1430 opts
[optslen
] = '\0';
1433 m
->init
.options
= true;
1437 return mod
->options
;
1440 ERR(mod
->ctx
, "out of memory\n");
1445 * kmod_module_get_install_commands:
1448 * Get install commands for this kmod module. Install commands come from the
1449 * configuration file and are cached in @mod. The first call to this function
1450 * will search for this module in configuration and subsequent calls return
1451 * the cached string. The install commands are returned as they were in the
1452 * configuration, concatenated by ';'. No other processing is made in this
1455 * Returns: a string with all install commands separated by semicolons. This
1456 * string is owned by @mod, do not free it.
1458 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1463 if (!mod
->init
.install_commands
) {
1465 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1466 const struct kmod_list
*l
;
1467 const struct kmod_config
*config
;
1469 config
= kmod_get_config(mod
->ctx
);
1471 kmod_list_foreach(l
, config
->install_commands
) {
1472 const char *modname
= kmod_command_get_modname(l
);
1474 if (fnmatch(modname
, mod
->name
, 0) != 0)
1477 m
->install_commands
= kmod_command_get_command(l
);
1480 * find only the first command, as modprobe from
1481 * module-init-tools does
1486 m
->init
.install_commands
= true;
1489 return mod
->install_commands
;
1492 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1494 mod
->init
.install_commands
= true;
1495 mod
->install_commands
= cmd
;
1498 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1500 struct kmod_list
*ret
= NULL
;
1503 for (i
= 0; i
< count
; i
++) {
1504 const char *depname
= array
[i
];
1505 struct kmod_list
*lst
= NULL
;
1508 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1510 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1512 } else if (lst
!= NULL
)
1513 ret
= kmod_list_append_list(ret
, lst
);
1519 * kmod_module_get_softdeps:
1521 * @pre: where to save the list of preceding soft dependencies.
1522 * @post: where to save the list of post soft dependencies.
1524 * Get soft dependencies for this kmod module. Soft dependencies come
1525 * from configuration file and are not cached in @mod because it may include
1526 * dependency cycles that would make we leak kmod_module. Any call
1527 * to this function will search for this module in configuration, allocate a
1528 * list and return the result.
1530 * Both @pre and @post are newly created list of kmod_module and
1531 * should be unreferenced with kmod_module_unref_list().
1533 * Returns: 0 on success or < 0 otherwise.
1535 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1536 struct kmod_list
**pre
,
1537 struct kmod_list
**post
)
1539 const struct kmod_list
*l
;
1540 const struct kmod_config
*config
;
1542 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1545 assert(*pre
== NULL
);
1546 assert(*post
== NULL
);
1548 config
= kmod_get_config(mod
->ctx
);
1550 kmod_list_foreach(l
, config
->softdeps
) {
1551 const char *modname
= kmod_softdep_get_name(l
);
1552 const char * const *array
;
1555 if (fnmatch(modname
, mod
->name
, 0) != 0)
1558 array
= kmod_softdep_get_pre(l
, &count
);
1559 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1560 array
= kmod_softdep_get_post(l
, &count
);
1561 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1564 * find only the first command, as modprobe from
1565 * module-init-tools does
1574 * kmod_module_get_remove_commands:
1577 * Get remove commands for this kmod module. Remove commands come from the
1578 * configuration file and are cached in @mod. The first call to this function
1579 * will search for this module in configuration and subsequent calls return
1580 * the cached string. The remove commands are returned as they were in the
1581 * configuration, concatenated by ';'. No other processing is made in this
1584 * Returns: a string with all remove commands separated by semicolons. This
1585 * string is owned by @mod, do not free it.
1587 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1592 if (!mod
->init
.remove_commands
) {
1594 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1595 const struct kmod_list
*l
;
1596 const struct kmod_config
*config
;
1598 config
= kmod_get_config(mod
->ctx
);
1600 kmod_list_foreach(l
, config
->remove_commands
) {
1601 const char *modname
= kmod_command_get_modname(l
);
1603 if (fnmatch(modname
, mod
->name
, 0) != 0)
1606 m
->remove_commands
= kmod_command_get_command(l
);
1609 * find only the first command, as modprobe from
1610 * module-init-tools does
1615 m
->init
.remove_commands
= true;
1618 return mod
->remove_commands
;
1621 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1623 mod
->init
.remove_commands
= true;
1624 mod
->remove_commands
= cmd
;
1628 * SECTION:libkmod-loaded
1629 * @short_description: currently loaded modules
1631 * Information about currently loaded modules, as reported by Linux kernel.
1632 * These information are not cached by libkmod and are always read from /sys
1633 * and /proc/modules.
1637 * kmod_module_new_from_loaded:
1638 * @ctx: kmod library context
1639 * @list: where to save the list of loaded modules
1641 * Create a new list of kmod modules with all modules currently loaded in
1642 * kernel. It uses /proc/modules to get the names of loaded modules and to
1643 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1644 * They are put in @list in no particular order.
1646 * The initial refcount is 1, and needs to be decremented to release the
1647 * resources of the kmod_module. The returned @list must be released by
1648 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1649 * kmod_modules created, they are all released upon @ctx destruction too. Do
1650 * not unref @ctx before all the desired operations with the returned list are
1653 * Returns: 0 on success or < 0 on error.
1655 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1656 struct kmod_list
**list
)
1658 struct kmod_list
*l
= NULL
;
1662 if (ctx
== NULL
|| list
== NULL
)
1665 fp
= fopen("/proc/modules", "re");
1668 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1672 while (fgets(line
, sizeof(line
), fp
)) {
1673 struct kmod_module
*m
;
1674 struct kmod_list
*node
;
1676 size_t len
= strlen(line
);
1677 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1679 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1681 ERR(ctx
, "could not get module from name '%s': %s\n",
1682 name
, strerror(-err
));
1686 node
= kmod_list_append(l
, m
);
1690 ERR(ctx
, "out of memory\n");
1691 kmod_module_unref(m
);
1694 while (line
[len
- 1] != '\n' && fgets(line
, sizeof(line
), fp
))
1705 * kmod_module_initstate_str:
1706 * @state: the state as returned by kmod_module_get_initstate()
1708 * Translate a initstate to a string.
1710 * Returns: the string associated to the @state. This string is statically
1711 * allocated, do not free it.
1713 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1716 case KMOD_MODULE_BUILTIN
:
1718 case KMOD_MODULE_LIVE
:
1720 case KMOD_MODULE_COMING
:
1722 case KMOD_MODULE_GOING
:
1730 * kmod_module_get_initstate:
1733 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1736 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1737 * KMOD_MODULE_BUILTIN: module is builtin;
1738 * KMOD_MODULE_LIVE: module is live in kernel;
1739 * KMOD_MODULE_COMING: module is being loaded;
1740 * KMOD_MODULE_GOING: module is being unloaded.
1742 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1744 char path
[PATH_MAX
], buf
[32];
1745 int fd
, err
, pathlen
;
1750 /* remove const: this can only change internal state */
1751 if (kmod_module_is_builtin((struct kmod_module
*)mod
))
1752 return KMOD_MODULE_BUILTIN
;
1754 pathlen
= snprintf(path
, sizeof(path
),
1755 "/sys/module/%s/initstate", mod
->name
);
1756 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1760 DBG(mod
->ctx
, "could not open '%s': %s\n",
1761 path
, strerror(-err
));
1763 if (pathlen
> (int)sizeof("/initstate") - 1) {
1765 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1766 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1767 return KMOD_MODULE_COMING
;
1770 DBG(mod
->ctx
, "could not open '%s': %s\n",
1771 path
, strerror(-err
));
1775 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1778 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1779 path
, strerror(-err
));
1783 if (streq(buf
, "live\n"))
1784 return KMOD_MODULE_LIVE
;
1785 else if (streq(buf
, "coming\n"))
1786 return KMOD_MODULE_COMING
;
1787 else if (streq(buf
, "going\n"))
1788 return KMOD_MODULE_GOING
;
1790 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1795 * kmod_module_get_size:
1798 * Get the size of this kmod module as returned by Linux kernel. If supported,
1799 * the size is read from the coresize attribute in /sys/module. For older
1800 * kernels, this falls back on /proc/modules and searches for the specified
1801 * module to get its size.
1803 * Returns: the size of this kmod module.
1805 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1810 long size
= -ENOENT
;
1816 /* try to open the module dir in /sys. If this fails, don't
1817 * bother trying to find the size as we know the module isn't
1820 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1821 dfd
= open(line
, O_RDONLY
|O_CLOEXEC
);
1825 /* available as of linux 3.3.x */
1826 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1828 if (read_str_long(cfd
, &size
, 10) < 0)
1829 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1834 /* fall back on parsing /proc/modules */
1835 fp
= fopen("/proc/modules", "re");
1839 "could not open /proc/modules: %s\n", strerror(errno
));
1844 while (fgets(line
, sizeof(line
), fp
)) {
1845 size_t len
= strlen(line
);
1846 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1850 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1853 tok
= strtok_r(NULL
, " \t", &saveptr
);
1856 "invalid line format at /proc/modules:%d\n", lineno
);
1860 value
= strtol(tok
, &endptr
, 10);
1861 if (endptr
== tok
|| *endptr
!= '\0') {
1863 "invalid line format at /proc/modules:%d\n", lineno
);
1870 while (line
[len
- 1] != '\n' && fgets(line
, sizeof(line
), fp
))
1881 * kmod_module_get_refcnt:
1884 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1887 * Returns: the reference count on success or < 0 on failure.
1889 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1891 char path
[PATH_MAX
];
1898 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1899 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1902 DBG(mod
->ctx
, "could not open '%s': %s\n",
1903 path
, strerror(errno
));
1907 err
= read_str_long(fd
, &refcnt
, 10);
1910 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1911 path
, strerror(-err
));
1919 * kmod_module_get_holders:
1922 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1923 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1925 * Returns: a new list of kmod modules on success or NULL on failure.
1927 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1929 char dname
[PATH_MAX
];
1930 struct kmod_list
*list
= NULL
;
1931 struct dirent
*dent
;
1934 if (mod
== NULL
|| mod
->ctx
== NULL
)
1937 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1941 ERR(mod
->ctx
, "could not open '%s': %s\n",
1942 dname
, strerror(errno
));
1946 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
1947 struct kmod_module
*holder
;
1948 struct kmod_list
*l
;
1951 if (dent
->d_name
[0] == '.') {
1952 if (dent
->d_name
[1] == '\0' ||
1953 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1957 err
= kmod_module_new_from_name(mod
->ctx
, dent
->d_name
,
1960 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1961 dent
->d_name
, strerror(-err
));
1965 l
= kmod_list_append(list
, holder
);
1969 ERR(mod
->ctx
, "out of memory\n");
1970 kmod_module_unref(holder
);
1980 kmod_module_unref_list(list
);
1984 struct kmod_module_section
{
1985 unsigned long address
;
1989 static void kmod_module_section_free(struct kmod_module_section
*section
)
1995 * kmod_module_get_sections:
1998 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1999 * structure contained in this list is internal to libkmod and their fields
2000 * can be obtained by calling kmod_module_section_get_name() and
2001 * kmod_module_section_get_address().
2003 * After use, free the @list by calling kmod_module_section_free_list().
2005 * Returns: a new list of kmod module sections on success or NULL on failure.
2007 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
2009 char dname
[PATH_MAX
];
2010 struct kmod_list
*list
= NULL
;
2011 struct dirent
*dent
;
2018 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
2022 ERR(mod
->ctx
, "could not open '%s': %s\n",
2023 dname
, strerror(errno
));
2029 for (dent
= readdir(d
); dent
; dent
= readdir(d
)) {
2030 struct kmod_module_section
*section
;
2031 struct kmod_list
*l
;
2032 unsigned long address
;
2036 if (dent
->d_name
[0] == '.') {
2037 if (dent
->d_name
[1] == '\0' ||
2038 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
2042 fd
= openat(dfd
, dent
->d_name
, O_RDONLY
|O_CLOEXEC
);
2044 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
2045 dname
, dent
->d_name
);
2049 err
= read_str_ulong(fd
, &address
, 16);
2052 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
2053 dname
, dent
->d_name
);
2057 namesz
= strlen(dent
->d_name
) + 1;
2058 section
= malloc(sizeof(*section
) + namesz
);
2060 if (section
== NULL
) {
2061 ERR(mod
->ctx
, "out of memory\n");
2065 section
->address
= address
;
2066 memcpy(section
->name
, dent
->d_name
, namesz
);
2068 l
= kmod_list_append(list
, section
);
2072 ERR(mod
->ctx
, "out of memory\n");
2083 kmod_module_unref_list(list
);
2088 * kmod_module_section_get_module_name:
2089 * @entry: a list entry representing a kmod module section
2091 * Get the name of a kmod module section.
2093 * After use, free the @list by calling kmod_module_section_free_list().
2095 * Returns: the name of this kmod module section on success or NULL on
2096 * failure. The string is owned by the section, do not free it.
2098 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2100 struct kmod_module_section
*section
;
2105 section
= entry
->data
;
2106 return section
->name
;
2110 * kmod_module_section_get_address:
2111 * @entry: a list entry representing a kmod module section
2113 * Get the address of a kmod module section.
2115 * After use, free the @list by calling kmod_module_section_free_list().
2117 * Returns: the address of this kmod module section on success or ULONG_MAX
2120 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2122 struct kmod_module_section
*section
;
2125 return (unsigned long)-1;
2127 section
= entry
->data
;
2128 return section
->address
;
2132 * kmod_module_section_free_list:
2133 * @list: kmod module section list
2135 * Release the resources taken by @list
2137 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2140 kmod_module_section_free(list
->data
);
2141 list
= kmod_list_remove(list
);
2145 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2147 if (mod
->file
== NULL
) {
2148 const char *path
= kmod_module_get_path(mod
);
2155 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2157 if (mod
->file
== NULL
)
2161 return kmod_file_get_elf(mod
->file
);
2164 struct kmod_module_info
{
2169 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2171 struct kmod_module_info
*info
;
2173 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2177 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2179 memcpy(info
->key
, key
, keylen
);
2180 info
->key
[keylen
] = '\0';
2181 memcpy(info
->value
, value
, valuelen
);
2182 info
->value
[valuelen
] = '\0';
2186 static void kmod_module_info_free(struct kmod_module_info
*info
)
2191 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2193 struct kmod_module_info
*info
;
2194 struct kmod_list
*n
;
2196 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2199 n
= kmod_list_append(*list
, info
);
2203 kmod_module_info_free(info
);
2207 static char *kmod_module_hex_to_str(const char *hex
, size_t len
)
2212 const size_t line_limit
= 20;
2215 str_len
= len
* 3; /* XX: or XX\0 */
2216 str_len
+= ((str_len
+ line_limit
- 1) / line_limit
- 1) * 3; /* \n\t\t */
2218 str
= malloc(str_len
);
2222 for (i
= 0, j
= 0; i
< (int)len
; i
++) {
2223 j
+= sprintf(str
+ j
, "%02X", (unsigned char)hex
[i
]);
2224 if (i
< (int)len
- 1) {
2227 if ((i
+ 1) % line_limit
== 0)
2228 j
+= sprintf(str
+ j
, "\n\t\t");
2234 static struct kmod_list
*kmod_module_info_append_hex(struct kmod_list
**list
,
2241 struct kmod_list
*n
;
2244 /* Display as 01:12:DE:AD:BE:EF:... */
2245 hex
= kmod_module_hex_to_str(value
, valuelen
);
2248 n
= kmod_module_info_append(list
, key
, keylen
, hex
, strlen(hex
));
2253 n
= kmod_module_info_append(list
, key
, keylen
, NULL
, 0);
2265 * kmod_module_get_info:
2267 * @list: where to return list of module information. Use
2268 * kmod_module_info_get_key() and
2269 * kmod_module_info_get_value(). Release this list with
2270 * kmod_module_info_free_list()
2272 * Get a list of entries in ELF section ".modinfo", these contain
2273 * alias, license, depends, vermagic and other keys with respective
2274 * values. If the module is signed (CONFIG_MODULE_SIG), information
2275 * about the module signature is included as well: signer,
2276 * sig_key and sig_hashalgo.
2278 * After use, free the @list by calling kmod_module_info_free_list().
2280 * Returns: 0 on success or < 0 otherwise.
2282 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2284 struct kmod_elf
*elf
;
2286 int i
, count
, ret
= -ENOMEM
;
2287 struct kmod_signature_info sig_info
= {};
2289 if (mod
== NULL
|| list
== NULL
)
2292 assert(*list
== NULL
);
2294 /* remove const: this can only change internal state */
2295 if (kmod_module_is_builtin((struct kmod_module
*)mod
)) {
2296 count
= kmod_builtin_get_modinfo(mod
->ctx
,
2297 kmod_module_get_name(mod
),
2302 elf
= kmod_module_get_elf(mod
);
2306 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2311 for (i
= 0; i
< count
; i
++) {
2312 struct kmod_list
*n
;
2313 const char *key
, *value
;
2314 size_t keylen
, valuelen
;
2317 value
= strchr(key
, '=');
2318 if (value
== NULL
) {
2319 keylen
= strlen(key
);
2323 keylen
= value
- key
;
2325 valuelen
= strlen(value
);
2328 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2333 if (mod
->file
&& kmod_module_signature_info(mod
->file
, &sig_info
)) {
2334 struct kmod_list
*n
;
2336 n
= kmod_module_info_append(list
, "sig_id", strlen("sig_id"),
2337 sig_info
.id_type
, strlen(sig_info
.id_type
));
2342 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2343 sig_info
.signer
, sig_info
.signer_len
);
2349 n
= kmod_module_info_append_hex(list
, "sig_key", strlen("sig_key"),
2351 sig_info
.key_id_len
);
2356 n
= kmod_module_info_append(list
,
2357 "sig_hashalgo", strlen("sig_hashalgo"),
2358 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2364 * Omit sig_info.algo for now, as these
2365 * are currently constant.
2367 n
= kmod_module_info_append_hex(list
, "signature",
2368 strlen("signature"),
2380 /* aux structures freed in normal case also */
2381 kmod_module_signature_info_free(&sig_info
);
2384 kmod_module_info_free_list(*list
);
2392 * kmod_module_info_get_key:
2393 * @entry: a list entry representing a kmod module info
2395 * Get the key of a kmod module info.
2397 * Returns: the key of this kmod module info on success or NULL on
2398 * failure. The string is owned by the info, do not free it.
2400 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2402 struct kmod_module_info
*info
;
2412 * kmod_module_info_get_value:
2413 * @entry: a list entry representing a kmod module info
2415 * Get the value of a kmod module info.
2417 * Returns: the value of this kmod module info on success or NULL on
2418 * failure. The string is owned by the info, do not free it.
2420 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2422 struct kmod_module_info
*info
;
2432 * kmod_module_info_free_list:
2433 * @list: kmod module info list
2435 * Release the resources taken by @list
2437 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2440 kmod_module_info_free(list
->data
);
2441 list
= kmod_list_remove(list
);
2445 struct kmod_module_version
{
2450 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2452 struct kmod_module_version
*mv
;
2453 size_t symbollen
= strlen(symbol
) + 1;
2455 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2460 memcpy(mv
->symbol
, symbol
, symbollen
);
2464 static void kmod_module_version_free(struct kmod_module_version
*version
)
2470 * kmod_module_get_versions:
2472 * @list: where to return list of module versions. Use
2473 * kmod_module_version_get_symbol() and
2474 * kmod_module_version_get_crc(). Release this list with
2475 * kmod_module_versions_free_list()
2477 * Get a list of entries in ELF section "__versions".
2479 * After use, free the @list by calling kmod_module_versions_free_list().
2481 * Returns: 0 on success or < 0 otherwise.
2483 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2485 struct kmod_elf
*elf
;
2486 struct kmod_modversion
*versions
;
2487 int i
, count
, ret
= 0;
2489 if (mod
== NULL
|| list
== NULL
)
2492 assert(*list
== NULL
);
2494 elf
= kmod_module_get_elf(mod
);
2498 count
= kmod_elf_get_modversions(elf
, &versions
);
2502 for (i
= 0; i
< count
; i
++) {
2503 struct kmod_module_version
*mv
;
2504 struct kmod_list
*n
;
2506 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2509 kmod_module_versions_free_list(*list
);
2514 n
= kmod_list_append(*list
, mv
);
2518 kmod_module_version_free(mv
);
2519 kmod_module_versions_free_list(*list
);
2533 * kmod_module_version_get_symbol:
2534 * @entry: a list entry representing a kmod module versions
2536 * Get the symbol of a kmod module versions.
2538 * Returns: the symbol of this kmod module versions on success or NULL
2539 * on failure. The string is owned by the versions, do not free it.
2541 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2543 struct kmod_module_version
*version
;
2545 if (entry
== NULL
|| entry
->data
== NULL
)
2548 version
= entry
->data
;
2549 return version
->symbol
;
2553 * kmod_module_version_get_crc:
2554 * @entry: a list entry representing a kmod module version
2556 * Get the crc of a kmod module version.
2558 * Returns: the crc of this kmod module version if available, otherwise default to 0.
2560 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2562 struct kmod_module_version
*version
;
2564 if (entry
== NULL
|| entry
->data
== NULL
)
2567 version
= entry
->data
;
2568 return version
->crc
;
2572 * kmod_module_versions_free_list:
2573 * @list: kmod module versions list
2575 * Release the resources taken by @list
2577 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2580 kmod_module_version_free(list
->data
);
2581 list
= kmod_list_remove(list
);
2585 struct kmod_module_symbol
{
2590 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2592 struct kmod_module_symbol
*mv
;
2593 size_t symbollen
= strlen(symbol
) + 1;
2595 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2600 memcpy(mv
->symbol
, symbol
, symbollen
);
2604 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2610 * kmod_module_get_symbols:
2612 * @list: where to return list of module symbols. Use
2613 * kmod_module_symbol_get_symbol() and
2614 * kmod_module_symbol_get_crc(). Release this list with
2615 * kmod_module_symbols_free_list()
2617 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2619 * After use, free the @list by calling kmod_module_symbols_free_list().
2621 * Returns: 0 on success or < 0 otherwise.
2623 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2625 struct kmod_elf
*elf
;
2626 struct kmod_modversion
*symbols
;
2627 int i
, count
, ret
= 0;
2629 if (mod
== NULL
|| list
== NULL
)
2632 assert(*list
== NULL
);
2634 elf
= kmod_module_get_elf(mod
);
2638 count
= kmod_elf_get_symbols(elf
, &symbols
);
2642 for (i
= 0; i
< count
; i
++) {
2643 struct kmod_module_symbol
*mv
;
2644 struct kmod_list
*n
;
2646 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2649 kmod_module_symbols_free_list(*list
);
2654 n
= kmod_list_append(*list
, mv
);
2658 kmod_module_symbol_free(mv
);
2659 kmod_module_symbols_free_list(*list
);
2673 * kmod_module_symbol_get_symbol:
2674 * @entry: a list entry representing a kmod module symbols
2676 * Get the symbol of a kmod module symbols.
2678 * Returns: the symbol of this kmod module symbols on success or NULL
2679 * on failure. The string is owned by the symbols, do not free it.
2681 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2683 struct kmod_module_symbol
*symbol
;
2685 if (entry
== NULL
|| entry
->data
== NULL
)
2688 symbol
= entry
->data
;
2689 return symbol
->symbol
;
2693 * kmod_module_symbol_get_crc:
2694 * @entry: a list entry representing a kmod module symbol
2696 * Get the crc of a kmod module symbol.
2698 * Returns: the crc of this kmod module symbol if available, otherwise default to 0.
2700 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2702 struct kmod_module_symbol
*symbol
;
2704 if (entry
== NULL
|| entry
->data
== NULL
)
2707 symbol
= entry
->data
;
2712 * kmod_module_symbols_free_list:
2713 * @list: kmod module symbols list
2715 * Release the resources taken by @list
2717 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2720 kmod_module_symbol_free(list
->data
);
2721 list
= kmod_list_remove(list
);
2725 struct kmod_module_dependency_symbol
{
2731 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2733 struct kmod_module_dependency_symbol
*mv
;
2734 size_t symbollen
= strlen(symbol
) + 1;
2736 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2742 memcpy(mv
->symbol
, symbol
, symbollen
);
2746 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2748 free(dependency_symbol
);
2752 * kmod_module_get_dependency_symbols:
2754 * @list: where to return list of module dependency_symbols. Use
2755 * kmod_module_dependency_symbol_get_symbol() and
2756 * kmod_module_dependency_symbol_get_crc(). Release this list with
2757 * kmod_module_dependency_symbols_free_list()
2759 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2761 * After use, free the @list by calling
2762 * kmod_module_dependency_symbols_free_list().
2764 * Returns: 0 on success or < 0 otherwise.
2766 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2768 struct kmod_elf
*elf
;
2769 struct kmod_modversion
*symbols
;
2770 int i
, count
, ret
= 0;
2772 if (mod
== NULL
|| list
== NULL
)
2775 assert(*list
== NULL
);
2777 elf
= kmod_module_get_elf(mod
);
2781 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2785 for (i
= 0; i
< count
; i
++) {
2786 struct kmod_module_dependency_symbol
*mv
;
2787 struct kmod_list
*n
;
2789 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2794 kmod_module_dependency_symbols_free_list(*list
);
2799 n
= kmod_list_append(*list
, mv
);
2803 kmod_module_dependency_symbol_free(mv
);
2804 kmod_module_dependency_symbols_free_list(*list
);
2818 * kmod_module_dependency_symbol_get_symbol:
2819 * @entry: a list entry representing a kmod module dependency_symbols
2821 * Get the dependency symbol of a kmod module
2823 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2824 * on failure. The string is owned by the dependency_symbols, do not free it.
2826 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2828 struct kmod_module_dependency_symbol
*dependency_symbol
;
2830 if (entry
== NULL
|| entry
->data
== NULL
)
2833 dependency_symbol
= entry
->data
;
2834 return dependency_symbol
->symbol
;
2838 * kmod_module_dependency_symbol_get_crc:
2839 * @entry: a list entry representing a kmod module dependency_symbol
2841 * Get the crc of a kmod module dependency_symbol.
2843 * Returns: the crc of this kmod module dependency_symbol if available, otherwise default to 0.
2845 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2847 struct kmod_module_dependency_symbol
*dependency_symbol
;
2849 if (entry
== NULL
|| entry
->data
== NULL
)
2852 dependency_symbol
= entry
->data
;
2853 return dependency_symbol
->crc
;
2857 * kmod_module_dependency_symbol_get_bind:
2858 * @entry: a list entry representing a kmod module dependency_symbol
2860 * Get the bind type of a kmod module dependency_symbol.
2862 * Returns: the bind of this kmod module dependency_symbol on success
2863 * or < 0 on failure.
2865 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2867 struct kmod_module_dependency_symbol
*dependency_symbol
;
2869 if (entry
== NULL
|| entry
->data
== NULL
)
2872 dependency_symbol
= entry
->data
;
2873 return dependency_symbol
->bind
;
2877 * kmod_module_dependency_symbols_free_list:
2878 * @list: kmod module dependency_symbols list
2880 * Release the resources taken by @list
2882 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2885 kmod_module_dependency_symbol_free(list
->data
);
2886 list
= kmod_list_remove(list
);
2891 * kmod_module_get_builtin:
2892 * @ctx: kmod library context
2893 * @list: where to save the builtin module list
2895 * Returns: 0 on success or < 0 otherwise.
2897 int kmod_module_get_builtin(struct kmod_ctx
*ctx
, struct kmod_list
**list
)
2899 struct kmod_builtin_iter
*iter
;
2902 iter
= kmod_builtin_iter_new(ctx
);
2906 while (kmod_builtin_iter_next(iter
)) {
2907 struct kmod_module
*mod
= NULL
;
2908 char modname
[PATH_MAX
];
2910 if (!kmod_builtin_iter_get_modname(iter
, modname
)) {
2915 kmod_module_new_from_name(ctx
, modname
, &mod
);
2916 kmod_module_set_builtin(mod
, true);
2918 *list
= kmod_list_append(*list
, mod
);
2921 kmod_builtin_iter_free(iter
);
2924 kmod_builtin_iter_free(iter
);
2925 kmod_module_unref_list(*list
);