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>
40 #include "libkmod-private.h"
43 * SECTION:libkmod-module
44 * @short_description: operate on kernel modules
50 * Opaque object representing a module.
57 struct kmod_list
*dep
;
59 const char *install_commands
; /* owned by kmod_config */
60 const char *remove_commands
; /* owned by kmod_config */
61 char *alias
; /* only set if this module was created from an alias */
67 bool install_commands
: 1;
68 bool remove_commands
: 1;
72 static inline const char *path_join(const char *path
, size_t prefixlen
,
80 pathlen
= strlen(path
);
81 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
84 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
88 int kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
90 struct kmod_ctx
*ctx
= mod
->ctx
;
91 struct kmod_list
*list
= NULL
;
100 assert(mod
->dep
== NULL
);
101 mod
->init
.dep
= true;
103 p
= strchr(line
, ':');
108 dirname
= kmod_get_dirname(mod
->ctx
);
109 dirnamelen
= strlen(dirname
);
110 if (dirnamelen
+ 2 >= PATH_MAX
)
113 memcpy(buf
, dirname
, dirnamelen
);
114 buf
[dirnamelen
] = '/';
116 buf
[dirnamelen
] = '\0';
118 if (mod
->path
== NULL
) {
119 const char *str
= path_join(line
, dirnamelen
, buf
);
122 mod
->path
= strdup(str
);
123 if (mod
->path
== NULL
)
128 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
129 p
= strtok_r(NULL
, " \t", &saveptr
)) {
130 struct kmod_module
*depmod
;
133 path
= path_join(p
, dirnamelen
, buf
);
135 ERR(ctx
, "could not join path '%s' and '%s'.\n",
140 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
142 ERR(ctx
, "ctx=%p path=%s error=%s\n",
143 ctx
, path
, strerror(-err
));
147 DBG(ctx
, "add dep: %s\n", path
);
149 list
= kmod_list_prepend(list
, depmod
);
153 DBG(ctx
, "%d dependencies for %s\n", n
, mod
->name
);
160 kmod_module_unref_list(list
);
161 mod
->init
.dep
= false;
166 * kmod_module_new_from_name:
167 * @ctx: kmod library context
168 * @name: name of the module
169 * @mod: where to save the created struct kmod_module
171 * Create a new struct kmod_module using the module name. @name can not be an
172 * alias, file name or anything else; it must be a module name. There's no
173 * check if the module does exists in the system.
175 * This function is also used internally by many others that return a new
176 * struct kmod_module or a new list of modules.
178 * The initial refcount is 1, and needs to be decremented to release the
179 * resources of the kmod_module. Since libkmod keeps track of all
180 * kmod_modules created, they are all released upon @ctx destruction too. Do
181 * not unref @ctx before all the desired operations with the returned
182 * kmod_module are done.
184 * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
185 * module name or if memory allocation failed.
187 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
,
189 struct kmod_module
**mod
)
191 struct kmod_module
*m
;
193 char name_norm
[PATH_MAX
];
196 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
199 if (alias_normalize(name
, name_norm
, &namelen
) < 0) {
200 DBG(ctx
, "invalid alias: %s\n", name
);
204 m
= kmod_pool_get_module(ctx
, name_norm
);
206 *mod
= kmod_module_ref(m
);
210 namesep
= strchr(name_norm
, '/');
211 m
= malloc(sizeof(*m
) + (namesep
== NULL
? 1 : 2) * namelen
+ 2);
217 memset(m
, 0, sizeof(*m
));
219 m
->ctx
= kmod_ref(ctx
);
220 m
->name
= (char *)m
+ sizeof(*m
);
221 memcpy(m
->name
, name_norm
, namelen
+ 1);
224 size_t len
= namesep
- name_norm
;
227 m
->alias
= m
->name
+ len
+ 1;
228 m
->hashkey
= m
->name
+ namelen
+ 1;
229 memcpy(m
->hashkey
, name_norm
, namelen
+ 1);
231 m
->hashkey
= m
->name
;
235 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
241 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
,
242 const char *name
, struct kmod_module
**mod
)
246 size_t namelen
= strlen(name
);
247 size_t aliaslen
= strlen(alias
);
249 if (namelen
+ aliaslen
+ 2 > PATH_MAX
)
250 return -ENAMETOOLONG
;
252 memcpy(key
, name
, namelen
);
253 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
256 err
= kmod_module_new_from_name(ctx
, key
, mod
);
264 * kmod_module_new_from_path:
265 * @ctx: kmod library context
266 * @path: path where to find the given module
267 * @mod: where to save the created struct kmod_module
269 * Create a new struct kmod_module using the module path. @path must be an
270 * existent file with in the filesystem and must be accessible to libkmod.
272 * The initial refcount is 1, and needs to be decremented to release the
273 * resources of the kmod_module. Since libkmod keeps track of all
274 * kmod_modules created, they are all released upon @ctx destruction too. Do
275 * not unref @ctx before all the desired operations with the returned
276 * kmod_module are done.
278 * If @path is relative, it's treated as relative to the current working
279 * directory. Otherwise, give an absolute path.
281 * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
282 * it's not a valid file for a kmod_module or if memory allocation failed.
284 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
,
286 struct kmod_module
**mod
)
288 struct kmod_module
*m
;
295 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
298 abspath
= path_make_absolute_cwd(path
);
299 if (abspath
== NULL
) {
300 DBG(ctx
, "no absolute path for %s\n", path
);
304 err
= stat(abspath
, &st
);
307 DBG(ctx
, "stat %s: %s\n", path
, strerror(errno
));
312 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
313 DBG(ctx
, "could not get modname from path %s\n", path
);
318 m
= kmod_pool_get_module(ctx
, name
);
322 else if (streq(m
->path
, abspath
))
325 ERR(ctx
, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
326 name
, abspath
, m
->path
);
331 *mod
= kmod_module_ref(m
);
335 m
= malloc(sizeof(*m
) + namelen
+ 1);
339 memset(m
, 0, sizeof(*m
));
341 m
->ctx
= kmod_ref(ctx
);
342 m
->name
= (char *)m
+ sizeof(*m
);
343 memcpy(m
->name
, name
, namelen
+ 1);
345 m
->hashkey
= m
->name
;
348 kmod_pool_add_module(ctx
, m
, m
->hashkey
);
359 * Drop a reference of the kmod module. If the refcount reaches zero, its
360 * resources are released.
362 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
363 * returns the passed @mod with its refcount decremented.
365 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
370 if (--mod
->refcount
> 0)
373 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
375 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
376 kmod_module_unref_list(mod
->dep
);
377 kmod_unref(mod
->ctx
);
388 * Take a reference of the kmod module, incrementing its refcount.
390 * Returns: the passed @module with its refcount incremented.
392 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
402 #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
406 if (*(_list) != NULL) \
411 * kmod_module_new_from_lookup:
412 * @ctx: kmod library context
413 * @given_alias: alias to look for
414 * @list: an empty list where to save the list of modules matching
417 * Create a new list of kmod modules using an alias or module name and lookup
418 * libkmod's configuration files and indexes in order to find the module.
419 * Once it's found in one of the places, it stops searching and create the
420 * list of modules that is saved in @list.
422 * The search order is: 1. aliases in configuration file; 2. module names in
423 * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
424 * in modules.alias index.
426 * The initial refcount is 1, and needs to be decremented to release the
427 * resources of the kmod_module. The returned @list must be released by
428 * calling kmod_module_unref_list(). Since libkmod keeps track of all
429 * kmod_modules created, they are all released upon @ctx destruction too. Do
430 * not unref @ctx before all the desired operations with the returned list are
433 * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
434 * methods failed, which is basically due to memory allocation fail. If module
435 * is not found, it still returns 0, but @list is an empty list.
437 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
,
438 const char *given_alias
,
439 struct kmod_list
**list
)
442 char alias
[PATH_MAX
];
444 if (ctx
== NULL
|| given_alias
== NULL
)
447 if (list
== NULL
|| *list
!= NULL
) {
448 ERR(ctx
, "An empty list is needed to create lookup\n");
452 if (alias_normalize(given_alias
, alias
, NULL
) < 0) {
453 DBG(ctx
, "invalid alias: %s\n", given_alias
);
457 DBG(ctx
, "input alias=%s, normalized=%s\n", given_alias
, alias
);
459 /* Aliases from config file override all the others */
460 err
= kmod_lookup_alias_from_config(ctx
, alias
, list
);
461 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
463 DBG(ctx
, "lookup modules.dep %s\n", alias
);
464 err
= kmod_lookup_alias_from_moddep_file(ctx
, alias
, list
);
465 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
467 DBG(ctx
, "lookup modules.symbols %s\n", alias
);
468 err
= kmod_lookup_alias_from_symbols_file(ctx
, alias
, list
);
469 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
471 DBG(ctx
, "lookup install and remove commands %s\n", alias
);
472 err
= kmod_lookup_alias_from_commands(ctx
, alias
, list
);
473 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
475 DBG(ctx
, "lookup modules.aliases %s\n", alias
);
476 err
= kmod_lookup_alias_from_aliases_file(ctx
, alias
, list
);
477 CHECK_ERR_AND_FINISH(err
, fail
, list
, finish
);
480 DBG(ctx
, "lookup %s=%d, list=%p\n", alias
, err
, *list
);
483 DBG(ctx
, "Failed to lookup %s\n", alias
);
484 kmod_module_unref_list(*list
);
488 #undef CHECK_ERR_AND_FINISH
491 * kmod_module_unref_list:
492 * @list: list of kmod modules
494 * Drop a reference of each kmod module in @list and releases the resources
495 * taken by the list itself.
497 * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
498 * returns the passed @mod with its refcount decremented.
500 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
502 for (; list
!= NULL
; list
= kmod_list_remove(list
))
503 kmod_module_unref(list
->data
);
509 * kmod_module_get_filtered_blacklist:
510 * @ctx: kmod library context
511 * @input: list of kmod_module to be filtered with blacklist
512 * @output: where to save the new list
514 * Given a list @input, this function filter it out with config's blacklist
515 * ans save it in @output.
517 * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
520 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
521 const struct kmod_list
*input
,
522 struct kmod_list
**output
)
524 const struct kmod_list
*li
;
525 const struct kmod_list
*blacklist
;
527 if (ctx
== NULL
|| output
== NULL
)
534 blacklist
= kmod_get_blacklists(ctx
);
535 kmod_list_foreach(li
, input
) {
536 struct kmod_module
*mod
= li
->data
;
537 const struct kmod_list
*lb
;
538 struct kmod_list
*node
;
539 bool filtered
= false;
541 kmod_list_foreach(lb
, blacklist
) {
542 const char *name
= lb
->data
;
544 if (streq(name
, mod
->name
)) {
553 node
= kmod_list_append(*output
, mod
);
558 kmod_module_ref(mod
);
564 kmod_module_unref_list(*output
);
569 static const struct kmod_list
*module_get_dependencies_noref(const struct kmod_module
*mod
)
571 if (!mod
->init
.dep
) {
573 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
578 kmod_module_parse_depline((struct kmod_module
*)mod
, line
);
589 * kmod_module_get_dependencies:
592 * Search the modules.dep index to find the dependencies of the given @mod.
593 * The result is cached in @mod, so subsequent calls to this function will
594 * return the already searched list of modules.
596 * Returns: NULL on failure or if there are any dependencies. Otherwise it
597 * returns a list of kmod modules that can be released by calling
598 * kmod_module_unref_list().
600 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
602 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
607 module_get_dependencies_noref(mod
);
609 kmod_list_foreach(l
, mod
->dep
) {
610 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
612 kmod_module_unref(l
->data
);
622 ERR(mod
->ctx
, "out of memory\n");
623 kmod_module_unref_list(list_new
);
628 * kmod_module_get_module:
629 * @entry: an entry in a list of kmod modules.
631 * Get the kmod module of this @entry in the list, increasing its refcount.
632 * After it's used, unref it. Since the refcount is incremented upon return,
633 * you still have to call kmod_module_unref_list() to release the list of kmod
636 * Returns: NULL on failure or the kmod_module contained in this list entry
637 * with its refcount incremented.
639 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
644 return kmod_module_ref(entry
->data
);
648 * kmod_module_get_name:
651 * Get the name of this kmod module. Name is always available, independently
652 * if it was created by kmod_module_new_from_name() or another function and
653 * it's always normalized (dashes are replaced with underscores).
655 * Returns: the name of this kmod module.
657 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
666 * kmod_module_get_path:
669 * Get the path of this kmod module. If this kmod module was not created by
670 * path, it can search the modules.dep index in order to find out the module
671 * under context's dirname.
673 * Returns: the path of this kmod module or NULL if such information is not
676 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
683 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
685 if (mod
->path
!= NULL
)
691 line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
695 kmod_module_parse_depline((struct kmod_module
*) mod
, line
);
702 extern long delete_module(const char *name
, unsigned int flags
);
705 * kmod_module_remove_module:
707 * @flags: flags to pass to Linux kernel when removing the module
709 * Remove a module from Linux kernel.
711 * Returns: 0 on success or < 0 on failure.
713 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
,
721 /* Filter out other flags */
722 flags
&= (KMOD_REMOVE_FORCE
| KMOD_REMOVE_NOWAIT
);
724 err
= delete_module(mod
->name
, flags
);
727 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
733 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
736 * kmod_module_insert_module:
738 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
739 * behavior of this function.
740 * @options: module's options to pass to Linux Kernel.
742 * Insert a module in Linux kernel. It opens the file pointed by @mod,
743 * mmap'ing it and passing to kernel.
745 * Returns: 0 on success or < 0 on failure. If module is already loaded it
748 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
,
755 struct kmod_file
*file
;
756 struct kmod_elf
*elf
= NULL
;
758 const char *args
= options
? options
: "";
763 path
= kmod_module_get_path(mod
);
765 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
769 file
= kmod_file_open(mod
->ctx
, path
);
775 size
= kmod_file_get_size(file
);
776 mem
= kmod_file_get_contents(file
);
778 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
779 elf
= kmod_elf_new(mem
, size
);
785 if (flags
& KMOD_INSERT_FORCE_MODVERSION
) {
786 err
= kmod_elf_strip_section(elf
, "__versions");
788 INFO(mod
->ctx
, "Failed to strip modversion: %s\n", strerror(-err
));
791 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
) {
792 err
= kmod_elf_strip_vermagic(elf
);
794 INFO(mod
->ctx
, "Failed to strip vermagic: %s\n", strerror(-err
));
797 mem
= kmod_elf_get_memory(elf
);
800 err
= init_module(mem
, size
, args
);
803 INFO(mod
->ctx
, "Failed to insert module '%s': %m\n", path
);
809 kmod_file_unref(file
);
814 static bool module_is_blacklisted(struct kmod_module
*mod
)
816 struct kmod_ctx
*ctx
= mod
->ctx
;
817 const struct kmod_list
*bl
= kmod_get_blacklists(ctx
);
818 const struct kmod_list
*l
;
820 kmod_list_foreach(l
, bl
) {
821 const char *modname
= kmod_blacklist_get_modname(l
);
823 if (streq(modname
, mod
->name
))
830 #define RECURSION_CHECK_STEP 10
831 #define RET_CHECK_NOLOOP_OR_FAIL(_ret, _flags, _label) \
834 if (_ret == -ELOOP || _ret == -ENOMEM \
835 || (_flags & KMOD_PROBE_STOP_ON_FAILURE)) \
840 struct probe_insert_cb
{
841 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
845 int module_probe_insert_module(struct kmod_module
*mod
,
846 unsigned int flags
, const char *extra_options
,
847 struct probe_insert_cb
*cb
,
848 struct kmod_list
*rec
, unsigned int reccount
);
850 static int command_do(struct kmod_module
*mod
, const char *type
,
853 const char *modname
= kmod_module_get_name(mod
);
856 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
858 setenv("MODPROBE_MODULE", modname
, 1);
860 unsetenv("MODPROBE_MODULE");
862 if (err
== -1 || WEXITSTATUS(err
)) {
863 ERR(mod
->ctx
, "Error running %s command for %s\n",
866 err
= -WEXITSTATUS(err
);
872 static int module_do_install_commands(struct kmod_module
*mod
,
874 struct probe_insert_cb
*cb
)
876 const char *command
= kmod_module_get_install_commands(mod
);
879 size_t cmdlen
, options_len
, varlen
;
886 options_len
= strlen(options
);
887 cmdlen
= strlen(command
);
888 varlen
= sizeof("$CMDLINE_OPTS") - 1;
890 cmd
= memdup(command
, cmdlen
+ 1);
894 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
895 size_t prefixlen
= p
- cmd
;
896 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
897 size_t slen
= cmdlen
- varlen
+ options_len
;
898 char *suffix
= p
+ varlen
;
899 char *s
= malloc(slen
+ 1);
904 memcpy(s
, cmd
, p
- cmd
);
905 memcpy(s
+ prefixlen
, options
, options_len
);
906 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
914 if (cb
->run_install
!= NULL
)
915 err
= cb
->run_install(mod
, cmd
, cb
->data
);
917 err
= command_do(mod
, "install", cmd
);
924 static bool module_dep_has_loop(const struct kmod_list
*deps
,
925 struct kmod_list
*rec
,
926 unsigned int reccount
)
929 struct kmod_module
*mod
;
931 if (reccount
< RECURSION_CHECK_STEP
|| deps
== NULL
)
936 kmod_list_foreach(l
, rec
) {
937 struct kmod_list
*loop
;
942 ERR(mod
->ctx
, "Dependency loop detected while inserting '%s'. Operation aborted\n",
945 for (loop
= l
; loop
!= NULL
;
946 loop
= kmod_list_next(rec
, loop
)) {
947 struct kmod_module
*m
= loop
->data
;
948 ERR(mod
->ctx
, "%s\n", m
->name
);
957 static int module_do_insmod_dep(const struct kmod_list
*deps
,
958 unsigned int flags
, struct probe_insert_cb
*cb
,
959 struct kmod_list
*rec
, unsigned int reccount
)
961 const struct kmod_list
*d
;
964 if (module_dep_has_loop(deps
, rec
, reccount
))
967 kmod_list_foreach(d
, deps
) {
968 struct kmod_module
*dm
= d
->data
;
969 struct kmod_list
*tmp
;
971 tmp
= kmod_list_append(rec
, dm
);
976 err
= module_probe_insert_module(dm
, flags
, NULL
, cb
,
979 rec
= kmod_list_remove_n_latest(rec
, 1);
980 RET_CHECK_NOLOOP_OR_FAIL(err
, flags
, finish
);
987 static char *module_options_concat(const char *opt
, const char *xopt
)
989 // TODO: we might need to check if xopt overrides options on opt
990 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
991 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
994 if (optlen
== 0 && xoptlen
== 0)
997 r
= malloc(optlen
+ xoptlen
+ 2);
1000 memcpy(r
, opt
, optlen
);
1006 memcpy(r
+ optlen
, xopt
, xoptlen
);
1008 r
[optlen
+ xoptlen
] = '\0';
1014 * Do the probe_insert work recursively. We traverse the dependencies in
1015 * depth-first order, checking the following conditions:
1018 * - Is install command?
1019 * - Is already loaded?
1021 * Then we insert the modules (calling module_do_insmod_dep(), which will
1022 * re-enter this function) needed to load @mod in the following order:
1029 int module_probe_insert_module(struct kmod_module
*mod
,
1030 unsigned int flags
, const char *extra_options
,
1031 struct probe_insert_cb
*cb
,
1032 struct kmod_list
*rec
, unsigned int reccount
)
1035 const char *install_cmds
;
1036 const struct kmod_list
*dep
;
1037 struct kmod_list
*pre
= NULL
, *post
= NULL
;
1040 if ((flags
& KMOD_PROBE_STOP_ON_BLACKLIST
)
1041 && module_is_blacklisted(mod
)) {
1042 DBG(mod
->ctx
, "Stopping on '%s': blacklisted\n", mod
->name
);
1046 install_cmds
= kmod_module_get_install_commands(mod
);
1047 if (install_cmds
!= NULL
) {
1048 if (flags
& KMOD_PROBE_STOP_ON_COMMAND
) {
1049 DBG(mod
->ctx
, "Stopping on '%s': install command\n",
1054 int state
= kmod_module_get_initstate(mod
);
1056 if (state
== KMOD_MODULE_LIVE
||
1057 state
== KMOD_MODULE_COMING
||
1058 state
== KMOD_MODULE_BUILTIN
)
1062 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
1066 err
= module_do_insmod_dep(pre
, flags
, cb
, rec
, reccount
);
1067 RET_CHECK_NOLOOP_OR_FAIL(err
, flags
, finish
);
1069 dep
= module_get_dependencies_noref(mod
);
1070 err
= module_do_insmod_dep(dep
, flags
, cb
, rec
, reccount
);
1071 RET_CHECK_NOLOOP_OR_FAIL(err
, flags
, finish
);
1073 options
= module_options_concat(kmod_module_get_options(mod
),
1076 if (install_cmds
!= NULL
)
1077 err
= module_do_install_commands(mod
, options
, cb
);
1079 err
= kmod_module_insert_module(mod
, flags
, options
);
1084 * Ignore "already loaded" error. We need to check here because of
1085 * race conditions. We checked first if module was already loaded but
1086 * it may have been loaded between the check and the moment we try to
1089 if (err
< 0 && err
!= -EEXIST
&& (flags
& KMOD_PROBE_STOP_ON_FAILURE
))
1092 err
= module_do_insmod_dep(post
, flags
, cb
, rec
, reccount
);
1095 kmod_module_unref_list(pre
);
1096 kmod_module_unref_list(post
);
1102 * kmod_module_probe_insert_module:
1104 * @flags: flags are not passed to Linux Kernel, but instead they dictate the
1105 * behavior of this function.
1106 * @extra_options: module's options to pass to Linux Kernel.
1107 * @run_install: function to run when @mod is backed by a install command.
1108 * @data: data to give back to @run_install callback
1110 * Insert a module in Linux kernel resolving dependencies, soft dependencies
1111 * install commands and applying blacklist.
1113 * If @run_install is NULL, and the flag KMOD_PROBE_STOP_ON_COMMANDS is not
1114 * given, this function will fork and exec by calling system(3). If you need
1115 * control over the execution of an install command, give a callback function
1118 * Returns: 0 on success or < 0 on failure.
1120 KMOD_EXPORT
int kmod_module_probe_insert_module(struct kmod_module
*mod
,
1121 unsigned int flags
, const char *extra_options
,
1122 int (*run_install
)(struct kmod_module
*m
,
1123 const char *cmd
, void *data
),
1126 struct probe_insert_cb cb
;
1128 cb
.run_install
= run_install
;
1129 cb
.data
= (void *) data
;
1131 return module_probe_insert_module(mod
, flags
, extra_options
, &cb
,
1135 #undef RECURSION_CHECK_STEP
1136 #undef RET_CHECK_NOLOOP_OR_FAIL
1140 * kmod_module_get_options:
1143 * Get options of this kmod module. Options come from the configuration file
1144 * and are cached in @mod. The first call to this function will search for
1145 * this module in configuration and subsequent calls return the cached string.
1147 * Returns: a string with all the options separated by spaces. This string is
1148 * owned by @mod, do not free it.
1150 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1155 if (!mod
->init
.options
) {
1157 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1158 const struct kmod_list
*l
, *ctx_options
;
1162 ctx_options
= kmod_get_options(mod
->ctx
);
1164 kmod_list_foreach(l
, ctx_options
) {
1165 const char *modname
= kmod_option_get_modname(l
);
1170 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1171 if (!(streq(modname
, mod
->name
) || (mod
->alias
!= NULL
&&
1172 streq(modname
, mod
->alias
))))
1175 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname
, mod
->name
, mod
->alias
);
1176 str
= kmod_option_get_options(l
);
1181 tmp
= realloc(opts
, optslen
+ len
+ 2);
1190 opts
[optslen
] = ' ';
1194 memcpy(opts
+ optslen
, str
, len
);
1196 opts
[optslen
] = '\0';
1199 m
->init
.options
= true;
1203 return mod
->options
;
1206 ERR(mod
->ctx
, "out of memory\n");
1211 * kmod_module_get_install_commands:
1214 * Get install commands for this kmod module. Install commands come from the
1215 * configuration file and are cached in @mod. The first call to this function
1216 * will search for this module in configuration and subsequent calls return
1217 * the cached string. The install commands are returned as they were in the
1218 * configuration, concatenated by ';'. No other processing is made in this
1221 * Returns: a string with all install commands separated by semicolons. This
1222 * string is owned by @mod, do not free it.
1224 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1229 if (!mod
->init
.install_commands
) {
1231 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1232 const struct kmod_list
*l
, *ctx_install_commands
;
1234 ctx_install_commands
= kmod_get_install_commands(mod
->ctx
);
1236 kmod_list_foreach(l
, ctx_install_commands
) {
1237 const char *modname
= kmod_command_get_modname(l
);
1239 if (fnmatch(modname
, mod
->name
, 0) != 0)
1242 m
->install_commands
= kmod_command_get_command(l
);
1245 * find only the first command, as modprobe from
1246 * module-init-tools does
1251 m
->init
.install_commands
= true;
1254 return mod
->install_commands
;
1257 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1259 mod
->init
.install_commands
= true;
1260 mod
->install_commands
= cmd
;
1263 static struct kmod_list
*lookup_softdep(struct kmod_ctx
*ctx
, const char * const * array
, unsigned int count
)
1265 struct kmod_list
*ret
= NULL
;
1268 for (i
= 0; i
< count
; i
++) {
1269 const char *depname
= array
[i
];
1270 struct kmod_list
*lst
= NULL
;
1273 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1275 ERR(ctx
, "failed to lookup soft dependency '%s', continuing anyway.\n", depname
);
1277 } else if (lst
!= NULL
)
1278 ret
= kmod_list_append_list(ret
, lst
);
1284 * kmod_module_get_softdeps:
1286 * @pre: where to save the list of preceding soft dependencies.
1287 * @post: where to save the list of post soft dependencies.
1289 * Get soft dependencies for this kmod module. Soft dependencies come
1290 * from configuration file and are not cached in @mod because it may include
1291 * dependency cycles that would make we leak kmod_module. Any call
1292 * to this function will search for this module in configuration, allocate a
1293 * list and return the result.
1295 * Both @pre and @post are newly created list of kmod_module and
1296 * should be unreferenced with kmod_module_unref_list().
1298 * Returns: 0 on success or < 0 otherwise.
1300 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1301 struct kmod_list
**pre
,
1302 struct kmod_list
**post
)
1304 const struct kmod_list
*l
, *ctx_softdeps
;
1306 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1309 assert(*pre
== NULL
);
1310 assert(*post
== NULL
);
1312 ctx_softdeps
= kmod_get_softdeps(mod
->ctx
);
1314 kmod_list_foreach(l
, ctx_softdeps
) {
1315 const char *modname
= kmod_softdep_get_name(l
);
1316 const char * const *array
;
1319 if (fnmatch(modname
, mod
->name
, 0) != 0)
1322 array
= kmod_softdep_get_pre(l
, &count
);
1323 *pre
= lookup_softdep(mod
->ctx
, array
, count
);
1324 array
= kmod_softdep_get_post(l
, &count
);
1325 *post
= lookup_softdep(mod
->ctx
, array
, count
);
1328 * find only the first command, as modprobe from
1329 * module-init-tools does
1338 * kmod_module_get_remove_commands:
1341 * Get remove commands for this kmod module. Remove 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 remove 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 remove commands separated by semicolons. This
1349 * string is owned by @mod, do not free it.
1351 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1356 if (!mod
->init
.remove_commands
) {
1358 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1359 const struct kmod_list
*l
, *ctx_remove_commands
;
1361 ctx_remove_commands
= kmod_get_remove_commands(mod
->ctx
);
1363 kmod_list_foreach(l
, ctx_remove_commands
) {
1364 const char *modname
= kmod_command_get_modname(l
);
1366 if (fnmatch(modname
, mod
->name
, 0) != 0)
1369 m
->remove_commands
= kmod_command_get_command(l
);
1372 * find only the first command, as modprobe from
1373 * module-init-tools does
1378 m
->init
.remove_commands
= true;
1381 return mod
->remove_commands
;
1384 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1386 mod
->init
.remove_commands
= true;
1387 mod
->remove_commands
= cmd
;
1391 * SECTION:libkmod-loaded
1392 * @short_description: currently loaded modules
1394 * Information about currently loaded modules, as reported by Linux kernel.
1395 * These information are not cached by libkmod and are always read from /sys
1396 * and /proc/modules.
1400 * kmod_module_new_from_loaded:
1401 * @ctx: kmod library context
1402 * @list: where to save the list of loaded modules
1404 * Create a new list of kmod modules with all modules currently loaded in
1405 * kernel. It uses /proc/modules to get the names of loaded modules and to
1406 * create kmod modules by calling kmod_module_new_from_name() in each of them.
1407 * They are put are put in @list in no particular order.
1409 * The initial refcount is 1, and needs to be decremented to release the
1410 * resources of the kmod_module. The returned @list must be released by
1411 * calling kmod_module_unref_list(). Since libkmod keeps track of all
1412 * kmod_modules created, they are all released upon @ctx destruction too. Do
1413 * not unref @ctx before all the desired operations with the returned list are
1416 * Returns: 0 on success or < 0 on error.
1418 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
,
1419 struct kmod_list
**list
)
1421 struct kmod_list
*l
= NULL
;
1425 if (ctx
== NULL
|| list
== NULL
)
1428 fp
= fopen("/proc/modules", "re");
1431 ERR(ctx
, "could not open /proc/modules: %s\n", strerror(errno
));
1435 while (fgets(line
, sizeof(line
), fp
)) {
1436 struct kmod_module
*m
;
1437 struct kmod_list
*node
;
1439 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1441 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1443 ERR(ctx
, "could not get module from name '%s': %s\n",
1444 name
, strerror(-err
));
1448 node
= kmod_list_append(l
, m
);
1452 ERR(ctx
, "out of memory\n");
1453 kmod_module_unref(m
);
1464 * kmod_module_initstate_str:
1465 * @state: the state as returned by kmod_module_get_initstate()
1467 * Translate a initstate to a string.
1469 * Returns: the string associated to the @state. This string is statically
1470 * allocated, do not free it.
1472 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1475 case KMOD_MODULE_BUILTIN
:
1477 case KMOD_MODULE_LIVE
:
1479 case KMOD_MODULE_COMING
:
1481 case KMOD_MODULE_GOING
:
1489 * kmod_module_get_initstate:
1492 * Get the initstate of this @mod, as returned by Linux Kernel, by reading
1495 * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1497 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1499 char path
[PATH_MAX
], buf
[32];
1500 int fd
, err
, pathlen
;
1505 pathlen
= snprintf(path
, sizeof(path
),
1506 "/sys/module/%s/initstate", mod
->name
);
1507 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1511 DBG(mod
->ctx
, "could not open '%s': %s\n",
1512 path
, strerror(-err
));
1514 if (pathlen
> (int)sizeof("/initstate") - 1) {
1516 path
[pathlen
- (sizeof("/initstate") - 1)] = '\0';
1517 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1518 return KMOD_MODULE_BUILTIN
;
1521 DBG(mod
->ctx
, "could not open '%s': %s\n",
1522 path
, strerror(-err
));
1526 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1529 ERR(mod
->ctx
, "could not read from '%s': %s\n",
1530 path
, strerror(-err
));
1534 if (streq(buf
, "live\n"))
1535 return KMOD_MODULE_LIVE
;
1536 else if (streq(buf
, "coming\n"))
1537 return KMOD_MODULE_COMING
;
1538 else if (streq(buf
, "going\n"))
1539 return KMOD_MODULE_GOING
;
1541 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1546 * kmod_module_get_size:
1549 * Get the size of this kmod module as returned by Linux kernel. It reads the
1550 * file /proc/modules to search for this module and get its size.
1552 * Returns: the size of this kmod module.
1554 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1556 // FIXME TODO: this should be available from /sys/module/foo
1560 long size
= -ENOENT
;
1565 fp
= fopen("/proc/modules", "re");
1569 "could not open /proc/modules: %s\n", strerror(errno
));
1573 while (fgets(line
, sizeof(line
), fp
)) {
1574 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1578 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1581 tok
= strtok_r(NULL
, " \t", &saveptr
);
1584 "invalid line format at /proc/modules:%d\n", lineno
);
1588 value
= strtol(tok
, &endptr
, 10);
1589 if (endptr
== tok
|| *endptr
!= '\0') {
1591 "invalid line format at /proc/modules:%d\n", lineno
);
1603 * kmod_module_get_refcnt:
1606 * Get the ref count of this @mod, as returned by Linux Kernel, by reading
1609 * Returns: 0 on success or < 0 on failure.
1611 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1613 char path
[PATH_MAX
];
1620 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1621 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1624 ERR(mod
->ctx
, "could not open '%s': %s\n",
1625 path
, strerror(errno
));
1629 err
= read_str_long(fd
, &refcnt
, 10);
1632 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n",
1633 path
, strerror(-err
));
1641 * kmod_module_get_holders:
1644 * Get a list of kmod modules that are holding this @mod, as returned by Linux
1645 * Kernel. After use, free the @list by calling kmod_module_unref_list().
1647 * Returns: a new list of kmod modules on success or NULL on failure.
1649 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1651 char dname
[PATH_MAX
];
1652 struct kmod_list
*list
= NULL
;
1658 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1662 ERR(mod
->ctx
, "could not open '%s': %s\n",
1663 dname
, strerror(errno
));
1668 struct dirent de
, *entp
;
1669 struct kmod_module
*holder
;
1670 struct kmod_list
*l
;
1673 err
= readdir_r(d
, &de
, &entp
);
1675 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1676 mod
->name
, strerror(-err
));
1683 if (de
.d_name
[0] == '.') {
1684 if (de
.d_name
[1] == '\0' ||
1685 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1689 err
= kmod_module_new_from_name(mod
->ctx
, de
.d_name
, &holder
);
1691 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1692 de
.d_name
, strerror(-err
));
1696 l
= kmod_list_append(list
, holder
);
1700 ERR(mod
->ctx
, "out of memory\n");
1701 kmod_module_unref(holder
);
1711 kmod_module_unref_list(list
);
1715 struct kmod_module_section
{
1716 unsigned long address
;
1720 static void kmod_module_section_free(struct kmod_module_section
*section
)
1726 * kmod_module_get_sections:
1729 * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1730 * structure contained in this list is internal to libkmod and their fields
1731 * can be obtained by calling kmod_module_section_get_name() and
1732 * kmod_module_section_get_address().
1734 * After use, free the @list by calling kmod_module_section_free_list().
1736 * Returns: a new list of kmod module sections on success or NULL on failure.
1738 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1740 char dname
[PATH_MAX
];
1741 struct kmod_list
*list
= NULL
;
1748 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1752 ERR(mod
->ctx
, "could not open '%s': %s\n",
1753 dname
, strerror(errno
));
1760 struct dirent de
, *entp
;
1761 struct kmod_module_section
*section
;
1762 struct kmod_list
*l
;
1763 unsigned long address
;
1767 err
= readdir_r(d
, &de
, &entp
);
1769 ERR(mod
->ctx
, "could not iterate for module '%s': %s\n",
1770 mod
->name
, strerror(-err
));
1774 if (de
.d_name
[0] == '.') {
1775 if (de
.d_name
[1] == '\0' ||
1776 (de
.d_name
[1] == '.' && de
.d_name
[2] == '\0'))
1780 fd
= openat(dfd
, de
.d_name
, O_RDONLY
|O_CLOEXEC
);
1782 ERR(mod
->ctx
, "could not open '%s/%s': %m\n",
1787 err
= read_str_ulong(fd
, &address
, 16);
1790 ERR(mod
->ctx
, "could not read long from '%s/%s': %m\n",
1795 namesz
= strlen(de
.d_name
) + 1;
1796 section
= malloc(sizeof(*section
) + namesz
);
1798 if (section
== NULL
) {
1799 ERR(mod
->ctx
, "out of memory\n");
1803 section
->address
= address
;
1804 memcpy(section
->name
, de
.d_name
, namesz
);
1806 l
= kmod_list_append(list
, section
);
1810 ERR(mod
->ctx
, "out of memory\n");
1821 kmod_module_unref_list(list
);
1826 * kmod_module_section_get_module_name:
1827 * @entry: a list entry representing a kmod module section
1829 * Get the name of a kmod module section.
1831 * After use, free the @list by calling kmod_module_section_free_list().
1833 * Returns: the name of this kmod module section on success or NULL on
1834 * failure. The string is owned by the section, do not free it.
1836 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1838 struct kmod_module_section
*section
;
1843 section
= entry
->data
;
1844 return section
->name
;
1848 * kmod_module_section_get_address:
1849 * @entry: a list entry representing a kmod module section
1851 * Get the address of a kmod module section.
1853 * After use, free the @list by calling kmod_module_section_free_list().
1855 * Returns: the address of this kmod module section on success or ULONG_MAX
1858 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1860 struct kmod_module_section
*section
;
1863 return (unsigned long)-1;
1865 section
= entry
->data
;
1866 return section
->address
;
1870 * kmod_module_section_free_list:
1871 * @list: kmod module section list
1873 * Release the resources taken by @list
1875 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
1878 kmod_module_section_free(list
->data
);
1879 list
= kmod_list_remove(list
);
1883 struct kmod_module_info
{
1888 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
, const char *value
, size_t valuelen
)
1890 struct kmod_module_info
*info
;
1892 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
1896 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
)
1898 memcpy(info
->key
, key
, keylen
);
1899 info
->key
[keylen
] = '\0';
1900 memcpy(info
->value
, value
, valuelen
);
1901 info
->value
[valuelen
] = '\0';
1905 static void kmod_module_info_free(struct kmod_module_info
*info
)
1911 * kmod_module_get_info:
1913 * @list: where to return list of module information. Use
1914 * kmod_module_info_get_key() and
1915 * kmod_module_info_get_value(). Release this list with
1916 * kmod_module_info_free_list()
1918 * Get a list of entries in ELF section ".modinfo", these contain
1919 * alias, license, depends, vermagic and other keys with respective
1922 * After use, free the @list by calling kmod_module_info_free_list().
1924 * Returns: 0 on success or < 0 otherwise.
1926 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
, struct kmod_list
**list
)
1928 struct kmod_file
*file
;
1929 struct kmod_elf
*elf
;
1934 int i
, count
, ret
= 0;
1936 if (mod
== NULL
|| list
== NULL
)
1939 assert(*list
== NULL
);
1941 path
= kmod_module_get_path(mod
);
1945 file
= kmod_file_open(mod
->ctx
, path
);
1949 size
= kmod_file_get_size(file
);
1950 mem
= kmod_file_get_contents(file
);
1952 elf
= kmod_elf_new(mem
, size
);
1955 goto elf_open_error
;
1958 count
= kmod_elf_get_strings(elf
, ".modinfo", &strings
);
1961 goto get_strings_error
;
1964 for (i
= 0; i
< count
; i
++) {
1965 struct kmod_module_info
*info
;
1966 struct kmod_list
*n
;
1967 const char *key
, *value
;
1968 size_t keylen
, valuelen
;
1971 value
= strchr(key
, '=');
1972 if (value
== NULL
) {
1973 keylen
= strlen(key
);
1976 keylen
= value
- key
;
1978 valuelen
= strlen(value
);
1981 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
1984 kmod_module_info_free_list(*list
);
1989 n
= kmod_list_append(*list
, info
);
1993 kmod_module_info_free(info
);
1994 kmod_module_info_free_list(*list
);
2005 kmod_elf_unref(elf
);
2007 kmod_file_unref(file
);
2013 * kmod_module_info_get_key:
2014 * @entry: a list entry representing a kmod module info
2016 * Get the key of a kmod module info.
2018 * Returns: the key of this kmod module info on success or NULL on
2019 * failure. The string is owned by the info, do not free it.
2021 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
2023 struct kmod_module_info
*info
;
2033 * kmod_module_info_get_value:
2034 * @entry: a list entry representing a kmod module info
2036 * Get the value of a kmod module info.
2038 * Returns: the value of this kmod module info on success or NULL on
2039 * failure. The string is owned by the info, do not free it.
2041 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
2043 struct kmod_module_info
*info
;
2053 * kmod_module_info_free_list:
2054 * @list: kmod module info list
2056 * Release the resources taken by @list
2058 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
2061 kmod_module_info_free(list
->data
);
2062 list
= kmod_list_remove(list
);
2066 struct kmod_module_version
{
2071 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
, const char *symbol
)
2073 struct kmod_module_version
*mv
;
2074 size_t symbollen
= strlen(symbol
) + 1;
2076 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2081 memcpy(mv
->symbol
, symbol
, symbollen
);
2085 static void kmod_module_version_free(struct kmod_module_version
*version
)
2091 * kmod_module_get_versions:
2093 * @list: where to return list of module versions. Use
2094 * kmod_module_version_get_symbol() and
2095 * kmod_module_version_get_crc(). Release this list with
2096 * kmod_module_versions_free_list()
2098 * Get a list of entries in ELF section "__versions".
2100 * After use, free the @list by calling kmod_module_versions_free_list().
2102 * Returns: 0 on success or < 0 otherwise.
2104 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
, struct kmod_list
**list
)
2106 struct kmod_file
*file
;
2107 struct kmod_elf
*elf
;
2110 struct kmod_modversion
*versions
;
2112 int i
, count
, ret
= 0;
2114 if (mod
== NULL
|| list
== NULL
)
2117 assert(*list
== NULL
);
2119 path
= kmod_module_get_path(mod
);
2123 file
= kmod_file_open(mod
->ctx
, path
);
2127 size
= kmod_file_get_size(file
);
2128 mem
= kmod_file_get_contents(file
);
2130 elf
= kmod_elf_new(mem
, size
);
2133 goto elf_open_error
;
2136 count
= kmod_elf_get_modversions(elf
, &versions
);
2139 goto get_strings_error
;
2142 for (i
= 0; i
< count
; i
++) {
2143 struct kmod_module_version
*mv
;
2144 struct kmod_list
*n
;
2146 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2149 kmod_module_versions_free_list(*list
);
2154 n
= kmod_list_append(*list
, mv
);
2158 kmod_module_version_free(mv
);
2159 kmod_module_versions_free_list(*list
);
2170 kmod_elf_unref(elf
);
2172 kmod_file_unref(file
);
2178 * kmod_module_versions_get_symbol:
2179 * @entry: a list entry representing a kmod module versions
2181 * Get the symbol of a kmod module versions.
2183 * Returns: the symbol of this kmod module versions on success or NULL
2184 * on failure. The string is owned by the versions, do not free it.
2186 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2188 struct kmod_module_version
*version
;
2193 version
= entry
->data
;
2194 return version
->symbol
;
2198 * kmod_module_version_get_crc:
2199 * @entry: a list entry representing a kmod module version
2201 * Get the crc of a kmod module version.
2203 * Returns: the crc of this kmod module version on success or NULL on
2204 * failure. The string is owned by the version, do not free it.
2206 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2208 struct kmod_module_version
*version
;
2213 version
= entry
->data
;
2214 return version
->crc
;
2218 * kmod_module_versions_free_list:
2219 * @list: kmod module versions list
2221 * Release the resources taken by @list
2223 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2226 kmod_module_version_free(list
->data
);
2227 list
= kmod_list_remove(list
);
2231 struct kmod_module_symbol
{
2236 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2238 struct kmod_module_symbol
*mv
;
2239 size_t symbollen
= strlen(symbol
) + 1;
2241 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2246 memcpy(mv
->symbol
, symbol
, symbollen
);
2250 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2256 * kmod_module_get_symbols:
2258 * @list: where to return list of module symbols. Use
2259 * kmod_module_symbol_get_symbol() and
2260 * kmod_module_symbol_get_crc(). Release this list with
2261 * kmod_module_symbols_free_list()
2263 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2265 * After use, free the @list by calling kmod_module_symbols_free_list().
2267 * Returns: 0 on success or < 0 otherwise.
2269 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2271 struct kmod_file
*file
;
2272 struct kmod_elf
*elf
;
2275 struct kmod_modversion
*symbols
;
2277 int i
, count
, ret
= 0;
2279 if (mod
== NULL
|| list
== NULL
)
2282 assert(*list
== NULL
);
2284 path
= kmod_module_get_path(mod
);
2288 file
= kmod_file_open(mod
->ctx
, path
);
2292 size
= kmod_file_get_size(file
);
2293 mem
= kmod_file_get_contents(file
);
2295 elf
= kmod_elf_new(mem
, size
);
2298 goto elf_open_error
;
2301 count
= kmod_elf_get_symbols(elf
, &symbols
);
2304 goto get_strings_error
;
2307 for (i
= 0; i
< count
; i
++) {
2308 struct kmod_module_symbol
*mv
;
2309 struct kmod_list
*n
;
2311 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2314 kmod_module_symbols_free_list(*list
);
2319 n
= kmod_list_append(*list
, mv
);
2323 kmod_module_symbol_free(mv
);
2324 kmod_module_symbols_free_list(*list
);
2335 kmod_elf_unref(elf
);
2337 kmod_file_unref(file
);
2343 * kmod_module_symbol_get_symbol:
2344 * @entry: a list entry representing a kmod module symbols
2346 * Get the symbol of a kmod module symbols.
2348 * Returns: the symbol of this kmod module symbols on success or NULL
2349 * on failure. The string is owned by the symbols, do not free it.
2351 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2353 struct kmod_module_symbol
*symbol
;
2358 symbol
= entry
->data
;
2359 return symbol
->symbol
;
2363 * kmod_module_symbol_get_crc:
2364 * @entry: a list entry representing a kmod module symbol
2366 * Get the crc of a kmod module symbol.
2368 * Returns: the crc of this kmod module symbol on success or NULL on
2369 * failure. The string is owned by the symbol, do not free it.
2371 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2373 struct kmod_module_symbol
*symbol
;
2378 symbol
= entry
->data
;
2383 * kmod_module_symbols_free_list:
2384 * @list: kmod module symbols list
2386 * Release the resources taken by @list
2388 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2391 kmod_module_symbol_free(list
->data
);
2392 list
= kmod_list_remove(list
);
2396 struct kmod_module_dependency_symbol
{
2402 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2404 struct kmod_module_dependency_symbol
*mv
;
2405 size_t symbollen
= strlen(symbol
) + 1;
2407 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2413 memcpy(mv
->symbol
, symbol
, symbollen
);
2417 static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol
*dependency_symbol
)
2419 free(dependency_symbol
);
2423 * kmod_module_get_dependency_symbols:
2425 * @list: where to return list of module dependency_symbols. Use
2426 * kmod_module_dependency_symbol_get_symbol() and
2427 * kmod_module_dependency_symbol_get_crc(). Release this list with
2428 * kmod_module_dependency_symbols_free_list()
2430 * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2432 * After use, free the @list by calling
2433 * kmod_module_dependency_symbols_free_list().
2435 * Returns: 0 on success or < 0 otherwise.
2437 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
, struct kmod_list
**list
)
2439 struct kmod_file
*file
;
2440 struct kmod_elf
*elf
;
2443 struct kmod_modversion
*symbols
;
2445 int i
, count
, ret
= 0;
2447 if (mod
== NULL
|| list
== NULL
)
2450 assert(*list
== NULL
);
2452 path
= kmod_module_get_path(mod
);
2456 file
= kmod_file_open(mod
->ctx
, path
);
2460 size
= kmod_file_get_size(file
);
2461 mem
= kmod_file_get_contents(file
);
2463 elf
= kmod_elf_new(mem
, size
);
2466 goto elf_open_error
;
2469 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2472 goto get_strings_error
;
2475 for (i
= 0; i
< count
; i
++) {
2476 struct kmod_module_dependency_symbol
*mv
;
2477 struct kmod_list
*n
;
2479 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
,
2484 kmod_module_dependency_symbols_free_list(*list
);
2489 n
= kmod_list_append(*list
, mv
);
2493 kmod_module_dependency_symbol_free(mv
);
2494 kmod_module_dependency_symbols_free_list(*list
);
2505 kmod_elf_unref(elf
);
2507 kmod_file_unref(file
);
2513 * kmod_module_dependency_symbol_get_symbol:
2514 * @entry: a list entry representing a kmod module dependency_symbols
2516 * Get the dependency symbol of a kmod module
2518 * Returns: the symbol of this kmod module dependency_symbols on success or NULL
2519 * on failure. The string is owned by the dependency_symbols, do not free it.
2521 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2523 struct kmod_module_dependency_symbol
*dependency_symbol
;
2528 dependency_symbol
= entry
->data
;
2529 return dependency_symbol
->symbol
;
2533 * kmod_module_dependency_symbol_get_crc:
2534 * @entry: a list entry representing a kmod module dependency_symbol
2536 * Get the crc of a kmod module dependency_symbol.
2538 * Returns: the crc of this kmod module dependency_symbol on success or NULL on
2539 * failure. The string is owned by the dependency_symbol, do not free it.
2541 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2543 struct kmod_module_dependency_symbol
*dependency_symbol
;
2548 dependency_symbol
= entry
->data
;
2549 return dependency_symbol
->crc
;
2553 * kmod_module_dependency_symbol_get_bind:
2554 * @entry: a list entry representing a kmod module dependency_symbol
2556 * Get the bind type of a kmod module dependency_symbol.
2558 * Returns: the bind of this kmod module dependency_symbol on success
2559 * or < 0 on failure.
2561 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2563 struct kmod_module_dependency_symbol
*dependency_symbol
;
2568 dependency_symbol
= entry
->data
;
2569 return dependency_symbol
->bind
;
2573 * kmod_module_dependency_symbols_free_list:
2574 * @list: kmod module dependency_symbols list
2576 * Release the resources taken by @list
2578 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2581 kmod_module_dependency_symbol_free(list
->data
);
2582 list
= kmod_list_remove(list
);