2
* libkmod - interface to kernel module operations
4
* Copyright (C) 2011-2012 ProFUSION embedded systems
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34
#include <sys/types.h>
41
#include "libkmod-private.h"
44
* SECTION:libkmod-module
45
* @short_description: operate on kernel modules
51
* Opaque object representing a module.
58
struct kmod_list *dep;
60
const char *install_commands; /* owned by kmod_config */
61
const char *remove_commands; /* owned by kmod_config */
62
char *alias; /* only set if this module was created from an alias */
68
bool install_commands : 1;
69
bool remove_commands : 1;
73
* private field used by kmod_module_get_probe_list() to detect
79
* set by kmod_module_get_probe_list: indicates for probe_insert()
80
* whether the module's command and softdep should be ignored
85
static inline const char *path_join(const char *path, size_t prefixlen,
93
pathlen = strlen(path);
94
if (prefixlen + pathlen + 1 >= PATH_MAX)
97
memcpy(buf + prefixlen, path, pathlen + 1);
101
static inline bool module_is_inkernel(struct kmod_module *mod)
103
int state = kmod_module_get_initstate(mod);
104
if (state == KMOD_MODULE_LIVE ||
105
state == KMOD_MODULE_COMING ||
106
state == KMOD_MODULE_BUILTIN)
112
int kmod_module_parse_depline(struct kmod_module *mod, char *line)
114
struct kmod_ctx *ctx = mod->ctx;
115
struct kmod_list *list = NULL;
124
assert(mod->dep == NULL);
125
mod->init.dep = true;
127
p = strchr(line, ':');
132
dirname = kmod_get_dirname(mod->ctx);
133
dirnamelen = strlen(dirname);
134
if (dirnamelen + 2 >= PATH_MAX)
137
memcpy(buf, dirname, dirnamelen);
138
buf[dirnamelen] = '/';
140
buf[dirnamelen] = '\0';
142
if (mod->path == NULL) {
143
const char *str = path_join(line, dirnamelen, buf);
146
mod->path = strdup(str);
147
if (mod->path == NULL)
152
for (p = strtok_r(p, " \t", &saveptr); p != NULL;
153
p = strtok_r(NULL, " \t", &saveptr)) {
154
struct kmod_module *depmod;
157
path = path_join(p, dirnamelen, buf);
159
ERR(ctx, "could not join path '%s' and '%s'.\n",
164
err = kmod_module_new_from_path(ctx, path, &depmod);
166
ERR(ctx, "ctx=%p path=%s error=%s\n",
167
ctx, path, strerror(-err));
171
DBG(ctx, "add dep: %s\n", path);
173
list = kmod_list_prepend(list, depmod);
177
DBG(ctx, "%d dependencies for %s\n", n, mod->name);
184
kmod_module_unref_list(list);
185
mod->init.dep = false;
189
void kmod_module_set_visited(struct kmod_module *mod, bool visited)
191
mod->visited = visited;
195
* Memory layout with alias:
197
* struct kmod_module {
202
* name <----------' | |
203
* alias <-----------' |
204
* name\alias <--------'
206
* Memory layout without alias:
208
* struct kmod_module {
210
* alias -----|----> NULL
213
* name <----------'-'
215
* @key is "name\alias" or "name" (in which case alias == NULL)
217
static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
218
const char *name, size_t namelen,
219
const char *alias, size_t aliaslen,
220
struct kmod_module **mod)
222
struct kmod_module *m;
225
m = kmod_pool_get_module(ctx, key);
227
*mod = kmod_module_ref(m);
234
keylen = namelen + aliaslen + 1;
236
m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
242
memset(m, 0, sizeof(*m));
244
m->ctx = kmod_ref(ctx);
245
m->name = (char *)m + sizeof(*m);
246
memcpy(m->name, key, keylen + 1);
248
m->hashkey = m->name;
251
m->name[namelen] = '\0';
252
m->alias = m->name + namelen + 1;
253
m->hashkey = m->name + keylen + 1;
254
memcpy(m->hashkey, key, keylen + 1);
258
kmod_pool_add_module(ctx, m, m->hashkey);
265
* kmod_module_new_from_name:
266
* @ctx: kmod library context
267
* @name: name of the module
268
* @mod: where to save the created struct kmod_module
270
* Create a new struct kmod_module using the module name. @name can not be an
271
* alias, file name or anything else; it must be a module name. There's no
272
* check if the module exists in the system.
274
* This function is also used internally by many others that return a new
275
* struct kmod_module or a new list of modules.
277
* The initial refcount is 1, and needs to be decremented to release the
278
* resources of the kmod_module. Since libkmod keeps track of all
279
* kmod_modules created, they are all released upon @ctx destruction too. Do
280
* not unref @ctx before all the desired operations with the returned
281
* kmod_module are done.
283
* Returns: 0 on success or < 0 otherwise. It fails if name is not a valid
284
* module name or if memory allocation failed.
286
KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
288
struct kmod_module **mod)
291
char name_norm[PATH_MAX];
293
if (ctx == NULL || name == NULL || mod == NULL)
296
modname_normalize(name, name_norm, &namelen);
298
return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
301
int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
302
const char *name, struct kmod_module **mod)
306
size_t namelen = strlen(name);
307
size_t aliaslen = strlen(alias);
309
if (namelen + aliaslen + 2 > PATH_MAX)
310
return -ENAMETOOLONG;
312
memcpy(key, name, namelen);
313
memcpy(key + namelen + 1, alias, aliaslen + 1);
316
err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
324
* kmod_module_new_from_path:
325
* @ctx: kmod library context
326
* @path: path where to find the given module
327
* @mod: where to save the created struct kmod_module
329
* Create a new struct kmod_module using the module path. @path must be an
330
* existent file with in the filesystem and must be accessible to libkmod.
332
* The initial refcount is 1, and needs to be decremented to release the
333
* resources of the kmod_module. Since libkmod keeps track of all
334
* kmod_modules created, they are all released upon @ctx destruction too. Do
335
* not unref @ctx before all the desired operations with the returned
336
* kmod_module are done.
338
* If @path is relative, it's treated as relative to the current working
339
* directory. Otherwise, give an absolute path.
341
* Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if
342
* it's not a valid file for a kmod_module or if memory allocation failed.
344
KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
346
struct kmod_module **mod)
348
struct kmod_module *m;
355
if (ctx == NULL || path == NULL || mod == NULL)
358
abspath = path_make_absolute_cwd(path);
359
if (abspath == NULL) {
360
DBG(ctx, "no absolute path for %s\n", path);
364
err = stat(abspath, &st);
367
DBG(ctx, "stat %s: %s\n", path, strerror(errno));
372
if (path_to_modname(path, name, &namelen) == NULL) {
373
DBG(ctx, "could not get modname from path %s\n", path);
378
m = kmod_pool_get_module(ctx, name);
382
else if (streq(m->path, abspath))
385
ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
386
name, abspath, m->path);
391
*mod = kmod_module_ref(m);
395
err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
409
* Drop a reference of the kmod module. If the refcount reaches zero, its
410
* resources are released.
412
* Returns: NULL if @mod is NULL or if the module was released. Otherwise it
413
* returns the passed @mod with its refcount decremented.
415
KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
420
if (--mod->refcount > 0)
423
DBG(mod->ctx, "kmod_module %p released\n", mod);
425
kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
426
kmod_module_unref_list(mod->dep);
427
kmod_unref(mod->ctx);
438
* Take a reference of the kmod module, incrementing its refcount.
440
* Returns: the passed @module with its refcount incremented.
442
KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod)
452
#define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \
456
if (*(_list) != NULL) \
461
* kmod_module_new_from_lookup:
462
* @ctx: kmod library context
463
* @given_alias: alias to look for
464
* @list: an empty list where to save the list of modules matching
467
* Create a new list of kmod modules using an alias or module name and lookup
468
* libkmod's configuration files and indexes in order to find the module.
469
* Once it's found in one of the places, it stops searching and create the
470
* list of modules that is saved in @list.
472
* The search order is: 1. aliases in configuration file; 2. module names in
473
* modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases
474
* in modules.alias index.
476
* The initial refcount is 1, and needs to be decremented to release the
477
* resources of the kmod_module. The returned @list must be released by
478
* calling kmod_module_unref_list(). Since libkmod keeps track of all
479
* kmod_modules created, they are all released upon @ctx destruction too. Do
480
* not unref @ctx before all the desired operations with the returned list are
483
* Returns: 0 on success or < 0 otherwise. It fails if any of the lookup
484
* methods failed, which is basically due to memory allocation fail. If module
485
* is not found, it still returns 0, but @list is an empty list.
487
KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
488
const char *given_alias,
489
struct kmod_list **list)
492
char alias[PATH_MAX];
494
if (ctx == NULL || given_alias == NULL)
497
if (list == NULL || *list != NULL) {
498
ERR(ctx, "An empty list is needed to create lookup\n");
502
if (alias_normalize(given_alias, alias, NULL) < 0) {
503
DBG(ctx, "invalid alias: %s\n", given_alias);
507
DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias);
509
/* Aliases from config file override all the others */
510
err = kmod_lookup_alias_from_config(ctx, alias, list);
511
CHECK_ERR_AND_FINISH(err, fail, list, finish);
513
DBG(ctx, "lookup modules.dep %s\n", alias);
514
err = kmod_lookup_alias_from_moddep_file(ctx, alias, list);
515
CHECK_ERR_AND_FINISH(err, fail, list, finish);
517
DBG(ctx, "lookup modules.symbols %s\n", alias);
518
err = kmod_lookup_alias_from_symbols_file(ctx, alias, list);
519
CHECK_ERR_AND_FINISH(err, fail, list, finish);
521
DBG(ctx, "lookup install and remove commands %s\n", alias);
522
err = kmod_lookup_alias_from_commands(ctx, alias, list);
523
CHECK_ERR_AND_FINISH(err, fail, list, finish);
525
DBG(ctx, "lookup modules.aliases %s\n", alias);
526
err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
527
CHECK_ERR_AND_FINISH(err, fail, list, finish);
530
DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
533
DBG(ctx, "Failed to lookup %s\n", alias);
534
kmod_module_unref_list(*list);
538
#undef CHECK_ERR_AND_FINISH
541
* kmod_module_unref_list:
542
* @list: list of kmod modules
544
* Drop a reference of each kmod module in @list and releases the resources
545
* taken by the list itself.
547
* Returns: NULL if @mod is NULL or if the module was released. Otherwise it
548
* returns the passed @mod with its refcount decremented.
550
KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
552
for (; list != NULL; list = kmod_list_remove(list))
553
kmod_module_unref(list->data);
559
* kmod_module_get_filtered_blacklist:
560
* @ctx: kmod library context
561
* @input: list of kmod_module to be filtered with blacklist
562
* @output: where to save the new list
564
* Given a list @input, this function filter it out with config's blacklist
565
* ans save it in @output.
567
* Returns: 0 on success or < 0 otherwise. @output is saved with the updated
570
KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
571
const struct kmod_list *input,
572
struct kmod_list **output)
574
const struct kmod_list *li;
575
const struct kmod_list *blacklist;
577
if (ctx == NULL || output == NULL)
584
blacklist = kmod_get_blacklists(ctx);
585
kmod_list_foreach(li, input) {
586
struct kmod_module *mod = li->data;
587
const struct kmod_list *lb;
588
struct kmod_list *node;
589
bool filtered = false;
591
kmod_list_foreach(lb, blacklist) {
592
const char *name = lb->data;
594
if (streq(name, mod->name)) {
603
node = kmod_list_append(*output, mod);
608
kmod_module_ref(mod);
614
kmod_module_unref_list(*output);
619
static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
621
if (!mod->init.dep) {
623
char *line = kmod_search_moddep(mod->ctx, mod->name);
628
kmod_module_parse_depline((struct kmod_module *)mod, line);
639
* kmod_module_get_dependencies:
642
* Search the modules.dep index to find the dependencies of the given @mod.
643
* The result is cached in @mod, so subsequent calls to this function will
644
* return the already searched list of modules.
646
* Returns: NULL on failure or if there are any dependencies. Otherwise it
647
* returns a list of kmod modules that can be released by calling
648
* kmod_module_unref_list().
650
KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
652
struct kmod_list *l, *l_new, *list_new = NULL;
657
module_get_dependencies_noref(mod);
659
kmod_list_foreach(l, mod->dep) {
660
l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
662
kmod_module_unref(l->data);
672
ERR(mod->ctx, "out of memory\n");
673
kmod_module_unref_list(list_new);
678
* kmod_module_get_module:
679
* @entry: an entry in a list of kmod modules.
681
* Get the kmod module of this @entry in the list, increasing its refcount.
682
* After it's used, unref it. Since the refcount is incremented upon return,
683
* you still have to call kmod_module_unref_list() to release the list of kmod
686
* Returns: NULL on failure or the kmod_module contained in this list entry
687
* with its refcount incremented.
689
KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry)
694
return kmod_module_ref(entry->data);
698
* kmod_module_get_name:
701
* Get the name of this kmod module. Name is always available, independently
702
* if it was created by kmod_module_new_from_name() or another function and
703
* it's always normalized (dashes are replaced with underscores).
705
* Returns: the name of this kmod module.
707
KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
716
* kmod_module_get_path:
719
* Get the path of this kmod module. If this kmod module was not created by
720
* path, it can search the modules.dep index in order to find out the module
721
* under context's dirname.
723
* Returns: the path of this kmod module or NULL if such information is not
726
KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
733
DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path);
735
if (mod->path != NULL)
741
line = kmod_search_moddep(mod->ctx, mod->name);
745
kmod_module_parse_depline((struct kmod_module *) mod, line);
752
extern long delete_module(const char *name, unsigned int flags);
755
* kmod_module_remove_module:
757
* @flags: flags to pass to Linux kernel when removing the module
759
* Remove a module from Linux kernel.
761
* Returns: 0 on success or < 0 on failure.
763
KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
771
/* Filter out other flags */
772
flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
774
err = delete_module(mod->name, flags);
777
ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
783
extern long init_module(const void *mem, unsigned long len, const char *args);
786
* kmod_module_insert_module:
788
* @flags: flags are not passed to Linux Kernel, but instead they dictate the
789
* behavior of this function.
790
* @options: module's options to pass to Linux Kernel.
792
* Insert a module in Linux kernel. It opens the file pointed by @mod,
793
* mmap'ing it and passing to kernel.
795
* Returns: 0 on success or < 0 on failure. If module is already loaded it
798
KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
805
struct kmod_file *file;
806
struct kmod_elf *elf = NULL;
808
const char *args = options ? options : "";
813
path = kmod_module_get_path(mod);
815
ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
819
file = kmod_file_open(mod->ctx, path);
825
size = kmod_file_get_size(file);
826
mem = kmod_file_get_contents(file);
828
if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
829
elf = kmod_elf_new(mem, size);
835
if (flags & KMOD_INSERT_FORCE_MODVERSION) {
836
err = kmod_elf_strip_section(elf, "__versions");
838
INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
841
if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
842
err = kmod_elf_strip_vermagic(elf);
844
INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
847
mem = kmod_elf_get_memory(elf);
850
err = init_module(mem, size, args);
853
INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
859
kmod_file_unref(file);
864
static bool module_is_blacklisted(struct kmod_module *mod)
866
struct kmod_ctx *ctx = mod->ctx;
867
const struct kmod_list *bl = kmod_get_blacklists(ctx);
868
const struct kmod_list *l;
870
kmod_list_foreach(l, bl) {
871
const char *modname = kmod_blacklist_get_modname(l);
873
if (streq(modname, mod->name))
880
static int command_do(struct kmod_module *mod, const char *type,
883
const char *modname = kmod_module_get_name(mod);
886
DBG(mod->ctx, "%s %s\n", type, cmd);
888
setenv("MODPROBE_MODULE", modname, 1);
890
unsetenv("MODPROBE_MODULE");
892
if (err == -1 || WEXITSTATUS(err)) {
893
ERR(mod->ctx, "Error running %s command for %s\n",
896
err = -WEXITSTATUS(err);
902
struct probe_insert_cb {
903
int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
907
static int module_do_install_commands(struct kmod_module *mod,
909
struct probe_insert_cb *cb)
911
const char *command = kmod_module_get_install_commands(mod);
914
size_t cmdlen, options_len, varlen;
921
options_len = strlen(options);
922
cmdlen = strlen(command);
923
varlen = sizeof("$CMDLINE_OPTS") - 1;
925
cmd = memdup(command, cmdlen + 1);
929
while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
930
size_t prefixlen = p - cmd;
931
size_t suffixlen = cmdlen - prefixlen - varlen;
932
size_t slen = cmdlen - varlen + options_len;
933
char *suffix = p + varlen;
934
char *s = malloc(slen + 1);
939
memcpy(s, cmd, p - cmd);
940
memcpy(s + prefixlen, options, options_len);
941
memcpy(s + prefixlen + options_len, suffix, suffixlen);
949
if (cb->run_install != NULL)
950
err = cb->run_install(mod, cmd, cb->data);
952
err = command_do(mod, "install", cmd);
959
static char *module_options_concat(const char *opt, const char *xopt)
961
// TODO: we might need to check if xopt overrides options on opt
962
size_t optlen = opt == NULL ? 0 : strlen(opt);
963
size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
966
if (optlen == 0 && xoptlen == 0)
969
r = malloc(optlen + xoptlen + 2);
972
memcpy(r, opt, optlen);
978
memcpy(r + optlen, xopt, xoptlen);
980
r[optlen + xoptlen] = '\0';
985
static int __kmod_module_get_probe_list(struct kmod_module *mod,
987
struct kmod_list **list);
990
static int __kmod_module_fill_softdep(struct kmod_module *mod,
991
struct kmod_list **list)
993
struct kmod_list *pre = NULL, *post = NULL, *l;
996
err = kmod_module_get_softdeps(mod, &pre, &post);
998
ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
1002
kmod_list_foreach(l, pre) {
1003
struct kmod_module *m = l->data;
1004
err = __kmod_module_get_probe_list(m, false, list);
1009
l = kmod_list_append(*list, kmod_module_ref(mod));
1011
kmod_module_unref(mod);
1016
mod->ignorecmd = (pre != NULL || post != NULL);
1018
kmod_list_foreach(l, post) {
1019
struct kmod_module *m = l->data;
1020
err = __kmod_module_get_probe_list(m, false, list);
1026
kmod_module_unref_list(pre);
1027
kmod_module_unref_list(post);
1033
static int __kmod_module_get_probe_list(struct kmod_module *mod,
1035
struct kmod_list **list)
1037
struct kmod_list *dep, *l;
1041
DBG(mod->ctx, "Ignore module '%s': already visited\n",
1045
mod->visited = true;
1047
dep = kmod_module_get_dependencies(mod);
1048
kmod_list_foreach(l, dep) {
1049
struct kmod_module *m = l->data;
1050
err = __kmod_module_fill_softdep(m, list);
1056
l = kmod_list_append(*list, kmod_module_ref(mod));
1058
kmod_module_unref(mod);
1063
mod->ignorecmd = true;
1065
err = __kmod_module_fill_softdep(mod, list);
1068
kmod_module_unref_list(dep);
1072
static int kmod_module_get_probe_list(struct kmod_module *mod,
1074
struct kmod_list **list)
1078
assert(mod != NULL);
1079
assert(list != NULL && *list == NULL);
1082
* Make sure we don't get screwed by previous calls to this function
1084
kmod_set_modules_visited(mod->ctx, false);
1086
err = __kmod_module_get_probe_list(mod, ignorecmd, list);
1088
kmod_module_unref_list(*list);
1096
* kmod_module_probe_insert_module:
1098
* @flags: flags are not passed to Linux Kernel, but instead they dictate the
1099
* behavior of this function.
1100
* @extra_options: module's options to pass to Linux Kernel. It applies only
1101
* to @mod, not to its dependencies.
1102
* @run_install: function to run when @mod is backed by an install command.
1103
* @data: data to give back to @run_install callback
1104
* @print_action: function to call with the action being taken (install or
1105
* insmod). It's useful for tools like modprobe when running with verbose
1106
* output or in dry-run mode.
1108
* Insert a module in Linux kernel resolving dependencies, soft dependencies,
1109
* install commands and applying blacklist.
1111
* If @run_install is NULL, this function will fork and exec by calling
1112
* system(3). Don't pass a NULL argument in @run_install if your binary is
1113
* setuid/setgid (see warning in system(3)). If you need control over the
1114
* execution of an install command, give a callback function instead.
1116
* Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
1119
KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
1120
unsigned int flags, const char *extra_options,
1121
int (*run_install)(struct kmod_module *m,
1122
const char *cmd, void *data),
1124
void (*print_action)(struct kmod_module *m,
1126
const char *options))
1128
struct kmod_list *list = NULL, *l;
1129
struct probe_insert_cb cb;
1135
if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1136
&& module_is_inkernel(mod)) {
1137
if (flags & KMOD_PROBE_FAIL_ON_LOADED)
1143
err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
1144
KMOD_PROBE_APPLY_BLACKLIST_ALL);
1146
if (module_is_blacklisted(mod))
1150
err = kmod_module_get_probe_list(mod,
1151
!!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
1155
if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
1156
struct kmod_list *filtered = NULL;
1158
err = kmod_module_get_filtered_blacklist(mod->ctx,
1163
kmod_module_unref_list(list);
1164
if (filtered == NULL)
1165
return KMOD_PROBE_APPLY_BLACKLIST_ALL;
1170
cb.run_install = run_install;
1171
cb.data = (void *) data;
1173
kmod_list_foreach(l, list) {
1174
struct kmod_module *m = l->data;
1175
const char *moptions = kmod_module_get_options(m);
1176
const char *cmd = kmod_module_get_install_commands(m);
1177
char *options = module_options_concat(moptions,
1178
m == mod ? extra_options : NULL);
1180
if (cmd != NULL && !m->ignorecmd) {
1181
if (print_action != NULL)
1182
print_action(m, true, options ?: "");
1184
if (!(flags & KMOD_PROBE_DRY_RUN))
1185
err = module_do_install_commands(m, options,
1188
if (!(flags & KMOD_PROBE_IGNORE_LOADED)
1189
&& module_is_inkernel(m)) {
1190
DBG(mod->ctx, "Ignoring module '%s': "
1191
"already loaded\n", m->name);
1195
if (print_action != NULL)
1196
print_action(m, false, options ?: "");
1198
if (!(flags & KMOD_PROBE_DRY_RUN))
1199
err = kmod_module_insert_module(m, flags,
1207
* Treat "already loaded" error. If we were told to stop on
1208
* already loaded and the module being loaded is not a softdep
1209
* or dep, bail out. Otherwise, just ignore and continue.
1211
* We need to check here because of race conditions. We
1212
* checked first if module was already loaded but it may have
1213
* been loaded between the check and the moment we try to
1216
if (err == -EEXIST && m == mod &&
1217
(flags & KMOD_PROBE_FAIL_ON_LOADED))
1226
kmod_module_unref_list(list);
1231
* kmod_module_get_options:
1234
* Get options of this kmod module. Options come from the configuration file
1235
* and are cached in @mod. The first call to this function will search for
1236
* this module in configuration and subsequent calls return the cached string.
1238
* Returns: a string with all the options separated by spaces. This string is
1239
* owned by @mod, do not free it.
1241
KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
1246
if (!mod->init.options) {
1248
struct kmod_module *m = (struct kmod_module *)mod;
1249
const struct kmod_list *l, *ctx_options;
1253
ctx_options = kmod_get_options(mod->ctx);
1255
kmod_list_foreach(l, ctx_options) {
1256
const char *modname = kmod_option_get_modname(l);
1261
DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1262
if (!(streq(modname, mod->name) || (mod->alias != NULL &&
1263
streq(modname, mod->alias))))
1266
DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias);
1267
str = kmod_option_get_options(l);
1272
tmp = realloc(opts, optslen + len + 2);
1281
opts[optslen] = ' ';
1285
memcpy(opts + optslen, str, len);
1287
opts[optslen] = '\0';
1290
m->init.options = true;
1294
return mod->options;
1297
ERR(mod->ctx, "out of memory\n");
1302
* kmod_module_get_install_commands:
1305
* Get install commands for this kmod module. Install commands come from the
1306
* configuration file and are cached in @mod. The first call to this function
1307
* will search for this module in configuration and subsequent calls return
1308
* the cached string. The install commands are returned as they were in the
1309
* configuration, concatenated by ';'. No other processing is made in this
1312
* Returns: a string with all install commands separated by semicolons. This
1313
* string is owned by @mod, do not free it.
1315
KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod)
1320
if (!mod->init.install_commands) {
1322
struct kmod_module *m = (struct kmod_module *)mod;
1323
const struct kmod_list *l, *ctx_install_commands;
1325
ctx_install_commands = kmod_get_install_commands(mod->ctx);
1327
kmod_list_foreach(l, ctx_install_commands) {
1328
const char *modname = kmod_command_get_modname(l);
1330
if (fnmatch(modname, mod->name, 0) != 0)
1333
m->install_commands = kmod_command_get_command(l);
1336
* find only the first command, as modprobe from
1337
* module-init-tools does
1342
m->init.install_commands = true;
1345
return mod->install_commands;
1348
void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd)
1350
mod->init.install_commands = true;
1351
mod->install_commands = cmd;
1354
static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count)
1356
struct kmod_list *ret = NULL;
1359
for (i = 0; i < count; i++) {
1360
const char *depname = array[i];
1361
struct kmod_list *lst = NULL;
1364
err = kmod_module_new_from_lookup(ctx, depname, &lst);
1366
ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname);
1368
} else if (lst != NULL)
1369
ret = kmod_list_append_list(ret, lst);
1375
* kmod_module_get_softdeps:
1377
* @pre: where to save the list of preceding soft dependencies.
1378
* @post: where to save the list of post soft dependencies.
1380
* Get soft dependencies for this kmod module. Soft dependencies come
1381
* from configuration file and are not cached in @mod because it may include
1382
* dependency cycles that would make we leak kmod_module. Any call
1383
* to this function will search for this module in configuration, allocate a
1384
* list and return the result.
1386
* Both @pre and @post are newly created list of kmod_module and
1387
* should be unreferenced with kmod_module_unref_list().
1389
* Returns: 0 on success or < 0 otherwise.
1391
KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
1392
struct kmod_list **pre,
1393
struct kmod_list **post)
1395
const struct kmod_list *l, *ctx_softdeps;
1397
if (mod == NULL || pre == NULL || post == NULL)
1400
assert(*pre == NULL);
1401
assert(*post == NULL);
1403
ctx_softdeps = kmod_get_softdeps(mod->ctx);
1405
kmod_list_foreach(l, ctx_softdeps) {
1406
const char *modname = kmod_softdep_get_name(l);
1407
const char * const *array;
1410
if (fnmatch(modname, mod->name, 0) != 0)
1413
array = kmod_softdep_get_pre(l, &count);
1414
*pre = lookup_softdep(mod->ctx, array, count);
1415
array = kmod_softdep_get_post(l, &count);
1416
*post = lookup_softdep(mod->ctx, array, count);
1419
* find only the first command, as modprobe from
1420
* module-init-tools does
1429
* kmod_module_get_remove_commands:
1432
* Get remove commands for this kmod module. Remove commands come from the
1433
* configuration file and are cached in @mod. The first call to this function
1434
* will search for this module in configuration and subsequent calls return
1435
* the cached string. The remove commands are returned as they were in the
1436
* configuration, concatenated by ';'. No other processing is made in this
1439
* Returns: a string with all remove commands separated by semicolons. This
1440
* string is owned by @mod, do not free it.
1442
KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod)
1447
if (!mod->init.remove_commands) {
1449
struct kmod_module *m = (struct kmod_module *)mod;
1450
const struct kmod_list *l, *ctx_remove_commands;
1452
ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
1454
kmod_list_foreach(l, ctx_remove_commands) {
1455
const char *modname = kmod_command_get_modname(l);
1457
if (fnmatch(modname, mod->name, 0) != 0)
1460
m->remove_commands = kmod_command_get_command(l);
1463
* find only the first command, as modprobe from
1464
* module-init-tools does
1469
m->init.remove_commands = true;
1472
return mod->remove_commands;
1475
void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
1477
mod->init.remove_commands = true;
1478
mod->remove_commands = cmd;
1482
* SECTION:libkmod-loaded
1483
* @short_description: currently loaded modules
1485
* Information about currently loaded modules, as reported by Linux kernel.
1486
* These information are not cached by libkmod and are always read from /sys
1487
* and /proc/modules.
1491
* kmod_module_new_from_loaded:
1492
* @ctx: kmod library context
1493
* @list: where to save the list of loaded modules
1495
* Create a new list of kmod modules with all modules currently loaded in
1496
* kernel. It uses /proc/modules to get the names of loaded modules and to
1497
* create kmod modules by calling kmod_module_new_from_name() in each of them.
1498
* They are put are put in @list in no particular order.
1500
* The initial refcount is 1, and needs to be decremented to release the
1501
* resources of the kmod_module. The returned @list must be released by
1502
* calling kmod_module_unref_list(). Since libkmod keeps track of all
1503
* kmod_modules created, they are all released upon @ctx destruction too. Do
1504
* not unref @ctx before all the desired operations with the returned list are
1507
* Returns: 0 on success or < 0 on error.
1509
KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
1510
struct kmod_list **list)
1512
struct kmod_list *l = NULL;
1516
if (ctx == NULL || list == NULL)
1519
fp = fopen("/proc/modules", "re");
1522
ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno));
1526
while (fgets(line, sizeof(line), fp)) {
1527
struct kmod_module *m;
1528
struct kmod_list *node;
1530
char *saveptr, *name = strtok_r(line, " \t", &saveptr);
1532
err = kmod_module_new_from_name(ctx, name, &m);
1534
ERR(ctx, "could not get module from name '%s': %s\n",
1535
name, strerror(-err));
1539
node = kmod_list_append(l, m);
1543
ERR(ctx, "out of memory\n");
1544
kmod_module_unref(m);
1555
* kmod_module_initstate_str:
1556
* @state: the state as returned by kmod_module_get_initstate()
1558
* Translate a initstate to a string.
1560
* Returns: the string associated to the @state. This string is statically
1561
* allocated, do not free it.
1563
KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
1566
case KMOD_MODULE_BUILTIN:
1568
case KMOD_MODULE_LIVE:
1570
case KMOD_MODULE_COMING:
1572
case KMOD_MODULE_GOING:
1580
* kmod_module_get_initstate:
1583
* Get the initstate of this @mod, as returned by Linux Kernel, by reading
1586
* Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
1588
KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
1590
char path[PATH_MAX], buf[32];
1591
int fd, err, pathlen;
1596
pathlen = snprintf(path, sizeof(path),
1597
"/sys/module/%s/initstate", mod->name);
1598
fd = open(path, O_RDONLY|O_CLOEXEC);
1602
DBG(mod->ctx, "could not open '%s': %s\n",
1603
path, strerror(-err));
1605
if (pathlen > (int)sizeof("/initstate") - 1) {
1607
path[pathlen - (sizeof("/initstate") - 1)] = '\0';
1608
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1609
return KMOD_MODULE_BUILTIN;
1612
DBG(mod->ctx, "could not open '%s': %s\n",
1613
path, strerror(-err));
1617
err = read_str_safe(fd, buf, sizeof(buf));
1620
ERR(mod->ctx, "could not read from '%s': %s\n",
1621
path, strerror(-err));
1625
if (streq(buf, "live\n"))
1626
return KMOD_MODULE_LIVE;
1627
else if (streq(buf, "coming\n"))
1628
return KMOD_MODULE_COMING;
1629
else if (streq(buf, "going\n"))
1630
return KMOD_MODULE_GOING;
1632
ERR(mod->ctx, "unknown %s: '%s'\n", path, buf);
1637
* kmod_module_get_size:
1640
* Get the size of this kmod module as returned by Linux kernel. It reads the
1641
* file /proc/modules to search for this module and get its size.
1643
* Returns: the size of this kmod module.
1645
KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
1647
// FIXME TODO: this should be available from /sys/module/foo
1651
long size = -ENOENT;
1656
fp = fopen("/proc/modules", "re");
1660
"could not open /proc/modules: %s\n", strerror(errno));
1664
while (fgets(line, sizeof(line), fp)) {
1665
char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
1669
if (tok == NULL || !streq(tok, mod->name))
1672
tok = strtok_r(NULL, " \t", &saveptr);
1675
"invalid line format at /proc/modules:%d\n", lineno);
1679
value = strtol(tok, &endptr, 10);
1680
if (endptr == tok || *endptr != '\0') {
1682
"invalid line format at /proc/modules:%d\n", lineno);
1694
* kmod_module_get_refcnt:
1697
* Get the ref count of this @mod, as returned by Linux Kernel, by reading
1700
* Returns: 0 on success or < 0 on failure.
1702
KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
1704
char path[PATH_MAX];
1711
snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name);
1712
fd = open(path, O_RDONLY|O_CLOEXEC);
1715
ERR(mod->ctx, "could not open '%s': %s\n",
1716
path, strerror(errno));
1720
err = read_str_long(fd, &refcnt, 10);
1723
ERR(mod->ctx, "could not read integer from '%s': '%s'\n",
1724
path, strerror(-err));
1732
* kmod_module_get_holders:
1735
* Get a list of kmod modules that are holding this @mod, as returned by Linux
1736
* Kernel. After use, free the @list by calling kmod_module_unref_list().
1738
* Returns: a new list of kmod modules on success or NULL on failure.
1740
KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod)
1742
char dname[PATH_MAX];
1743
struct kmod_list *list = NULL;
1749
snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
1753
ERR(mod->ctx, "could not open '%s': %s\n",
1754
dname, strerror(errno));
1759
struct dirent de, *entp;
1760
struct kmod_module *holder;
1761
struct kmod_list *l;
1764
err = readdir_r(d, &de, &entp);
1766
ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1767
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
err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
1782
ERR(mod->ctx, "could not create module for '%s': %s\n",
1783
de.d_name, strerror(-err));
1787
l = kmod_list_append(list, holder);
1791
ERR(mod->ctx, "out of memory\n");
1792
kmod_module_unref(holder);
1802
kmod_module_unref_list(list);
1806
struct kmod_module_section {
1807
unsigned long address;
1811
static void kmod_module_section_free(struct kmod_module_section *section)
1817
* kmod_module_get_sections:
1820
* Get a list of kmod sections of this @mod, as returned by Linux Kernel. The
1821
* structure contained in this list is internal to libkmod and their fields
1822
* can be obtained by calling kmod_module_section_get_name() and
1823
* kmod_module_section_get_address().
1825
* After use, free the @list by calling kmod_module_section_free_list().
1827
* Returns: a new list of kmod module sections on success or NULL on failure.
1829
KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod)
1831
char dname[PATH_MAX];
1832
struct kmod_list *list = NULL;
1839
snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name);
1843
ERR(mod->ctx, "could not open '%s': %s\n",
1844
dname, strerror(errno));
1851
struct dirent de, *entp;
1852
struct kmod_module_section *section;
1853
struct kmod_list *l;
1854
unsigned long address;
1858
err = readdir_r(d, &de, &entp);
1860
ERR(mod->ctx, "could not iterate for module '%s': %s\n",
1861
mod->name, strerror(-err));
1865
if (de.d_name[0] == '.') {
1866
if (de.d_name[1] == '\0' ||
1867
(de.d_name[1] == '.' && de.d_name[2] == '\0'))
1871
fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
1873
ERR(mod->ctx, "could not open '%s/%s': %m\n",
1878
err = read_str_ulong(fd, &address, 16);
1881
ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
1886
namesz = strlen(de.d_name) + 1;
1887
section = malloc(sizeof(*section) + namesz);
1889
if (section == NULL) {
1890
ERR(mod->ctx, "out of memory\n");
1894
section->address = address;
1895
memcpy(section->name, de.d_name, namesz);
1897
l = kmod_list_append(list, section);
1901
ERR(mod->ctx, "out of memory\n");
1912
kmod_module_unref_list(list);
1917
* kmod_module_section_get_module_name:
1918
* @entry: a list entry representing a kmod module section
1920
* Get the name of a kmod module section.
1922
* After use, free the @list by calling kmod_module_section_free_list().
1924
* Returns: the name of this kmod module section on success or NULL on
1925
* failure. The string is owned by the section, do not free it.
1927
KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry)
1929
struct kmod_module_section *section;
1934
section = entry->data;
1935
return section->name;
1939
* kmod_module_section_get_address:
1940
* @entry: a list entry representing a kmod module section
1942
* Get the address of a kmod module section.
1944
* After use, free the @list by calling kmod_module_section_free_list().
1946
* Returns: the address of this kmod module section on success or ULONG_MAX
1949
KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry)
1951
struct kmod_module_section *section;
1954
return (unsigned long)-1;
1956
section = entry->data;
1957
return section->address;
1961
* kmod_module_section_free_list:
1962
* @list: kmod module section list
1964
* Release the resources taken by @list
1966
KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
1969
kmod_module_section_free(list->data);
1970
list = kmod_list_remove(list);
1974
struct kmod_module_info {
1979
static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen)
1981
struct kmod_module_info *info;
1983
info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2);
1987
info->key = (char *)info + sizeof(struct kmod_module_info)
1989
memcpy(info->key, key, keylen);
1990
info->key[keylen] = '\0';
1991
memcpy(info->value, value, valuelen);
1992
info->value[valuelen] = '\0';
1996
static void kmod_module_info_free(struct kmod_module_info *info)
2002
* kmod_module_get_info:
2004
* @list: where to return list of module information. Use
2005
* kmod_module_info_get_key() and
2006
* kmod_module_info_get_value(). Release this list with
2007
* kmod_module_info_free_list()
2009
* Get a list of entries in ELF section ".modinfo", these contain
2010
* alias, license, depends, vermagic and other keys with respective
2013
* After use, free the @list by calling kmod_module_info_free_list().
2015
* Returns: 0 on success or < 0 otherwise.
2017
KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
2019
struct kmod_file *file;
2020
struct kmod_elf *elf;
2025
int i, count, ret = 0;
2027
if (mod == NULL || list == NULL)
2030
assert(*list == NULL);
2032
path = kmod_module_get_path(mod);
2036
file = kmod_file_open(mod->ctx, path);
2040
size = kmod_file_get_size(file);
2041
mem = kmod_file_get_contents(file);
2043
elf = kmod_elf_new(mem, size);
2046
goto elf_open_error;
2049
count = kmod_elf_get_strings(elf, ".modinfo", &strings);
2052
goto get_strings_error;
2055
for (i = 0; i < count; i++) {
2056
struct kmod_module_info *info;
2057
struct kmod_list *n;
2058
const char *key, *value;
2059
size_t keylen, valuelen;
2062
value = strchr(key, '=');
2063
if (value == NULL) {
2064
keylen = strlen(key);
2067
keylen = value - key;
2069
valuelen = strlen(value);
2072
info = kmod_module_info_new(key, keylen, value, valuelen);
2075
kmod_module_info_free_list(*list);
2080
n = kmod_list_append(*list, info);
2084
kmod_module_info_free(info);
2085
kmod_module_info_free_list(*list);
2096
kmod_elf_unref(elf);
2098
kmod_file_unref(file);
2104
* kmod_module_info_get_key:
2105
* @entry: a list entry representing a kmod module info
2107
* Get the key of a kmod module info.
2109
* Returns: the key of this kmod module info on success or NULL on
2110
* failure. The string is owned by the info, do not free it.
2112
KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry)
2114
struct kmod_module_info *info;
2124
* kmod_module_info_get_value:
2125
* @entry: a list entry representing a kmod module info
2127
* Get the value of a kmod module info.
2129
* Returns: the value of this kmod module info on success or NULL on
2130
* failure. The string is owned by the info, do not free it.
2132
KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry)
2134
struct kmod_module_info *info;
2144
* kmod_module_info_free_list:
2145
* @list: kmod module info list
2147
* Release the resources taken by @list
2149
KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list)
2152
kmod_module_info_free(list->data);
2153
list = kmod_list_remove(list);
2157
struct kmod_module_version {
2162
static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol)
2164
struct kmod_module_version *mv;
2165
size_t symbollen = strlen(symbol) + 1;
2167
mv = malloc(sizeof(struct kmod_module_version) + symbollen);
2172
memcpy(mv->symbol, symbol, symbollen);
2176
static void kmod_module_version_free(struct kmod_module_version *version)
2182
* kmod_module_get_versions:
2184
* @list: where to return list of module versions. Use
2185
* kmod_module_version_get_symbol() and
2186
* kmod_module_version_get_crc(). Release this list with
2187
* kmod_module_versions_free_list()
2189
* Get a list of entries in ELF section "__versions".
2191
* After use, free the @list by calling kmod_module_versions_free_list().
2193
* Returns: 0 on success or < 0 otherwise.
2195
KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
2197
struct kmod_file *file;
2198
struct kmod_elf *elf;
2201
struct kmod_modversion *versions;
2203
int i, count, ret = 0;
2205
if (mod == NULL || list == NULL)
2208
assert(*list == NULL);
2210
path = kmod_module_get_path(mod);
2214
file = kmod_file_open(mod->ctx, path);
2218
size = kmod_file_get_size(file);
2219
mem = kmod_file_get_contents(file);
2221
elf = kmod_elf_new(mem, size);
2224
goto elf_open_error;
2227
count = kmod_elf_get_modversions(elf, &versions);
2230
goto get_strings_error;
2233
for (i = 0; i < count; i++) {
2234
struct kmod_module_version *mv;
2235
struct kmod_list *n;
2237
mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol);
2240
kmod_module_versions_free_list(*list);
2245
n = kmod_list_append(*list, mv);
2249
kmod_module_version_free(mv);
2250
kmod_module_versions_free_list(*list);
2261
kmod_elf_unref(elf);
2263
kmod_file_unref(file);
2269
* kmod_module_versions_get_symbol:
2270
* @entry: a list entry representing a kmod module versions
2272
* Get the symbol of a kmod module versions.
2274
* Returns: the symbol of this kmod module versions on success or NULL
2275
* on failure. The string is owned by the versions, do not free it.
2277
KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry)
2279
struct kmod_module_version *version;
2284
version = entry->data;
2285
return version->symbol;
2289
* kmod_module_version_get_crc:
2290
* @entry: a list entry representing a kmod module version
2292
* Get the crc of a kmod module version.
2294
* Returns: the crc of this kmod module version on success or NULL on
2295
* failure. The string is owned by the version, do not free it.
2297
KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
2299
struct kmod_module_version *version;
2304
version = entry->data;
2305
return version->crc;
2309
* kmod_module_versions_free_list:
2310
* @list: kmod module versions list
2312
* Release the resources taken by @list
2314
KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list)
2317
kmod_module_version_free(list->data);
2318
list = kmod_list_remove(list);
2322
struct kmod_module_symbol {
2327
static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol)
2329
struct kmod_module_symbol *mv;
2330
size_t symbollen = strlen(symbol) + 1;
2332
mv = malloc(sizeof(struct kmod_module_symbol) + symbollen);
2337
memcpy(mv->symbol, symbol, symbollen);
2341
static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
2347
* kmod_module_get_symbols:
2349
* @list: where to return list of module symbols. Use
2350
* kmod_module_symbol_get_symbol() and
2351
* kmod_module_symbol_get_crc(). Release this list with
2352
* kmod_module_symbols_free_list()
2354
* Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2356
* After use, free the @list by calling kmod_module_symbols_free_list().
2358
* Returns: 0 on success or < 0 otherwise.
2360
KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
2362
struct kmod_file *file;
2363
struct kmod_elf *elf;
2366
struct kmod_modversion *symbols;
2368
int i, count, ret = 0;
2370
if (mod == NULL || list == NULL)
2373
assert(*list == NULL);
2375
path = kmod_module_get_path(mod);
2379
file = kmod_file_open(mod->ctx, path);
2383
size = kmod_file_get_size(file);
2384
mem = kmod_file_get_contents(file);
2386
elf = kmod_elf_new(mem, size);
2389
goto elf_open_error;
2392
count = kmod_elf_get_symbols(elf, &symbols);
2395
goto get_strings_error;
2398
for (i = 0; i < count; i++) {
2399
struct kmod_module_symbol *mv;
2400
struct kmod_list *n;
2402
mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol);
2405
kmod_module_symbols_free_list(*list);
2410
n = kmod_list_append(*list, mv);
2414
kmod_module_symbol_free(mv);
2415
kmod_module_symbols_free_list(*list);
2426
kmod_elf_unref(elf);
2428
kmod_file_unref(file);
2434
* kmod_module_symbol_get_symbol:
2435
* @entry: a list entry representing a kmod module symbols
2437
* Get the symbol of a kmod module symbols.
2439
* Returns: the symbol of this kmod module symbols on success or NULL
2440
* on failure. The string is owned by the symbols, do not free it.
2442
KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry)
2444
struct kmod_module_symbol *symbol;
2449
symbol = entry->data;
2450
return symbol->symbol;
2454
* kmod_module_symbol_get_crc:
2455
* @entry: a list entry representing a kmod module symbol
2457
* Get the crc of a kmod module symbol.
2459
* Returns: the crc of this kmod module symbol on success or NULL on
2460
* failure. The string is owned by the symbol, do not free it.
2462
KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
2464
struct kmod_module_symbol *symbol;
2469
symbol = entry->data;
2474
* kmod_module_symbols_free_list:
2475
* @list: kmod module symbols list
2477
* Release the resources taken by @list
2479
KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list)
2482
kmod_module_symbol_free(list->data);
2483
list = kmod_list_remove(list);
2487
struct kmod_module_dependency_symbol {
2493
static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol)
2495
struct kmod_module_dependency_symbol *mv;
2496
size_t symbollen = strlen(symbol) + 1;
2498
mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen);
2504
memcpy(mv->symbol, symbol, symbollen);
2508
static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol)
2510
free(dependency_symbol);
2514
* kmod_module_get_dependency_symbols:
2516
* @list: where to return list of module dependency_symbols. Use
2517
* kmod_module_dependency_symbol_get_symbol() and
2518
* kmod_module_dependency_symbol_get_crc(). Release this list with
2519
* kmod_module_dependency_symbols_free_list()
2521
* Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
2523
* After use, free the @list by calling
2524
* kmod_module_dependency_symbols_free_list().
2526
* Returns: 0 on success or < 0 otherwise.
2528
KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
2530
struct kmod_file *file;
2531
struct kmod_elf *elf;
2534
struct kmod_modversion *symbols;
2536
int i, count, ret = 0;
2538
if (mod == NULL || list == NULL)
2541
assert(*list == NULL);
2543
path = kmod_module_get_path(mod);
2547
file = kmod_file_open(mod->ctx, path);
2551
size = kmod_file_get_size(file);
2552
mem = kmod_file_get_contents(file);
2554
elf = kmod_elf_new(mem, size);
2557
goto elf_open_error;
2560
count = kmod_elf_get_dependency_symbols(elf, &symbols);
2563
goto get_strings_error;
2566
for (i = 0; i < count; i++) {
2567
struct kmod_module_dependency_symbol *mv;
2568
struct kmod_list *n;
2570
mv = kmod_module_dependency_symbols_new(symbols[i].crc,
2575
kmod_module_dependency_symbols_free_list(*list);
2580
n = kmod_list_append(*list, mv);
2584
kmod_module_dependency_symbol_free(mv);
2585
kmod_module_dependency_symbols_free_list(*list);
2596
kmod_elf_unref(elf);
2598
kmod_file_unref(file);
2604
* kmod_module_dependency_symbol_get_symbol:
2605
* @entry: a list entry representing a kmod module dependency_symbols
2607
* Get the dependency symbol of a kmod module
2609
* Returns: the symbol of this kmod module dependency_symbols on success or NULL
2610
* on failure. The string is owned by the dependency_symbols, do not free it.
2612
KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry)
2614
struct kmod_module_dependency_symbol *dependency_symbol;
2619
dependency_symbol = entry->data;
2620
return dependency_symbol->symbol;
2624
* kmod_module_dependency_symbol_get_crc:
2625
* @entry: a list entry representing a kmod module dependency_symbol
2627
* Get the crc of a kmod module dependency_symbol.
2629
* Returns: the crc of this kmod module dependency_symbol on success or NULL on
2630
* failure. The string is owned by the dependency_symbol, do not free it.
2632
KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
2634
struct kmod_module_dependency_symbol *dependency_symbol;
2639
dependency_symbol = entry->data;
2640
return dependency_symbol->crc;
2644
* kmod_module_dependency_symbol_get_bind:
2645
* @entry: a list entry representing a kmod module dependency_symbol
2647
* Get the bind type of a kmod module dependency_symbol.
2649
* Returns: the bind of this kmod module dependency_symbol on success
2650
* or < 0 on failure.
2652
KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry)
2654
struct kmod_module_dependency_symbol *dependency_symbol;
2659
dependency_symbol = entry->data;
2660
return dependency_symbol->bind;
2664
* kmod_module_dependency_symbols_free_list:
2665
* @list: kmod module dependency_symbols list
2667
* Release the resources taken by @list
2669
KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list)
2672
kmod_module_dependency_symbol_free(list->data);
2673
list = kmod_list_remove(list);