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 static inline const char *path_join(const char *path
, size_t prefixlen
,
93 pathlen
= strlen(path
);
94 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
97 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
101 static inline bool module_is_inkernel(struct kmod_module
*mod
)
103 int state
= kmod_module_get_initstate(mod
);
104 if (state
== KMOD_MODULE_LIVE
||
105 state
== KMOD_MODULE_COMING
||
106 state
== KMOD_MODULE_BUILTIN
)
112 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
114 struct kmod_ctx
*ctx
= mod
->ctx
;
115 struct kmod_list
*list
= NULL
;
124 assert(mod
->dep
== NULL
);
125 mod
->init
.dep
= true;
127 p
= strchr(line
, ':');
132 dirname
= kmod_get_dirname(mod
->ctx
);
133 dirnamelen
= strlen(dirname
);
134 if (dirnamelen
+ 2 >= PATH_MAX
)
137 memcpy(buf
, dirname
, dirnamelen
);
138 buf
[dirnamelen
] = '/';
140 buf
[dirnamelen
] = '\0';
142 if (mod
->path
== NULL
) {
143 const char *str
= path_join(line
, dirnamelen
, buf
);
146 mod
->path
= strdup(str
);
147 if (mod
->path
== NULL
)
152 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
153 p
= strtok_r(NULL
, " \t", &saveptr
)) {
154 struct kmod_module
*depmod
;
157 path
= path_join(p
, dirnamelen
, buf
);
159 ERR(ctx
, "could not join path '%s' and '%s'.\n",
164 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
166 ERR(ctx
, "ctx=%p path=%s error=%s\n",
167 ctx
, path
, strerror(-err
));
171 DBG(ctx
, "add dep: %s\n", path
);
173 list
= kmod_list_prepend(list
, depmod
);
177 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
184 kmod_module_unref_list(list
);
185 mod
->init
.dep
= false;
189 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
191 mod
->visited
= visited
;
195 * Memory layout with alias:
197 * struct kmod_module {
202 * name <----------' | |
203 * alias <-----------' |
204 * name\alias <--------'
206 * Memory layout without alias:
208 * struct kmod_module {
210 * alias -----|----> NULL
213 * name <----------'-'
215 * @key is "name\alias" or "name" (in which case alias == NULL)
217 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
218 const char *name
, size_t namelen
,
219 const char *alias
, size_t aliaslen
,
220 struct kmod_module
**mod
)
222 struct kmod_module
*m
;
225 m
= kmod_pool_get_module(ctx
, key
);
227 *mod
= kmod_module_ref(m
);
234 keylen
= namelen
+ aliaslen
+ 1;
236 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
242 memset(m
, 0, sizeof(*m
));
244 m
->ctx
= kmod_ref(ctx
);
245 m
->name
= (char *)m
+ sizeof(*m
);
246 memcpy(m
->name
, key
, keylen
+ 1);
248 m
->hashkey
= m
->name
;
251 m
->name
[namelen
] = '\0';
252 m
->alias
= m
->name
+ namelen
+ 1;
253 m
->hashkey
= m
->name
+ keylen
+ 1;
254 memcpy(m
->hashkey
, key
, keylen
+ 1);
258 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
265 * kmod_module_new_from_name:
266 * @ctx: kmod library context
267 * @name: name of the module
268 * @mod: where to save the created struct kmod_module
270 * Create a new struct kmod_module using the module name. @name can not be an
271 * alias, file name or anything else; it must be a module name. There's no
272 * check if the module exists in the system.
274 * This function is also used internally by many others that return a new
275 * struct kmod_module or a new list of modules.
277 * The initial refcount is 1, and needs to be decremented to release the
278 * resources of the kmod_module. Since libkmod keeps track of all
279 * kmod_modules created, they are all released upon @ctx destruction too. Do
280 * not unref @ctx before all the desired operations with the returned
281 * kmod_module are done.
283 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
284 * module name or if memory allocation failed.
286 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
288 struct kmod_module
**mod
)
291 char name_norm
[PATH_MAX
];
293 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
296 modname_normalize(name
, name_norm
, &namelen
);
298 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
301 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
302 const char *name
, struct kmod_module
**mod
)
306 size_t namelen
= strlen(name
);
307 size_t aliaslen
= strlen(alias
);
309 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
310 return -ENAMETOOLONG
;
312 memcpy(key
, name
, namelen
);
313 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
316 err
= kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
324 * kmod_module_new_from_path:
325 * @ctx: kmod library context
326 * @path: path where to find the given module
327 * @mod: where to save the created struct kmod_module
329 * Create a new struct kmod_module using the module path. @path must be an
330 * existent file with in the filesystem and must be accessible to libkmod.
332 * The initial refcount is 1, and needs to be decremented to release the
333 * resources of the kmod_module. Since libkmod keeps track of all
334 * kmod_modules created, they are all released upon @ctx destruction too. Do
335 * not unref @ctx before all the desired operations with the returned
336 * kmod_module are done.
338 * If @path is relative, it's treated as relative to the current working
339 * directory. Otherwise, give an absolute path.
341 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
342 * it's not a valid file for a kmod_module or if memory allocation failed.
344 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
346 struct kmod_module
**mod
)
348 struct kmod_module
*m
;
355 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
358 abspath
= path_make_absolute_cwd(path
);
359 if (abspath
== NULL
) {
360 DBG(ctx
, "no absolute path for %s\n", path
);
364 err
= stat(abspath
, &st
);
367 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
372 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
373 DBG(ctx
, "could not get modname from path %s\n", path
);
378 m
= kmod_pool_get_module(ctx
, name
);
382 else if (streq(m
->path
, abspath
))
385 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
386 name
, abspath
, m
->path
);
391 *mod
= kmod_module_ref(m
);
395 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
409 * Drop a reference of the kmod module. If the refcount reaches zero, its
410 * resources are released.
412 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
413 * returns the passed @mod with its refcount decremented.
415 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
420 if (--mod
->refcount
> 0)
423 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
425 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
426 kmod_module_unref_list(mod
->dep
);
427 kmod_unref(mod
->ctx
);
438 * Take a reference of the kmod module, incrementing its refcount.
440 * Returns: the passed @module with its refcount incremented.
442 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
452 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
456 if (*(_list) != NULL) \
461 * kmod_module_new_from_lookup:
462 * @ctx: kmod library context
463 * @given_alias: alias to look for
464 * @list: an empty list where to save the list of modules matching
467 * Create a new list of kmod modules using an alias or module name and lookup
468 * libkmod's configuration files and indexes in order to find the module.
469 * Once it's found in one of the places, it stops searching and create the
470 * list of modules that is saved in @list.
472 * The search order is: 1. aliases in configuration file; 2. module names in
473 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
474 * in modules.alias index.
476 * The initial refcount is 1, and needs to be decremented to release the
477 * resources of the kmod_module. The returned @list must be released by
478 * calling kmod_module_unref_list(). Since libkmod keeps track of all
479 * kmod_modules created, they are all released upon @ctx destruction too. Do
480 * not unref @ctx before all the desired operations with the returned list are
483 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
484 * methods failed, which is basically due to memory allocation fail. If module
485 * is not found, it still returns 0, but @list is an empty list.
487 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
488 const char *given_alias
,
489 struct kmod_list
**list
)
492 char alias
[PATH_MAX
];
494 if (ctx
== NULL
|| given_alias
== NULL
)
497 if (list
== NULL
|| *list
!= NULL
) {
498 ERR(ctx
, "An empty list is needed to create lookup\n");
502 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
503 DBG(ctx
, "invalid alias: %s\n", given_alias
);
507 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
509 /* Aliases from config file override all the others */
510 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
511 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
513 DBG(ctx
, "lookup modules.dep %s\n", alias
);
514 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
515 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
517 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
518 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
519 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
521 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
522 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
523 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
525 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
526 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
527 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
530 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
533 DBG(ctx
, "Failed to lookup %s\n", alias
);
534 kmod_module_unref_list(*list
);
538 #undef CHECK_ERR_AND_FINISH
541 * kmod_module_unref_list:
542 * @list: list of kmod modules
544 * Drop a reference of each kmod module in @list and releases the resources
545 * taken by the list itself.
547 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
548 * returns the passed @mod with its refcount decremented.
550 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
552 for (; list
!= NULL
; list
= kmod_list_remove(list
))
553 kmod_module_unref(list
->data
);
559 * kmod_module_get_filtered_blacklist:
560 * @ctx: kmod library context
561 * @input: list of kmod_module to be filtered with blacklist
562 * @output: where to save the new list
564 * Given a list @input, this function filter it out with config's blacklist
565 * ans save it in @output.
567 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
570 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
571 const struct kmod_list
*input
,
572 struct kmod_list
**output
)
574 const struct kmod_list
*li
;
575 const struct kmod_list
*blacklist
;
577 if (ctx
== NULL
|| output
== NULL
)
584 blacklist
= kmod_get_blacklists(ctx
);
585 kmod_list_foreach(li
, input
) {
586 struct kmod_module
*mod
= li
->data
;
587 const struct kmod_list
*lb
;
588 struct kmod_list
*node
;
589 bool filtered
= false;
591 kmod_list_foreach(lb
, blacklist
) {
592 const char *name
= lb
->data
;
594 if (streq(name
, mod
->name
)) {
603 node
= kmod_list_append(*output
, mod
);
608 kmod_module_ref(mod
);
614 kmod_module_unref_list(*output
);
619 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
621 if (!mod
->init
.dep
) {
623 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
628 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
639 * kmod_module_get_dependencies:
642 * Search the modules.dep index to find the dependencies of the given @mod.
643 * The result is cached in @mod, so subsequent calls to this function will
644 * return the already searched list of modules.
646 * Returns: NULL on failure or if there are any dependencies. Otherwise it
647 * returns a list of kmod modules that can be released by calling
648 * kmod_module_unref_list().
650 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
652 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
657 module_get_dependencies_noref(mod
);
659 kmod_list_foreach(l
, mod
->dep
) {
660 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
662 kmod_module_unref(l
->data
);
672 ERR(mod
->ctx
, "out of memory\n");
673 kmod_module_unref_list(list_new
);
678 * kmod_module_get_module:
679 * @entry: an entry in a list of kmod modules.
681 * Get the kmod module of this @entry in the list, increasing its refcount.
682 * After it's used, unref it. Since the refcount is incremented upon return,
683 * you still have to call kmod_module_unref_list() to release the list of kmod
686 * Returns: NULL on failure or the kmod_module contained in this list entry
687 * with its refcount incremented.
689 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
694 return kmod_module_ref(entry
->data
);
698 * kmod_module_get_name:
701 * Get the name of this kmod module. Name is always available, independently
702 * if it was created by kmod_module_new_from_name() or another function and
703 * it's always normalized (dashes are replaced with underscores).
705 * Returns: the name of this kmod module.
707 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
716 * kmod_module_get_path:
719 * Get the path of this kmod module. If this kmod module was not created by
720 * path, it can search the modules.dep index in order to find out the module
721 * under context's dirname.
723 * Returns: the path of this kmod module or NULL if such information is not
726 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
733 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
735 if (mod
->path
!= NULL
)
741 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
745 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
752 extern long delete_module(const char *name
, unsigned int flags
);
755 * kmod_module_remove_module:
757 * @flags: flags to pass to Linux kernel when removing the module
759 * Remove a module from Linux kernel.
761 * Returns: 0 on success or < 0 on failure.
763 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
771 /* Filter out other flags */
772 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
774 err
= delete_module(mod
->name
, flags
);
777 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
783 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
786 * kmod_module_insert_module:
788 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
789 * behavior of this function.
790 * @options: module's options to pass to Linux Kernel.
792 * Insert a module in Linux kernel. It opens the file pointed by @mod,
793 * mmap'ing it and passing to kernel.
795 * Returns: 0 on success or < 0 on failure. If module is already loaded it
798 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
805 struct kmod_file
*file
;
806 struct kmod_elf
*elf
= NULL
;
808 const char *args
= options
? options
: "";
813 path
= kmod_module_get_path(mod
);
815 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
819 file
= kmod_file_open(mod
->ctx
, path
);
825 size
= kmod_file_get_size(file
);
826 mem
= kmod_file_get_contents(file
);
828 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
829 elf
= kmod_elf_new(mem
, size
);
835 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
836 err
= kmod_elf_strip_section(elf
, "__versions");
838 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
841 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
842 err
= kmod_elf_strip_vermagic(elf
);
844 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
847 mem
= kmod_elf_get_memory(elf
);
850 err
= init_module(mem
, size
, args
);
853 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
859 kmod_file_unref(file
);
864 static bool module_is_blacklisted(struct kmod_module
*mod
)
866 struct kmod_ctx
*ctx
= mod
->ctx
;
867 const struct kmod_list
*bl
= kmod_get_blacklists(ctx
);
868 const struct kmod_list
*l
;
870 kmod_list_foreach(l
, bl
) {
871 const char *modname
= kmod_blacklist_get_modname(l
);
873 if (streq(modname
, mod
->name
))
880 static int command_do(struct kmod_module
*mod
, const char *type
,
883 const char *modname
= kmod_module_get_name(mod
);
886 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
888 setenv("MODPROBE_MODULE", modname
, 1);
890 unsetenv("MODPROBE_MODULE");
892 if (err
== -1 || WEXITSTATUS(err
)) {
893 ERR(mod
->ctx
, "Error running %s command for %s\n",
896 err
= -WEXITSTATUS(err
);
902 struct probe_insert_cb
{
903 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
907 static int module_do_install_commands(struct kmod_module
*mod
,
909 struct probe_insert_cb
*cb
)
911 const char *command
= kmod_module_get_install_commands(mod
);
914 size_t cmdlen
, options_len
, varlen
;
921 options_len
= strlen(options
);
922 cmdlen
= strlen(command
);
923 varlen
= sizeof("$CMDLINE_OPTS") - 1;
925 cmd
= memdup(command
, cmdlen
+ 1);
929 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
930 size_t prefixlen
= p
- cmd
;
931 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
932 size_t slen
= cmdlen
- varlen
+ options_len
;
933 char *suffix
= p
+ varlen
;
934 char *s
= malloc(slen
+ 1);
939 memcpy(s
, cmd
, p
- cmd
);
940 memcpy(s
+ prefixlen
, options
, options_len
);
941 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
949 if (cb
->run_install
!= NULL
)
950 err
= cb
->run_install(mod
, cmd
, cb
->data
);
952 err
= command_do(mod
, "install", cmd
);
959 static char *module_options_concat(const char *opt
, const char *xopt
)
961 // TODO: we might need to check if xopt overrides options on opt
962 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
963 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
966 if (optlen
== 0 && xoptlen
== 0)
969 r
= malloc(optlen
+ xoptlen
+ 2);
972 memcpy(r
, opt
, optlen
);
978 memcpy(r
+ optlen
, xopt
, xoptlen
);
980 r
[optlen
+ xoptlen
] = '\0';
985 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
987 struct kmod_list
**list
);
990 static int __kmod_module_fill_softdep(struct kmod_module
*mod
,
991 struct kmod_list
**list
)
993 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
996 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
998 ERR(mod
->ctx
, "could not get softdep: %s", strerror(-err
));
1002 kmod_list_foreach(l
, pre
) {
1003 struct kmod_module
*m
= l
->data
;
1004 err
= __kmod_module_get_probe_list(m
, false, list
);
1009 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1011 kmod_module_unref(mod
);
1016 mod
->visited
= true;
1017 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
1019 kmod_list_foreach(l
, post
) {
1020 struct kmod_module
*m
= l
->data
;
1021 err
= __kmod_module_get_probe_list(m
, false, list
);
1027 kmod_module_unref_list(pre
);
1028 kmod_module_unref_list(post
);
1034 static int __kmod_module_get_probe_list(struct kmod_module
*mod
,
1036 struct kmod_list
**list
)
1038 struct kmod_list
*dep
, *l
;
1042 DBG(mod
->ctx
, "Ignore module '%s': already visited\n",
1047 dep
= kmod_module_get_dependencies(mod
);
1048 kmod_list_foreach(l
, dep
) {
1049 struct kmod_module
*m
= l
->data
;
1050 err
= __kmod_module_fill_softdep(m
, list
);
1056 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
1058 kmod_module_unref(mod
);
1063 mod
->ignorecmd
= true;
1065 err
= __kmod_module_fill_softdep(mod
, list
);
1068 kmod_module_unref_list(dep
);
1072 static int kmod_module_get_probe_list(struct kmod_module
*mod
,
1074 struct kmod_list
**list
)
1078 assert(mod
!= NULL
);
1079 assert(list
!= NULL
&& *list
== NULL
);
1082 * Make sure we don't get screwed by previous calls to this function
1084 kmod_set_modules_visited(mod
->ctx
, false);
1086 err
= __kmod_module_get_probe_list(mod
, ignorecmd
, list
);
1088 kmod_module_unref_list(*list
);
1096 * kmod_module_probe_insert_module:
1098 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1099 * behavior of this function.
1100 * @extra_options: module's options to pass to Linux Kernel. It applies only
1101 * to @mod, not to its dependencies.
1102 * @run_install: function to run when @mod is backed by an install command.
1103 * @data: data to give back to @run_install callback
1104 * @print_action: function to call with the action being taken (install or
1105 * insmod). It's useful for tools like modprobe when running with verbose
1106 * output or in dry-run mode.
1108 * Insert a module in Linux kernel resolving dependencies, soft dependencies,
1109 * install commands and applying blacklist.
1111 * If @run_install is NULL, this function will fork and exec by calling
1112 * system(3). Don't pass a NULL argument in @run_install if your binary is
1113 * setuid/setgid (see warning in system(3)). If you need control over the
1114 * execution of an install command, give a callback function instead.
1116 * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1119 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1120 unsigned int flags
, const char *extra_options
,
1121 int (*run_install
)(struct kmod_module
*m
,
1122 const char *cmd
, void *data
),
1124 void (*print_action
)(struct kmod_module
*m
,
1126 const char *options
))
1128 struct kmod_list
*list
= NULL
, *l
;
1129 struct probe_insert_cb cb
;
1135 if (module_is_inkernel(mod
)) {
1136 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1142 err
= flags
& (KMOD_PROBE_APPLY_BLACKLIST
|
1143 KMOD_PROBE_APPLY_BLACKLIST_ALL
);
1145 if (module_is_blacklisted(mod
))
1149 err
= kmod_module_get_probe_list(mod
,
1150 !!(flags
& KMOD_PROBE_IGNORE_COMMAND
), &list
);
1154 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1155 struct kmod_list
*filtered
= NULL
;
1157 err
= kmod_module_get_filtered_blacklist(mod
->ctx
,
1162 kmod_module_unref_list(list
);
1163 if (filtered
== NULL
)
1164 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1169 cb
.run_install
= run_install
;
1170 cb
.data
= (void *) data
;
1172 kmod_list_foreach(l
, list
) {
1173 struct kmod_module
*m
= l
->data
;
1174 const char *moptions
= kmod_module_get_options(m
);
1175 const char *cmd
= kmod_module_get_install_commands(m
);
1176 char *options
= module_options_concat(moptions
,
1177 m
== mod
? extra_options
: NULL
);
1179 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1180 if (print_action
!= NULL
)
1181 print_action(m
, true, options
?: "");
1183 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1184 err
= module_do_install_commands(m
, options
,
1187 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
)
1188 && module_is_inkernel(m
)) {
1189 DBG(mod
->ctx
, "Ignoring module '%s': "
1190 "already loaded\n", m
->name
);
1194 if (print_action
!= NULL
)
1195 print_action(m
, false, options
?: "");
1197 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1198 err
= kmod_module_insert_module(m
, flags
,
1206 * Treat "already loaded" error. If we were told to stop on
1207 * already loaded and the module being loaded is not a softdep
1208 * or dep, bail out. Otherwise, just ignore and continue.
1210 * We need to check here because of race conditions. We
1211 * checked first if module was already loaded but it may have
1212 * been loaded between the check and the moment we try to
1215 if (err
== -EEXIST
&& m
== mod
&&
1216 (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1225 kmod_module_unref_list(list
);
1230 * kmod_module_get_options:
1233 * Get options of this kmod module. Options come from the configuration file
1234 * and are cached in @mod. The first call to this function will search for
1235 * this module in configuration and subsequent calls return the cached string.
1237 * Returns: a string with all the options separated by spaces. This string is
1238 * owned by @mod, do not free it.
1240 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1245 if (!mod
->init
.options
) {
1247 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1248 const struct kmod_list
*l
, *ctx_options
;
1252 ctx_options
= kmod_get_options(mod
->ctx
);
1254 kmod_list_foreach(l
, ctx_options
) {
1255 const char *modname
= kmod_option_get_modname(l
);
1260 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1261 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1262 streq(modname
, mod
->alias
))))
1265 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1266 str
= kmod_option_get_options(l
);
1271 tmp
= realloc(opts
, optslen
+ len
+ 2);
1280 opts
[optslen
] = ' ';
1284 memcpy(opts
+ optslen
, str
, len
);
1286 opts
[optslen
] = '\0';
1289 m
->init
.options
= true;
1293 return mod
->options
;
1296 ERR(mod
->ctx
, "out of memory\n");
1301 * kmod_module_get_install_commands:
1304 * Get install commands for this kmod module. Install commands come from the
1305 * configuration file and are cached in @mod. The first call to this function
1306 * will search for this module in configuration and subsequent calls return
1307 * the cached string. The install commands are returned as they were in the
1308 * configuration, concatenated by ';'. No other processing is made in this
1311 * Returns: a string with all install commands separated by semicolons. This
1312 * string is owned by @mod, do not free it.
1314 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1319 if (!mod
->init
.install_commands
) {
1321 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1322 const struct kmod_list
*l
, *ctx_install_commands
;
1324 ctx_install_commands
= kmod_get_install_commands(mod
->ctx
);
1326 kmod_list_foreach(l
, ctx_install_commands
) {
1327 const char *modname
= kmod_command_get_modname(l
);
1329 if (fnmatch(modname
, mod
->name
, 0) != 0)
1332 m
->install_commands
= kmod_command_get_command(l
);
1335 * find only the first command, as modprobe from
1336 * module-init-tools does
1341 m
->init
.install_commands
= true;
1344 return mod
->install_commands
;
1347 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1349 mod
->init
.install_commands
= true;
1350 mod
->install_commands
= cmd
;
1353 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1355 struct kmod_list
*ret
= NULL
;
1358 for (i
= 0; i
< count
; i
++) {
1359 const char *depname
= array
[i
];
1360 struct kmod_list
*lst
= NULL
;
1363 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1365 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1367 } else if (lst
!= NULL
)
1368 ret
= kmod_list_append_list(ret
, lst
);
1374 * kmod_module_get_softdeps:
1376 * @pre: where to save the list of preceding soft dependencies.
1377 * @post: where to save the list of post soft dependencies.
1379 * Get soft dependencies for this kmod module. Soft dependencies come
1380 * from configuration file and are not cached in @mod because it may include
1381 * dependency cycles that would make we leak kmod_module. Any call
1382 * to this function will search for this module in configuration, allocate a
1383 * list and return the result.
1385 * Both @pre and @post are newly created list of kmod_module and
1386 * should be unreferenced with kmod_module_unref_list().
1388 * Returns: 0 on success or < 0 otherwise.
1390 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1391 struct kmod_list
**pre
,
1392 struct kmod_list
**post
)
1394 const struct kmod_list
*l
, *ctx_softdeps
;
1396 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1399 assert(*pre
== NULL
);
1400 assert(*post
== NULL
);
1402 ctx_softdeps
= kmod_get_softdeps(mod
->ctx
);
1404 kmod_list_foreach(l
, ctx_softdeps
) {
1405 const char *modname
= kmod_softdep_get_name(l
);
1406 const char * const *array
;
1409 if (fnmatch(modname
, mod
->name
, 0) != 0)
1412 array
= kmod_softdep_get_pre(l
, &count
);
1413 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1414 array
= kmod_softdep_get_post(l
, &count
);
1415 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1418 * find only the first command, as modprobe from
1419 * module-init-tools does
1428 * kmod_module_get_remove_commands:
1431 * Get remove commands for this kmod module. Remove commands come from the
1432 * configuration file and are cached in @mod. The first call to this function
1433 * will search for this module in configuration and subsequent calls return
1434 * the cached string. The remove commands are returned as they were in the
1435 * configuration, concatenated by ';'. No other processing is made in this
1438 * Returns: a string with all remove commands separated by semicolons. This
1439 * string is owned by @mod, do not free it.
1441 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1446 if (!mod
->init
.remove_commands
) {
1448 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1449 const struct kmod_list
*l
, *ctx_remove_commands
;
1451 ctx_remove_commands
= kmod_get_remove_commands(mod
->ctx
);
1453 kmod_list_foreach(l
, ctx_remove_commands
) {
1454 const char *modname
= kmod_command_get_modname(l
);
1456 if (fnmatch(modname
, mod
->name
, 0) != 0)
1459 m
->remove_commands
= kmod_command_get_command(l
);
1462 * find only the first command, as modprobe from
1463 * module-init-tools does
1468 m
->init
.remove_commands
= true;
1471 return mod
->remove_commands
;
1474 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1476 mod
->init
.remove_commands
= true;
1477 mod
->remove_commands
= cmd
;
1481 * SECTION:libkmod-loaded
1482 * @short_description: currently loaded modules
1484 * Information about currently loaded modules, as reported by Linux kernel.
1485 * These information are not cached by libkmod and are always read from /sys
1486 * and /proc/modules.
1490 * kmod_module_new_from_loaded:
1491 * @ctx: kmod library context
1492 * @list: where to save the list of loaded modules
1494 * Create a new list of kmod modules with all modules currently loaded in
1495 * kernel. It uses /proc/modules to get the names of loaded modules and to
1496 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1497 * They are put are put in @list in no particular order.
1499 * The initial refcount is 1, and needs to be decremented to release the
1500 * resources of the kmod_module. The returned @list must be released by
1501 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1502 * kmod_modules created, they are all released upon @ctx destruction too. Do
1503 * not unref @ctx before all the desired operations with the returned list are
1506 * Returns: 0 on success or < 0 on error.
1508 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1509 struct kmod_list
**list
)
1511 struct kmod_list
*l
= NULL
;
1515 if (ctx
== NULL
|| list
== NULL
)
1518 fp
= fopen("/proc/modules", "re");
1521 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1525 while (fgets(line
, sizeof(line
), fp
)) {
1526 struct kmod_module
*m
;
1527 struct kmod_list
*node
;
1529 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1531 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1533 ERR(ctx
, "could not get module from name '%s': %s\n",
1534 name
, strerror(-err
));
1538 node
= kmod_list_append(l
, m
);
1542 ERR(ctx
, "out of memory\n");
1543 kmod_module_unref(m
);
1554 * kmod_module_initstate_str:
1555 * @state: the state as returned by kmod_module_get_initstate()
1557 * Translate a initstate to a string.
1559 * Returns: the string associated to the @state. This string is statically
1560 * allocated, do not free it.
1562 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1565 case KMOD_MODULE_BUILTIN
:
1567 case KMOD_MODULE_LIVE
:
1569 case KMOD_MODULE_COMING
:
1571 case KMOD_MODULE_GOING
:
1579 * kmod_module_get_initstate:
1582 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1585 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1587 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1589 char path
[PATH_MAX
], buf
[32];
1590 int fd
, err
, pathlen
;
1595 pathlen
= snprintf(path
, sizeof(path
),
1596 "/sys/module/%s/initstate", mod
->name
);
1597 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1601 DBG(mod
->ctx
, "could not open '%s': %s\n",
1602 path
, strerror(-err
));
1604 if (pathlen
> (int)sizeof("/initstate") - 1) {
1606 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1607 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1608 return KMOD_MODULE_BUILTIN
;
1611 DBG(mod
->ctx
, "could not open '%s': %s\n",
1612 path
, strerror(-err
));
1616 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1619 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1620 path
, strerror(-err
));
1624 if (streq(buf
, "live\n"))
1625 return KMOD_MODULE_LIVE
;
1626 else if (streq(buf
, "coming\n"))
1627 return KMOD_MODULE_COMING
;
1628 else if (streq(buf
, "going\n"))
1629 return KMOD_MODULE_GOING
;
1631 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1636 * kmod_module_get_size:
1639 * Get the size of this kmod module as returned by Linux kernel. It reads the
1640 * file /proc/modules to search for this module and get its size.
1642 * Returns: the size of this kmod module.
1644 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1646 // FIXME TODO: this should be available from /sys/module/foo
1650 long size
= -ENOENT
;
1655 fp
= fopen("/proc/modules", "re");
1659 "could not open /proc/modules: %s\n", strerror(errno
));
1663 while (fgets(line
, sizeof(line
), fp
)) {
1664 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1668 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1671 tok
= strtok_r(NULL
, " \t", &saveptr
);
1674 "invalid line format at /proc/modules:%d\n", lineno
);
1678 value
= strtol(tok
, &endptr
, 10);
1679 if (endptr
== tok
|| *endptr
!= '\0') {
1681 "invalid line format at /proc/modules:%d\n", lineno
);
1693 * kmod_module_get_refcnt:
1696 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1699 * Returns: 0 on success or < 0 on failure.
1701 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1703 char path
[PATH_MAX
];
1710 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1711 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1714 ERR(mod
->ctx
, "could not open '%s': %s\n",
1715 path
, strerror(errno
));
1719 err
= read_str_long(fd
, &refcnt
, 10);
1722 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1723 path
, strerror(-err
));
1731 * kmod_module_get_holders:
1734 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1735 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1737 * Returns: a new list of kmod modules on success or NULL on failure.
1739 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1741 char dname
[PATH_MAX
];
1742 struct kmod_list
*list
= NULL
;
1748 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1752 ERR(mod
->ctx
, "could not open '%s': %s\n",
1753 dname
, strerror(errno
));
1758 struct dirent de
, *entp
;
1759 struct kmod_module
*holder
;
1760 struct kmod_list
*l
;
1763 err
= readdir_r(d
, &de
, &entp
);
1765 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1766 mod
->name
, strerror(-err
));
1773 if (de
.d_name
[0] == '.') {
1774 if (de
.d_name
[1] == '\0' ||
1775 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1779 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1781 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1782 de
.d_name
, strerror(-err
));
1786 l
= kmod_list_append(list
, holder
);
1790 ERR(mod
->ctx
, "out of memory\n");
1791 kmod_module_unref(holder
);
1801 kmod_module_unref_list(list
);
1805 struct kmod_module_section
{
1806 unsigned long address
;
1810 static void kmod_module_section_free(struct kmod_module_section
*section
)
1816 * kmod_module_get_sections:
1819 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1820 * structure contained in this list is internal to libkmod and their fields
1821 * can be obtained by calling kmod_module_section_get_name() and
1822 * kmod_module_section_get_address().
1824 * After use, free the @list by calling kmod_module_section_free_list().
1826 * Returns: a new list of kmod module sections on success or NULL on failure.
1828 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1830 char dname
[PATH_MAX
];
1831 struct kmod_list
*list
= NULL
;
1838 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1842 ERR(mod
->ctx
, "could not open '%s': %s\n",
1843 dname
, strerror(errno
));
1850 struct dirent de
, *entp
;
1851 struct kmod_module_section
*section
;
1852 struct kmod_list
*l
;
1853 unsigned long address
;
1857 err
= readdir_r(d
, &de
, &entp
);
1859 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1860 mod
->name
, strerror(-err
));
1864 if (de
.d_name
[0] == '.') {
1865 if (de
.d_name
[1] == '\0' ||
1866 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1870 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
1872 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1877 err
= read_str_ulong(fd
, &address
, 16);
1880 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1885 namesz
= strlen(de
.d_name
) + 1;
1886 section
= malloc(sizeof(*section
) + namesz
);
1888 if (section
== NULL
) {
1889 ERR(mod
->ctx
, "out of memory\n");
1893 section
->address
= address
;
1894 memcpy(section
->name
, de
.d_name
, namesz
);
1896 l
= kmod_list_append(list
, section
);
1900 ERR(mod
->ctx
, "out of memory\n");
1911 kmod_module_unref_list(list
);
1916 * kmod_module_section_get_module_name:
1917 * @entry: a list entry representing a kmod module section
1919 * Get the name of a kmod module section.
1921 * After use, free the @list by calling kmod_module_section_free_list().
1923 * Returns: the name of this kmod module section on success or NULL on
1924 * failure. The string is owned by the section, do not free it.
1926 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1928 struct kmod_module_section
*section
;
1933 section
= entry
->data
;
1934 return section
->name
;
1938 * kmod_module_section_get_address:
1939 * @entry: a list entry representing a kmod module section
1941 * Get the address of a kmod module section.
1943 * After use, free the @list by calling kmod_module_section_free_list().
1945 * Returns: the address of this kmod module section on success or ULONG_MAX
1948 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1950 struct kmod_module_section
*section
;
1953 return (unsigned long)-1;
1955 section
= entry
->data
;
1956 return section
->address
;
1960 * kmod_module_section_free_list:
1961 * @list: kmod module section list
1963 * Release the resources taken by @list
1965 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
1968 kmod_module_section_free(list
->data
);
1969 list
= kmod_list_remove(list
);
1973 struct kmod_module_info
{
1978 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
1980 struct kmod_module_info
*info
;
1982 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
1986 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
1988 memcpy(info
->key
, key
, keylen
);
1989 info
->key
[keylen
] = '\0';
1990 memcpy(info
->value
, value
, valuelen
);
1991 info
->value
[valuelen
] = '\0';
1995 static void kmod_module_info_free(struct kmod_module_info
*info
)
2001 * kmod_module_get_info:
2003 * @list: where to return list of module information. Use
2004 * kmod_module_info_get_key() and
2005 * kmod_module_info_get_value(). Release this list with
2006 * kmod_module_info_free_list()
2008 * Get a list of entries in ELF section ".modinfo", these contain
2009 * alias, license, depends, vermagic and other keys with respective
2012 * After use, free the @list by calling kmod_module_info_free_list().
2014 * Returns: 0 on success or < 0 otherwise.
2016 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
2018 struct kmod_file
*file
;
2019 struct kmod_elf
*elf
;
2024 int i
, count
, ret
= 0;
2026 if (mod
== NULL
|| list
== NULL
)
2029 assert(*list
== NULL
);
2031 path
= kmod_module_get_path(mod
);
2035 file
= kmod_file_open(mod
->ctx
, path
);
2039 size
= kmod_file_get_size(file
);
2040 mem
= kmod_file_get_contents(file
);
2042 elf
= kmod_elf_new(mem
, size
);
2045 goto elf_open_error
;
2048 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
2051 goto get_strings_error
;
2054 for (i
= 0; i
< count
; i
++) {
2055 struct kmod_module_info
*info
;
2056 struct kmod_list
*n
;
2057 const char *key
, *value
;
2058 size_t keylen
, valuelen
;
2061 value
= strchr(key
, '=');
2062 if (value
== NULL
) {
2063 keylen
= strlen(key
);
2066 keylen
= value
- key
;
2068 valuelen
= strlen(value
);
2071 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
2074 kmod_module_info_free_list(*list
);
2079 n
= kmod_list_append(*list
, info
);
2083 kmod_module_info_free(info
);
2084 kmod_module_info_free_list(*list
);
2095 kmod_elf_unref(elf
);
2097 kmod_file_unref(file
);
2103 * kmod_module_info_get_key:
2104 * @entry: a list entry representing a kmod module info
2106 * Get the key of a kmod module info.
2108 * Returns: the key of this kmod module info on success or NULL on
2109 * failure. The string is owned by the info, do not free it.
2111 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2113 struct kmod_module_info
*info
;
2123 * kmod_module_info_get_value:
2124 * @entry: a list entry representing a kmod module info
2126 * Get the value of a kmod module info.
2128 * Returns: the value of this kmod module info on success or NULL on
2129 * failure. The string is owned by the info, do not free it.
2131 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2133 struct kmod_module_info
*info
;
2143 * kmod_module_info_free_list:
2144 * @list: kmod module info list
2146 * Release the resources taken by @list
2148 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2151 kmod_module_info_free(list
->data
);
2152 list
= kmod_list_remove(list
);
2156 struct kmod_module_version
{
2161 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2163 struct kmod_module_version
*mv
;
2164 size_t symbollen
= strlen(symbol
) + 1;
2166 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2171 memcpy(mv
->symbol
, symbol
, symbollen
);
2175 static void kmod_module_version_free(struct kmod_module_version
*version
)
2181 * kmod_module_get_versions:
2183 * @list: where to return list of module versions. Use
2184 * kmod_module_version_get_symbol() and
2185 * kmod_module_version_get_crc(). Release this list with
2186 * kmod_module_versions_free_list()
2188 * Get a list of entries in ELF section "__versions".
2190 * After use, free the @list by calling kmod_module_versions_free_list().
2192 * Returns: 0 on success or < 0 otherwise.
2194 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2196 struct kmod_file
*file
;
2197 struct kmod_elf
*elf
;
2200 struct kmod_modversion
*versions
;
2202 int i
, count
, ret
= 0;
2204 if (mod
== NULL
|| list
== NULL
)
2207 assert(*list
== NULL
);
2209 path
= kmod_module_get_path(mod
);
2213 file
= kmod_file_open(mod
->ctx
, path
);
2217 size
= kmod_file_get_size(file
);
2218 mem
= kmod_file_get_contents(file
);
2220 elf
= kmod_elf_new(mem
, size
);
2223 goto elf_open_error
;
2226 count
= kmod_elf_get_modversions(elf
, &versions
);
2229 goto get_strings_error
;
2232 for (i
= 0; i
< count
; i
++) {
2233 struct kmod_module_version
*mv
;
2234 struct kmod_list
*n
;
2236 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2239 kmod_module_versions_free_list(*list
);
2244 n
= kmod_list_append(*list
, mv
);
2248 kmod_module_version_free(mv
);
2249 kmod_module_versions_free_list(*list
);
2260 kmod_elf_unref(elf
);
2262 kmod_file_unref(file
);
2268 * kmod_module_versions_get_symbol:
2269 * @entry: a list entry representing a kmod module versions
2271 * Get the symbol of a kmod module versions.
2273 * Returns: the symbol of this kmod module versions on success or NULL
2274 * on failure. The string is owned by the versions, do not free it.
2276 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2278 struct kmod_module_version
*version
;
2283 version
= entry
->data
;
2284 return version
->symbol
;
2288 * kmod_module_version_get_crc:
2289 * @entry: a list entry representing a kmod module version
2291 * Get the crc of a kmod module version.
2293 * Returns: the crc of this kmod module version on success or NULL on
2294 * failure. The string is owned by the version, do not free it.
2296 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2298 struct kmod_module_version
*version
;
2303 version
= entry
->data
;
2304 return version
->crc
;
2308 * kmod_module_versions_free_list:
2309 * @list: kmod module versions list
2311 * Release the resources taken by @list
2313 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2316 kmod_module_version_free(list
->data
);
2317 list
= kmod_list_remove(list
);
2321 struct kmod_module_symbol
{
2326 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2328 struct kmod_module_symbol
*mv
;
2329 size_t symbollen
= strlen(symbol
) + 1;
2331 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2336 memcpy(mv
->symbol
, symbol
, symbollen
);
2340 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2346 * kmod_module_get_symbols:
2348 * @list: where to return list of module symbols. Use
2349 * kmod_module_symbol_get_symbol() and
2350 * kmod_module_symbol_get_crc(). Release this list with
2351 * kmod_module_symbols_free_list()
2353 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2355 * After use, free the @list by calling kmod_module_symbols_free_list().
2357 * Returns: 0 on success or < 0 otherwise.
2359 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2361 struct kmod_file
*file
;
2362 struct kmod_elf
*elf
;
2365 struct kmod_modversion
*symbols
;
2367 int i
, count
, ret
= 0;
2369 if (mod
== NULL
|| list
== NULL
)
2372 assert(*list
== NULL
);
2374 path
= kmod_module_get_path(mod
);
2378 file
= kmod_file_open(mod
->ctx
, path
);
2382 size
= kmod_file_get_size(file
);
2383 mem
= kmod_file_get_contents(file
);
2385 elf
= kmod_elf_new(mem
, size
);
2388 goto elf_open_error
;
2391 count
= kmod_elf_get_symbols(elf
, &symbols
);
2394 goto get_strings_error
;
2397 for (i
= 0; i
< count
; i
++) {
2398 struct kmod_module_symbol
*mv
;
2399 struct kmod_list
*n
;
2401 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2404 kmod_module_symbols_free_list(*list
);
2409 n
= kmod_list_append(*list
, mv
);
2413 kmod_module_symbol_free(mv
);
2414 kmod_module_symbols_free_list(*list
);
2425 kmod_elf_unref(elf
);
2427 kmod_file_unref(file
);
2433 * kmod_module_symbol_get_symbol:
2434 * @entry: a list entry representing a kmod module symbols
2436 * Get the symbol of a kmod module symbols.
2438 * Returns: the symbol of this kmod module symbols on success or NULL
2439 * on failure. The string is owned by the symbols, do not free it.
2441 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2443 struct kmod_module_symbol
*symbol
;
2448 symbol
= entry
->data
;
2449 return symbol
->symbol
;
2453 * kmod_module_symbol_get_crc:
2454 * @entry: a list entry representing a kmod module symbol
2456 * Get the crc of a kmod module symbol.
2458 * Returns: the crc of this kmod module symbol on success or NULL on
2459 * failure. The string is owned by the symbol, do not free it.
2461 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2463 struct kmod_module_symbol
*symbol
;
2468 symbol
= entry
->data
;
2473 * kmod_module_symbols_free_list:
2474 * @list: kmod module symbols list
2476 * Release the resources taken by @list
2478 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2481 kmod_module_symbol_free(list
->data
);
2482 list
= kmod_list_remove(list
);
2486 struct kmod_module_dependency_symbol
{
2492 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2494 struct kmod_module_dependency_symbol
*mv
;
2495 size_t symbollen
= strlen(symbol
) + 1;
2497 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2503 memcpy(mv
->symbol
, symbol
, symbollen
);
2507 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2509 free(dependency_symbol
);
2513 * kmod_module_get_dependency_symbols:
2515 * @list: where to return list of module dependency_symbols. Use
2516 * kmod_module_dependency_symbol_get_symbol() and
2517 * kmod_module_dependency_symbol_get_crc(). Release this list with
2518 * kmod_module_dependency_symbols_free_list()
2520 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2522 * After use, free the @list by calling
2523 * kmod_module_dependency_symbols_free_list().
2525 * Returns: 0 on success or < 0 otherwise.
2527 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2529 struct kmod_file
*file
;
2530 struct kmod_elf
*elf
;
2533 struct kmod_modversion
*symbols
;
2535 int i
, count
, ret
= 0;
2537 if (mod
== NULL
|| list
== NULL
)
2540 assert(*list
== NULL
);
2542 path
= kmod_module_get_path(mod
);
2546 file
= kmod_file_open(mod
->ctx
, path
);
2550 size
= kmod_file_get_size(file
);
2551 mem
= kmod_file_get_contents(file
);
2553 elf
= kmod_elf_new(mem
, size
);
2556 goto elf_open_error
;
2559 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2562 goto get_strings_error
;
2565 for (i
= 0; i
< count
; i
++) {
2566 struct kmod_module_dependency_symbol
*mv
;
2567 struct kmod_list
*n
;
2569 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2574 kmod_module_dependency_symbols_free_list(*list
);
2579 n
= kmod_list_append(*list
, mv
);
2583 kmod_module_dependency_symbol_free(mv
);
2584 kmod_module_dependency_symbols_free_list(*list
);
2595 kmod_elf_unref(elf
);
2597 kmod_file_unref(file
);
2603 * kmod_module_dependency_symbol_get_symbol:
2604 * @entry: a list entry representing a kmod module dependency_symbols
2606 * Get the dependency symbol of a kmod module
2608 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2609 * on failure. The string is owned by the dependency_symbols, do not free it.
2611 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2613 struct kmod_module_dependency_symbol
*dependency_symbol
;
2618 dependency_symbol
= entry
->data
;
2619 return dependency_symbol
->symbol
;
2623 * kmod_module_dependency_symbol_get_crc:
2624 * @entry: a list entry representing a kmod module dependency_symbol
2626 * Get the crc of a kmod module dependency_symbol.
2628 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2629 * failure. The string is owned by the dependency_symbol, do not free it.
2631 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2633 struct kmod_module_dependency_symbol
*dependency_symbol
;
2638 dependency_symbol
= entry
->data
;
2639 return dependency_symbol
->crc
;
2643 * kmod_module_dependency_symbol_get_bind:
2644 * @entry: a list entry representing a kmod module dependency_symbol
2646 * Get the bind type of a kmod module dependency_symbol.
2648 * Returns: the bind of this kmod module dependency_symbol on success
2649 * or < 0 on failure.
2651 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2653 struct kmod_module_dependency_symbol
*dependency_symbol
;
2658 dependency_symbol
= entry
->data
;
2659 return dependency_symbol
->bind
;
2663 * kmod_module_dependency_symbols_free_list:
2664 * @list: kmod module dependency_symbols list
2666 * Release the resources taken by @list
2668 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2671 kmod_module_dependency_symbol_free(list
->data
);
2672 list
= kmod_list_remove(list
);