2 * libkmod - interface to kernel module operations
4 * Copyright (C) 2011-2012 ProFUSION embedded systems
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <sys/types.h>
41 #include "libkmod-private.h"
44 * SECTION:libkmod-module
45 * @short_description: operate on kernel modules
51 * Opaque object representing a module.
58 struct kmod_list
*dep
;
60 const char *install_commands
; /* owned by kmod_config */
61 const char *remove_commands
; /* owned by kmod_config */
62 char *alias
; /* only set if this module was created from an alias */
68 bool install_commands
: 1;
69 bool remove_commands
: 1;
73 * private field used by kmod_module_get_probe_list() to detect
79 * set by kmod_module_get_probe_list: indicates for probe_insert()
80 * whether the module's command and softdep should be ignored
85 * if module was created by searching the modules.builtin file, this
86 * is set. There's nothing much useful one can do with such a
87 * "module", except knowing it's builtin.
92 static inline const char *path_join(const char *path
, size_t prefixlen
,
100 pathlen
= strlen(path
);
101 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
104 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
108 static inline bool module_is_inkernel(struct kmod_module
*mod
)
110 int state
= kmod_module_get_initstate(mod
);
112 if (state
== KMOD_MODULE_LIVE
||
113 state
== KMOD_MODULE_BUILTIN
)
119 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
121 struct kmod_ctx
*ctx
= mod
->ctx
;
122 struct kmod_list
*list
= NULL
;
131 assert(mod
->dep
== NULL
);
132 mod
->init
.dep
= true;
134 p
= strchr(line
, ':');
139 dirname
= kmod_get_dirname(mod
->ctx
);
140 dirnamelen
= strlen(dirname
);
141 if (dirnamelen
+ 2 >= PATH_MAX
)
144 memcpy(buf
, dirname
, dirnamelen
);
145 buf
[dirnamelen
] = '/';
147 buf
[dirnamelen
] = '\0';
149 if (mod
->path
== NULL
) {
150 const char *str
= path_join(line
, dirnamelen
, buf
);
153 mod
->path
= strdup(str
);
154 if (mod
->path
== NULL
)
159 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
160 p
= strtok_r(NULL
, " \t", &saveptr
)) {
161 struct kmod_module
*depmod
;
164 path
= path_join(p
, dirnamelen
, buf
);
166 ERR(ctx
, "could not join path '%s' and '%s'.\n",
171 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
173 ERR(ctx
, "ctx=%p path=%s error=%s\n",
174 ctx
, path
, strerror(-err
));
178 DBG(ctx
, "add dep: %s\n", path
);
180 list
= kmod_list_prepend(list
, depmod
);
184 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
191 kmod_module_unref_list(list
);
192 mod
->init
.dep
= false;
196 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
198 mod
->visited
= visited
;
201 void kmod_module_set_builtin(struct kmod_module
*mod
, bool builtin
)
203 mod
->builtin
= builtin
;
207 * Memory layout with alias:
209 * struct kmod_module {
214 * name <----------' | |
215 * alias <-----------' |
216 * name\alias <--------'
218 * Memory layout without alias:
220 * struct kmod_module {
222 * alias -----|----> NULL
225 * name <----------'-'
227 * @key is "name\alias" or "name" (in which case alias == NULL)
229 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
230 const char *name
, size_t namelen
,
231 const char *alias
, size_t aliaslen
,
232 struct kmod_module
**mod
)
234 struct kmod_module
*m
;
237 m
= kmod_pool_get_module(ctx
, key
);
239 *mod
= kmod_module_ref(m
);
246 keylen
= namelen
+ aliaslen
+ 1;
248 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
254 memset(m
, 0, sizeof(*m
));
256 m
->ctx
= kmod_ref(ctx
);
257 m
->name
= (char *)m
+ sizeof(*m
);
258 memcpy(m
->name
, key
, keylen
+ 1);
260 m
->hashkey
= m
->name
;
263 m
->name
[namelen
] = '\0';
264 m
->alias
= m
->name
+ namelen
+ 1;
265 m
->hashkey
= m
->name
+ keylen
+ 1;
266 memcpy(m
->hashkey
, key
, keylen
+ 1);
270 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
277 * kmod_module_new_from_name:
278 * @ctx: kmod library context
279 * @name: name of the module
280 * @mod: where to save the created struct kmod_module
282 * Create a new struct kmod_module using the module name. @name can not be an
283 * alias, file name or anything else; it must be a module name. There's no
284 * check if the module exists in the system.
286 * This function is also used internally by many others that return a new
287 * struct kmod_module or a new list of modules.
289 * The initial refcount is 1, and needs to be decremented to release the
290 * resources of the kmod_module. Since libkmod keeps track of all
291 * kmod_modules created, they are all released upon @ctx destruction too. Do
292 * not unref @ctx before all the desired operations with the returned
293 * kmod_module are done.
295 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
296 * module name or if memory allocation failed.
298 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
300 struct kmod_module
**mod
)
303 char name_norm
[PATH_MAX
];
305 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
308 modname_normalize(name
, name_norm
, &namelen
);
310 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
313 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
314 const char *name
, struct kmod_module
**mod
)
318 size_t namelen
= strlen(name
);
319 size_t aliaslen
= strlen(alias
);
321 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
322 return -ENAMETOOLONG
;
324 memcpy(key
, name
, namelen
);
325 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
328 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
336 * kmod_module_new_from_path:
337 * @ctx: kmod library context
338 * @path: path where to find the given module
339 * @mod: where to save the created struct kmod_module
341 * Create a new struct kmod_module using the module path. @path must be an
342 * existent file with in the filesystem and must be accessible to libkmod.
344 * The initial refcount is 1, and needs to be decremented to release the
345 * resources of the kmod_module. Since libkmod keeps track of all
346 * kmod_modules created, they are all released upon @ctx destruction too. Do
347 * not unref @ctx before all the desired operations with the returned
348 * kmod_module are done.
350 * If @path is relative, it's treated as relative to the current working
351 * directory. Otherwise, give an absolute path.
353 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
354 * it's not a valid file for a kmod_module or if memory allocation failed.
356 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
358 struct kmod_module
**mod
)
360 struct kmod_module
*m
;
367 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
370 abspath
= path_make_absolute_cwd(path
);
371 if (abspath
== NULL
) {
372 DBG(ctx
, "no absolute path for %s\n", path
);
376 err
= stat(abspath
, &st
);
379 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
384 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
385 DBG(ctx
, "could not get modname from path %s\n", path
);
390 m
= kmod_pool_get_module(ctx
, name
);
394 else if (streq(m
->path
, abspath
))
397 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
398 name
, abspath
, m
->path
);
403 *mod
= kmod_module_ref(m
);
407 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
421 * Drop a reference of the kmod module. If the refcount reaches zero, its
422 * resources are released.
424 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
425 * returns the passed @mod with its refcount decremented.
427 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
432 if (--mod
->refcount
> 0)
435 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
437 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
438 kmod_module_unref_list(mod
->dep
);
439 kmod_unref(mod
->ctx
);
450 * Take a reference of the kmod module, incrementing its refcount.
452 * Returns: the passed @module with its refcount incremented.
454 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
464 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
468 if (*(_list) != NULL) \
473 * kmod_module_new_from_lookup:
474 * @ctx: kmod library context
475 * @given_alias: alias to look for
476 * @list: an empty list where to save the list of modules matching
479 * Create a new list of kmod modules using an alias or module name and lookup
480 * libkmod's configuration files and indexes in order to find the module.
481 * Once it's found in one of the places, it stops searching and create the
482 * list of modules that is saved in @list.
484 * The search order is: 1. aliases in configuration file; 2. module names in
485 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
486 * in modules.alias index.
488 * The initial refcount is 1, and needs to be decremented to release the
489 * resources of the kmod_module. The returned @list must be released by
490 * calling kmod_module_unref_list(). Since libkmod keeps track of all
491 * kmod_modules created, they are all released upon @ctx destruction too. Do
492 * not unref @ctx before all the desired operations with the returned list are
495 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
496 * methods failed, which is basically due to memory allocation fail. If module
497 * is not found, it still returns 0, but @list is an empty list.
499 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
500 const char *given_alias
,
501 struct kmod_list
**list
)
504 char alias
[PATH_MAX
];
506 if (ctx
== NULL
|| given_alias
== NULL
)
509 if (list
== NULL
|| *list
!= NULL
) {
510 ERR(ctx
, "An empty list is needed to create lookup\n");
514 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
515 DBG(ctx
, "invalid alias: %s\n", given_alias
);
519 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
521 /* Aliases from config file override all the others */
522 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
523 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
525 DBG(ctx
, "lookup modules.dep %s\n", alias
);
526 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
527 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
529 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
530 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
531 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
533 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
534 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
535 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
537 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
538 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
539 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
541 DBG(ctx
, "lookup modules.builtin %s\n", alias
);
542 err
= kmod_lookup_alias_from_builtin_file(ctx
, alias
, list
);
543 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
546 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
549 DBG(ctx
, "Failed to lookup %s\n", alias
);
550 kmod_module_unref_list(*list
);
554 #undef CHECK_ERR_AND_FINISH
557 * kmod_module_unref_list:
558 * @list: list of kmod modules
560 * Drop a reference of each kmod module in @list and releases the resources
561 * taken by the list itself.
563 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
564 * returns the passed @mod with its refcount decremented.
566 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
568 for (; list
!= NULL
; list
= kmod_list_remove(list
))
569 kmod_module_unref(list
->data
);
575 * kmod_module_get_filtered_blacklist:
576 * @ctx: kmod library context
577 * @input: list of kmod_module to be filtered with blacklist
578 * @output: where to save the new list
580 * This function should not be used. Use kmod_module_apply_filter instead.
582 * Given a list @input, this function filter it out with config's blacklist
583 * and save it in @output.
585 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
588 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
589 const struct kmod_list
*input
,
590 struct kmod_list
**output
)
592 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
595 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
597 if (!mod
->init
.dep
) {
599 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
604 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
615 * kmod_module_get_dependencies:
618 * Search the modules.dep index to find the dependencies of the given @mod.
619 * The result is cached in @mod, so subsequent calls to this function will
620 * return the already searched list of modules.
622 * Returns: NULL on failure or if there are any dependencies. Otherwise it
623 * returns a list of kmod modules that can be released by calling
624 * kmod_module_unref_list().
626 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
628 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
633 module_get_dependencies_noref(mod
);
635 kmod_list_foreach(l
, mod
->dep
) {
636 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
638 kmod_module_unref(l
->data
);
648 ERR(mod
->ctx
, "out of memory\n");
649 kmod_module_unref_list(list_new
);
654 * kmod_module_get_module:
655 * @entry: an entry in a list of kmod modules.
657 * Get the kmod module of this @entry in the list, increasing its refcount.
658 * After it's used, unref it. Since the refcount is incremented upon return,
659 * you still have to call kmod_module_unref_list() to release the list of kmod
662 * Returns: NULL on failure or the kmod_module contained in this list entry
663 * with its refcount incremented.
665 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
670 return kmod_module_ref(entry
->data
);
674 * kmod_module_get_name:
677 * Get the name of this kmod module. Name is always available, independently
678 * if it was created by kmod_module_new_from_name() or another function and
679 * it's always normalized (dashes are replaced with underscores).
681 * Returns: the name of this kmod module.
683 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
692 * kmod_module_get_path:
695 * Get the path of this kmod module. If this kmod module was not created by
696 * path, it can search the modules.dep index in order to find out the module
697 * under context's dirname.
699 * Returns: the path of this kmod module or NULL if such information is not
702 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
709 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
711 if (mod
->path
!= NULL
)
717 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
721 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
728 extern long delete_module(const char *name
, unsigned int flags
);
731 * kmod_module_remove_module:
733 * @flags: flags to pass to Linux kernel when removing the module
735 * Remove a module from Linux kernel.
737 * Returns: 0 on success or < 0 on failure.
739 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
747 /* Filter out other flags */
748 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
750 err
= delete_module(mod
->name
, flags
);
753 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
759 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
762 * kmod_module_insert_module:
764 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
765 * behavior of this function.
766 * @options: module's options to pass to Linux Kernel.
768 * Insert a module in Linux kernel. It opens the file pointed by @mod,
769 * mmap'ing it and passing to kernel.
771 * Returns: 0 on success or < 0 on failure. If module is already loaded it
774 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
781 struct kmod_file
*file
;
782 struct kmod_elf
*elf
= NULL
;
784 const char *args
= options
? options
: "";
789 path
= kmod_module_get_path(mod
);
791 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
795 file
= kmod_file_open(mod
->ctx
, path
);
801 size
= kmod_file_get_size(file
);
802 mem
= kmod_file_get_contents(file
);
804 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
805 elf
= kmod_elf_new(mem
, size
);
811 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
812 err
= kmod_elf_strip_section(elf
, "__versions");
814 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
817 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
818 err
= kmod_elf_strip_vermagic(elf
);
820 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
823 mem
= kmod_elf_get_memory(elf
);
826 err
= init_module(mem
, size
, args
);
829 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
835 kmod_file_unref(file
);
840 static bool module_is_blacklisted(struct kmod_module
*mod
)
842 struct kmod_ctx
*ctx
= mod
->ctx
;
843 const struct kmod_config
*config
= kmod_get_config(ctx
);
844 const struct kmod_list
*bl
= config
->blacklists
;
845 const struct kmod_list
*l
;
847 kmod_list_foreach(l
, bl
) {
848 const char *modname
= kmod_blacklist_get_modname(l
);
850 if (streq(modname
, mod
->name
))
858 * kmod_module_apply_filter
859 * @ctx: kmod library context
860 * @filter_type: bitmask to filter modules on
861 * @input: list of kmod_module to be filtered
862 * @output: where to save the new list
864 * Given a list @input, this function filter it out by the filter mask
865 * and save it in @output.
867 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
870 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
871 enum kmod_filter filter_type
,
872 const struct kmod_list
*input
,
873 struct kmod_list
**output
)
875 const struct kmod_list
*li
;
877 if (ctx
== NULL
|| output
== NULL
)
884 kmod_list_foreach(li
, input
) {
885 struct kmod_module
*mod
= li
->data
;
886 struct kmod_list
*node
;
888 if ((filter_type
& KMOD_FILTER_BLACKLIST
) &&
889 module_is_blacklisted(mod
))
892 if ((filter_type
& KMOD_FILTER_BUILTIN
) && mod
->builtin
)
895 node
= kmod_list_append(*output
, mod
);
900 kmod_module_ref(mod
);
906 kmod_module_unref_list(*output
);
911 static int command_do(struct kmod_module
*mod
, const char *type
,
914 const char *modname
= kmod_module_get_name(mod
);
917 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
919 setenv("MODPROBE_MODULE", modname
, 1);
921 unsetenv("MODPROBE_MODULE");
923 if (err
== -1 || WEXITSTATUS(err
)) {
924 ERR(mod
->ctx
, "Error running %s command for %s\n",
927 err
= -WEXITSTATUS(err
);
933 struct probe_insert_cb
{
934 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
938 static int module_do_install_commands(struct kmod_module
*mod
,
940 struct probe_insert_cb
*cb
)
942 const char *command
= kmod_module_get_install_commands(mod
);
945 size_t cmdlen
, options_len
, varlen
;
952 options_len
= strlen(options
);
953 cmdlen
= strlen(command
);
954 varlen
= sizeof("$CMDLINE_OPTS") - 1;
956 cmd
= memdup(command
, cmdlen
+ 1);
960 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
961 size_t prefixlen
= p
- cmd
;
962 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
963 size_t slen
= cmdlen
- varlen
+ options_len
;
964 char *suffix
= p
+ varlen
;
965 char *s
= malloc(slen
+ 1);
970 memcpy(s
, cmd
, p
- cmd
);
971 memcpy(s
+ prefixlen
, options
, options_len
);
972 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
980 if (cb
->run_install
!= NULL
)
981 err
= cb
->run_install(mod
, cmd
, cb
->data
);
983 err
= command_do(mod
, "install", cmd
);
990 static char *module_options_concat(const char *opt
, const char *xopt
)
992 // TODO: we might need to check if xopt overrides options on opt
993 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
994 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
997 if (optlen
== 0 && xoptlen
== 0)
1000 r
= malloc(optlen
+ xoptlen
+ 2);
1003 memcpy(r
, opt
, optlen
);
1009 memcpy(r
+ optlen
, xopt
, xoptlen
);
1011 r
[optlen
+ xoptlen
] = '\0';
1016 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1018 struct kmod_list
**list
);
1021 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
1022 struct kmod_list
**list
)
1024 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
1027 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1029 ERR(mod
->ctx
, "could not get softdep: %s\n",
1034 kmod_list_foreach(l
, pre
) {
1035 struct kmod_module
*m
= l
->data
;
1036 err
= __kmod_module_get_probe_list(m
, false, list
);
1041 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1043 kmod_module_unref(mod
);
1048 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1050 kmod_list_foreach(l
, post
) {
1051 struct kmod_module
*m
= l
->data
;
1052 err
= __kmod_module_get_probe_list(m
, false, list
);
1058 kmod_module_unref_list(pre
);
1059 kmod_module_unref_list(post
);
1065 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1067 struct kmod_list
**list
)
1069 struct kmod_list
*dep
, *l
;
1073 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1077 mod
->visited
= true;
1079 dep
= kmod_module_get_dependencies(mod
);
1080 kmod_list_foreach(l
, dep
) {
1081 struct kmod_module
*m
= l
->data
;
1082 err
= __kmod_module_fill_softdep(m
, list
);
1088 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1090 kmod_module_unref(mod
);
1095 mod
->ignorecmd
= true;
1097 err
= __kmod_module_fill_softdep(mod
, list
);
1100 kmod_module_unref_list(dep
);
1104 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1106 struct kmod_list
**list
)
1110 assert(mod
!= NULL
);
1111 assert(list
!= NULL
&& *list
== NULL
);
1114 * Make sure we don't get screwed by previous calls to this function
1116 kmod_set_modules_visited(mod
->ctx
, false);
1118 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1120 kmod_module_unref_list(*list
);
1128 * kmod_module_probe_insert_module:
1130 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1131 * behavior of this function.
1132 * @extra_options: module's options to pass to Linux Kernel. It applies only
1133 * to @mod, not to its dependencies.
1134 * @run_install: function to run when @mod is backed by an install command.
1135 * @data: data to give back to @run_install callback
1136 * @print_action: function to call with the action being taken (install or
1137 * insmod). It's useful for tools like modprobe when running with verbose
1138 * output or in dry-run mode.
1140 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1141 * install commands and applying blacklist.
1143 * If @run_install is NULL, this function will fork and exec by calling
1144 * system(3). Don't pass a NULL argument in @run_install if your binary is
1145 * setuid/setgid (see warning in system(3)). If you need control over the
1146 * execution of an install command, give a callback function instead.
1148 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1151 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1152 unsigned int flags
, const char *extra_options
,
1153 int (*run_install
)(struct kmod_module
*m
,
1154 const char *cmd
, void *data
),
1156 void (*print_action
)(struct kmod_module
*m
,
1158 const char *options
))
1160 struct kmod_list
*list
= NULL
, *l
;
1161 struct probe_insert_cb cb
;
1167 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1168 && module_is_inkernel(mod
)) {
1169 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1175 err
= flags
& (KMOD_PROBE_APPLY_BLACKLIST
|
1176 KMOD_PROBE_APPLY_BLACKLIST_ALL
);
1178 if (module_is_blacklisted(mod
))
1182 err
= kmod_module_get_probe_list(mod
,
1183 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1187 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1188 struct kmod_list
*filtered
= NULL
;
1190 err
= kmod_module_apply_filter(mod
->ctx
,
1191 KMOD_FILTER_BLACKLIST
, list
, &filtered
);
1195 kmod_module_unref_list(list
);
1196 if (filtered
== NULL
)
1197 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1202 cb
.run_install
= run_install
;
1203 cb
.data
= (void *) data
;
1205 kmod_list_foreach(l
, list
) {
1206 struct kmod_module
*m
= l
->data
;
1207 const char *moptions
= kmod_module_get_options(m
);
1208 const char *cmd
= kmod_module_get_install_commands(m
);
1211 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1212 && module_is_inkernel(m
)) {
1213 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n",
1219 options
= module_options_concat(moptions
,
1220 m
== mod
? extra_options
: NULL
);
1222 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1223 if (print_action
!= NULL
)
1224 print_action(m
, true, options
?: "");
1226 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1227 err
= module_do_install_commands(m
, options
,
1230 if (print_action
!= NULL
)
1231 print_action(m
, false, options
?: "");
1233 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1234 err
= kmod_module_insert_module(m
, flags
,
1242 * Treat "already loaded" error. If we were told to stop on
1243 * already loaded and the module being loaded is not a softdep
1244 * or dep, bail out. Otherwise, just ignore and continue.
1246 * We need to check here because of race conditions. We
1247 * checked first if module was already loaded but it may have
1248 * been loaded between the check and the moment we try to
1251 if (err
== -EEXIST
&& m
== mod
&&
1252 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1261 kmod_module_unref_list(list
);
1266 * kmod_module_get_options:
1269 * Get options of this kmod module. Options come from the configuration file
1270 * and are cached in @mod. The first call to this function will search for
1271 * this module in configuration and subsequent calls return the cached string.
1273 * Returns: a string with all the options separated by spaces. This string is
1274 * owned by @mod, do not free it.
1276 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1281 if (!mod
->init
.options
) {
1283 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1284 const struct kmod_list
*l
;
1285 const struct kmod_config
*config
;
1289 config
= kmod_get_config(mod
->ctx
);
1291 kmod_list_foreach(l
, config
->options
) {
1292 const char *modname
= kmod_option_get_modname(l
);
1297 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1298 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1299 streq(modname
, mod
->alias
))))
1302 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1303 str
= kmod_option_get_options(l
);
1308 tmp
= realloc(opts
, optslen
+ len
+ 2);
1317 opts
[optslen
] = ' ';
1321 memcpy(opts
+ optslen
, str
, len
);
1323 opts
[optslen
] = '\0';
1326 m
->init
.options
= true;
1330 return mod
->options
;
1333 ERR(mod
->ctx
, "out of memory\n");
1338 * kmod_module_get_install_commands:
1341 * Get install commands for this kmod module. Install commands come from the
1342 * configuration file and are cached in @mod. The first call to this function
1343 * will search for this module in configuration and subsequent calls return
1344 * the cached string. The install commands are returned as they were in the
1345 * configuration, concatenated by ';'. No other processing is made in this
1348 * Returns: a string with all install commands separated by semicolons. This
1349 * string is owned by @mod, do not free it.
1351 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1356 if (!mod
->init
.install_commands
) {
1358 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1359 const struct kmod_list
*l
;
1360 const struct kmod_config
*config
;
1362 config
= kmod_get_config(mod
->ctx
);
1364 kmod_list_foreach(l
, config
->install_commands
) {
1365 const char *modname
= kmod_command_get_modname(l
);
1367 if (fnmatch(modname
, mod
->name
, 0) != 0)
1370 m
->install_commands
= kmod_command_get_command(l
);
1373 * find only the first command, as modprobe from
1374 * module-init-tools does
1379 m
->init
.install_commands
= true;
1382 return mod
->install_commands
;
1385 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1387 mod
->init
.install_commands
= true;
1388 mod
->install_commands
= cmd
;
1391 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1393 struct kmod_list
*ret
= NULL
;
1396 for (i
= 0; i
< count
; i
++) {
1397 const char *depname
= array
[i
];
1398 struct kmod_list
*lst
= NULL
;
1401 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1403 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1405 } else if (lst
!= NULL
)
1406 ret
= kmod_list_append_list(ret
, lst
);
1412 * kmod_module_get_softdeps:
1414 * @pre: where to save the list of preceding soft dependencies.
1415 * @post: where to save the list of post soft dependencies.
1417 * Get soft dependencies for this kmod module. Soft dependencies come
1418 * from configuration file and are not cached in @mod because it may include
1419 * dependency cycles that would make we leak kmod_module. Any call
1420 * to this function will search for this module in configuration, allocate a
1421 * list and return the result.
1423 * Both @pre and @post are newly created list of kmod_module and
1424 * should be unreferenced with kmod_module_unref_list().
1426 * Returns: 0 on success or < 0 otherwise.
1428 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1429 struct kmod_list
**pre
,
1430 struct kmod_list
**post
)
1432 const struct kmod_list
*l
;
1433 const struct kmod_config
*config
;
1435 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1438 assert(*pre
== NULL
);
1439 assert(*post
== NULL
);
1441 config
= kmod_get_config(mod
->ctx
);
1443 kmod_list_foreach(l
, config
->softdeps
) {
1444 const char *modname
= kmod_softdep_get_name(l
);
1445 const char * const *array
;
1448 if (fnmatch(modname
, mod
->name
, 0) != 0)
1451 array
= kmod_softdep_get_pre(l
, &count
);
1452 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1453 array
= kmod_softdep_get_post(l
, &count
);
1454 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1457 * find only the first command, as modprobe from
1458 * module-init-tools does
1467 * kmod_module_get_remove_commands:
1470 * Get remove commands for this kmod module. Remove commands come from the
1471 * configuration file and are cached in @mod. The first call to this function
1472 * will search for this module in configuration and subsequent calls return
1473 * the cached string. The remove commands are returned as they were in the
1474 * configuration, concatenated by ';'. No other processing is made in this
1477 * Returns: a string with all remove commands separated by semicolons. This
1478 * string is owned by @mod, do not free it.
1480 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1485 if (!mod
->init
.remove_commands
) {
1487 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1488 const struct kmod_list
*l
;
1489 const struct kmod_config
*config
;
1491 config
= kmod_get_config(mod
->ctx
);
1493 kmod_list_foreach(l
, config
->remove_commands
) {
1494 const char *modname
= kmod_command_get_modname(l
);
1496 if (fnmatch(modname
, mod
->name
, 0) != 0)
1499 m
->remove_commands
= kmod_command_get_command(l
);
1502 * find only the first command, as modprobe from
1503 * module-init-tools does
1508 m
->init
.remove_commands
= true;
1511 return mod
->remove_commands
;
1514 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1516 mod
->init
.remove_commands
= true;
1517 mod
->remove_commands
= cmd
;
1521 * SECTION:libkmod-loaded
1522 * @short_description: currently loaded modules
1524 * Information about currently loaded modules, as reported by Linux kernel.
1525 * These information are not cached by libkmod and are always read from /sys
1526 * and /proc/modules.
1530 * kmod_module_new_from_loaded:
1531 * @ctx: kmod library context
1532 * @list: where to save the list of loaded modules
1534 * Create a new list of kmod modules with all modules currently loaded in
1535 * kernel. It uses /proc/modules to get the names of loaded modules and to
1536 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1537 * They are put are put in @list in no particular order.
1539 * The initial refcount is 1, and needs to be decremented to release the
1540 * resources of the kmod_module. The returned @list must be released by
1541 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1542 * kmod_modules created, they are all released upon @ctx destruction too. Do
1543 * not unref @ctx before all the desired operations with the returned list are
1546 * Returns: 0 on success or < 0 on error.
1548 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1549 struct kmod_list
**list
)
1551 struct kmod_list
*l
= NULL
;
1555 if (ctx
== NULL
|| list
== NULL
)
1558 fp
= fopen("/proc/modules", "re");
1561 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1565 while (fgets(line
, sizeof(line
), fp
)) {
1566 struct kmod_module
*m
;
1567 struct kmod_list
*node
;
1569 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1571 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1573 ERR(ctx
, "could not get module from name '%s': %s\n",
1574 name
, strerror(-err
));
1578 node
= kmod_list_append(l
, m
);
1582 ERR(ctx
, "out of memory\n");
1583 kmod_module_unref(m
);
1594 * kmod_module_initstate_str:
1595 * @state: the state as returned by kmod_module_get_initstate()
1597 * Translate a initstate to a string.
1599 * Returns: the string associated to the @state. This string is statically
1600 * allocated, do not free it.
1602 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1605 case KMOD_MODULE_BUILTIN
:
1607 case KMOD_MODULE_LIVE
:
1609 case KMOD_MODULE_COMING
:
1611 case KMOD_MODULE_GOING
:
1619 * kmod_module_get_initstate:
1622 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1625 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1627 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1629 char path
[PATH_MAX
], buf
[32];
1630 int fd
, err
, pathlen
;
1636 return KMOD_MODULE_BUILTIN
;
1638 pathlen
= snprintf(path
, sizeof(path
),
1639 "/sys/module/%s/initstate", mod
->name
);
1640 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1644 DBG(mod
->ctx
, "could not open '%s': %s\n",
1645 path
, strerror(-err
));
1647 if (pathlen
> (int)sizeof("/initstate") - 1) {
1649 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1650 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1651 return KMOD_MODULE_BUILTIN
;
1654 DBG(mod
->ctx
, "could not open '%s': %s\n",
1655 path
, strerror(-err
));
1659 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1662 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1663 path
, strerror(-err
));
1667 if (streq(buf
, "live\n"))
1668 return KMOD_MODULE_LIVE
;
1669 else if (streq(buf
, "coming\n"))
1670 return KMOD_MODULE_COMING
;
1671 else if (streq(buf
, "going\n"))
1672 return KMOD_MODULE_GOING
;
1674 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1679 * kmod_module_get_size:
1682 * Get the size of this kmod module as returned by Linux kernel. It reads the
1683 * file /proc/modules to search for this module and get its size.
1685 * Returns: the size of this kmod module.
1687 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1689 // FIXME TODO: this should be available from /sys/module/foo
1693 long size
= -ENOENT
;
1698 fp
= fopen("/proc/modules", "re");
1702 "could not open /proc/modules: %s\n", strerror(errno
));
1706 while (fgets(line
, sizeof(line
), fp
)) {
1707 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1711 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1714 tok
= strtok_r(NULL
, " \t", &saveptr
);
1717 "invalid line format at /proc/modules:%d\n", lineno
);
1721 value
= strtol(tok
, &endptr
, 10);
1722 if (endptr
== tok
|| *endptr
!= '\0') {
1724 "invalid line format at /proc/modules:%d\n", lineno
);
1736 * kmod_module_get_refcnt:
1739 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1742 * Returns: 0 on success or < 0 on failure.
1744 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1746 char path
[PATH_MAX
];
1753 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1754 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1757 DBG(mod
->ctx
, "could not open '%s': %s\n",
1758 path
, strerror(errno
));
1762 err
= read_str_long(fd
, &refcnt
, 10);
1765 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1766 path
, strerror(-err
));
1774 * kmod_module_get_holders:
1777 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1778 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1780 * Returns: a new list of kmod modules on success or NULL on failure.
1782 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1784 char dname
[PATH_MAX
];
1785 struct kmod_list
*list
= NULL
;
1791 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1795 ERR(mod
->ctx
, "could not open '%s': %s\n",
1796 dname
, strerror(errno
));
1801 struct dirent de
, *entp
;
1802 struct kmod_module
*holder
;
1803 struct kmod_list
*l
;
1806 err
= readdir_r(d
, &de
, &entp
);
1808 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1809 mod
->name
, strerror(-err
));
1816 if (de
.d_name
[0] == '.') {
1817 if (de
.d_name
[1] == '\0' ||
1818 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1822 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1824 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1825 de
.d_name
, strerror(-err
));
1829 l
= kmod_list_append(list
, holder
);
1833 ERR(mod
->ctx
, "out of memory\n");
1834 kmod_module_unref(holder
);
1844 kmod_module_unref_list(list
);
1848 struct kmod_module_section
{
1849 unsigned long address
;
1853 static void kmod_module_section_free(struct kmod_module_section
*section
)
1859 * kmod_module_get_sections:
1862 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1863 * structure contained in this list is internal to libkmod and their fields
1864 * can be obtained by calling kmod_module_section_get_name() and
1865 * kmod_module_section_get_address().
1867 * After use, free the @list by calling kmod_module_section_free_list().
1869 * Returns: a new list of kmod module sections on success or NULL on failure.
1871 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1873 char dname
[PATH_MAX
];
1874 struct kmod_list
*list
= NULL
;
1881 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1885 ERR(mod
->ctx
, "could not open '%s': %s\n",
1886 dname
, strerror(errno
));
1893 struct dirent de
, *entp
;
1894 struct kmod_module_section
*section
;
1895 struct kmod_list
*l
;
1896 unsigned long address
;
1900 err
= readdir_r(d
, &de
, &entp
);
1902 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1903 mod
->name
, strerror(-err
));
1907 if (de
.d_name
[0] == '.') {
1908 if (de
.d_name
[1] == '\0' ||
1909 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1913 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
1915 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1920 err
= read_str_ulong(fd
, &address
, 16);
1923 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1928 namesz
= strlen(de
.d_name
) + 1;
1929 section
= malloc(sizeof(*section
) + namesz
);
1931 if (section
== NULL
) {
1932 ERR(mod
->ctx
, "out of memory\n");
1936 section
->address
= address
;
1937 memcpy(section
->name
, de
.d_name
, namesz
);
1939 l
= kmod_list_append(list
, section
);
1943 ERR(mod
->ctx
, "out of memory\n");
1954 kmod_module_unref_list(list
);
1959 * kmod_module_section_get_module_name:
1960 * @entry: a list entry representing a kmod module section
1962 * Get the name of a kmod module section.
1964 * After use, free the @list by calling kmod_module_section_free_list().
1966 * Returns: the name of this kmod module section on success or NULL on
1967 * failure. The string is owned by the section, do not free it.
1969 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1971 struct kmod_module_section
*section
;
1976 section
= entry
->data
;
1977 return section
->name
;
1981 * kmod_module_section_get_address:
1982 * @entry: a list entry representing a kmod module section
1984 * Get the address of a kmod module section.
1986 * After use, free the @list by calling kmod_module_section_free_list().
1988 * Returns: the address of this kmod module section on success or ULONG_MAX
1991 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1993 struct kmod_module_section
*section
;
1996 return (unsigned long)-1;
1998 section
= entry
->data
;
1999 return section
->address
;
2003 * kmod_module_section_free_list:
2004 * @list: kmod module section list
2006 * Release the resources taken by @list
2008 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
2011 kmod_module_section_free(list
->data
);
2012 list
= kmod_list_remove(list
);
2016 struct kmod_module_info
{
2021 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
2023 struct kmod_module_info
*info
;
2025 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
2029 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
2031 memcpy(info
->key
, key
, keylen
);
2032 info
->key
[keylen
] = '\0';
2033 memcpy(info
->value
, value
, valuelen
);
2034 info
->value
[valuelen
] = '\0';
2038 static void kmod_module_info_free(struct kmod_module_info
*info
)
2044 * kmod_module_get_info:
2046 * @list: where to return list of module information. Use
2047 * kmod_module_info_get_key() and
2048 * kmod_module_info_get_value(). Release this list with
2049 * kmod_module_info_free_list()
2051 * Get a list of entries in ELF section ".modinfo", these contain
2052 * alias, license, depends, vermagic and other keys with respective
2055 * After use, free the @list by calling kmod_module_info_free_list().
2057 * Returns: 0 on success or < 0 otherwise.
2059 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2061 struct kmod_file
*file
;
2062 struct kmod_elf
*elf
;
2067 int i
, count
, ret
= 0;
2069 if (mod
== NULL
|| list
== NULL
)
2072 assert(*list
== NULL
);
2074 path
= kmod_module_get_path(mod
);
2078 file
= kmod_file_open(mod
->ctx
, path
);
2082 size
= kmod_file_get_size(file
);
2083 mem
= kmod_file_get_contents(file
);
2085 elf
= kmod_elf_new(mem
, size
);
2088 goto elf_open_error
;
2091 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2094 goto get_strings_error
;
2097 for (i
= 0; i
< count
; i
++) {
2098 struct kmod_module_info
*info
;
2099 struct kmod_list
*n
;
2100 const char *key
, *value
;
2101 size_t keylen
, valuelen
;
2104 value
= strchr(key
, '=');
2105 if (value
== NULL
) {
2106 keylen
= strlen(key
);
2109 keylen
= value
- key
;
2111 valuelen
= strlen(value
);
2114 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2117 kmod_module_info_free_list(*list
);
2122 n
= kmod_list_append(*list
, info
);
2126 kmod_module_info_free(info
);
2127 kmod_module_info_free_list(*list
);
2138 kmod_elf_unref(elf
);
2140 kmod_file_unref(file
);
2146 * kmod_module_info_get_key:
2147 * @entry: a list entry representing a kmod module info
2149 * Get the key of a kmod module info.
2151 * Returns: the key of this kmod module info on success or NULL on
2152 * failure. The string is owned by the info, do not free it.
2154 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2156 struct kmod_module_info
*info
;
2166 * kmod_module_info_get_value:
2167 * @entry: a list entry representing a kmod module info
2169 * Get the value of a kmod module info.
2171 * Returns: the value of this kmod module info on success or NULL on
2172 * failure. The string is owned by the info, do not free it.
2174 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2176 struct kmod_module_info
*info
;
2186 * kmod_module_info_free_list:
2187 * @list: kmod module info list
2189 * Release the resources taken by @list
2191 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2194 kmod_module_info_free(list
->data
);
2195 list
= kmod_list_remove(list
);
2199 struct kmod_module_version
{
2204 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2206 struct kmod_module_version
*mv
;
2207 size_t symbollen
= strlen(symbol
) + 1;
2209 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2214 memcpy(mv
->symbol
, symbol
, symbollen
);
2218 static void kmod_module_version_free(struct kmod_module_version
*version
)
2224 * kmod_module_get_versions:
2226 * @list: where to return list of module versions. Use
2227 * kmod_module_version_get_symbol() and
2228 * kmod_module_version_get_crc(). Release this list with
2229 * kmod_module_versions_free_list()
2231 * Get a list of entries in ELF section "__versions".
2233 * After use, free the @list by calling kmod_module_versions_free_list().
2235 * Returns: 0 on success or < 0 otherwise.
2237 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2239 struct kmod_file
*file
;
2240 struct kmod_elf
*elf
;
2243 struct kmod_modversion
*versions
;
2245 int i
, count
, ret
= 0;
2247 if (mod
== NULL
|| list
== NULL
)
2250 assert(*list
== NULL
);
2252 path
= kmod_module_get_path(mod
);
2256 file
= kmod_file_open(mod
->ctx
, path
);
2260 size
= kmod_file_get_size(file
);
2261 mem
= kmod_file_get_contents(file
);
2263 elf
= kmod_elf_new(mem
, size
);
2266 goto elf_open_error
;
2269 count
= kmod_elf_get_modversions(elf
, &versions
);
2272 goto get_strings_error
;
2275 for (i
= 0; i
< count
; i
++) {
2276 struct kmod_module_version
*mv
;
2277 struct kmod_list
*n
;
2279 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2282 kmod_module_versions_free_list(*list
);
2287 n
= kmod_list_append(*list
, mv
);
2291 kmod_module_version_free(mv
);
2292 kmod_module_versions_free_list(*list
);
2303 kmod_elf_unref(elf
);
2305 kmod_file_unref(file
);
2311 * kmod_module_versions_get_symbol:
2312 * @entry: a list entry representing a kmod module versions
2314 * Get the symbol of a kmod module versions.
2316 * Returns: the symbol of this kmod module versions on success or NULL
2317 * on failure. The string is owned by the versions, do not free it.
2319 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2321 struct kmod_module_version
*version
;
2326 version
= entry
->data
;
2327 return version
->symbol
;
2331 * kmod_module_version_get_crc:
2332 * @entry: a list entry representing a kmod module version
2334 * Get the crc of a kmod module version.
2336 * Returns: the crc of this kmod module version on success or NULL on
2337 * failure. The string is owned by the version, do not free it.
2339 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2341 struct kmod_module_version
*version
;
2346 version
= entry
->data
;
2347 return version
->crc
;
2351 * kmod_module_versions_free_list:
2352 * @list: kmod module versions list
2354 * Release the resources taken by @list
2356 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2359 kmod_module_version_free(list
->data
);
2360 list
= kmod_list_remove(list
);
2364 struct kmod_module_symbol
{
2369 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2371 struct kmod_module_symbol
*mv
;
2372 size_t symbollen
= strlen(symbol
) + 1;
2374 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2379 memcpy(mv
->symbol
, symbol
, symbollen
);
2383 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2389 * kmod_module_get_symbols:
2391 * @list: where to return list of module symbols. Use
2392 * kmod_module_symbol_get_symbol() and
2393 * kmod_module_symbol_get_crc(). Release this list with
2394 * kmod_module_symbols_free_list()
2396 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2398 * After use, free the @list by calling kmod_module_symbols_free_list().
2400 * Returns: 0 on success or < 0 otherwise.
2402 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2404 struct kmod_file
*file
;
2405 struct kmod_elf
*elf
;
2408 struct kmod_modversion
*symbols
;
2410 int i
, count
, ret
= 0;
2412 if (mod
== NULL
|| list
== NULL
)
2415 assert(*list
== NULL
);
2417 path
= kmod_module_get_path(mod
);
2421 file
= kmod_file_open(mod
->ctx
, path
);
2425 size
= kmod_file_get_size(file
);
2426 mem
= kmod_file_get_contents(file
);
2428 elf
= kmod_elf_new(mem
, size
);
2431 goto elf_open_error
;
2434 count
= kmod_elf_get_symbols(elf
, &symbols
);
2437 goto get_strings_error
;
2440 for (i
= 0; i
< count
; i
++) {
2441 struct kmod_module_symbol
*mv
;
2442 struct kmod_list
*n
;
2444 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2447 kmod_module_symbols_free_list(*list
);
2452 n
= kmod_list_append(*list
, mv
);
2456 kmod_module_symbol_free(mv
);
2457 kmod_module_symbols_free_list(*list
);
2468 kmod_elf_unref(elf
);
2470 kmod_file_unref(file
);
2476 * kmod_module_symbol_get_symbol:
2477 * @entry: a list entry representing a kmod module symbols
2479 * Get the symbol of a kmod module symbols.
2481 * Returns: the symbol of this kmod module symbols on success or NULL
2482 * on failure. The string is owned by the symbols, do not free it.
2484 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2486 struct kmod_module_symbol
*symbol
;
2491 symbol
= entry
->data
;
2492 return symbol
->symbol
;
2496 * kmod_module_symbol_get_crc:
2497 * @entry: a list entry representing a kmod module symbol
2499 * Get the crc of a kmod module symbol.
2501 * Returns: the crc of this kmod module symbol on success or NULL on
2502 * failure. The string is owned by the symbol, do not free it.
2504 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2506 struct kmod_module_symbol
*symbol
;
2511 symbol
= entry
->data
;
2516 * kmod_module_symbols_free_list:
2517 * @list: kmod module symbols list
2519 * Release the resources taken by @list
2521 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2524 kmod_module_symbol_free(list
->data
);
2525 list
= kmod_list_remove(list
);
2529 struct kmod_module_dependency_symbol
{
2535 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2537 struct kmod_module_dependency_symbol
*mv
;
2538 size_t symbollen
= strlen(symbol
) + 1;
2540 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2546 memcpy(mv
->symbol
, symbol
, symbollen
);
2550 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2552 free(dependency_symbol
);
2556 * kmod_module_get_dependency_symbols:
2558 * @list: where to return list of module dependency_symbols. Use
2559 * kmod_module_dependency_symbol_get_symbol() and
2560 * kmod_module_dependency_symbol_get_crc(). Release this list with
2561 * kmod_module_dependency_symbols_free_list()
2563 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2565 * After use, free the @list by calling
2566 * kmod_module_dependency_symbols_free_list().
2568 * Returns: 0 on success or < 0 otherwise.
2570 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2572 struct kmod_file
*file
;
2573 struct kmod_elf
*elf
;
2576 struct kmod_modversion
*symbols
;
2578 int i
, count
, ret
= 0;
2580 if (mod
== NULL
|| list
== NULL
)
2583 assert(*list
== NULL
);
2585 path
= kmod_module_get_path(mod
);
2589 file
= kmod_file_open(mod
->ctx
, path
);
2593 size
= kmod_file_get_size(file
);
2594 mem
= kmod_file_get_contents(file
);
2596 elf
= kmod_elf_new(mem
, size
);
2599 goto elf_open_error
;
2602 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2605 goto get_strings_error
;
2608 for (i
= 0; i
< count
; i
++) {
2609 struct kmod_module_dependency_symbol
*mv
;
2610 struct kmod_list
*n
;
2612 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2617 kmod_module_dependency_symbols_free_list(*list
);
2622 n
= kmod_list_append(*list
, mv
);
2626 kmod_module_dependency_symbol_free(mv
);
2627 kmod_module_dependency_symbols_free_list(*list
);
2638 kmod_elf_unref(elf
);
2640 kmod_file_unref(file
);
2646 * kmod_module_dependency_symbol_get_symbol:
2647 * @entry: a list entry representing a kmod module dependency_symbols
2649 * Get the dependency symbol of a kmod module
2651 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2652 * on failure. The string is owned by the dependency_symbols, do not free it.
2654 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2656 struct kmod_module_dependency_symbol
*dependency_symbol
;
2661 dependency_symbol
= entry
->data
;
2662 return dependency_symbol
->symbol
;
2666 * kmod_module_dependency_symbol_get_crc:
2667 * @entry: a list entry representing a kmod module dependency_symbol
2669 * Get the crc of a kmod module dependency_symbol.
2671 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2672 * failure. The string is owned by the dependency_symbol, do not free it.
2674 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2676 struct kmod_module_dependency_symbol
*dependency_symbol
;
2681 dependency_symbol
= entry
->data
;
2682 return dependency_symbol
->crc
;
2686 * kmod_module_dependency_symbol_get_bind:
2687 * @entry: a list entry representing a kmod module dependency_symbol
2689 * Get the bind type of a kmod module dependency_symbol.
2691 * Returns: the bind of this kmod module dependency_symbol on success
2692 * or < 0 on failure.
2694 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2696 struct kmod_module_dependency_symbol
*dependency_symbol
;
2701 dependency_symbol
= entry
->data
;
2702 return dependency_symbol
->bind
;
2706 * kmod_module_dependency_symbols_free_list:
2707 * @list: kmod module dependency_symbols list
2709 * Release the resources taken by @list
2711 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2714 kmod_module_dependency_symbol_free(list
->data
);
2715 list
= kmod_list_remove(list
);