1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2011-2013 ProFUSION embedded systems
21 #include <sys/syscall.h>
22 #include <sys/types.h>
25 #include <shared/strbuf.h>
26 #include <shared/util.h>
29 #include "libkmod-internal.h"
31 enum kmod_module_builtin
{
32 KMOD_MODULE_BUILTIN_UNKNOWN
,
33 KMOD_MODULE_BUILTIN_NO
,
34 KMOD_MODULE_BUILTIN_YES
,
42 struct kmod_list
*dep
;
44 const char *install_commands
; /* owned by kmod_config */
45 const char *remove_commands
; /* owned by kmod_config */
46 char *alias
; /* only set if this module was created from an alias */
47 struct kmod_file
*file
;
52 bool install_commands
: 1;
53 bool remove_commands
: 1;
57 * mark if module is builtin, i.e. it's present on modules.builtin
58 * file. This is set as soon as it is needed or as soon as we know
59 * about it, i.e. the module was created from builtin lookup.
61 enum kmod_module_builtin builtin
;
64 * private field used by kmod_module_get_probe_list() to detect
70 * set by kmod_module_get_probe_list: indicates for probe_insert()
71 * whether the module's command and softdep should be ignored
76 * set by kmod_module_get_probe_list: indicates whether this is the
77 * module the user asked for or its dependency, or whether this
83 static inline const char *path_join(const char *path
, size_t prefixlen
, char buf
[PATH_MAX
])
90 pathlen
= strlen(path
);
91 if (prefixlen
+ pathlen
+ 1 >= PATH_MAX
)
94 memcpy(buf
+ prefixlen
, path
, pathlen
+ 1);
98 static inline bool module_is_inkernel(struct kmod_module
*mod
)
100 int state
= kmod_module_get_initstate(mod
);
102 if (state
== KMOD_MODULE_LIVE
|| state
== KMOD_MODULE_BUILTIN
)
108 void kmod_module_parse_depline(struct kmod_module
*mod
, char *line
)
110 struct kmod_ctx
*ctx
= mod
->ctx
;
111 struct kmod_list
*list
= NULL
;
120 assert(mod
->dep
== NULL
);
121 mod
->init
.dep
= true;
123 p
= strchr(line
, ':');
128 dirname
= kmod_get_dirname(mod
->ctx
);
129 dirnamelen
= strlen(dirname
);
130 if (dirnamelen
+ 2 >= sizeof(buf
))
133 memcpy(buf
, dirname
, dirnamelen
);
134 buf
[dirnamelen
] = '/';
136 buf
[dirnamelen
] = '\0';
138 if (mod
->path
== NULL
) {
139 const char *str
= path_join(line
, dirnamelen
, buf
);
142 mod
->path
= strdup(str
);
143 if (mod
->path
== NULL
)
148 for (p
= strtok_r(p
, " \t", &saveptr
); p
!= NULL
;
149 p
= strtok_r(NULL
, " \t", &saveptr
)) {
150 struct kmod_list
*l_new
;
151 struct kmod_module
*depmod
= NULL
;
155 path
= path_join(p
, dirnamelen
, buf
);
157 ERR(ctx
, "could not join path '%s' and '%s'.\n", dirname
, p
);
161 err
= kmod_module_new_from_path(ctx
, path
, &depmod
);
163 ERR(ctx
, "ctx=%p path=%s error=%s\n", ctx
, path
, strerror(-err
));
167 DBG(ctx
, "add dep: %s\n", path
);
169 l_new
= kmod_list_prepend(list
, depmod
);
171 ERR(ctx
, "could not add dependency for %s\n", mod
->name
);
178 DBG(ctx
, "%zu dependencies for %s\n", n
, mod
->name
);
184 kmod_module_unref_list(list
);
185 mod
->init
.dep
= false;
188 void kmod_module_set_visited(struct kmod_module
*mod
, bool visited
)
190 mod
->visited
= visited
;
193 void kmod_module_set_builtin(struct kmod_module
*mod
, bool builtin
)
195 mod
->builtin
= builtin
? KMOD_MODULE_BUILTIN_YES
: KMOD_MODULE_BUILTIN_NO
;
198 void kmod_module_set_required(struct kmod_module
*mod
, bool required
)
200 mod
->required
= required
;
203 bool kmod_module_is_builtin(struct kmod_module
*mod
)
205 if (mod
->builtin
== KMOD_MODULE_BUILTIN_UNKNOWN
) {
206 kmod_module_set_builtin(mod
, kmod_lookup_alias_is_builtin(mod
->ctx
,
210 return mod
->builtin
== KMOD_MODULE_BUILTIN_YES
;
213 * Memory layout with alias:
215 * struct kmod_module {
220 * name <----------' | |
221 * alias <-----------' |
222 * name\alias <--------'
224 * Memory layout without alias:
226 * struct kmod_module {
228 * alias -----|----> NULL
231 * name <----------'-'
233 * @key is "name\alias" or "name" (in which case alias == NULL)
235 /* TODO: rework to create the hash within this function and remove the _maybe_unused_
237 static int kmod_module_new(struct kmod_ctx
*ctx
, const char *key
,
238 _maybe_unused_
const char *name
, size_t namelen
,
239 const char *alias
, size_t aliaslen
, struct kmod_module
**mod
)
241 struct kmod_module
*m
;
245 m
= kmod_pool_get_module(ctx
, key
);
247 *mod
= kmod_module_ref(m
);
254 keylen
= namelen
+ aliaslen
+ 1;
256 m
= malloc(sizeof(*m
) + (alias
== NULL
? 1 : 2) * (keylen
+ 1));
260 memset(m
, 0, sizeof(*m
));
262 m
->ctx
= kmod_ref(ctx
);
263 m
->name
= (char *)m
+ sizeof(*m
);
264 memcpy(m
->name
, key
, keylen
+ 1);
266 m
->hashkey
= m
->name
;
269 m
->name
[namelen
] = '\0';
270 m
->alias
= m
->name
+ namelen
+ 1;
271 m
->hashkey
= m
->name
+ keylen
+ 1;
272 memcpy(m
->hashkey
, key
, keylen
+ 1);
276 err
= kmod_pool_add_module(ctx
, m
, m
->hashkey
);
286 KMOD_EXPORT
int kmod_module_new_from_name(struct kmod_ctx
*ctx
, const char *name
,
287 struct kmod_module
**mod
)
290 char name_norm
[PATH_MAX
];
292 if (ctx
== NULL
|| name
== NULL
|| mod
== NULL
)
295 modname_normalize(name
, name_norm
, &namelen
);
297 return kmod_module_new(ctx
, name_norm
, name_norm
, namelen
, NULL
, 0, mod
);
300 int kmod_module_new_from_alias(struct kmod_ctx
*ctx
, const char *alias
, const char *name
,
301 struct kmod_module
**mod
)
304 size_t namelen
= strlen(name
);
305 size_t aliaslen
= strlen(alias
);
307 if (namelen
+ aliaslen
+ 2 > sizeof(key
))
308 return -ENAMETOOLONG
;
310 memcpy(key
, name
, namelen
);
311 memcpy(key
+ namelen
+ 1, alias
, aliaslen
+ 1);
314 return kmod_module_new(ctx
, key
, name
, namelen
, alias
, aliaslen
, mod
);
317 KMOD_EXPORT
int kmod_module_new_from_path(struct kmod_ctx
*ctx
, const char *path
,
318 struct kmod_module
**mod
)
320 struct kmod_module
*m
;
327 if (ctx
== NULL
|| path
== NULL
|| mod
== NULL
)
330 abspath
= path_make_absolute_cwd(path
);
331 if (abspath
== NULL
) {
332 DBG(ctx
, "no absolute path for %s\n", path
);
336 err
= stat(abspath
, &st
);
339 DBG(ctx
, "stat %s: %m\n", path
);
344 if (path_to_modname(path
, name
, &namelen
) == NULL
) {
345 DBG(ctx
, "could not get modname from path %s\n", path
);
350 err
= kmod_module_new(ctx
, name
, name
, namelen
, NULL
, 0, &m
);
357 else if (streq(m
->path
, abspath
))
360 kmod_module_unref(m
);
362 "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
363 name
, abspath
, m
->path
);
368 m
->builtin
= KMOD_MODULE_BUILTIN_NO
;
374 KMOD_EXPORT
struct kmod_module
*kmod_module_unref(struct kmod_module
*mod
)
379 if (--mod
->refcount
> 0)
382 DBG(mod
->ctx
, "kmod_module %p released\n", mod
);
384 kmod_pool_del_module(mod
->ctx
, mod
, mod
->hashkey
);
385 kmod_module_unref_list(mod
->dep
);
388 kmod_file_unref(mod
->file
);
390 kmod_unref(mod
->ctx
);
397 KMOD_EXPORT
struct kmod_module
*kmod_module_ref(struct kmod_module
*mod
)
407 typedef _nonnull_all_
int (*lookup_func
)(struct kmod_ctx
*ctx
, const char *name
,
408 struct kmod_list
**list
);
410 static int __kmod_module_new_from_lookup(struct kmod_ctx
*ctx
, const lookup_func lookup
[],
411 size_t lookup_count
, const char *s
,
412 struct kmod_list
**list
)
416 for (i
= 0; i
< lookup_count
; i
++) {
419 err
= lookup
[i
](ctx
, s
, list
);
420 if (err
< 0 && err
!= -ENOSYS
)
422 else if (*list
!= NULL
)
429 KMOD_EXPORT
int kmod_module_new_from_lookup(struct kmod_ctx
*ctx
, const char *given_alias
,
430 struct kmod_list
**list
)
432 static const lookup_func lookup
[] = {
433 kmod_lookup_alias_from_config
,
434 kmod_lookup_alias_from_moddep_file
,
435 kmod_lookup_alias_from_symbols_file
,
436 kmod_lookup_alias_from_commands
,
437 kmod_lookup_alias_from_aliases_file
,
438 kmod_lookup_alias_from_builtin_file
,
439 kmod_lookup_alias_from_kernel_builtin_file
,
441 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 err
= __kmod_module_new_from_lookup(ctx
, lookup
, ARRAY_SIZE(lookup
), alias
, list
);
461 DBG(ctx
, "lookup=%s found=%d\n", alias
, err
>= 0 && *list
);
464 kmod_module_unref_list(*list
);
471 KMOD_EXPORT
int kmod_module_new_from_name_lookup(struct kmod_ctx
*ctx
,
473 struct kmod_module
**mod
)
475 static const lookup_func lookup
[] = {
476 kmod_lookup_alias_from_moddep_file
,
477 kmod_lookup_alias_from_builtin_file
,
478 kmod_lookup_alias_from_kernel_builtin_file
,
480 char name_norm
[PATH_MAX
];
481 struct kmod_list
*list
= NULL
;
484 if (ctx
== NULL
|| modname
== NULL
|| mod
== NULL
)
487 modname_normalize(modname
, name_norm
, NULL
);
489 DBG(ctx
, "input modname=%s, normalized=%s\n", modname
, name_norm
);
491 err
= __kmod_module_new_from_lookup(ctx
, lookup
, ARRAY_SIZE(lookup
), name_norm
,
494 DBG(ctx
, "lookup=%s found=%d\n", name_norm
, err
>= 0 && list
);
496 if (err
>= 0 && list
!= NULL
)
497 *mod
= kmod_module_get_module(list
);
499 kmod_module_unref_list(list
);
504 KMOD_EXPORT
int kmod_module_unref_list(struct kmod_list
*list
)
506 kmod_list_release(list
, kmod_module_unref
);
511 KMOD_EXPORT
int kmod_module_get_filtered_blacklist(const struct kmod_ctx
*ctx
,
512 const struct kmod_list
*input
,
513 struct kmod_list
**output
)
515 return kmod_module_apply_filter(ctx
, KMOD_FILTER_BLACKLIST
, input
, output
);
518 static void module_get_dependencies_noref(struct kmod_module
*mod
)
520 if (!mod
->init
.dep
) {
522 char *line
= kmod_search_moddep(mod
->ctx
, mod
->name
);
525 kmod_module_parse_depline(mod
, line
);
531 KMOD_EXPORT
struct kmod_list
*kmod_module_get_dependencies(const struct kmod_module
*mod
)
533 struct kmod_list
*l
, *l_new
, *list_new
= NULL
;
538 module_get_dependencies_noref((struct kmod_module
*)mod
);
540 kmod_list_foreach(l
, mod
->dep
) {
541 l_new
= kmod_list_append(list_new
, kmod_module_ref(l
->data
));
543 kmod_module_unref(l
->data
);
553 ERR(mod
->ctx
, "out of memory\n");
554 kmod_module_unref_list(list_new
);
558 KMOD_EXPORT
struct kmod_module
*kmod_module_get_module(const struct kmod_list
*entry
)
563 return kmod_module_ref(entry
->data
);
566 KMOD_EXPORT
const char *kmod_module_get_name(const struct kmod_module
*mod
)
574 KMOD_EXPORT
const char *kmod_module_get_path(const struct kmod_module
*mod
)
579 DBG(mod
->ctx
, "name='%s' path='%s'\n", mod
->name
, mod
->path
);
581 if (mod
->path
!= NULL
)
587 module_get_dependencies_noref((struct kmod_module
*)mod
);
592 extern long delete_module(const char *name
, unsigned int flags
);
594 KMOD_EXPORT
int kmod_module_remove_module(struct kmod_module
*mod
, unsigned int flags
)
596 unsigned int libkmod_flags
= flags
& 0xff;
603 /* Filter out other flags and force ONONBLOCK */
604 flags
&= KMOD_REMOVE_FORCE
;
605 flags
|= KMOD_REMOVE_NOWAIT
;
607 err
= delete_module(mod
->name
, flags
);
610 if (!(libkmod_flags
& KMOD_REMOVE_NOLOG
))
611 ERR(mod
->ctx
, "could not remove '%s': %m\n", mod
->name
);
617 extern long init_module(const void *mem
, unsigned long len
, const char *args
);
619 static int do_finit_module(struct kmod_module
*mod
, unsigned int flags
, const char *args
)
621 enum kmod_file_compression_type compression
, kernel_compression
;
622 unsigned int kernel_flags
= 0;
626 * When module is not compressed or its compression type matches the
627 * one in use by the kernel, there is no need to read the file
628 * in userspace. Otherwise, reuse ENOSYS to trigger the same fallback
629 * as when finit_module() is not supported.
631 compression
= kmod_file_get_compression(mod
->file
);
632 kernel_compression
= kmod_get_kernel_compression(mod
->ctx
);
633 if (!(compression
== KMOD_FILE_COMPRESSION_NONE
||
634 compression
== kernel_compression
))
637 if (compression
!= KMOD_FILE_COMPRESSION_NONE
)
638 kernel_flags
|= MODULE_INIT_COMPRESSED_FILE
;
640 if (flags
& KMOD_INSERT_FORCE_VERMAGIC
)
641 kernel_flags
|= MODULE_INIT_IGNORE_VERMAGIC
;
642 if (flags
& KMOD_INSERT_FORCE_MODVERSION
)
643 kernel_flags
|= MODULE_INIT_IGNORE_MODVERSIONS
;
645 err
= finit_module(kmod_file_get_fd(mod
->file
), args
, kernel_flags
);
652 static int do_init_module(struct kmod_module
*mod
, unsigned int flags
, const char *args
)
654 _cleanup_free_
const void *stripped
= NULL
;
655 struct kmod_elf
*elf
;
660 if (flags
& (KMOD_INSERT_FORCE_VERMAGIC
| KMOD_INSERT_FORCE_MODVERSION
)) {
661 err
= kmod_file_get_elf(mod
->file
, &elf
);
665 err
= kmod_elf_strip(elf
, flags
, &stripped
);
667 ERR(mod
->ctx
, "Failed to strip version information: %s\n",
673 err
= kmod_file_load_contents(mod
->file
);
677 mem
= kmod_file_get_contents(mod
->file
);
679 size
= kmod_file_get_size(mod
->file
);
681 err
= init_module(mem
, size
, args
);
688 KMOD_EXPORT
int kmod_module_insert_module(struct kmod_module
*mod
, unsigned int flags
,
693 const char *args
= options
? options
: "";
698 path
= kmod_module_get_path(mod
);
700 ERR(mod
->ctx
, "could not find module by name='%s'\n", mod
->name
);
705 err
= kmod_file_open(mod
->ctx
, path
, &mod
->file
);
710 err
= do_finit_module(mod
, flags
, args
);
712 err
= do_init_module(mod
, flags
, args
);
715 INFO(mod
->ctx
, "Failed to insert module '%s': %s\n", path
, strerror(-err
));
720 static bool module_is_blacklisted(const struct kmod_module
*mod
)
722 const struct kmod_ctx
*ctx
= mod
->ctx
;
723 const struct kmod_config
*config
= kmod_get_config(ctx
);
724 const struct kmod_list
*bl
= config
->blacklists
;
725 const struct kmod_list
*l
;
727 kmod_list_foreach(l
, bl
) {
728 const char *modname
= kmod_blacklist_get_modname(l
);
730 if (streq(modname
, mod
->name
))
737 KMOD_EXPORT
int kmod_module_apply_filter(const struct kmod_ctx
*ctx
,
738 enum kmod_filter filter_type
,
739 const struct kmod_list
*input
,
740 struct kmod_list
**output
)
742 const struct kmod_list
*li
;
744 if (ctx
== NULL
|| output
== NULL
)
751 kmod_list_foreach(li
, input
) {
752 struct kmod_module
*mod
= li
->data
;
753 struct kmod_list
*node
;
755 if ((filter_type
& KMOD_FILTER_BLACKLIST
) && module_is_blacklisted(mod
))
758 if ((filter_type
& KMOD_FILTER_BUILTIN
) && kmod_module_is_builtin(mod
))
761 node
= kmod_list_append(*output
, mod
);
766 kmod_module_ref(mod
);
772 kmod_module_unref_list(*output
);
777 static int command_do(struct kmod_module
*mod
, const char *type
, const char *cmd
)
779 const char *modname
= kmod_module_get_name(mod
);
782 DBG(mod
->ctx
, "%s %s\n", type
, cmd
);
784 setenv("MODPROBE_MODULE", modname
, 1);
786 unsetenv("MODPROBE_MODULE");
789 ERR(mod
->ctx
, "Could not run %s command '%s' for module %s: %m\n", type
,
794 if (WEXITSTATUS(err
)) {
795 ERR(mod
->ctx
, "Error running %s command '%s' for module %s: retcode %d\n",
796 type
, cmd
, modname
, WEXITSTATUS(err
));
803 struct probe_insert_cb
{
804 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
);
808 static int module_do_install_commands(struct kmod_module
*mod
, const char *options
,
809 struct probe_insert_cb
*cb
)
811 const char *command
= kmod_module_get_install_commands(mod
);
813 _cleanup_free_
char *cmd
;
815 size_t cmdlen
, options_len
, varlen
;
822 options_len
= strlen(options
);
823 cmdlen
= strlen(command
);
824 varlen
= strlen("$CMDLINE_OPTS");
826 cmd
= memdup(command
, cmdlen
+ 1);
830 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
831 size_t prefixlen
= p
- cmd
;
832 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
833 size_t slen
= cmdlen
- varlen
+ options_len
;
834 char *suffix
= p
+ varlen
;
835 _clang_suppress_alloc_
char *s
= malloc(slen
+ 1);
839 memcpy(s
, cmd
, p
- cmd
);
840 memcpy(s
+ prefixlen
, options
, options_len
);
841 memcpy(s
+ prefixlen
+ options_len
, suffix
, suffixlen
);
849 if (cb
->run_install
!= NULL
)
850 err
= cb
->run_install(mod
, cmd
, cb
->data
);
852 err
= command_do(mod
, "install", cmd
);
857 static char *module_options_concat(const char *opt
, const char *xopt
)
859 // TODO: we might need to check if xopt overrides options on opt
860 size_t optlen
= opt
== NULL
? 0 : strlen(opt
);
861 size_t xoptlen
= xopt
== NULL
? 0 : strlen(xopt
);
864 if (optlen
== 0 && xoptlen
== 0)
867 r
= malloc(optlen
+ xoptlen
+ 2);
870 memcpy(r
, opt
, optlen
);
876 memcpy(r
+ optlen
, xopt
, xoptlen
);
878 r
[optlen
+ xoptlen
] = '\0';
883 static int __kmod_module_get_probe_list(struct kmod_module
*mod
, bool required
,
884 bool ignorecmd
, struct kmod_list
**list
);
887 static int __kmod_module_fill_softdep(struct kmod_module
*mod
, struct kmod_list
**list
)
889 struct kmod_list
*pre
= NULL
, *post
= NULL
, *l
;
892 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
894 ERR(mod
->ctx
, "could not get softdep: %s\n", strerror(-err
));
898 kmod_list_foreach(l
, pre
) {
899 struct kmod_module
*m
= l
->data
;
900 err
= __kmod_module_get_probe_list(m
, false, false, list
);
905 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
907 kmod_module_unref(mod
);
912 mod
->ignorecmd
= (pre
!= NULL
|| post
!= NULL
);
914 kmod_list_foreach(l
, post
) {
915 struct kmod_module
*m
= l
->data
;
916 err
= __kmod_module_get_probe_list(m
, false, false, list
);
922 kmod_module_unref_list(pre
);
923 kmod_module_unref_list(post
);
929 static int __kmod_module_get_probe_list(struct kmod_module
*mod
, bool required
,
930 bool ignorecmd
, struct kmod_list
**list
)
932 struct kmod_list
*dep
, *l
;
936 DBG(mod
->ctx
, "Ignore module '%s': already visited\n", mod
->name
);
941 dep
= kmod_module_get_dependencies(mod
);
944 * Called from kmod_module_probe_insert_module(); set the
945 * ->required flag on mod and all its dependencies before
946 * they are possibly visited through some softdeps.
948 mod
->required
= true;
949 kmod_list_foreach(l
, dep
) {
950 struct kmod_module
*m
= l
->data
;
955 kmod_list_foreach(l
, dep
) {
956 struct kmod_module
*m
= l
->data
;
957 err
= __kmod_module_fill_softdep(m
, list
);
963 l
= kmod_list_append(*list
, kmod_module_ref(mod
));
965 kmod_module_unref(mod
);
970 mod
->ignorecmd
= true;
972 err
= __kmod_module_fill_softdep(mod
, list
);
975 kmod_module_unref_list(dep
);
979 static int kmod_module_get_probe_list(struct kmod_module
*mod
, bool ignorecmd
,
980 struct kmod_list
**list
)
985 assert(list
!= NULL
&& *list
== NULL
);
988 * Make sure we don't get screwed by previous calls to this function
990 kmod_set_modules_visited(mod
->ctx
, false);
991 kmod_set_modules_required(mod
->ctx
, false);
993 err
= __kmod_module_get_probe_list(mod
, true, ignorecmd
, list
);
995 kmod_module_unref_list(*list
);
1002 KMOD_EXPORT
int kmod_module_probe_insert_module(
1003 struct kmod_module
*mod
, unsigned int flags
, const char *extra_options
,
1004 int (*run_install
)(struct kmod_module
*m
, const char *cmd
, void *data
),
1006 void (*print_action
)(struct kmod_module
*m
, bool install
, const char *options
))
1008 struct kmod_list
*list
= NULL
, *l
;
1009 struct probe_insert_cb cb
;
1015 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
) && module_is_inkernel(mod
)) {
1016 if (flags
& KMOD_PROBE_FAIL_ON_LOADED
)
1022 if (module_is_blacklisted(mod
)) {
1023 if (mod
->alias
!= NULL
&& (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
))
1024 return KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
;
1026 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
)
1027 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1029 if (flags
& KMOD_PROBE_APPLY_BLACKLIST
)
1030 return KMOD_PROBE_APPLY_BLACKLIST
;
1033 err
= kmod_module_get_probe_list(mod
, !!(flags
& KMOD_PROBE_IGNORE_COMMAND
),
1038 if (flags
& KMOD_PROBE_APPLY_BLACKLIST_ALL
) {
1039 struct kmod_list
*filtered
= NULL
;
1041 err
= kmod_module_apply_filter(mod
->ctx
, KMOD_FILTER_BLACKLIST
, list
,
1046 kmod_module_unref_list(list
);
1047 if (filtered
== NULL
)
1048 return KMOD_PROBE_APPLY_BLACKLIST_ALL
;
1053 cb
.run_install
= run_install
;
1054 cb
.data
= (void *)data
;
1056 kmod_list_foreach(l
, list
) {
1057 struct kmod_module
*m
= l
->data
;
1058 const char *moptions
= kmod_module_get_options(m
);
1059 const char *cmd
= kmod_module_get_install_commands(m
);
1062 if (!(flags
& KMOD_PROBE_IGNORE_LOADED
) && module_is_inkernel(m
)) {
1063 DBG(mod
->ctx
, "Ignoring module '%s': already loaded\n", m
->name
);
1069 module_options_concat(moptions
, m
== mod
? extra_options
: NULL
);
1071 if (cmd
!= NULL
&& !m
->ignorecmd
) {
1072 if (print_action
!= NULL
)
1073 print_action(m
, true, options
?: "");
1075 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1076 err
= module_do_install_commands(m
, options
, &cb
);
1078 if (print_action
!= NULL
)
1079 print_action(m
, false, options
?: "");
1081 if (!(flags
& KMOD_PROBE_DRY_RUN
))
1082 err
= kmod_module_insert_module(m
, flags
, options
);
1089 * Treat "already loaded" error. If we were told to stop on
1090 * already loaded and the module being loaded is not a softdep
1091 * or dep, bail out. Otherwise, just ignore and continue.
1093 * We need to check here because of race conditions. We
1094 * checked first if module was already loaded but it may have
1095 * been loaded between the check and the moment we try to
1098 if (err
== -EEXIST
&& m
== mod
&& (flags
& KMOD_PROBE_FAIL_ON_LOADED
))
1102 * Ignore errors from softdeps
1104 if (err
== -EEXIST
|| !m
->required
)
1111 kmod_module_unref_list(list
);
1115 KMOD_EXPORT
const char *kmod_module_get_options(const struct kmod_module
*mod
)
1120 if (!mod
->init
.options
) {
1122 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1123 const struct kmod_list
*l
;
1124 const struct kmod_config
*config
;
1128 config
= kmod_get_config(mod
->ctx
);
1130 kmod_list_foreach(l
, config
->options
) {
1131 const char *modname
= kmod_option_get_modname(l
);
1136 DBG(mod
->ctx
, "modname=%s mod->name=%s mod->alias=%s\n", modname
,
1137 mod
->name
, mod
->alias
);
1138 if (!(streq(modname
, mod
->name
) ||
1139 (mod
->alias
!= NULL
&& streq(modname
, mod
->alias
))))
1142 DBG(mod
->ctx
, "passed = modname=%s mod->name=%s mod->alias=%s\n",
1143 modname
, mod
->name
, mod
->alias
);
1144 str
= kmod_option_get_options(l
);
1149 tmp
= realloc(opts
, optslen
+ len
+ 2);
1158 opts
[optslen
] = ' ';
1162 memcpy(opts
+ optslen
, str
, len
);
1164 opts
[optslen
] = '\0';
1167 m
->init
.options
= true;
1171 return mod
->options
;
1174 ERR(mod
->ctx
, "out of memory\n");
1178 KMOD_EXPORT
const char *kmod_module_get_install_commands(const struct kmod_module
*mod
)
1183 if (!mod
->init
.install_commands
) {
1185 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1186 const struct kmod_list
*l
;
1187 const struct kmod_config
*config
;
1189 config
= kmod_get_config(mod
->ctx
);
1191 kmod_list_foreach(l
, config
->install_commands
) {
1192 const char *modname
= kmod_command_get_modname(l
);
1194 if (fnmatch(modname
, mod
->name
, 0) != 0)
1197 m
->install_commands
= kmod_command_get_command(l
);
1200 * find only the first command, as modprobe from
1201 * module-init-tools does
1206 m
->init
.install_commands
= true;
1209 return mod
->install_commands
;
1212 void kmod_module_set_install_commands(struct kmod_module
*mod
, const char *cmd
)
1214 mod
->init
.install_commands
= true;
1215 mod
->install_commands
= cmd
;
1218 static struct kmod_list
*lookup_dep(struct kmod_ctx
*ctx
, const char *const *array
,
1221 struct kmod_list
*ret
= NULL
;
1224 for (i
= 0; i
< count
; i
++) {
1225 const char *depname
= array
[i
];
1226 struct kmod_list
*lst
= NULL
;
1229 err
= kmod_module_new_from_lookup(ctx
, depname
, &lst
);
1231 ERR(ctx
, "failed to lookup dependency '%s', continuing anyway.\n",
1234 } else if (lst
!= NULL
)
1235 ret
= kmod_list_append_list(ret
, lst
);
1240 KMOD_EXPORT
int kmod_module_get_softdeps(const struct kmod_module
*mod
,
1241 struct kmod_list
**pre
, struct kmod_list
**post
)
1243 const struct kmod_list
*l
;
1244 const struct kmod_config
*config
;
1246 if (mod
== NULL
|| pre
== NULL
|| post
== NULL
)
1249 assert(*pre
== NULL
);
1250 assert(*post
== NULL
);
1252 config
= kmod_get_config(mod
->ctx
);
1254 kmod_list_foreach(l
, config
->softdeps
) {
1255 const char *modname
= kmod_softdep_get_name(l
);
1256 const char *const *array
;
1259 if (fnmatch(modname
, mod
->name
, 0) != 0)
1262 array
= kmod_softdep_get_pre(l
, &count
);
1263 *pre
= lookup_dep(mod
->ctx
, array
, count
);
1264 array
= kmod_softdep_get_post(l
, &count
);
1265 *post
= lookup_dep(mod
->ctx
, array
, count
);
1268 * find only the first command, as modprobe from
1269 * module-init-tools does
1277 KMOD_EXPORT
int kmod_module_get_weakdeps(const struct kmod_module
*mod
,
1278 struct kmod_list
**weak
)
1280 const struct kmod_list
*l
;
1281 const struct kmod_config
*config
;
1283 if (mod
== NULL
|| weak
== NULL
)
1286 assert(*weak
== NULL
);
1288 config
= kmod_get_config(mod
->ctx
);
1290 kmod_list_foreach(l
, config
->weakdeps
) {
1291 const char *modname
= kmod_weakdep_get_name(l
);
1292 const char *const *array
;
1295 if (fnmatch(modname
, mod
->name
, 0) != 0)
1298 array
= kmod_weakdep_get_weak(l
, &count
);
1299 *weak
= lookup_dep(mod
->ctx
, array
, count
);
1302 * find only the first command, as modprobe from
1303 * module-init-tools does
1311 KMOD_EXPORT
const char *kmod_module_get_remove_commands(const struct kmod_module
*mod
)
1316 if (!mod
->init
.remove_commands
) {
1318 struct kmod_module
*m
= (struct kmod_module
*)mod
;
1319 const struct kmod_list
*l
;
1320 const struct kmod_config
*config
;
1322 config
= kmod_get_config(mod
->ctx
);
1324 kmod_list_foreach(l
, config
->remove_commands
) {
1325 const char *modname
= kmod_command_get_modname(l
);
1327 if (fnmatch(modname
, mod
->name
, 0) != 0)
1330 m
->remove_commands
= kmod_command_get_command(l
);
1333 * find only the first command, as modprobe from
1334 * module-init-tools does
1339 m
->init
.remove_commands
= true;
1342 return mod
->remove_commands
;
1345 void kmod_module_set_remove_commands(struct kmod_module
*mod
, const char *cmd
)
1347 mod
->init
.remove_commands
= true;
1348 mod
->remove_commands
= cmd
;
1351 KMOD_EXPORT
int kmod_module_new_from_loaded(struct kmod_ctx
*ctx
, struct kmod_list
**list
)
1353 struct kmod_list
*l
= NULL
;
1357 if (ctx
== NULL
|| list
== NULL
)
1360 fp
= fopen("/proc/modules", "re");
1363 ERR(ctx
, "could not open /proc/modules: %m\n");
1367 while (fgets(line
, sizeof(line
), fp
)) {
1368 struct kmod_module
*m
;
1369 struct kmod_list
*node
;
1371 size_t len
= strlen(line
);
1372 char *saveptr
, *name
= strtok_r(line
, " \t", &saveptr
);
1374 err
= kmod_module_new_from_name(ctx
, name
, &m
);
1376 ERR(ctx
, "could not get module from name '%s': %s\n", name
,
1381 node
= kmod_list_append(l
, m
);
1385 ERR(ctx
, "out of memory\n");
1386 kmod_module_unref(m
);
1389 while (len
> 0 && line
[len
- 1] != '\n' && fgets(line
, sizeof(line
), fp
))
1399 KMOD_EXPORT
const char *kmod_module_initstate_str(enum kmod_module_initstate state
)
1402 case KMOD_MODULE_BUILTIN
:
1404 case KMOD_MODULE_LIVE
:
1406 case KMOD_MODULE_COMING
:
1408 case KMOD_MODULE_GOING
:
1415 KMOD_EXPORT
int kmod_module_get_initstate(const struct kmod_module
*mod
)
1417 char path
[PATH_MAX
], buf
[32];
1418 int fd
, err
, pathlen
;
1423 /* remove const: this can only change internal state */
1424 if (kmod_module_is_builtin((struct kmod_module
*)mod
))
1425 return KMOD_MODULE_BUILTIN
;
1427 pathlen
= snprintf(path
, sizeof(path
), "/sys/module/%s/initstate", mod
->name
);
1428 if (pathlen
>= (int)sizeof(path
)) {
1429 /* Too long path was truncated */
1430 return -ENAMETOOLONG
;
1432 fd
= open(path
, O_RDONLY
| O_CLOEXEC
);
1435 DBG(mod
->ctx
, "could not open '%s': %m\n", path
);
1437 if (pathlen
> (int)strlen("/initstate")) {
1439 path
[pathlen
- strlen("/initstate")] = '\0';
1440 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
1441 return KMOD_MODULE_COMING
;
1444 DBG(mod
->ctx
, "could not open '%s': %m\n", path
);
1449 err
= read_str_safe(fd
, buf
, sizeof(buf
));
1452 ERR(mod
->ctx
, "could not read from '%s': %s\n", path
, strerror(-err
));
1456 if (streq(buf
, "live\n"))
1457 return KMOD_MODULE_LIVE
;
1458 else if (streq(buf
, "coming\n"))
1459 return KMOD_MODULE_COMING
;
1460 else if (streq(buf
, "going\n"))
1461 return KMOD_MODULE_GOING
;
1463 ERR(mod
->ctx
, "unknown %s: '%s'\n", path
, buf
);
1467 KMOD_EXPORT
long kmod_module_get_size(const struct kmod_module
*mod
)
1472 long size
= -ENOENT
;
1478 /* try to open the module dir in /sys. If this fails, don't
1479 * bother trying to find the size as we know the module isn't
1482 snprintf(line
, sizeof(line
), "/sys/module/%s", mod
->name
);
1483 dfd
= open(line
, O_RDONLY
| O_CLOEXEC
);
1487 /* available as of linux 3.3.x */
1488 cfd
= openat(dfd
, "coresize", O_RDONLY
| O_CLOEXEC
);
1490 if (read_str_long(cfd
, &size
, 10) < 0)
1491 ERR(mod
->ctx
, "failed to read coresize from %s\n", line
);
1496 /* fall back on parsing /proc/modules */
1497 fp
= fopen("/proc/modules", "re");
1500 ERR(mod
->ctx
, "could not open /proc/modules: %m\n");
1505 while (fgets(line
, sizeof(line
), fp
)) {
1506 size_t len
= strlen(line
);
1507 char *saveptr
, *endptr
, *tok
= strtok_r(line
, " \t", &saveptr
);
1511 if (tok
== NULL
|| !streq(tok
, mod
->name
))
1514 tok
= strtok_r(NULL
, " \t", &saveptr
);
1516 ERR(mod
->ctx
, "invalid line format at /proc/modules:%d\n", lineno
);
1521 value
= strtol(tok
, &endptr
, 10);
1522 if (endptr
== tok
|| *endptr
!= '\0' || errno
== ERANGE
|| value
< 0) {
1523 ERR(mod
->ctx
, "invalid line format at /proc/modules:%d\n", lineno
);
1530 while (len
> 0 && line
[len
- 1] != '\n' && fgets(line
, sizeof(line
), fp
))
1540 KMOD_EXPORT
int kmod_module_get_refcnt(const struct kmod_module
*mod
)
1542 char path
[PATH_MAX
];
1549 snprintf(path
, sizeof(path
), "/sys/module/%s/refcnt", mod
->name
);
1550 fd
= open(path
, O_RDONLY
| O_CLOEXEC
);
1553 DBG(mod
->ctx
, "could not open '%s': %m\n", path
);
1557 err
= read_str_long(fd
, &refcnt
, 10);
1560 ERR(mod
->ctx
, "could not read integer from '%s': '%s'\n", path
,
1568 KMOD_EXPORT
struct kmod_list
*kmod_module_get_holders(const struct kmod_module
*mod
)
1570 char dname
[PATH_MAX
];
1571 struct kmod_list
*list
= NULL
;
1572 struct dirent
*dent
;
1575 if (mod
== NULL
|| mod
->ctx
== NULL
)
1578 snprintf(dname
, sizeof(dname
), "/sys/module/%s/holders", mod
->name
);
1582 ERR(mod
->ctx
, "could not open '%s': %m\n", dname
);
1586 for (dent
= readdir(d
); dent
!= NULL
; dent
= readdir(d
)) {
1587 struct kmod_module
*holder
;
1588 struct kmod_list
*l
;
1591 if (dent
->d_name
[0] == '.') {
1592 if (dent
->d_name
[1] == '\0' ||
1593 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1597 err
= kmod_module_new_from_name(mod
->ctx
, dent
->d_name
, &holder
);
1599 ERR(mod
->ctx
, "could not create module for '%s': %s\n",
1600 dent
->d_name
, strerror(-err
));
1604 l
= kmod_list_append(list
, holder
);
1608 ERR(mod
->ctx
, "out of memory\n");
1609 kmod_module_unref(holder
);
1619 kmod_module_unref_list(list
);
1623 struct kmod_module_section
{
1624 unsigned long address
;
1628 static void kmod_module_section_free(struct kmod_module_section
*section
)
1633 KMOD_EXPORT
struct kmod_list
*kmod_module_get_sections(const struct kmod_module
*mod
)
1635 char dname
[PATH_MAX
];
1636 struct kmod_list
*list
= NULL
;
1637 struct dirent
*dent
;
1644 snprintf(dname
, sizeof(dname
), "/sys/module/%s/sections", mod
->name
);
1648 ERR(mod
->ctx
, "could not open '%s': %m\n", dname
);
1654 for (dent
= readdir(d
); dent
; dent
= readdir(d
)) {
1655 struct kmod_module_section
*section
;
1656 struct kmod_list
*l
;
1657 unsigned long address
;
1661 if (dent
->d_name
[0] == '.') {
1662 if (dent
->d_name
[1] == '\0' ||
1663 (dent
->d_name
[1] == '.' && dent
->d_name
[2] == '\0'))
1667 fd
= openat(dfd
, dent
->d_name
, O_RDONLY
| O_CLOEXEC
);
1669 ERR(mod
->ctx
, "could not open '%s/%s': %m\n", dname
, dent
->d_name
);
1673 err
= read_str_ulong(fd
, &address
, 16);
1676 ERR(mod
->ctx
, "could not read long from '%s/%s': %s\n", dname
,
1677 dent
->d_name
, strerror(-err
));
1681 namesz
= strlen(dent
->d_name
) + 1;
1682 section
= malloc(sizeof(*section
) + namesz
);
1684 if (section
== NULL
) {
1685 ERR(mod
->ctx
, "out of memory\n");
1689 section
->address
= address
;
1690 memcpy(section
->name
, dent
->d_name
, namesz
);
1692 l
= kmod_list_append(list
, section
);
1696 ERR(mod
->ctx
, "out of memory\n");
1707 kmod_module_unref_list(list
);
1711 KMOD_EXPORT
const char *kmod_module_section_get_name(const struct kmod_list
*entry
)
1713 struct kmod_module_section
*section
;
1718 section
= entry
->data
;
1719 return section
->name
;
1722 KMOD_EXPORT
unsigned long kmod_module_section_get_address(const struct kmod_list
*entry
)
1724 struct kmod_module_section
*section
;
1727 return (unsigned long)-1;
1729 section
= entry
->data
;
1730 return section
->address
;
1733 KMOD_EXPORT
void kmod_module_section_free_list(struct kmod_list
*list
)
1735 kmod_list_release(list
, kmod_module_section_free
);
1738 static int kmod_module_get_elf(const struct kmod_module
*mod
, struct kmod_elf
**elf
)
1740 if (mod
->file
== NULL
) {
1741 const char *path
= kmod_module_get_path(mod
);
1747 ret
= kmod_file_open(mod
->ctx
, path
, &((struct kmod_module
*)mod
)->file
);
1752 return kmod_file_get_elf(mod
->file
, elf
);
1755 struct kmod_module_info
{
1760 static struct kmod_module_info
*kmod_module_info_new(const char *key
, size_t keylen
,
1761 const char *value
, size_t valuelen
)
1763 struct kmod_module_info
*info
;
1765 info
= malloc(sizeof(struct kmod_module_info
) + keylen
+ valuelen
+ 2);
1769 info
->key
= (char *)info
+ sizeof(struct kmod_module_info
) + valuelen
+ 1;
1770 memcpy(info
->key
, key
, keylen
);
1771 info
->key
[keylen
] = '\0';
1773 memcpy(info
->value
, value
, valuelen
);
1774 info
->value
[valuelen
] = '\0';
1778 static void kmod_module_info_free(struct kmod_module_info
*info
)
1783 static struct kmod_list
*kmod_module_info_append(struct kmod_list
**list
, const char *key
,
1784 size_t keylen
, const char *value
,
1787 struct kmod_module_info
*info
;
1788 struct kmod_list
*n
;
1790 info
= kmod_module_info_new(key
, keylen
, value
, valuelen
);
1793 n
= kmod_list_append(*list
, info
);
1797 kmod_module_info_free(info
);
1801 static bool kmod_module_strbuf_pushhex(struct strbuf
*sbuf
, const char *hex
, size_t len
)
1803 static const char digits
[] = "0123456789ABCDEF";
1804 const size_t line_limit
= 20;
1806 for (size_t i
= 0; i
< len
; i
++) {
1807 if (!strbuf_pushchar(sbuf
, digits
[(hex
[i
] >> 4) & 0x0F]) ||
1808 !strbuf_pushchar(sbuf
, digits
[hex
[i
] & 0x0F]))
1812 if (!strbuf_pushchar(sbuf
, ':'))
1815 if ((i
+ 1) % line_limit
== 0 && !strbuf_pushchars(sbuf
, "\n\t\t"))
1823 static struct kmod_list
*kmod_module_info_append_hex(struct kmod_list
**list
,
1824 const char *key
, size_t keylen
,
1825 const char *value
, size_t valuelen
)
1827 struct kmod_list
*n
;
1830 DECLARE_STRBUF_WITH_STACK(sbuf
, 512);
1833 /* Display as 01:12:DE:AD:BE:EF:... */
1834 if (!kmod_module_strbuf_pushhex(&sbuf
, value
, valuelen
))
1836 hex
= strbuf_str(&sbuf
);
1838 n
= kmod_module_info_append(list
, key
, keylen
, hex
, strlen(hex
));
1842 n
= kmod_module_info_append(list
, key
, keylen
, NULL
, 0);
1853 KMOD_EXPORT
int kmod_module_get_info(const struct kmod_module
*mod
,
1854 struct kmod_list
**list
)
1856 struct kmod_elf
*elf
;
1858 int i
, count
, ret
= -ENOMEM
;
1859 struct kmod_signature_info sig_info
= {};
1861 if (mod
== NULL
|| list
== NULL
)
1864 assert(*list
== NULL
);
1866 /* remove const: this can only change internal state */
1867 if (kmod_module_is_builtin((struct kmod_module
*)mod
)) {
1868 count
= kmod_builtin_get_modinfo(mod
->ctx
, kmod_module_get_name(mod
),
1873 ret
= kmod_module_get_elf(mod
, &elf
);
1877 count
= kmod_elf_get_modinfo_strings(elf
, &strings
);
1882 for (i
= 0; i
< count
; i
++) {
1883 struct kmod_list
*n
;
1884 const char *key
, *value
;
1885 size_t keylen
, valuelen
;
1888 value
= strchr(key
, '=');
1889 if (value
== NULL
) {
1890 keylen
= strlen(key
);
1894 keylen
= value
- key
;
1896 valuelen
= strlen(value
);
1899 n
= kmod_module_info_append(list
, key
, keylen
, value
, valuelen
);
1904 if (mod
->file
&& kmod_module_signature_info(mod
->file
, &sig_info
)) {
1905 struct kmod_list
*n
;
1907 n
= kmod_module_info_append(list
, "sig_id", strlen("sig_id"),
1908 sig_info
.id_type
, strlen(sig_info
.id_type
));
1913 n
= kmod_module_info_append(list
, "signer", strlen("signer"),
1914 sig_info
.signer
, sig_info
.signer_len
);
1919 n
= kmod_module_info_append_hex(list
, "sig_key", strlen("sig_key"),
1920 sig_info
.key_id
, sig_info
.key_id_len
);
1925 n
= kmod_module_info_append(list
, "sig_hashalgo", strlen("sig_hashalgo"),
1927 strlen(sig_info
.hash_algo
));
1933 * Omit sig_info.algo for now, as these
1934 * are currently constant.
1936 n
= kmod_module_info_append_hex(list
, "signature", strlen("signature"),
1937 sig_info
.sig
, sig_info
.sig_len
);
1946 /* aux structures freed in normal case also */
1947 kmod_module_signature_info_free(&sig_info
);
1950 kmod_module_info_free_list(*list
);
1957 KMOD_EXPORT
const char *kmod_module_info_get_key(const struct kmod_list
*entry
)
1959 struct kmod_module_info
*info
;
1968 KMOD_EXPORT
const char *kmod_module_info_get_value(const struct kmod_list
*entry
)
1970 struct kmod_module_info
*info
;
1979 KMOD_EXPORT
void kmod_module_info_free_list(struct kmod_list
*list
)
1981 kmod_list_release(list
, kmod_module_info_free
);
1984 struct kmod_module_version
{
1989 static struct kmod_module_version
*kmod_module_versions_new(uint64_t crc
,
1992 struct kmod_module_version
*mv
;
1993 size_t symbollen
= strlen(symbol
) + 1;
1995 mv
= malloc(sizeof(struct kmod_module_version
) + symbollen
);
2000 memcpy(mv
->symbol
, symbol
, symbollen
);
2004 static void kmod_module_version_free(struct kmod_module_version
*version
)
2009 KMOD_EXPORT
int kmod_module_get_versions(const struct kmod_module
*mod
,
2010 struct kmod_list
**list
)
2012 struct kmod_elf
*elf
;
2013 struct kmod_modversion
*versions
;
2014 int i
, count
, ret
= 0;
2016 if (mod
== NULL
|| list
== NULL
)
2019 assert(*list
== NULL
);
2021 ret
= kmod_module_get_elf(mod
, &elf
);
2025 count
= kmod_elf_get_modversions(elf
, &versions
);
2029 for (i
= 0; i
< count
; i
++) {
2030 struct kmod_module_version
*mv
;
2031 struct kmod_list
*n
;
2033 mv
= kmod_module_versions_new(versions
[i
].crc
, versions
[i
].symbol
);
2035 kmod_module_versions_free_list(*list
);
2041 n
= kmod_list_append(*list
, mv
);
2045 kmod_module_version_free(mv
);
2046 kmod_module_versions_free_list(*list
);
2059 KMOD_EXPORT
const char *kmod_module_version_get_symbol(const struct kmod_list
*entry
)
2061 struct kmod_module_version
*version
;
2063 if (entry
== NULL
|| entry
->data
== NULL
)
2066 version
= entry
->data
;
2067 return version
->symbol
;
2070 KMOD_EXPORT
uint64_t kmod_module_version_get_crc(const struct kmod_list
*entry
)
2072 struct kmod_module_version
*version
;
2074 if (entry
== NULL
|| entry
->data
== NULL
)
2077 version
= entry
->data
;
2078 return version
->crc
;
2081 KMOD_EXPORT
void kmod_module_versions_free_list(struct kmod_list
*list
)
2083 kmod_list_release(list
, kmod_module_version_free
);
2086 struct kmod_module_symbol
{
2091 static struct kmod_module_symbol
*kmod_module_symbols_new(uint64_t crc
, const char *symbol
)
2093 struct kmod_module_symbol
*mv
;
2094 size_t symbollen
= strlen(symbol
) + 1;
2096 mv
= malloc(sizeof(struct kmod_module_symbol
) + symbollen
);
2101 memcpy(mv
->symbol
, symbol
, symbollen
);
2105 static void kmod_module_symbol_free(struct kmod_module_symbol
*symbol
)
2110 KMOD_EXPORT
int kmod_module_get_symbols(const struct kmod_module
*mod
,
2111 struct kmod_list
**list
)
2113 struct kmod_elf
*elf
;
2114 struct kmod_modversion
*symbols
;
2115 int i
, count
, ret
= 0;
2117 if (mod
== NULL
|| list
== NULL
)
2120 assert(*list
== NULL
);
2122 ret
= kmod_module_get_elf(mod
, &elf
);
2126 count
= kmod_elf_get_symbols(elf
, &symbols
);
2130 for (i
= 0; i
< count
; i
++) {
2131 struct kmod_module_symbol
*mv
;
2132 struct kmod_list
*n
;
2134 mv
= kmod_module_symbols_new(symbols
[i
].crc
, symbols
[i
].symbol
);
2136 kmod_module_symbols_free_list(*list
);
2142 n
= kmod_list_append(*list
, mv
);
2146 kmod_module_symbol_free(mv
);
2147 kmod_module_symbols_free_list(*list
);
2160 KMOD_EXPORT
const char *kmod_module_symbol_get_symbol(const struct kmod_list
*entry
)
2162 struct kmod_module_symbol
*symbol
;
2164 if (entry
== NULL
|| entry
->data
== NULL
)
2167 symbol
= entry
->data
;
2168 return symbol
->symbol
;
2171 KMOD_EXPORT
uint64_t kmod_module_symbol_get_crc(const struct kmod_list
*entry
)
2173 struct kmod_module_symbol
*symbol
;
2175 if (entry
== NULL
|| entry
->data
== NULL
)
2178 symbol
= entry
->data
;
2182 KMOD_EXPORT
void kmod_module_symbols_free_list(struct kmod_list
*list
)
2184 kmod_list_release(list
, kmod_module_symbol_free
);
2187 struct kmod_module_dependency_symbol
{
2194 static struct kmod_module_dependency_symbol
*kmod_module_dependency_symbols_new(uint64_t crc
, uint8_t bind
, const char *symbol
)
2197 struct kmod_module_dependency_symbol
*mv
;
2198 size_t symbollen
= strlen(symbol
) + 1;
2200 mv
= malloc(sizeof(struct kmod_module_dependency_symbol
) + symbollen
);
2206 memcpy(mv
->symbol
, symbol
, symbollen
);
2210 static void kmod_module_dependency_symbol_free(
2211 struct kmod_module_dependency_symbol
*dependency_symbol
)
2213 free(dependency_symbol
);
2216 KMOD_EXPORT
int kmod_module_get_dependency_symbols(const struct kmod_module
*mod
,
2217 struct kmod_list
**list
)
2219 struct kmod_elf
*elf
;
2220 struct kmod_modversion
*symbols
;
2221 int i
, count
, ret
= 0;
2223 if (mod
== NULL
|| list
== NULL
)
2226 assert(*list
== NULL
);
2228 ret
= kmod_module_get_elf(mod
, &elf
);
2232 count
= kmod_elf_get_dependency_symbols(elf
, &symbols
);
2236 for (i
= 0; i
< count
; i
++) {
2237 struct kmod_module_dependency_symbol
*mv
;
2238 struct kmod_list
*n
;
2240 mv
= kmod_module_dependency_symbols_new(symbols
[i
].crc
, symbols
[i
].bind
,
2243 kmod_module_dependency_symbols_free_list(*list
);
2249 n
= kmod_list_append(*list
, mv
);
2253 kmod_module_dependency_symbol_free(mv
);
2254 kmod_module_dependency_symbols_free_list(*list
);
2268 KMOD_EXPORT
const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list
*entry
)
2271 struct kmod_module_dependency_symbol
*dependency_symbol
;
2273 if (entry
== NULL
|| entry
->data
== NULL
)
2276 dependency_symbol
= entry
->data
;
2277 return dependency_symbol
->symbol
;
2280 KMOD_EXPORT
uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list
*entry
)
2282 struct kmod_module_dependency_symbol
*dependency_symbol
;
2284 if (entry
== NULL
|| entry
->data
== NULL
)
2287 dependency_symbol
= entry
->data
;
2288 return dependency_symbol
->crc
;
2291 KMOD_EXPORT
int kmod_module_dependency_symbol_get_bind(const struct kmod_list
*entry
)
2293 struct kmod_module_dependency_symbol
*dependency_symbol
;
2295 if (entry
== NULL
|| entry
->data
== NULL
)
2298 dependency_symbol
= entry
->data
;
2299 return dependency_symbol
->bind
;
2302 KMOD_EXPORT
void kmod_module_dependency_symbols_free_list(struct kmod_list
*list
)
2304 kmod_list_release(list
, kmod_module_dependency_symbol_free
);