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 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
583 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
584 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");
983 if (err
== -1 || WEXITSTATUS(err
)) {
984 ERR(mod
->ctx
, "Error running %s command for %s\n",
987 err
= -WEXITSTATUS(err
);
993 struct probe_insert_cb
{
994 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
998 static int module_do_install_commands(struct kmod_module
*mod
,
1000 struct probe_insert_cb
*cb
)
1002 const char *command
= kmod_module_get_install_commands(mod
);
1004 _cleanup_free_
char *cmd
;
1006 size_t cmdlen
, options_len
, varlen
;
1010 if (options
== NULL
)
1013 options_len
= strlen(options
);
1014 cmdlen
= strlen(command
);
1015 varlen
= sizeof("$CMDLINE_OPTS") - 1;
1017 cmd
= memdup(command
, cmdlen
+ 1);
1021 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
1022 size_t prefixlen
= p
- cmd
;
1023 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
1024 size_t slen
= cmdlen
- varlen
+ options_len
;
1025 char *suffix
= p
+ varlen
;
1026 char *s
= malloc(slen
+ 1);
1030 memcpy(s
, cmd
, p
- cmd
);
1031 memcpy(s
+ prefixlen
, options
, options_len
);
1032 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
1040 if (cb
->run_install
!= NULL
)
1041 err
= cb
->run_install(mod
, cmd
, cb
->data
);
1043 err
= command_do(mod
, "install", cmd
);
1048 static char *module_options_concat(const char *opt
, const char *xopt
)
1050 // TODO: we might need to check if xopt overrides options on opt
1051 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
1052 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
1055 if (optlen
== 0 && xoptlen
== 0)
1058 r
= malloc(optlen
+ xoptlen
+ 2);
1061 memcpy(r
, opt
, optlen
);
1067 memcpy(r
+ optlen
, xopt
, xoptlen
);
1069 r
[optlen
+ xoptlen
] = '\0';
1074 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1077 struct kmod_list
**list
);
1080 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1081 struct kmod_list
**list
)
1083 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1086 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1088 ERR(mod
->ctx
, "could not get softdep: %s\n",
1093 kmod_list_foreach(l
, pre
) {
1094 struct kmod_module
*m
= l
->data
;
1095 err
= __kmod_module_get_probe_list(m
, false, false, list
);
1100 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1102 kmod_module_unref(mod
);
1107 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1109 kmod_list_foreach(l
, post
) {
1110 struct kmod_module
*m
= l
->data
;
1111 err
= __kmod_module_get_probe_list(m
, false, false, list
);
1117 kmod_module_unref_list(pre
);
1118 kmod_module_unref_list(post
);
1124 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1127 struct kmod_list
**list
)
1129 struct kmod_list
*dep
, *l
;
1133 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1137 mod
->visited
= true;
1139 dep
= kmod_module_get_dependencies(mod
);
1142 * Called from kmod_module_probe_insert_module(); set the
1143 * ->required flag on mod and all its dependencies before
1144 * they are possibly visited through some softdeps.
1146 mod
->required
= true;
1147 kmod_list_foreach(l
, dep
) {
1148 struct kmod_module
*m
= l
->data
;
1153 kmod_list_foreach(l
, dep
) {
1154 struct kmod_module
*m
= l
->data
;
1155 err
= __kmod_module_fill_softdep(m
, list
);
1161 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1163 kmod_module_unref(mod
);
1168 mod
->ignorecmd
= true;
1170 err
= __kmod_module_fill_softdep(mod
, list
);
1173 kmod_module_unref_list(dep
);
1177 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1179 struct kmod_list
**list
)
1183 assert(mod
!= NULL
);
1184 assert(list
!= NULL
&& *list
== NULL
);
1187 * Make sure we don't get screwed by previous calls to this function
1189 kmod_set_modules_visited(mod
->ctx
, false);
1190 kmod_set_modules_required(mod
->ctx
, false);
1192 err
= __kmod_module_get_probe_list(mod
, true, ignorecmd
, list
);
1194 kmod_module_unref_list(*list
);
1202 * kmod_module_probe_insert_module:
1204 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1205 * behavior of this function, valid flags are
1206 * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
1207 * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
1208 * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
1209 * commands and softdeps configured in the system;
1210 * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
1211 * live in kernel or not;
1212 * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
1213 * associated callback function;
1214 * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
1215 * and the module is already live in kernel, the function will fail if this
1216 * flag is specified;
1217 * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
1218 * filter to this module and its dependencies. If any of the dependencies (or
1219 * the module) is blacklisted, the probe will fail, unless the blacklisted
1220 * module is already live in kernel;
1221 * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
1222 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
1223 * alias and is blacklisted.
1224 * @extra_options: module's options to pass to Linux Kernel. It applies only
1225 * to @mod, not to its dependencies.
1226 * @run_install: function to run when @mod is backed by an install command.
1227 * @data: data to give back to @run_install callback
1228 * @print_action: function to call with the action being taken (install or
1229 * insmod). It's useful for tools like modprobe when running with verbose
1230 * output or in dry-run mode.
1232 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1233 * install commands and applying blacklist.
1235 * If @run_install is NULL, this function will fork and exec by calling
1236 * system(3). Don't pass a NULL argument in @run_install if your binary is
1237 * setuid/setgid (see warning in system(3)). If you need control over the
1238 * execution of an install command, give a callback function instead.
1240 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1243 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1244 unsigned int flags
, const char *extra_options
,
1245 int (*run_install
)(struct kmod_module
*m
,
1246 const char *cmd
, void *data
),
1248 void (*print_action
)(struct kmod_module
*m
,
1250 const char *options
))
1252 struct kmod_list
*list
= NULL
, *l
;
1253 struct probe_insert_cb cb
;
1259 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1260 && module_is_inkernel(mod
)) {
1261 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1268 * Ugly assignement + check. We need to check if we were told to check
1269 * blacklist and also return the reason why we failed.
1270 * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
1271 * module is an alias, so we also need to check it
1273 if ((mod
->alias
!= NULL
&& ((err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
)))
1274 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1275 || (err
= flags
& KMOD_PROBE_APPLY_BLACKLIST
)) {
1276 if (module_is_blacklisted(mod
))
1280 err
= kmod_module_get_probe_list(mod
,
1281 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1285 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1286 struct kmod_list
*filtered
= NULL
;
1288 err
= kmod_module_apply_filter(mod
->ctx
,
1289 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1293 kmod_module_unref_list(list
);
1294 if (filtered
== NULL
)
1295 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1300 cb
.run_install
= run_install
;
1301 cb
.data
= (void *) data
;
1303 kmod_list_foreach(l
, list
) {
1304 struct kmod_module
*m
= l
->data
;
1305 const char *moptions
= kmod_module_get_options(m
);
1306 const char *cmd
= kmod_module_get_install_commands(m
);
1309 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1310 && module_is_inkernel(m
)) {
1311 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1317 options
= module_options_concat(moptions
,
1318 m
== mod
? extra_options
: NULL
);
1320 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1321 if (print_action
!= NULL
)
1322 print_action(m
, true, options
?: "");
1324 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1325 err
= module_do_install_commands(m
, options
,
1328 if (print_action
!= NULL
)
1329 print_action(m
, false, options
?: "");
1331 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1332 err
= kmod_module_insert_module(m
, flags
,
1340 * Treat "already loaded" error. If we were told to stop on
1341 * already loaded and the module being loaded is not a softdep
1342 * or dep, bail out. Otherwise, just ignore and continue.
1344 * We need to check here because of race conditions. We
1345 * checked first if module was already loaded but it may have
1346 * been loaded between the check and the moment we try to
1349 if (err
== -EEXIST
&& m
== mod
&&
1350 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1354 * Ignore errors from softdeps
1356 if (err
== -EEXIST
|| !m
->required
)
1363 kmod_module_unref_list(list
);
1368 * kmod_module_get_options:
1371 * Get options of this kmod module. Options come from the configuration file
1372 * and are cached in @mod. The first call to this function will search for
1373 * this module in configuration and subsequent calls return the cached string.
1375 * Returns: a string with all the options separated by spaces. This string is
1376 * owned by @mod, do not free it.
1378 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1383 if (!mod
->init
.options
) {
1385 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1386 const struct kmod_list
*l
;
1387 const struct kmod_config
*config
;
1391 config
= kmod_get_config(mod
->ctx
);
1393 kmod_list_foreach(l
, config
->options
) {
1394 const char *modname
= kmod_option_get_modname(l
);
1399 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1400 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1401 streq(modname
, mod
->alias
))))
1404 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1405 str
= kmod_option_get_options(l
);
1410 tmp
= realloc(opts
, optslen
+ len
+ 2);
1419 opts
[optslen
] = ' ';
1423 memcpy(opts
+ optslen
, str
, len
);
1425 opts
[optslen
] = '\0';
1428 m
->init
.options
= true;
1432 return mod
->options
;
1435 ERR(mod
->ctx
, "out of memory\n");
1440 * kmod_module_get_install_commands:
1443 * Get install commands for this kmod module. Install commands come from the
1444 * configuration file and are cached in @mod. The first call to this function
1445 * will search for this module in configuration and subsequent calls return
1446 * the cached string. The install commands are returned as they were in the
1447 * configuration, concatenated by ';'. No other processing is made in this
1450 * Returns: a string with all install commands separated by semicolons. This
1451 * string is owned by @mod, do not free it.
1453 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1458 if (!mod
->init
.install_commands
) {
1460 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1461 const struct kmod_list
*l
;
1462 const struct kmod_config
*config
;
1464 config
= kmod_get_config(mod
->ctx
);
1466 kmod_list_foreach(l
, config
->install_commands
) {
1467 const char *modname
= kmod_command_get_modname(l
);
1469 if (fnmatch(modname
, mod
->name
, 0) != 0)
1472 m
->install_commands
= kmod_command_get_command(l
);
1475 * find only the first command, as modprobe from
1476 * module-init-tools does
1481 m
->init
.install_commands
= true;
1484 return mod
->install_commands
;
1487 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1489 mod
->init
.install_commands
= true;
1490 mod
->install_commands
= cmd
;
1493 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1495 struct kmod_list
*ret
= NULL
;
1498 for (i
= 0; i
< count
; i
++) {
1499 const char *depname
= array
[i
];
1500 struct kmod_list
*lst
= NULL
;
1503 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1505 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1507 } else if (lst
!= NULL
)
1508 ret
= kmod_list_append_list(ret
, lst
);
1514 * kmod_module_get_softdeps:
1516 * @pre: where to save the list of preceding soft dependencies.
1517 * @post: where to save the list of post soft dependencies.
1519 * Get soft dependencies for this kmod module. Soft dependencies come
1520 * from configuration file and are not cached in @mod because it may include
1521 * dependency cycles that would make we leak kmod_module. Any call
1522 * to this function will search for this module in configuration, allocate a
1523 * list and return the result.
1525 * Both @pre and @post are newly created list of kmod_module and
1526 * should be unreferenced with kmod_module_unref_list().
1528 * Returns: 0 on success or < 0 otherwise.
1530 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1531 struct kmod_list
**pre
,
1532 struct kmod_list
**post
)
1534 const struct kmod_list
*l
;
1535 const struct kmod_config
*config
;
1537 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1540 assert(*pre
== NULL
);
1541 assert(*post
== NULL
);
1543 config
= kmod_get_config(mod
->ctx
);
1545 kmod_list_foreach(l
, config
->softdeps
) {
1546 const char *modname
= kmod_softdep_get_name(l
);
1547 const char * const *array
;
1550 if (fnmatch(modname
, mod
->name
, 0) != 0)
1553 array
= kmod_softdep_get_pre(l
, &count
);
1554 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1555 array
= kmod_softdep_get_post(l
, &count
);
1556 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1559 * find only the first command, as modprobe from
1560 * module-init-tools does
1569 * kmod_module_get_remove_commands:
1572 * Get remove commands for this kmod module. Remove commands come from the
1573 * configuration file and are cached in @mod. The first call to this function
1574 * will search for this module in configuration and subsequent calls return
1575 * the cached string. The remove commands are returned as they were in the
1576 * configuration, concatenated by ';'. No other processing is made in this
1579 * Returns: a string with all remove commands separated by semicolons. This
1580 * string is owned by @mod, do not free it.
1582 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1587 if (!mod
->init
.remove_commands
) {
1589 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1590 const struct kmod_list
*l
;
1591 const struct kmod_config
*config
;
1593 config
= kmod_get_config(mod
->ctx
);
1595 kmod_list_foreach(l
, config
->remove_commands
) {
1596 const char *modname
= kmod_command_get_modname(l
);
1598 if (fnmatch(modname
, mod
->name
, 0) != 0)
1601 m
->remove_commands
= kmod_command_get_command(l
);
1604 * find only the first command, as modprobe from
1605 * module-init-tools does
1610 m
->init
.remove_commands
= true;
1613 return mod
->remove_commands
;
1616 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1618 mod
->init
.remove_commands
= true;
1619 mod
->remove_commands
= cmd
;
1623 * SECTION:libkmod-loaded
1624 * @short_description: currently loaded modules
1626 * Information about currently loaded modules, as reported by Linux kernel.
1627 * These information are not cached by libkmod and are always read from /sys
1628 * and /proc/modules.
1632 * kmod_module_new_from_loaded:
1633 * @ctx: kmod library context
1634 * @list: where to save the list of loaded modules
1636 * Create a new list of kmod modules with all modules currently loaded in
1637 * kernel. It uses /proc/modules to get the names of loaded modules and to
1638 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1639 * They are put in @list in no particular order.
1641 * The initial refcount is 1, and needs to be decremented to release the
1642 * resources of the kmod_module. The returned @list must be released by
1643 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1644 * kmod_modules created, they are all released upon @ctx destruction too. Do
1645 * not unref @ctx before all the desired operations with the returned list are
1648 * Returns: 0 on success or < 0 on error.
1650 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1651 struct kmod_list
**list
)
1653 struct kmod_list
*l
= NULL
;
1657 if (ctx
== NULL
|| list
== NULL
)
1660 fp
= fopen("/proc/modules", "re");
1663 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1667 while (fgets(line
, sizeof(line
), fp
)) {
1668 struct kmod_module
*m
;
1669 struct kmod_list
*node
;
1671 size_t len
= strlen(line
);
1672 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1674 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1676 ERR(ctx
, "could not get module from name '%s': %s\n",
1677 name
, strerror(-err
));
1681 node
= kmod_list_append(l
, m
);
1685 ERR(ctx
, "out of memory\n");
1686 kmod_module_unref(m
);
1689 while (line
[len
- 1] != '\n' && fgets(line
, sizeof(line
), fp
))
1700 * kmod_module_initstate_str:
1701 * @state: the state as returned by kmod_module_get_initstate()
1703 * Translate a initstate to a string.
1705 * Returns: the string associated to the @state. This string is statically
1706 * allocated, do not free it.
1708 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1711 case KMOD_MODULE_BUILTIN
:
1713 case KMOD_MODULE_LIVE
:
1715 case KMOD_MODULE_COMING
:
1717 case KMOD_MODULE_GOING
:
1725 * kmod_module_get_initstate:
1728 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1731 * Returns: < 0 on error or module state if module is found in kernel, valid states are
1732 * KMOD_MODULE_BUILTIN: module is builtin;
1733 * KMOD_MODULE_LIVE: module is live in kernel;
1734 * KMOD_MODULE_COMING: module is being loaded;
1735 * KMOD_MODULE_GOING: module is being unloaded.
1737 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1739 char path
[PATH_MAX
], buf
[32];
1740 int fd
, err
, pathlen
;
1745 /* remove const: this can only change internal state */
1746 if (kmod_module_is_builtin((struct kmod_module
*)mod
))
1747 return KMOD_MODULE_BUILTIN
;
1749 pathlen
= snprintf(path
, sizeof(path
),
1750 "/sys/module/%s/initstate", mod
->name
);
1751 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1755 DBG(mod
->ctx
, "could not open '%s': %s\n",
1756 path
, strerror(-err
));
1758 if (pathlen
> (int)sizeof("/initstate") - 1) {
1760 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1761 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1762 return KMOD_MODULE_COMING
;
1765 DBG(mod
->ctx
, "could not open '%s': %s\n",
1766 path
, strerror(-err
));
1770 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1773 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1774 path
, strerror(-err
));
1778 if (streq(buf
, "live\n"))
1779 return KMOD_MODULE_LIVE
;
1780 else if (streq(buf
, "coming\n"))
1781 return KMOD_MODULE_COMING
;
1782 else if (streq(buf
, "going\n"))
1783 return KMOD_MODULE_GOING
;
1785 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1790 * kmod_module_get_size:
1793 * Get the size of this kmod module as returned by Linux kernel. If supported,
1794 * the size is read from the coresize attribute in /sys/module. For older
1795 * kernels, this falls back on /proc/modules and searches for the specified
1796 * module to get its size.
1798 * Returns: the size of this kmod module.
1800 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1805 long size
= -ENOENT
;
1811 /* try to open the module dir in /sys. If this fails, don't
1812 * bother trying to find the size as we know the module isn't
1815 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1816 dfd
= open(line
, O_RDONLY
|O_CLOEXEC
);
1820 /* available as of linux 3.3.x */
1821 cfd
= openat(dfd
, "coresize", O_RDONLY
|O_CLOEXEC
);
1823 if (read_str_long(cfd
, &size
, 10) < 0)
1824 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1829 /* fall back on parsing /proc/modules */
1830 fp
= fopen("/proc/modules", "re");
1834 "could not open /proc/modules: %s\n", strerror(errno
));
1839 while (fgets(line
, sizeof(line
), fp
)) {
1840 size_t len
= strlen(line
);
1841 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1845 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1848 tok
= strtok_r(NULL
, " \t", &saveptr
);
1851 "invalid line format at /proc/modules:%d\n", lineno
);
1855 value
= strtol(tok
, &endptr
, 10);
1856 if (endptr
== tok
|| *endptr
!= '\0') {
1858 "invalid line format at /proc/modules:%d\n", lineno
);
1865 while (line
[len
- 1] != '\n' && fgets(line
, sizeof(line
), fp
))
1876 * kmod_module_get_refcnt:
1879 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1882 * Returns: the reference count on success or < 0 on failure.
1884 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1886 char path
[PATH_MAX
];
1893 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1894 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1897 DBG(mod
->ctx
, "could not open '%s': %s\n",
1898 path
, strerror(errno
));
1902 err
= read_str_long(fd
, &refcnt
, 10);
1905 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1906 path
, strerror(-err
));
1914 * kmod_module_get_holders:
1917 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1918 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1920 * Returns: a new list of kmod modules on success or NULL on failure.
1922 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1924 char dname
[PATH_MAX
];
1925 struct kmod_list
*list
= NULL
;
1926 struct dirent
*dent
;
1929 if (mod
== NULL
|| mod
->ctx
== NULL
)
1932 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1936 ERR(mod
->ctx
, "could not open '%s': %s\n",
1937 dname
, strerror(errno
));
1941 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
1942 struct kmod_module
*holder
;
1943 struct kmod_list
*l
;
1946 if (dent
->d_name
[0] == '.') {
1947 if (dent
->d_name
[1] == '\0' ||
1948 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1952 err
= kmod_module_new_from_name(mod
->ctx
, dent
->d_name
,
1955 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1956 dent
->d_name
, strerror(-err
));
1960 l
= kmod_list_append(list
, holder
);
1964 ERR(mod
->ctx
, "out of memory\n");
1965 kmod_module_unref(holder
);
1975 kmod_module_unref_list(list
);
1979 struct kmod_module_section
{
1980 unsigned long address
;
1984 static void kmod_module_section_free(struct kmod_module_section
*section
)
1990 * kmod_module_get_sections:
1993 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1994 * structure contained in this list is internal to libkmod and their fields
1995 * can be obtained by calling kmod_module_section_get_name() and
1996 * kmod_module_section_get_address().
1998 * After use, free the @list by calling kmod_module_section_free_list().
2000 * Returns: a new list of kmod module sections on success or NULL on failure.
2002 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
2004 char dname
[PATH_MAX
];
2005 struct kmod_list
*list
= NULL
;
2006 struct dirent
*dent
;
2013 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
2017 ERR(mod
->ctx
, "could not open '%s': %s\n",
2018 dname
, strerror(errno
));
2024 for (dent
= readdir(d
); dent
; dent
= readdir(d
)) {
2025 struct kmod_module_section
*section
;
2026 struct kmod_list
*l
;
2027 unsigned long address
;
2031 if (dent
->d_name
[0] == '.') {
2032 if (dent
->d_name
[1] == '\0' ||
2033 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
2037 fd
= openat(dfd
, dent
->d_name
, O_RDONLY
|O_CLOEXEC
);
2039 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
2040 dname
, dent
->d_name
);
2044 err
= read_str_ulong(fd
, &address
, 16);
2047 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
2048 dname
, dent
->d_name
);
2052 namesz
= strlen(dent
->d_name
) + 1;
2053 section
= malloc(sizeof(*section
) + namesz
);
2055 if (section
== NULL
) {
2056 ERR(mod
->ctx
, "out of memory\n");
2060 section
->address
= address
;
2061 memcpy(section
->name
, dent
->d_name
, namesz
);
2063 l
= kmod_list_append(list
, section
);
2067 ERR(mod
->ctx
, "out of memory\n");
2078 kmod_module_unref_list(list
);
2083 * kmod_module_section_get_module_name:
2084 * @entry: a list entry representing a kmod module section
2086 * Get the name of a kmod module section.
2088 * After use, free the @list by calling kmod_module_section_free_list().
2090 * Returns: the name of this kmod module section on success or NULL on
2091 * failure. The string is owned by the section, do not free it.
2093 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
2095 struct kmod_module_section
*section
;
2100 section
= entry
->data
;
2101 return section
->name
;
2105 * kmod_module_section_get_address:
2106 * @entry: a list entry representing a kmod module section
2108 * Get the address of a kmod module section.
2110 * After use, free the @list by calling kmod_module_section_free_list().
2112 * Returns: the address of this kmod module section on success or ULONG_MAX
2115 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
2117 struct kmod_module_section
*section
;
2120 return (unsigned long)-1;
2122 section
= entry
->data
;
2123 return section
->address
;
2127 * kmod_module_section_free_list:
2128 * @list: kmod module section list
2130 * Release the resources taken by @list
2132 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2135 kmod_module_section_free(list
->data
);
2136 list
= kmod_list_remove(list
);
2140 static struct kmod_elf
*kmod_module_get_elf(const struct kmod_module
*mod
)
2142 if (mod
->file
== NULL
) {
2143 const char *path
= kmod_module_get_path(mod
);
2150 ((struct kmod_module
*)mod
)->file
= kmod_file_open(mod
->ctx
,
2152 if (mod
->file
== NULL
)
2156 return kmod_file_get_elf(mod
->file
);
2159 struct kmod_module_info
{
2164 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2166 struct kmod_module_info
*info
;
2168 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2172 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2174 memcpy(info
->key
, key
, keylen
);
2175 info
->key
[keylen
] = '\0';
2176 memcpy(info
->value
, value
, valuelen
);
2177 info
->value
[valuelen
] = '\0';
2181 static void kmod_module_info_free(struct kmod_module_info
*info
)
2186 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2188 struct kmod_module_info
*info
;
2189 struct kmod_list
*n
;
2191 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2194 n
= kmod_list_append(*list
, info
);
2198 kmod_module_info_free(info
);
2202 static char *kmod_module_hex_to_str(const char *hex
, size_t len
)
2207 const size_t line_limit
= 20;
2210 str_len
= len
* 3; /* XX: or XX\0 */
2211 str_len
+= ((str_len
+ line_limit
- 1) / line_limit
- 1) * 3; /* \n\t\t */
2213 str
= malloc(str_len
);
2217 for (i
= 0, j
= 0; i
< (int)len
; i
++) {
2218 j
+= sprintf(str
+ j
, "%02X", (unsigned char)hex
[i
]);
2219 if (i
< (int)len
- 1) {
2222 if ((i
+ 1) % line_limit
== 0)
2223 j
+= sprintf(str
+ j
, "\n\t\t");
2229 static struct kmod_list
*kmod_module_info_append_hex(struct kmod_list
**list
,
2236 struct kmod_list
*n
;
2239 /* Display as 01:12:DE:AD:BE:EF:... */
2240 hex
= kmod_module_hex_to_str(value
, valuelen
);
2243 n
= kmod_module_info_append(list
, key
, keylen
, hex
, strlen(hex
));
2248 n
= kmod_module_info_append(list
, key
, keylen
, NULL
, 0);
2260 * kmod_module_get_info:
2262 * @list: where to return list of module information. Use
2263 * kmod_module_info_get_key() and
2264 * kmod_module_info_get_value(). Release this list with
2265 * kmod_module_info_free_list()
2267 * Get a list of entries in ELF section ".modinfo", these contain
2268 * alias, license, depends, vermagic and other keys with respective
2269 * values. If the module is signed (CONFIG_MODULE_SIG), information
2270 * about the module signature is included as well: signer,
2271 * sig_key and sig_hashalgo.
2273 * After use, free the @list by calling kmod_module_info_free_list().
2275 * Returns: 0 on success or < 0 otherwise.
2277 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2279 struct kmod_elf
*elf
;
2281 int i
, count
, ret
= -ENOMEM
;
2282 struct kmod_signature_info sig_info
= {};
2284 if (mod
== NULL
|| list
== NULL
)
2287 assert(*list
== NULL
);
2289 elf
= kmod_module_get_elf(mod
);
2293 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2297 for (i
= 0; i
< count
; i
++) {
2298 struct kmod_list
*n
;
2299 const char *key
, *value
;
2300 size_t keylen
, valuelen
;
2303 value
= strchr(key
, '=');
2304 if (value
== NULL
) {
2305 keylen
= strlen(key
);
2309 keylen
= value
- key
;
2311 valuelen
= strlen(value
);
2314 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
2319 if (kmod_module_signature_info(mod
->file
, &sig_info
)) {
2320 struct kmod_list
*n
;
2322 n
= kmod_module_info_append(list
, "sig_id", strlen("sig_id"),
2323 sig_info
.id_type
, strlen(sig_info
.id_type
));
2328 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
2329 sig_info
.signer
, sig_info
.signer_len
);
2335 n
= kmod_module_info_append_hex(list
, "sig_key", strlen("sig_key"),
2337 sig_info
.key_id_len
);
2342 n
= kmod_module_info_append(list
,
2343 "sig_hashalgo", strlen("sig_hashalgo"),
2344 sig_info
.hash_algo
, strlen(sig_info
.hash_algo
));
2350 * Omit sig_info.algo for now, as these
2351 * are currently constant.
2353 n
= kmod_module_info_append_hex(list
, "signature",
2354 strlen("signature"),
2366 /* aux structures freed in normal case also */
2367 kmod_module_signature_info_free(&sig_info
);
2370 kmod_module_info_free_list(*list
);
2378 * kmod_module_info_get_key:
2379 * @entry: a list entry representing a kmod module info
2381 * Get the key of a kmod module info.
2383 * Returns: the key of this kmod module info on success or NULL on
2384 * failure. The string is owned by the info, do not free it.
2386 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2388 struct kmod_module_info
*info
;
2398 * kmod_module_info_get_value:
2399 * @entry: a list entry representing a kmod module info
2401 * Get the value of a kmod module info.
2403 * Returns: the value of this kmod module info on success or NULL on
2404 * failure. The string is owned by the info, do not free it.
2406 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2408 struct kmod_module_info
*info
;
2418 * kmod_module_info_free_list:
2419 * @list: kmod module info list
2421 * Release the resources taken by @list
2423 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2426 kmod_module_info_free(list
->data
);
2427 list
= kmod_list_remove(list
);
2431 struct kmod_module_version
{
2436 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2438 struct kmod_module_version
*mv
;
2439 size_t symbollen
= strlen(symbol
) + 1;
2441 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2446 memcpy(mv
->symbol
, symbol
, symbollen
);
2450 static void kmod_module_version_free(struct kmod_module_version
*version
)
2456 * kmod_module_get_versions:
2458 * @list: where to return list of module versions. Use
2459 * kmod_module_version_get_symbol() and
2460 * kmod_module_version_get_crc(). Release this list with
2461 * kmod_module_versions_free_list()
2463 * Get a list of entries in ELF section "__versions".
2465 * After use, free the @list by calling kmod_module_versions_free_list().
2467 * Returns: 0 on success or < 0 otherwise.
2469 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2471 struct kmod_elf
*elf
;
2472 struct kmod_modversion
*versions
;
2473 int i
, count
, ret
= 0;
2475 if (mod
== NULL
|| list
== NULL
)
2478 assert(*list
== NULL
);
2480 elf
= kmod_module_get_elf(mod
);
2484 count
= kmod_elf_get_modversions(elf
, &versions
);
2488 for (i
= 0; i
< count
; i
++) {
2489 struct kmod_module_version
*mv
;
2490 struct kmod_list
*n
;
2492 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2495 kmod_module_versions_free_list(*list
);
2500 n
= kmod_list_append(*list
, mv
);
2504 kmod_module_version_free(mv
);
2505 kmod_module_versions_free_list(*list
);
2519 * kmod_module_version_get_symbol:
2520 * @entry: a list entry representing a kmod module versions
2522 * Get the symbol of a kmod module versions.
2524 * Returns: the symbol of this kmod module versions on success or NULL
2525 * on failure. The string is owned by the versions, do not free it.
2527 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2529 struct kmod_module_version
*version
;
2531 if (entry
== NULL
|| entry
->data
== NULL
)
2534 version
= entry
->data
;
2535 return version
->symbol
;
2539 * kmod_module_version_get_crc:
2540 * @entry: a list entry representing a kmod module version
2542 * Get the crc of a kmod module version.
2544 * Returns: the crc of this kmod module version if available, otherwise default to 0.
2546 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2548 struct kmod_module_version
*version
;
2550 if (entry
== NULL
|| entry
->data
== NULL
)
2553 version
= entry
->data
;
2554 return version
->crc
;
2558 * kmod_module_versions_free_list:
2559 * @list: kmod module versions list
2561 * Release the resources taken by @list
2563 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2566 kmod_module_version_free(list
->data
);
2567 list
= kmod_list_remove(list
);
2571 struct kmod_module_symbol
{
2576 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2578 struct kmod_module_symbol
*mv
;
2579 size_t symbollen
= strlen(symbol
) + 1;
2581 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2586 memcpy(mv
->symbol
, symbol
, symbollen
);
2590 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2596 * kmod_module_get_symbols:
2598 * @list: where to return list of module symbols. Use
2599 * kmod_module_symbol_get_symbol() and
2600 * kmod_module_symbol_get_crc(). Release this list with
2601 * kmod_module_symbols_free_list()
2603 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2605 * After use, free the @list by calling kmod_module_symbols_free_list().
2607 * Returns: 0 on success or < 0 otherwise.
2609 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2611 struct kmod_elf
*elf
;
2612 struct kmod_modversion
*symbols
;
2613 int i
, count
, ret
= 0;
2615 if (mod
== NULL
|| list
== NULL
)
2618 assert(*list
== NULL
);
2620 elf
= kmod_module_get_elf(mod
);
2624 count
= kmod_elf_get_symbols(elf
, &symbols
);
2628 for (i
= 0; i
< count
; i
++) {
2629 struct kmod_module_symbol
*mv
;
2630 struct kmod_list
*n
;
2632 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2635 kmod_module_symbols_free_list(*list
);
2640 n
= kmod_list_append(*list
, mv
);
2644 kmod_module_symbol_free(mv
);
2645 kmod_module_symbols_free_list(*list
);
2659 * kmod_module_symbol_get_symbol:
2660 * @entry: a list entry representing a kmod module symbols
2662 * Get the symbol of a kmod module symbols.
2664 * Returns: the symbol of this kmod module symbols on success or NULL
2665 * on failure. The string is owned by the symbols, do not free it.
2667 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2669 struct kmod_module_symbol
*symbol
;
2671 if (entry
== NULL
|| entry
->data
== NULL
)
2674 symbol
= entry
->data
;
2675 return symbol
->symbol
;
2679 * kmod_module_symbol_get_crc:
2680 * @entry: a list entry representing a kmod module symbol
2682 * Get the crc of a kmod module symbol.
2684 * Returns: the crc of this kmod module symbol if available, otherwise default to 0.
2686 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2688 struct kmod_module_symbol
*symbol
;
2690 if (entry
== NULL
|| entry
->data
== NULL
)
2693 symbol
= entry
->data
;
2698 * kmod_module_symbols_free_list:
2699 * @list: kmod module symbols list
2701 * Release the resources taken by @list
2703 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2706 kmod_module_symbol_free(list
->data
);
2707 list
= kmod_list_remove(list
);
2711 struct kmod_module_dependency_symbol
{
2717 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2719 struct kmod_module_dependency_symbol
*mv
;
2720 size_t symbollen
= strlen(symbol
) + 1;
2722 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2728 memcpy(mv
->symbol
, symbol
, symbollen
);
2732 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2734 free(dependency_symbol
);
2738 * kmod_module_get_dependency_symbols:
2740 * @list: where to return list of module dependency_symbols. Use
2741 * kmod_module_dependency_symbol_get_symbol() and
2742 * kmod_module_dependency_symbol_get_crc(). Release this list with
2743 * kmod_module_dependency_symbols_free_list()
2745 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2747 * After use, free the @list by calling
2748 * kmod_module_dependency_symbols_free_list().
2750 * Returns: 0 on success or < 0 otherwise.
2752 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2754 struct kmod_elf
*elf
;
2755 struct kmod_modversion
*symbols
;
2756 int i
, count
, ret
= 0;
2758 if (mod
== NULL
|| list
== NULL
)
2761 assert(*list
== NULL
);
2763 elf
= kmod_module_get_elf(mod
);
2767 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2771 for (i
= 0; i
< count
; i
++) {
2772 struct kmod_module_dependency_symbol
*mv
;
2773 struct kmod_list
*n
;
2775 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2780 kmod_module_dependency_symbols_free_list(*list
);
2785 n
= kmod_list_append(*list
, mv
);
2789 kmod_module_dependency_symbol_free(mv
);
2790 kmod_module_dependency_symbols_free_list(*list
);
2804 * kmod_module_dependency_symbol_get_symbol:
2805 * @entry: a list entry representing a kmod module dependency_symbols
2807 * Get the dependency symbol of a kmod module
2809 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2810 * on failure. The string is owned by the dependency_symbols, do not free it.
2812 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2814 struct kmod_module_dependency_symbol
*dependency_symbol
;
2816 if (entry
== NULL
|| entry
->data
== NULL
)
2819 dependency_symbol
= entry
->data
;
2820 return dependency_symbol
->symbol
;
2824 * kmod_module_dependency_symbol_get_crc:
2825 * @entry: a list entry representing a kmod module dependency_symbol
2827 * Get the crc of a kmod module dependency_symbol.
2829 * Returns: the crc of this kmod module dependency_symbol if available, otherwise default to 0.
2831 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2833 struct kmod_module_dependency_symbol
*dependency_symbol
;
2835 if (entry
== NULL
|| entry
->data
== NULL
)
2838 dependency_symbol
= entry
->data
;
2839 return dependency_symbol
->crc
;
2843 * kmod_module_dependency_symbol_get_bind:
2844 * @entry: a list entry representing a kmod module dependency_symbol
2846 * Get the bind type of a kmod module dependency_symbol.
2848 * Returns: the bind of this kmod module dependency_symbol on success
2849 * or < 0 on failure.
2851 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2853 struct kmod_module_dependency_symbol
*dependency_symbol
;
2855 if (entry
== NULL
|| entry
->data
== NULL
)
2858 dependency_symbol
= entry
->data
;
2859 return dependency_symbol
->bind
;
2863 * kmod_module_dependency_symbols_free_list:
2864 * @list: kmod module dependency_symbols list
2866 * Release the resources taken by @list
2868 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2871 kmod_module_dependency_symbol_free(list
->data
);
2872 list
= kmod_list_remove(list
);
2877 * kmod_module_get_builtin:
2878 * @ctx: kmod library context
2879 * @list: where to save the builtin module list
2881 * Returns: 0 on success or < 0 otherwise.
2883 int kmod_module_get_builtin(struct kmod_ctx
*ctx
, struct kmod_list
**list
)
2885 struct kmod_builtin_iter
*iter
;
2888 iter
= kmod_builtin_iter_new(ctx
);
2892 while (kmod_builtin_iter_next(iter
)) {
2893 struct kmod_module
*mod
= NULL
;
2894 char modname
[PATH_MAX
];
2896 if (!kmod_builtin_iter_get_modname(iter
, modname
)) {
2901 kmod_module_new_from_name(ctx
, modname
, &mod
);
2902 kmod_module_set_builtin(mod
, true);
2904 *list
= kmod_list_append(*list
, mod
);
2907 kmod_builtin_iter_free(iter
);
2910 kmod_builtin_iter_free(iter
);
2911 kmod_module_unref_list(*list
);