]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - libkmod/libkmod-module.c
depmod: prevent module dependency files missing during depmod invocation
[thirdparty/kmod.git] / libkmod / libkmod-module.c
index 4d85b76cacd227f402caca510faf3e0f52e37e04..889f26479a987ccb6c32e7a90a80080ae475beb7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libkmod - interface to kernel module operations
  *
- * Copyright (C) 2011  ProFUSION embedded systems
+ * Copyright (C) 2011-2013  ProFUSION embedded systems
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
 #include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fnmatch.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <dirent.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 #include <sys/types.h>
-#include <sys/mman.h>
-#include <string.h>
-#include <fnmatch.h>
+#include <sys/wait.h>
+#ifdef HAVE_LINUX_MODULE_H
+#include <linux/module.h>
+#endif
+
+#include <shared/util.h>
 
 #include "libkmod.h"
-#include "libkmod-private.h"
+#include "libkmod-internal.h"
+
+/**
+ * SECTION:libkmod-module
+ * @short_description: operate on kernel modules
+ */
+
+enum kmod_module_builtin {
+    KMOD_MODULE_BUILTIN_UNKNOWN,
+    KMOD_MODULE_BUILTIN_NO,
+    KMOD_MODULE_BUILTIN_YES,
+};
 
 /**
  * kmod_module:
@@ -53,11 +69,8 @@ struct kmod_module {
        char *options;
        const char *install_commands;   /* owned by kmod_config */
        const char *remove_commands;    /* owned by kmod_config */
-       struct {
-               struct kmod_list *pre;
-               struct kmod_list *post;
-       } softdeps;
        char *alias; /* only set if this module was created from an alias */
+       struct kmod_file *file;
        int n_dep;
        int refcount;
        struct {
@@ -65,43 +78,34 @@ struct kmod_module {
                bool options : 1;
                bool install_commands : 1;
                bool remove_commands : 1;
-               bool softdeps : 1;
        } init;
-};
-
-inline char *modname_normalize(const char *modname, char buf[NAME_MAX],
-                                                               size_t *len)
-{
-       size_t s;
-
-       for (s = 0; s < NAME_MAX - 1; s++) {
-               const char c = modname[s];
-               if (c == '-')
-                       buf[s] = '_';
-               else if (c == '\0' || c == '.')
-                       break;
-               else
-                       buf[s] = c;
-       }
 
-       buf[s] = '\0';
-
-       if (len)
-               *len = s;
-
-       return buf;
-}
-
-static char *path_to_modname(const char *path, char buf[NAME_MAX], size_t *len)
-{
-       char *modname;
-
-       modname = basename(path);
-       if (modname == NULL || modname[0] == '\0')
-               return NULL;
-
-       return modname_normalize(modname, buf, len);
-}
+       /*
+        * mark if module is builtin, i.e. it's present on modules.builtin
+        * file. This is set as soon as it is needed or as soon as we know
+        * about it, i.e. the module was created from builtin lookup.
+        */
+       enum kmod_module_builtin builtin;
+
+       /*
+        * private field used by kmod_module_get_probe_list() to detect
+        * dependency loops
+        */
+       bool visited : 1;
+
+       /*
+        * set by kmod_module_get_probe_list: indicates for probe_insert()
+        * whether the module's command and softdep should be ignored
+        */
+       bool ignorecmd : 1;
+
+       /*
+        * set by kmod_module_get_probe_list: indicates whether this is the
+        * module the user asked for or its dependency, or whether this
+        * is a softdep only
+        */
+       bool required : 1;
+};
 
 static inline const char *path_join(const char *path, size_t prefixlen,
                                                        char buf[PATH_MAX])
@@ -119,6 +123,17 @@ static inline const char *path_join(const char *path, size_t prefixlen,
        return buf;
 }
 
+static inline bool module_is_inkernel(struct kmod_module *mod)
+{
+       int state = kmod_module_get_initstate(mod);
+
+       if (state == KMOD_MODULE_LIVE ||
+                       state == KMOD_MODULE_BUILTIN)
+               return true;
+
+       return false;
+}
+
 int kmod_module_parse_depline(struct kmod_module *mod, char *line)
 {
        struct kmod_ctx *ctx = mod->ctx;
@@ -161,7 +176,7 @@ int kmod_module_parse_depline(struct kmod_module *mod, char *line)
        p++;
        for (p = strtok_r(p, " \t", &saveptr); p != NULL;
                                        p = strtok_r(NULL, " \t", &saveptr)) {
-               struct kmod_module *depmod;
+               struct kmod_module *depmod = NULL;
                const char *path;
 
                path = path_join(p, dirnamelen, buf);
@@ -180,7 +195,7 @@ int kmod_module_parse_depline(struct kmod_module *mod, char *line)
 
                DBG(ctx, "add dep: %s\n", path);
 
-               list = kmod_list_append(list, depmod);
+               list = kmod_list_prepend(list, depmod);
                n++;
        }
 
@@ -196,6 +211,99 @@ fail:
        return err;
 }
 
+void kmod_module_set_visited(struct kmod_module *mod, bool visited)
+{
+       mod->visited = visited;
+}
+
+void kmod_module_set_builtin(struct kmod_module *mod, bool builtin)
+{
+       mod->builtin =
+               builtin ? KMOD_MODULE_BUILTIN_YES : KMOD_MODULE_BUILTIN_NO;
+}
+
+void kmod_module_set_required(struct kmod_module *mod, bool required)
+{
+       mod->required = required;
+}
+
+bool kmod_module_is_builtin(struct kmod_module *mod)
+{
+       if (mod->builtin == KMOD_MODULE_BUILTIN_UNKNOWN) {
+               kmod_module_set_builtin(mod,
+                                       kmod_lookup_alias_is_builtin(mod->ctx, mod->name));
+       }
+
+       return mod->builtin == KMOD_MODULE_BUILTIN_YES;
+}
+/*
+ * Memory layout with alias:
+ *
+ * struct kmod_module {
+ *        hashkey -----.
+ *        alias -----. |
+ *        name ----. | |
+ * }               | | |
+ * name <----------' | |
+ * alias <-----------' |
+ * name\alias <--------'
+ *
+ * Memory layout without alias:
+ *
+ * struct kmod_module {
+ *        hashkey ---.
+ *        alias -----|----> NULL
+ *        name ----. |
+ * }               | |
+ * name <----------'-'
+ *
+ * @key is "name\alias" or "name" (in which case alias == NULL)
+ */
+static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
+                               const char *name, size_t namelen,
+                               const char *alias, size_t aliaslen,
+                               struct kmod_module **mod)
+{
+       struct kmod_module *m;
+       size_t keylen;
+
+       m = kmod_pool_get_module(ctx, key);
+       if (m != NULL) {
+               *mod = kmod_module_ref(m);
+               return 0;
+       }
+
+       if (alias == NULL)
+               keylen = namelen;
+       else
+               keylen = namelen + aliaslen + 1;
+
+       m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
+       if (m == NULL)
+               return -ENOMEM;
+
+       memset(m, 0, sizeof(*m));
+
+       m->ctx = kmod_ref(ctx);
+       m->name = (char *)m + sizeof(*m);
+       memcpy(m->name, key, keylen + 1);
+       if (alias == NULL) {
+               m->hashkey = m->name;
+               m->alias = NULL;
+       } else {
+               m->name[namelen] = '\0';
+               m->alias = m->name + namelen + 1;
+               m->hashkey = m->name + keylen + 1;
+               memcpy(m->hashkey, key, keylen + 1);
+       }
+
+       m->refcount = 1;
+       kmod_pool_add_module(ctx, m, m->hashkey);
+       *mod = m;
+
+       return 0;
+}
+
 /**
  * kmod_module_new_from_name:
  * @ctx: kmod library context
@@ -204,7 +312,7 @@ fail:
  *
  * Create a new struct kmod_module using the module name. @name can not be an
  * alias, file name or anything else; it must be a module name. There's no
- * check if the module does exists in the system.
+ * check if the module exists in the system.
  *
  * This function is also used internally by many others that return a new
  * struct kmod_module or a new list of modules.
@@ -222,72 +330,33 @@ KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
                                                const char *name,
                                                struct kmod_module **mod)
 {
-       struct kmod_module *m;
        size_t namelen;
-       char name_norm[NAME_MAX];
-       char *namesep;
+       char name_norm[PATH_MAX];
 
        if (ctx == NULL || name == NULL || mod == NULL)
                return -ENOENT;
 
-       if (alias_normalize(name, name_norm, &namelen) < 0) {
-               DBG(ctx, "invalid alias: %s\n", name);
-               return -EINVAL;
-       }
-
-       m = kmod_pool_get_module(ctx, name_norm);
-       if (m != NULL) {
-               *mod = kmod_module_ref(m);
-               return 0;
-       }
-
-       namesep = strchr(name_norm, '/');
-       m = malloc(sizeof(*m) + (namesep == NULL ? 1 : 2) * namelen + 2);
-       if (m == NULL) {
-               free(m);
-               return -ENOMEM;
-       }
-
-       memset(m, 0, sizeof(*m));
-
-       m->ctx = kmod_ref(ctx);
-       m->name = (char *)m + sizeof(*m);
-       memcpy(m->name, name_norm, namelen + 1);
-
-       if (namesep) {
-               size_t len = namesep - name_norm;
-
-               m->name[len] = '\0';
-               m->alias = m->name + len + 1;
-               m->hashkey = m->name + namelen + 1;
-               memcpy(m->hashkey, name_norm, namelen + 1);
-       } else {
-               m->hashkey = m->name;
-       }
+       modname_normalize(name, name_norm, &namelen);
 
-       m->refcount = 1;
-       kmod_pool_add_module(ctx, m, m->hashkey);
-       *mod = m;
-
-       return 0;
+       return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod);
 }
 
 int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
                                const char *name, struct kmod_module **mod)
 {
        int err;
-       char key[NAME_MAX];
+       char key[PATH_MAX];
        size_t namelen = strlen(name);
        size_t aliaslen = strlen(alias);
 
-       if (namelen + aliaslen + 2 > NAME_MAX)
+       if (namelen + aliaslen + 2 > PATH_MAX)
                return -ENAMETOOLONG;
 
        memcpy(key, name, namelen);
        memcpy(key + namelen + 1, alias, aliaslen + 1);
-       key[namelen] = '/';
+       key[namelen] = '\\';
 
-       err = kmod_module_new_from_name(ctx, key, mod);
+       err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod);
        if (err < 0)
                return err;
 
@@ -322,7 +391,7 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
        struct kmod_module *m;
        int err;
        struct stat st;
-       char name[NAME_MAX];
+       char name[PATH_MAX];
        char *abspath;
        size_t namelen;
 
@@ -356,8 +425,8 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
                else if (streq(m->path, abspath))
                        free(abspath);
                else {
-                       ERR(ctx, "kmod_module '%s' already exists with different path\n",
-                                                                       name);
+                       ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n",
+                                                       name, abspath, m->path);
                        free(abspath);
                        return -EEXIST;
                }
@@ -366,21 +435,13 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
                return 0;
        }
 
-       m = malloc(sizeof(*m) + namelen + 1);
-       if (m == NULL)
-               return -errno;
-
-       memset(m, 0, sizeof(*m));
+       err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
+       if (err < 0) {
+               free(abspath);
+               return err;
+       }
 
-       m->ctx = kmod_ref(ctx);
-       m->name = (char *)m + sizeof(*m);
-       memcpy(m->name, name, namelen + 1);
        m->path = abspath;
-       m->hashkey = m->name;
-       m->refcount = 1;
-
-       kmod_pool_add_module(ctx, m, m->hashkey);
-
        *mod = m;
 
        return 0;
@@ -407,9 +468,11 @@ KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
        DBG(mod->ctx, "kmod_module %p released\n", mod);
 
        kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
-       kmod_module_unref_list(mod->softdeps.pre);
-       kmod_module_unref_list(mod->softdeps.post);
        kmod_module_unref_list(mod->dep);
+
+       if (mod->file)
+               kmod_file_unref(mod->file);
+
        kmod_unref(mod->ctx);
        free(mod->options);
        free(mod->path);
@@ -475,7 +538,7 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
                                                struct kmod_list **list)
 {
        int err;
-       char alias[NAME_MAX];
+       char alias[PATH_MAX];
 
        if (ctx == NULL || given_alias == NULL)
                return -ENOENT;
@@ -512,6 +575,10 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx,
        err = kmod_lookup_alias_from_aliases_file(ctx, alias, list);
        CHECK_ERR_AND_FINISH(err, fail, list, finish);
 
+       DBG(ctx, "lookup modules.builtin %s\n", alias);
+       err = kmod_lookup_alias_from_builtin_file(ctx, alias, list);
+       CHECK_ERR_AND_FINISH(err, fail, list, finish);
+
 finish:
        DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list);
        return err;
@@ -530,8 +597,7 @@ fail:
  * Drop a reference of each kmod module in @list and releases the resources
  * taken by the list itself.
  *
- * Returns: NULL if @mod is NULL or if the module was released. Otherwise it
- * returns the passed @mod with its refcount decremented.
+ * Returns: 0
  */
 KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
 {
@@ -542,24 +608,28 @@ KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
 }
 
 /**
- * kmod_module_get_dependencies:
- * @mod: kmod module
+ * kmod_module_get_filtered_blacklist:
+ * @ctx: kmod library context
+ * @input: list of kmod_module to be filtered with blacklist
+ * @output: where to save the new list
  *
- * Search the modules.dep index to find the dependencies of the given @mod.
- * The result is cached in @mod, so subsequent calls to this function will
- * return the already searched list of modules.
+ * This function should not be used. Use kmod_module_apply_filter instead.
+ *
+ * Given a list @input, this function filter it out with config's blacklist
+ * and save it in @output.
  *
- * Returns: NULL on failure or if there are any dependencies. Otherwise it
- * returns a list of kmod modules that can be released by calling
- * kmod_module_unref_list().
+ * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
+ * list.
  */
-KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
+KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
+                                               const struct kmod_list *input,
+                                               struct kmod_list **output)
 {
-       struct kmod_list *l, *l_new, *list_new = NULL;
-
-       if (mod == NULL)
-               return NULL;
+       return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output);
+}
 
+static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod)
+{
        if (!mod->init.dep) {
                /* lazy init */
                char *line = kmod_search_moddep(mod->ctx, mod->name);
@@ -574,6 +644,29 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_mod
                        return NULL;
        }
 
+       return mod->dep;
+}
+
+/**
+ * kmod_module_get_dependencies:
+ * @mod: kmod module
+ *
+ * Search the modules.dep index to find the dependencies of the given @mod.
+ * The result is cached in @mod, so subsequent calls to this function will
+ * return the already searched list of modules.
+ *
+ * Returns: NULL on failure. Otherwise it returns a list of kmod modules
+ * that can be released by calling kmod_module_unref_list().
+ */
+KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod)
+{
+       struct kmod_list *l, *l_new, *list_new = NULL;
+
+       if (mod == NULL)
+               return NULL;
+
+       module_get_dependencies_noref(mod);
+
        kmod_list_foreach(l, mod->dep) {
                l_new = kmod_list_append(list_new, kmod_module_ref(l->data));
                if (l_new == NULL) {
@@ -636,7 +729,7 @@ KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod)
  *
  * Get the path of this kmod module. If this kmod module was not created by
  * path, it can search the modules.dep index in order to find out the module
- * under context's dirname (see kmod_get_dirname()).
+ * under context's dirname.
  *
  * Returns: the path of this kmod module or NULL if such information is not
  * available.
@@ -663,117 +756,605 @@ KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod)
        kmod_module_parse_depline((struct kmod_module *) mod, line);
        free(line);
 
-       return mod->path;
-}
+       return mod->path;
+}
+
+
+extern long delete_module(const char *name, unsigned int flags);
+
+/**
+ * kmod_module_remove_module:
+ * @mod: kmod module
+ * @flags: flags to pass to Linux kernel when removing the module. The only valid flag is
+ * KMOD_REMOVE_FORCE: force remove module regardless if it's still in
+ * use by a kernel subsystem or other process;
+ * KMOD_REMOVE_NOWAIT is always enforced, causing us to pass O_NONBLOCK to
+ * delete_module(2).
+ *
+ * Remove a module from Linux kernel.
+ *
+ * Returns: 0 on success or < 0 on failure.
+ */
+KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
+                                                       unsigned int flags)
+{
+       int err;
+
+       if (mod == NULL)
+               return -ENOENT;
+
+       /* Filter out other flags and force ONONBLOCK */
+       flags &= KMOD_REMOVE_FORCE;
+       flags |= KMOD_REMOVE_NOWAIT;
+
+       err = delete_module(mod->name, flags);
+       if (err != 0) {
+               err = -errno;
+               ERR(mod->ctx, "could not remove '%s': %m\n", mod->name);
+       }
+
+       return err;
+}
+
+extern long init_module(const void *mem, unsigned long len, const char *args);
+
+/**
+ * kmod_module_insert_module:
+ * @mod: kmod module
+ * @flags: flags are not passed to Linux Kernel, but instead they dictate the
+ * behavior of this function, valid flags are
+ * KMOD_INSERT_FORCE_VERMAGIC: ignore kernel version magic;
+ * KMOD_INSERT_FORCE_MODVERSION: ignore symbol version hashes.
+ * @options: module's options to pass to Linux Kernel.
+ *
+ * Insert a module in Linux kernel. It opens the file pointed by @mod,
+ * mmap'ing it and passing to kernel.
+ *
+ * Returns: 0 on success or < 0 on failure. If module is already loaded it
+ * returns -EEXIST.
+ */
+KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
+                                                       unsigned int flags,
+                                                       const char *options)
+{
+       int err;
+       const void *mem;
+       off_t size;
+       struct kmod_elf *elf;
+       const char *path;
+       const char *args = options ? options : "";
+
+       if (mod == NULL)
+               return -ENOENT;
+
+       path = kmod_module_get_path(mod);
+       if (path == NULL) {
+               ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
+               return -ENOENT;
+       }
+
+       if (!mod->file) {
+               mod->file = kmod_file_open(mod->ctx, path);
+               if (mod->file == NULL) {
+                       err = -errno;
+                       return err;
+               }
+       }
+
+       if (kmod_file_get_direct(mod->file)) {
+               unsigned int kernel_flags = 0;
+
+               if (flags & KMOD_INSERT_FORCE_VERMAGIC)
+                       kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC;
+               if (flags & KMOD_INSERT_FORCE_MODVERSION)
+                       kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS;
+
+               err = finit_module(kmod_file_get_fd(mod->file), args, kernel_flags);
+               if (err == 0 || errno != ENOSYS)
+                       goto init_finished;
+       }
+
+       if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
+               elf = kmod_file_get_elf(mod->file);
+               if (elf == NULL) {
+                       err = -errno;
+                       return err;
+               }
+
+               if (flags & KMOD_INSERT_FORCE_MODVERSION) {
+                       err = kmod_elf_strip_section(elf, "__versions");
+                       if (err < 0)
+                               INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
+               }
+
+               if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
+                       err = kmod_elf_strip_vermagic(elf);
+                       if (err < 0)
+                               INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
+               }
+
+               mem = kmod_elf_get_memory(elf);
+       } else {
+               mem = kmod_file_get_contents(mod->file);
+       }
+       size = kmod_file_get_size(mod->file);
+
+       err = init_module(mem, size, args);
+init_finished:
+       if (err < 0) {
+               err = -errno;
+               INFO(mod->ctx, "Failed to insert module '%s': %m\n", path);
+       }
+       return err;
+}
+
+static bool module_is_blacklisted(struct kmod_module *mod)
+{
+       struct kmod_ctx *ctx = mod->ctx;
+       const struct kmod_config *config = kmod_get_config(ctx);
+       const struct kmod_list *bl = config->blacklists;
+       const struct kmod_list *l;
+
+       kmod_list_foreach(l, bl) {
+               const char *modname = kmod_blacklist_get_modname(l);
+
+               if (streq(modname, mod->name))
+                       return true;
+       }
+
+       return false;
+}
+
+/**
+ * kmod_module_apply_filter
+ * @ctx: kmod library context
+ * @filter_type: bitmask to filter modules out, valid types are
+ * KMOD_FILTER_BLACKLIST: filter modules in blacklist out;
+ * KMOD_FILTER_BUILTIN: filter builtin modules out.
+ * @input: list of kmod_module to be filtered
+ * @output: where to save the new list
+ *
+ * Given a list @input, this function filter it out by the filter mask
+ * and save it in @output.
+ *
+ * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
+ * list.
+ */
+KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx,
+                                               enum kmod_filter filter_type,
+                                               const struct kmod_list *input,
+                                               struct kmod_list **output)
+{
+       const struct kmod_list *li;
+
+       if (ctx == NULL || output == NULL)
+               return -ENOENT;
+
+       *output = NULL;
+       if (input == NULL)
+               return 0;
+
+       kmod_list_foreach(li, input) {
+               struct kmod_module *mod = li->data;
+               struct kmod_list *node;
+
+               if ((filter_type & KMOD_FILTER_BLACKLIST) &&
+                               module_is_blacklisted(mod))
+                       continue;
+
+               if ((filter_type & KMOD_FILTER_BUILTIN)
+                   && kmod_module_is_builtin(mod))
+                       continue;
+
+               node = kmod_list_append(*output, mod);
+               if (node == NULL)
+                       goto fail;
+
+               *output = node;
+               kmod_module_ref(mod);
+       }
+
+       return 0;
+
+fail:
+       kmod_module_unref_list(*output);
+       *output = NULL;
+       return -ENOMEM;
+}
+
+static int command_do(struct kmod_module *mod, const char *type,
+                                                       const char *cmd)
+{
+       const char *modname = kmod_module_get_name(mod);
+       int err;
+
+       DBG(mod->ctx, "%s %s\n", type, cmd);
+
+       setenv("MODPROBE_MODULE", modname, 1);
+       err = system(cmd);
+       unsetenv("MODPROBE_MODULE");
+
+       if (err == -1 || WEXITSTATUS(err)) {
+               ERR(mod->ctx, "Error running %s command for %s\n",
+                                                               type, modname);
+               if (err != -1)
+                       err = -WEXITSTATUS(err);
+       }
+
+       return err;
+}
+
+struct probe_insert_cb {
+       int (*run_install)(struct kmod_module *m, const char *cmd, void *data);
+       void *data;
+};
+
+static int module_do_install_commands(struct kmod_module *mod,
+                                       const char *options,
+                                       struct probe_insert_cb *cb)
+{
+       const char *command = kmod_module_get_install_commands(mod);
+       char *p;
+       _cleanup_free_ char *cmd;
+       int err;
+       size_t cmdlen, options_len, varlen;
+
+       assert(command);
+
+       if (options == NULL)
+               options = "";
+
+       options_len = strlen(options);
+       cmdlen = strlen(command);
+       varlen = sizeof("$CMDLINE_OPTS") - 1;
+
+       cmd = memdup(command, cmdlen + 1);
+       if (cmd == NULL)
+               return -ENOMEM;
+
+       while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
+               size_t prefixlen = p - cmd;
+               size_t suffixlen = cmdlen - prefixlen - varlen;
+               size_t slen = cmdlen - varlen + options_len;
+               char *suffix = p + varlen;
+               char *s = malloc(slen + 1);
+               if (!s)
+                       return -ENOMEM;
+
+               memcpy(s, cmd, p - cmd);
+               memcpy(s + prefixlen, options, options_len);
+               memcpy(s + prefixlen + options_len, suffix, suffixlen);
+               s[slen] = '\0';
+
+               free(cmd);
+               cmd = s;
+               cmdlen = slen;
+       }
+
+       if (cb->run_install != NULL)
+               err = cb->run_install(mod, cmd, cb->data);
+       else
+               err = command_do(mod, "install", cmd);
+
+       return err;
+}
+
+static char *module_options_concat(const char *opt, const char *xopt)
+{
+       // TODO: we might need to check if xopt overrides options on opt
+       size_t optlen = opt == NULL ? 0 : strlen(opt);
+       size_t xoptlen = xopt == NULL ? 0 : strlen(xopt);
+       char *r;
+
+       if (optlen == 0 && xoptlen == 0)
+               return NULL;
+
+       r = malloc(optlen + xoptlen + 2);
+
+       if (opt != NULL) {
+               memcpy(r, opt, optlen);
+               r[optlen] = ' ';
+               optlen++;
+       }
+
+       if (xopt != NULL)
+               memcpy(r + optlen, xopt, xoptlen);
+
+       r[optlen + xoptlen] = '\0';
+
+       return r;
+}
+
+static int __kmod_module_get_probe_list(struct kmod_module *mod,
+                                               bool required,
+                                               bool ignorecmd,
+                                               struct kmod_list **list);
+
+/* re-entrant */
+static int __kmod_module_fill_softdep(struct kmod_module *mod,
+                                               struct kmod_list **list)
+{
+       struct kmod_list *pre = NULL, *post = NULL, *l;
+       int err;
+
+       err = kmod_module_get_softdeps(mod, &pre, &post);
+       if (err < 0) {
+               ERR(mod->ctx, "could not get softdep: %s\n",
+                                                       strerror(-err));
+               goto fail;
+       }
+
+       kmod_list_foreach(l, pre) {
+               struct kmod_module *m = l->data;
+               err = __kmod_module_get_probe_list(m, false, false, list);
+               if (err < 0)
+                       goto fail;
+       }
+
+       l = kmod_list_append(*list, kmod_module_ref(mod));
+       if (l == NULL) {
+               kmod_module_unref(mod);
+               err = -ENOMEM;
+               goto fail;
+       }
+       *list = l;
+       mod->ignorecmd = (pre != NULL || post != NULL);
+
+       kmod_list_foreach(l, post) {
+               struct kmod_module *m = l->data;
+               err = __kmod_module_get_probe_list(m, false, false, list);
+               if (err < 0)
+                       goto fail;
+       }
+
+fail:
+       kmod_module_unref_list(pre);
+       kmod_module_unref_list(post);
+
+       return err;
+}
+
+/* re-entrant */
+static int __kmod_module_get_probe_list(struct kmod_module *mod,
+                                               bool required,
+                                               bool ignorecmd,
+                                               struct kmod_list **list)
+{
+       struct kmod_list *dep, *l;
+       int err = 0;
+
+       if (mod->visited) {
+               DBG(mod->ctx, "Ignore module '%s': already visited\n",
+                                                               mod->name);
+               return 0;
+       }
+       mod->visited = true;
+
+       dep = kmod_module_get_dependencies(mod);
+       if (required) {
+               /*
+                * Called from kmod_module_probe_insert_module(); set the
+                * ->required flag on mod and all its dependencies before
+                * they are possibly visited through some softdeps.
+                */
+               mod->required = true;
+               kmod_list_foreach(l, dep) {
+                       struct kmod_module *m = l->data;
+                       m->required = true;
+               }
+       }
+
+       kmod_list_foreach(l, dep) {
+               struct kmod_module *m = l->data;
+               err = __kmod_module_fill_softdep(m, list);
+               if (err < 0)
+                       goto finish;
+       }
 
+       if (ignorecmd) {
+               l = kmod_list_append(*list, kmod_module_ref(mod));
+               if (l == NULL) {
+                       kmod_module_unref(mod);
+                       err = -ENOMEM;
+                       goto finish;
+               }
+               *list = l;
+               mod->ignorecmd = true;
+       } else
+               err = __kmod_module_fill_softdep(mod, list);
 
-extern long delete_module(const char *name, unsigned int flags);
+finish:
+       kmod_module_unref_list(dep);
+       return err;
+}
 
-/**
- * kmod_module_remove_module:
- * @mod: kmod module
- * @flags: flags to pass to Linux kernel when removing the module
- *
- * Remove a module from Linux kernel.
- *
- * Returns: 0 on success or < 0 on failure.
- */
-KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
-                                                       unsigned int flags)
+static int kmod_module_get_probe_list(struct kmod_module *mod,
+                                               bool ignorecmd,
+                                               struct kmod_list **list)
 {
        int err;
 
-       if (mod == NULL)
-               return -ENOENT;
+       assert(mod != NULL);
+       assert(list != NULL && *list == NULL);
 
-       /* Filter out other flags */
-       flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
+       /*
+        * Make sure we don't get screwed by previous calls to this function
+        */
+       kmod_set_modules_visited(mod->ctx, false);
+       kmod_set_modules_required(mod->ctx, false);
 
-       err = delete_module(mod->name, flags);
-       if (err != 0) {
-               ERR(mod->ctx, "Could not remove '%s': %s\n", mod->name,
-                                                       strerror(-err));
-               return err;
+       err = __kmod_module_get_probe_list(mod, true, ignorecmd, list);
+       if (err < 0) {
+               kmod_module_unref_list(*list);
+               *list = NULL;
        }
 
-       return 0;
+       return err;
 }
 
-extern long init_module(const void *mem, unsigned long len, const char *args);
-
 /**
- * kmod_module_insert_module:
+ * kmod_module_probe_insert_module:
  * @mod: kmod module
  * @flags: flags are not passed to Linux Kernel, but instead they dictate the
- * behavior of this function.
- * @options: module's options to pass to Linux Kernel.
- *
- * Insert a module in Linux kernel. It opens the file pointed by @mod,
- * mmap'ing it and passing to kernel.
- *
- * Returns: 0 on success or < 0 on failure.
+ * behavior of this function, valid flags are
+ * KMOD_PROBE_FORCE_VERMAGIC: ignore kernel version magic;
+ * KMOD_PROBE_FORCE_MODVERSION: ignore symbol version hashes;
+ * KMOD_PROBE_IGNORE_COMMAND: whether the probe should ignore install
+ * commands and softdeps configured in the system;
+ * KMOD_PROBE_IGNORE_LOADED: do not check whether the module is already
+ * live in kernel or not;
+ * KMOD_PROBE_DRY_RUN: dry run, do not insert module, just call the
+ * associated callback function;
+ * KMOD_PROBE_FAIL_ON_LOADED: if KMOD_PROBE_IGNORE_LOADED is not specified
+ * and the module is already live in kernel, the function will fail if this
+ * flag is specified;
+ * KMOD_PROBE_APPLY_BLACKLIST_ALL: probe will apply KMOD_FILTER_BLACKLIST
+ * filter to this module and its dependencies. If any of the dependencies (or
+ * the module) is blacklisted, the probe will fail, unless the blacklisted
+ * module is already live in kernel;
+ * KMOD_PROBE_APPLY_BLACKLIST: probe will fail if the module is blacklisted;
+ * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY: probe will fail if the module is an
+ * alias and is blacklisted.
+ * @extra_options: module's options to pass to Linux Kernel. It applies only
+ * to @mod, not to its dependencies.
+ * @run_install: function to run when @mod is backed by an install command.
+ * @data: data to give back to @run_install callback
+ * @print_action: function to call with the action being taken (install or
+ * insmod). It's useful for tools like modprobe when running with verbose
+ * output or in dry-run mode.
+ *
+ * Insert a module in Linux kernel resolving dependencies, soft dependencies,
+ * install commands and applying blacklist.
+ *
+ * If @run_install is NULL, this function will fork and exec by calling
+ * system(3). Don't pass a NULL argument in @run_install if your binary is
+ * setuid/setgid (see warning in system(3)). If you need control over the
+ * execution of an install command, give a callback function instead.
+ *
+ * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on
+ * failure.
  */
-KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
-                                                       unsigned int flags,
-                                                       const char *options)
+KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
+                       unsigned int flags, const char *extra_options,
+                       int (*run_install)(struct kmod_module *m,
+                                               const char *cmd, void *data),
+                       const void *data,
+                       void (*print_action)(struct kmod_module *m,
+                                               bool install,
+                                               const char *options))
 {
+       struct kmod_list *list = NULL, *l;
+       struct probe_insert_cb cb;
        int err;
-       const void *mem;
-       off_t size;
-       struct kmod_file *file;
-       struct kmod_elf *elf = NULL;
-       const char *path;
-       const char *args = options ? options : "";
 
        if (mod == NULL)
                return -ENOENT;
 
-       path = kmod_module_get_path(mod);
-       if (path == NULL) {
-               ERR(mod->ctx, "Could not find module by name='%s'\n", mod->name);
-               return -ENOSYS;
+       if (!(flags & KMOD_PROBE_IGNORE_LOADED)
+                                       && module_is_inkernel(mod)) {
+               if (flags & KMOD_PROBE_FAIL_ON_LOADED)
+                       return -EEXIST;
+               else
+                       return 0;
        }
 
-       file = kmod_file_open(path);
-       if (file == NULL) {
-               err = -errno;
-               return err;
+       /*
+        * Ugly assignement + check. We need to check if we were told to check
+        * blacklist and also return the reason why we failed.
+        * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the
+        * module is an alias, so we also need to check it
+        */
+       if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY)))
+                       || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL)
+                       || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) {
+               if (module_is_blacklisted(mod))
+                       return err;
        }
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
+       err = kmod_module_get_probe_list(mod,
+                               !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list);
+       if (err < 0)
+               return err;
 
-       if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) {
-               elf = kmod_elf_new(mem, size);
-               if (elf == NULL) {
-                       err = -errno;
-                       goto elf_failed;
-               }
+       if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
+               struct kmod_list *filtered = NULL;
 
-               if (flags & KMOD_INSERT_FORCE_MODVERSION) {
-                       err = kmod_elf_strip_section(elf, "__versions");
-                       if (err < 0)
-                               INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err));
+               err = kmod_module_apply_filter(mod->ctx,
+                               KMOD_FILTER_BLACKLIST, list, &filtered);
+               if (err < 0)
+                       return err;
+
+               kmod_module_unref_list(list);
+               if (filtered == NULL)
+                       return KMOD_PROBE_APPLY_BLACKLIST_ALL;
+
+               list = filtered;
+       }
+
+       cb.run_install = run_install;
+       cb.data = (void *) data;
+
+       kmod_list_foreach(l, list) {
+               struct kmod_module *m = l->data;
+               const char *moptions = kmod_module_get_options(m);
+               const char *cmd = kmod_module_get_install_commands(m);
+               char *options;
+
+               if (!(flags & KMOD_PROBE_IGNORE_LOADED)
+                                               && module_is_inkernel(m)) {
+                       DBG(mod->ctx, "Ignoring module '%s': already loaded\n",
+                                                               m->name);
+                       err = -EEXIST;
+                       goto finish_module;
                }
 
-               if (flags & KMOD_INSERT_FORCE_VERMAGIC) {
-                       err = kmod_elf_strip_vermagic(elf);
-                       if (err < 0)
-                               INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err));
+               options = module_options_concat(moptions,
+                                       m == mod ? extra_options : NULL);
+
+               if (cmd != NULL && !m->ignorecmd) {
+                       if (print_action != NULL)
+                               print_action(m, true, options ?: "");
+
+                       if (!(flags & KMOD_PROBE_DRY_RUN))
+                               err = module_do_install_commands(m, options,
+                                                                       &cb);
+               } else {
+                       if (print_action != NULL)
+                               print_action(m, false, options ?: "");
+
+                       if (!(flags & KMOD_PROBE_DRY_RUN))
+                               err = kmod_module_insert_module(m, flags,
+                                                               options);
                }
 
-               mem = kmod_elf_get_memory(elf);
-       }
+               free(options);
+
+finish_module:
+               /*
+                * Treat "already loaded" error. If we were told to stop on
+                * already loaded and the module being loaded is not a softdep
+                * or dep, bail out. Otherwise, just ignore and continue.
+                *
+                * We need to check here because of race conditions. We
+                * checked first if module was already loaded but it may have
+                * been loaded between the check and the moment we try to
+                * insert it.
+                */
+               if (err == -EEXIST && m == mod &&
+                               (flags & KMOD_PROBE_FAIL_ON_LOADED))
+                       break;
 
-       err = init_module(mem, size, args);
-       if (err < 0)
-               ERR(mod->ctx, "Failed to insert module '%s'\n", path);
+               /*
+                * Ignore errors from softdeps
+                */
+               if (err == -EEXIST || !m->required)
+                       err = 0;
 
-       if (elf != NULL)
-               kmod_elf_unref(elf);
-elf_failed:
-       kmod_file_unref(file);
+               else if (err < 0)
+                       break;
+       }
 
+       kmod_module_unref_list(list);
        return err;
 }
 
@@ -796,13 +1377,14 @@ KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod)
        if (!mod->init.options) {
                /* lazy init */
                struct kmod_module *m = (struct kmod_module *)mod;
-               const struct kmod_list *l, *ctx_options;
+               const struct kmod_list *l;
+               const struct kmod_config *config;
                char *opts = NULL;
                size_t optslen = 0;
 
-               ctx_options = kmod_get_options(mod->ctx);
+               config = kmod_get_config(mod->ctx);
 
-               kmod_list_foreach(l, ctx_options) {
+               kmod_list_foreach(l, config->options) {
                        const char *modname = kmod_option_get_modname(l);
                        const char *str;
                        size_t len;
@@ -870,11 +1452,12 @@ KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_modul
        if (!mod->init.install_commands) {
                /* lazy init */
                struct kmod_module *m = (struct kmod_module *)mod;
-               const struct kmod_list *l, *ctx_install_commands;
+               const struct kmod_list *l;
+               const struct kmod_config *config;
 
-               ctx_install_commands = kmod_get_install_commands(mod->ctx);
+               config = kmod_get_config(mod->ctx);
 
-               kmod_list_foreach(l, ctx_install_commands) {
+               kmod_list_foreach(l, config->install_commands) {
                        const char *modname = kmod_command_get_modname(l);
 
                        if (fnmatch(modname, mod->name, 0) != 0)
@@ -928,19 +1511,22 @@ static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const
  * @post: where to save the list of post soft dependencies.
  *
  * Get soft dependencies for this kmod module. Soft dependencies come
- * from configuration file and are cached in @mod. The first call
- * to this function will search for this module in configuration and
- * subsequent calls return the known results.
+ * from configuration file and are not cached in @mod because it may include
+ * dependency cycles that would make we leak kmod_module. Any call
+ * to this function will search for this module in configuration, allocate a
+ * list and return the result.
  *
  * Both @pre and @post are newly created list of kmod_module and
  * should be unreferenced with kmod_module_unref_list().
  *
  * Returns: 0 on success or < 0 otherwise.
  */
-KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, struct kmod_list **pre, struct kmod_list **post)
+KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
+                                               struct kmod_list **pre,
+                                               struct kmod_list **post)
 {
        const struct kmod_list *l;
-       struct kmod_list *l_new;
+       const struct kmod_config *config;
 
        if (mod == NULL || pre == NULL || post == NULL)
                return -ENOENT;
@@ -948,64 +1534,31 @@ KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, struct k
        assert(*pre == NULL);
        assert(*post == NULL);
 
-       if (!mod->init.softdeps) {
-               /* lazy init */
-               struct kmod_module *m = (struct kmod_module *)mod;
-               const struct kmod_list *ctx_softdeps;
-
-               ctx_softdeps = kmod_get_softdeps(mod->ctx);
-
-               kmod_list_foreach(l, ctx_softdeps) {
-                       const char *modname = kmod_softdep_get_name(l);
-                       const char * const *array;
-                       unsigned count;
+       config = kmod_get_config(mod->ctx);
 
-                       if (fnmatch(modname, mod->name, 0) != 0)
-                               continue;
-
-                       array = kmod_softdep_get_pre(l, &count);
-                       m->softdeps.pre = lookup_softdep(mod->ctx, array, count);
-                       array = kmod_softdep_get_post(l, &count);
-                       m->softdeps.post = lookup_softdep(mod->ctx, array, count);
-                       /*
-                        * find only the first command, as modprobe from
-                        * module-init-tools does
-                        */
-                       break;
-               }
+       kmod_list_foreach(l, config->softdeps) {
+               const char *modname = kmod_softdep_get_name(l);
+               const char * const *array;
+               unsigned count;
 
-               m->init.softdeps = true;
-       }
+               if (fnmatch(modname, mod->name, 0) != 0)
+                       continue;
 
-       kmod_list_foreach(l, mod->softdeps.pre) {
-               l_new = kmod_list_append(*pre, kmod_module_ref(l->data));
-               if (l_new == NULL) {
-                       kmod_module_unref(l->data);
-                       goto fail;
-               }
-               *pre = l_new;
-       }
+               array = kmod_softdep_get_pre(l, &count);
+               *pre = lookup_softdep(mod->ctx, array, count);
+               array = kmod_softdep_get_post(l, &count);
+               *post = lookup_softdep(mod->ctx, array, count);
 
-       kmod_list_foreach(l, mod->softdeps.post) {
-               l_new = kmod_list_append(*post, kmod_module_ref(l->data));
-               if (l_new == NULL) {
-                       kmod_module_unref(l->data);
-                       goto fail;
-               }
-               *post = l_new;
+               /*
+                * find only the first command, as modprobe from
+                * module-init-tools does
+                */
+               break;
        }
 
        return 0;
-
-fail:
-       kmod_module_unref_list(*pre);
-       *pre = NULL;
-       kmod_module_unref_list(*post);
-       *post = NULL;
-       return -ENOMEM;
 }
 
-
 /**
  * kmod_module_get_remove_commands:
  * @mod: kmod module
@@ -1028,11 +1581,12 @@ KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module
        if (!mod->init.remove_commands) {
                /* lazy init */
                struct kmod_module *m = (struct kmod_module *)mod;
-               const struct kmod_list *l, *ctx_remove_commands;
+               const struct kmod_list *l;
+               const struct kmod_config *config;
 
-               ctx_remove_commands = kmod_get_remove_commands(mod->ctx);
+               config = kmod_get_config(mod->ctx);
 
-               kmod_list_foreach(l, ctx_remove_commands) {
+               kmod_list_foreach(l, config->remove_commands) {
                        const char *modname = kmod_command_get_modname(l);
 
                        if (fnmatch(modname, mod->name, 0) != 0)
@@ -1076,7 +1630,7 @@ void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd)
  * Create a new list of kmod modules with all modules currently loaded in
  * kernel. It uses /proc/modules to get the names of loaded modules and to
  * create kmod modules by calling kmod_module_new_from_name() in each of them.
- * They are put are put in @list in no particular order.
+ * They are put in @list in no particular order.
  *
  * The initial refcount is 1, and needs to be decremented to release the
  * resources of the kmod_module. The returned @list must be released by
@@ -1108,13 +1662,14 @@ KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
                struct kmod_module *m;
                struct kmod_list *node;
                int err;
+               size_t len = strlen(line);
                char *saveptr, *name = strtok_r(line, " \t", &saveptr);
 
                err = kmod_module_new_from_name(ctx, name, &m);
                if (err < 0) {
                        ERR(ctx, "could not get module from name '%s': %s\n",
                                name, strerror(-err));
-                       continue;
+                       goto eat_line;
                }
 
                node = kmod_list_append(l, m);
@@ -1124,6 +1679,9 @@ KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
                        ERR(ctx, "out of memory\n");
                        kmod_module_unref(m);
                }
+eat_line:
+               while (line[len - 1] != '\n' && fgets(line, sizeof(line), fp))
+                       len = strlen(line);
        }
 
        fclose(fp);
@@ -1143,18 +1701,18 @@ KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx,
  */
 KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state)
 {
-    switch (state) {
-    case KMOD_MODULE_BUILTIN:
-       return "builtin";
-    case KMOD_MODULE_LIVE:
-       return "live";
-    case KMOD_MODULE_COMING:
-       return "coming";
-    case KMOD_MODULE_GOING:
-       return "going";
-    default:
-       return NULL;
-    }
+       switch (state) {
+       case KMOD_MODULE_BUILTIN:
+               return "builtin";
+       case KMOD_MODULE_LIVE:
+               return "live";
+       case KMOD_MODULE_COMING:
+               return "coming";
+       case KMOD_MODULE_GOING:
+               return "going";
+       default:
+               return NULL;
+       }
 }
 
 /**
@@ -1164,7 +1722,11 @@ KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate sta
  * Get the initstate of this @mod, as returned by Linux Kernel, by reading
  * /sys filesystem.
  *
- * Returns: < 0 on error or enum kmod_initstate if module is found in kernel.
+ * Returns: < 0 on error or module state if module is found in kernel, valid states are
+ * KMOD_MODULE_BUILTIN: module is builtin;
+ * KMOD_MODULE_LIVE: module is live in kernel;
+ * KMOD_MODULE_COMING: module is being loaded;
+ * KMOD_MODULE_GOING: module is being unloaded.
  */
 KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
 {
@@ -1174,17 +1736,24 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
        if (mod == NULL)
                return -ENOENT;
 
+       /* remove const: this can only change internal state */
+       if (kmod_module_is_builtin((struct kmod_module *)mod))
+               return KMOD_MODULE_BUILTIN;
+
        pathlen = snprintf(path, sizeof(path),
                                "/sys/module/%s/initstate", mod->name);
        fd = open(path, O_RDONLY|O_CLOEXEC);
        if (fd < 0) {
                err = -errno;
 
+               DBG(mod->ctx, "could not open '%s': %s\n",
+                       path, strerror(-err));
+
                if (pathlen > (int)sizeof("/initstate") - 1) {
                        struct stat st;
                        path[pathlen - (sizeof("/initstate") - 1)] = '\0';
                        if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
-                               return KMOD_MODULE_BUILTIN;
+                               return KMOD_MODULE_COMING;
                }
 
                DBG(mod->ctx, "could not open '%s': %s\n",
@@ -1215,37 +1784,60 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
  * kmod_module_get_size:
  * @mod: kmod module
  *
- * Get the size of this kmod module as returned by Linux kernel. It reads the
- * file /proc/modules to search for this module and get its size.
+ * Get the size of this kmod module as returned by Linux kernel. If supported,
+ * the size is read from the coresize attribute in /sys/module. For older
+ * kernels, this falls back on /proc/modules and searches for the specified
+ * module to get its size.
  *
  * Returns: the size of this kmod module.
  */
 KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
 {
-       // FIXME TODO: this should be available from /sys/module/foo
        FILE *fp;
        char line[4096];
        int lineno = 0;
        long size = -ENOENT;
+       int dfd, cfd;
 
        if (mod == NULL)
                return -ENOENT;
 
+       /* try to open the module dir in /sys. If this fails, don't
+        * bother trying to find the size as we know the module isn't
+        * loaded.
+        */
+       snprintf(line, sizeof(line), "/sys/module/%s", mod->name);
+       dfd = open(line, O_RDONLY|O_CLOEXEC);
+       if (dfd < 0)
+               return -errno;
+
+       /* available as of linux 3.3.x */
+       cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC);
+       if (cfd >= 0) {
+               if (read_str_long(cfd, &size, 10) < 0)
+                       ERR(mod->ctx, "failed to read coresize from %s\n", line);
+               close(cfd);
+               goto done;
+       }
+
+       /* fall back on parsing /proc/modules */
        fp = fopen("/proc/modules", "re");
        if (fp == NULL) {
                int err = -errno;
                ERR(mod->ctx,
                    "could not open /proc/modules: %s\n", strerror(errno));
+               close(dfd);
                return err;
        }
 
        while (fgets(line, sizeof(line), fp)) {
+               size_t len = strlen(line);
                char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr);
                long value;
 
                lineno++;
                if (tok == NULL || !streq(tok, mod->name))
-                       continue;
+                       goto eat_line;
 
                tok = strtok_r(NULL, " \t", &saveptr);
                if (tok == NULL) {
@@ -1263,8 +1855,14 @@ KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
 
                size = value;
                break;
+eat_line:
+               while (line[len - 1] != '\n' && fgets(line, sizeof(line), fp))
+                       len = strlen(line);
        }
        fclose(fp);
+
+done:
+       close(dfd);
        return size;
 }
 
@@ -1275,7 +1873,7 @@ KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod)
  * Get the ref count of this @mod, as returned by Linux Kernel, by reading
  * /sys filesystem.
  *
- * Returns: 0 on success or < 0 on failure.
+ * Returns: the reference count on success or < 0 on failure.
  */
 KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
 {
@@ -1290,7 +1888,7 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod)
        fd = open(path, O_RDONLY|O_CLOEXEC);
        if (fd < 0) {
                err = -errno;
-               ERR(mod->ctx, "could not open '%s': %s\n",
+               DBG(mod->ctx, "could not open '%s': %s\n",
                        path, strerror(errno));
                return err;
        }
@@ -1319,9 +1917,10 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *
 {
        char dname[PATH_MAX];
        struct kmod_list *list = NULL;
+       struct dirent *dent;
        DIR *d;
 
-       if (mod == NULL)
+       if (mod == NULL || mod->ctx == NULL)
                return NULL;
 
        snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name);
@@ -1333,32 +1932,22 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *
                return NULL;
        }
 
-       for (;;) {
-               struct dirent de, *entp;
+       for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
                struct kmod_module *holder;
                struct kmod_list *l;
                int err;
 
-               err = readdir_r(d, &de, &entp);
-               if (err != 0) {
-                       ERR(mod->ctx, "could not iterate for module '%s': %s\n",
-                                               mod->name, strerror(-err));
-                       goto fail;
-               }
-
-               if (entp == NULL)
-                       break;
-
-               if (de.d_name[0] == '.') {
-                       if (de.d_name[1] == '\0' ||
-                           (de.d_name[1] == '.' && de.d_name[2] == '\0'))
+               if (dent->d_name[0] == '.') {
+                       if (dent->d_name[1] == '\0' ||
+                           (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
                                continue;
                }
 
-               err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder);
+               err = kmod_module_new_from_name(mod->ctx, dent->d_name,
+                                               &holder);
                if (err < 0) {
                        ERR(mod->ctx, "could not create module for '%s': %s\n",
-                               de.d_name, strerror(-err));
+                               dent->d_name, strerror(-err));
                        goto fail;
                }
 
@@ -1408,6 +1997,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
 {
        char dname[PATH_MAX];
        struct kmod_list *list = NULL;
+       struct dirent *dent;
        DIR *d;
        int dfd;
 
@@ -1425,31 +2015,23 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
 
        dfd = dirfd(d);
 
-       for (;;) {
-               struct dirent de, *entp;
+       for (dent = readdir(d); dent; dent = readdir(d)) {
                struct kmod_module_section *section;
                struct kmod_list *l;
                unsigned long address;
                size_t namesz;
                int fd, err;
 
-               err = readdir_r(d, &de, &entp);
-               if (err != 0) {
-                       ERR(mod->ctx, "could not iterate for module '%s': %s\n",
-                                               mod->name, strerror(-err));
-                       goto fail;
-               }
-
-               if (de.d_name[0] == '.') {
-                       if (de.d_name[1] == '\0' ||
-                           (de.d_name[1] == '.' && de.d_name[2] == '\0'))
+               if (dent->d_name[0] == '.') {
+                       if (dent->d_name[1] == '\0' ||
+                           (dent->d_name[1] == '.' && dent->d_name[2] == '\0'))
                                continue;
                }
 
-               fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC);
+               fd = openat(dfd, dent->d_name, O_RDONLY|O_CLOEXEC);
                if (fd < 0) {
                        ERR(mod->ctx, "could not open '%s/%s': %m\n",
-                                                       dname, de.d_name);
+                                                       dname, dent->d_name);
                        goto fail;
                }
 
@@ -1457,11 +2039,11 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
                close(fd);
                if (err < 0) {
                        ERR(mod->ctx, "could not read long from '%s/%s': %m\n",
-                                                       dname, de.d_name);
+                                                       dname, dent->d_name);
                        goto fail;
                }
 
-               namesz = strlen(de.d_name) + 1;
+               namesz = strlen(dent->d_name) + 1;
                section = malloc(sizeof(*section) + namesz);
 
                if (section == NULL) {
@@ -1470,7 +2052,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module
                }
 
                section->address = address;
-               memcpy(section->name, de.d_name, namesz);
+               memcpy(section->name, dent->d_name, namesz);
 
                l = kmod_list_append(list, section);
                if (l != NULL) {
@@ -1549,6 +2131,25 @@ KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list)
        }
 }
 
+static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod)
+{
+       if (mod->file == NULL) {
+               const char *path = kmod_module_get_path(mod);
+
+               if (path == NULL) {
+                       errno = ENOENT;
+                       return NULL;
+               }
+
+               ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx,
+                                                                       path);
+               if (mod->file == NULL)
+                       return NULL;
+       }
+
+       return kmod_file_get_elf(mod->file);
+}
+
 struct kmod_module_info {
        char *key;
        char value[];
@@ -1563,7 +2164,7 @@ static struct kmod_module_info *kmod_module_info_new(const char *key, size_t key
                return NULL;
 
        info->key = (char *)info + sizeof(struct kmod_module_info)
-               + valuelen + 1;
+                   + valuelen + 1;
        memcpy(info->key, key, keylen);
        info->key[keylen] = '\0';
        memcpy(info->value, value, valuelen);
@@ -1576,17 +2177,92 @@ static void kmod_module_info_free(struct kmod_module_info *info)
        free(info);
 }
 
+static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen)
+{
+       struct kmod_module_info *info;
+       struct kmod_list *n;
+
+       info = kmod_module_info_new(key, keylen, value, valuelen);
+       if (info == NULL)
+               return NULL;
+       n = kmod_list_append(*list, info);
+       if (n != NULL)
+               *list = n;
+       else
+               kmod_module_info_free(info);
+       return n;
+}
+
+static char *kmod_module_hex_to_str(const char *hex, size_t len)
+{
+       char *str;
+       int i;
+       int j;
+       const size_t line_limit = 20;
+       size_t str_len;
+
+       str_len = len * 3; /* XX: or XX\0 */
+       str_len += ((str_len + line_limit - 1) / line_limit - 1) * 3; /* \n\t\t */
+
+       str = malloc(str_len);
+       if (str == NULL)
+               return NULL;
+
+       for (i = 0, j = 0; i < (int)len; i++) {
+               j += sprintf(str + j, "%02X", (unsigned char)hex[i]);
+               if (i < (int)len - 1) {
+                       str[j++] = ':';
+
+                       if ((i + 1) % line_limit == 0)
+                               j += sprintf(str + j, "\n\t\t");
+               }
+       }
+       return str;
+}
+
+static struct kmod_list *kmod_module_info_append_hex(struct kmod_list **list,
+                                                    const char *key,
+                                                    size_t keylen,
+                                                    const char *value,
+                                                    size_t valuelen)
+{
+       char *hex;
+       struct kmod_list *n;
+
+       if (valuelen > 0) {
+               /* Display as 01:12:DE:AD:BE:EF:... */
+               hex = kmod_module_hex_to_str(value, valuelen);
+               if (hex == NULL)
+                       goto list_error;
+               n = kmod_module_info_append(list, key, keylen, hex, strlen(hex));
+               free(hex);
+               if (n == NULL)
+                       goto list_error;
+       } else {
+               n = kmod_module_info_append(list, key, keylen, NULL, 0);
+               if (n == NULL)
+                       goto list_error;
+       }
+
+       return n;
+
+list_error:
+       return NULL;
+}
+
 /**
  * kmod_module_get_info:
  * @mod: kmod module
  * @list: where to return list of module information. Use
  *        kmod_module_info_get_key() and
  *        kmod_module_info_get_value(). Release this list with
- *        kmod_module_info_unref_list()
+ *        kmod_module_info_free_list()
  *
  * Get a list of entries in ELF section ".modinfo", these contain
  * alias, license, depends, vermagic and other keys with respective
- * values.
+ * values. If the module is signed (CONFIG_MODULE_SIG), information
+ * about the module signature is included as well: signer,
+ * sig_key and sig_hashalgo.
  *
  * After use, free the @list by calling kmod_module_info_free_list().
  *
@@ -1594,44 +2270,25 @@ static void kmod_module_info_free(struct kmod_module_info *info)
  */
 KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list)
 {
-       struct kmod_file *file;
        struct kmod_elf *elf;
-       const char *path;
-       const void *mem;
        char **strings;
-       size_t size;
-       int i, count, ret = 0;
+       int i, count, ret = -ENOMEM;
+       struct kmod_signature_info sig_info = {};
 
        if (mod == NULL || list == NULL)
                return -ENOENT;
 
        assert(*list == NULL);
 
-       path = kmod_module_get_path(mod);
-       if (path == NULL)
-               return -ENOENT;
-
-       file = kmod_file_open(path);
-       if (file == NULL)
+       elf = kmod_module_get_elf(mod);
+       if (elf == NULL)
                return -errno;
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
-
-       elf = kmod_elf_new(mem, size);
-       if (elf == NULL) {
-               ret = -errno;
-               goto elf_open_error;
-       }
-
        count = kmod_elf_get_strings(elf, ".modinfo", &strings);
-       if (count < 0) {
-               ret = count;
-               goto get_strings_error;
-       }
+       if (count < 0)
+               return count;
 
        for (i = 0; i < count; i++) {
-               struct kmod_module_info *info;
                struct kmod_list *n;
                const char *key, *value;
                size_t keylen, valuelen;
@@ -1641,40 +2298,70 @@ KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_
                if (value == NULL) {
                        keylen = strlen(key);
                        valuelen = 0;
+                       value = key;
                } else {
                        keylen = value - key;
                        value++;
                        valuelen = strlen(value);
                }
 
-               info = kmod_module_info_new(key, keylen, value, valuelen);
-               if (info == NULL) {
-                       ret = -errno;
-                       kmod_module_info_free_list(*list);
-                       *list = NULL;
+               n = kmod_module_info_append(list, key, keylen, value, valuelen);
+               if (n == NULL)
                        goto list_error;
-               }
+       }
 
-               n = kmod_list_append(*list, info);
-               if (n != NULL)
-                       *list = n;
-               else {
-                       kmod_module_info_free(info);
-                       kmod_module_info_free_list(*list);
-                       *list = NULL;
-                       ret = -ENOMEM;
+       if (kmod_module_signature_info(mod->file, &sig_info)) {
+               struct kmod_list *n;
+
+               n = kmod_module_info_append(list, "sig_id", strlen("sig_id"),
+                               sig_info.id_type, strlen(sig_info.id_type));
+               if (n == NULL)
                        goto list_error;
-               }
+               count++;
+
+               n = kmod_module_info_append(list, "signer", strlen("signer"),
+                               sig_info.signer, sig_info.signer_len);
+               if (n == NULL)
+                       goto list_error;
+               count++;
+
+
+               n = kmod_module_info_append_hex(list, "sig_key", strlen("sig_key"),
+                                               sig_info.key_id,
+                                               sig_info.key_id_len);
+               if (n == NULL)
+                       goto list_error;
+               count++;
+
+               n = kmod_module_info_append(list,
+                               "sig_hashalgo", strlen("sig_hashalgo"),
+                               sig_info.hash_algo, strlen(sig_info.hash_algo));
+               if (n == NULL)
+                       goto list_error;
+               count++;
+
+               /*
+                * Omit sig_info.algo for now, as these
+                * are currently constant.
+                */
+               n = kmod_module_info_append_hex(list, "signature",
+                                               strlen("signature"),
+                                               sig_info.sig,
+                                               sig_info.sig_len);
+
+               if (n == NULL)
+                       goto list_error;
+               count++;
+
        }
        ret = count;
 
 list_error:
+       if (ret < 0) {
+               kmod_module_info_free_list(*list);
+               *list = NULL;
+       }
        free(strings);
-get_strings_error:
-       kmod_elf_unref(elf);
-elf_open_error:
-       kmod_file_unref(file);
-
        return ret;
 }
 
@@ -1760,9 +2447,9 @@ static void kmod_module_version_free(struct kmod_module_version *version)
  * kmod_module_get_versions:
  * @mod: kmod module
  * @list: where to return list of module versions. Use
- *        kmod_module_versions_get_symbol() and
- *        kmod_module_versions_get_crc(). Release this list with
- *        kmod_module_versions_unref_list()
+ *        kmod_module_version_get_symbol() and
+ *        kmod_module_version_get_crc(). Release this list with
+ *        kmod_module_versions_free_list()
  *
  * Get a list of entries in ELF section "__versions".
  *
@@ -1772,12 +2459,8 @@ static void kmod_module_version_free(struct kmod_module_version *version)
  */
 KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list)
 {
-       struct kmod_file *file;
        struct kmod_elf *elf;
-       const char *path;
-       const void *mem;
        struct kmod_modversion *versions;
-       size_t size;
        int i, count, ret = 0;
 
        if (mod == NULL || list == NULL)
@@ -1785,28 +2468,13 @@ KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct k
 
        assert(*list == NULL);
 
-       path = kmod_module_get_path(mod);
-       if (path == NULL)
-               return -ENOENT;
-
-       file = kmod_file_open(path);
-       if (file == NULL)
+       elf = kmod_module_get_elf(mod);
+       if (elf == NULL)
                return -errno;
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
-
-       elf = kmod_elf_new(mem, size);
-       if (elf == NULL) {
-               ret = -errno;
-               goto elf_open_error;
-       }
-
        count = kmod_elf_get_modversions(elf, &versions);
-       if (count < 0) {
-               ret = count;
-               goto get_strings_error;
-       }
+       if (count < 0)
+               return count;
 
        for (i = 0; i < count; i++) {
                struct kmod_module_version *mv;
@@ -1835,16 +2503,11 @@ KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct k
 
 list_error:
        free(versions);
-get_strings_error:
-       kmod_elf_unref(elf);
-elf_open_error:
-       kmod_file_unref(file);
-
        return ret;
 }
 
 /**
- * kmod_module_versions_get_symbol:
+ * kmod_module_version_get_symbol:
  * @entry: a list entry representing a kmod module versions
  *
  * Get the symbol of a kmod module versions.
@@ -1856,7 +2519,7 @@ KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *e
 {
        struct kmod_module_version *version;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return NULL;
 
        version = entry->data;
@@ -1869,14 +2532,13 @@ KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *e
  *
  * Get the crc of a kmod module version.
  *
- * Returns: the crc of this kmod module version on success or NULL on
- * failure. The string is owned by the version, do not free it.
+ * Returns: the crc of this kmod module version if available, otherwise default to 0.
  */
 KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry)
 {
        struct kmod_module_version *version;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return 0;
 
        version = entry->data;
@@ -1925,9 +2587,9 @@ static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
  * kmod_module_get_symbols:
  * @mod: kmod module
  * @list: where to return list of module symbols. Use
- *        kmod_module_symbols_get_symbol() and
- *        kmod_module_symbols_get_crc(). Release this list with
- *        kmod_module_symbols_unref_list()
+ *        kmod_module_symbol_get_symbol() and
+ *        kmod_module_symbol_get_crc(). Release this list with
+ *        kmod_module_symbols_free_list()
  *
  * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings".
  *
@@ -1937,12 +2599,8 @@ static void kmod_module_symbol_free(struct kmod_module_symbol *symbol)
  */
 KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list)
 {
-       struct kmod_file *file;
        struct kmod_elf *elf;
-       const char *path;
-       const void *mem;
        struct kmod_modversion *symbols;
-       size_t size;
        int i, count, ret = 0;
 
        if (mod == NULL || list == NULL)
@@ -1950,28 +2608,13 @@ KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct km
 
        assert(*list == NULL);
 
-       path = kmod_module_get_path(mod);
-       if (path == NULL)
-               return -ENOENT;
-
-       file = kmod_file_open(path);
-       if (file == NULL)
+       elf = kmod_module_get_elf(mod);
+       if (elf == NULL)
                return -errno;
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
-
-       elf = kmod_elf_new(mem, size);
-       if (elf == NULL) {
-               ret = -errno;
-               goto elf_open_error;
-       }
-
        count = kmod_elf_get_symbols(elf, &symbols);
-       if (count < 0) {
-               ret = count;
-               goto get_strings_error;
-       }
+       if (count < 0)
+               return count;
 
        for (i = 0; i < count; i++) {
                struct kmod_module_symbol *mv;
@@ -2000,16 +2643,11 @@ KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct km
 
 list_error:
        free(symbols);
-get_strings_error:
-       kmod_elf_unref(elf);
-elf_open_error:
-       kmod_file_unref(file);
-
        return ret;
 }
 
 /**
- * kmod_module_symbols_get_symbol:
+ * kmod_module_symbol_get_symbol:
  * @entry: a list entry representing a kmod module symbols
  *
  * Get the symbol of a kmod module symbols.
@@ -2021,7 +2659,7 @@ KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *en
 {
        struct kmod_module_symbol *symbol;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return NULL;
 
        symbol = entry->data;
@@ -2034,14 +2672,13 @@ KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *en
  *
  * Get the crc of a kmod module symbol.
  *
- * Returns: the crc of this kmod module symbol on success or NULL on
- * failure. The string is owned by the symbol, do not free it.
+ * Returns: the crc of this kmod module symbol if available, otherwise default to 0.
  */
 KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry)
 {
        struct kmod_module_symbol *symbol;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return 0;
 
        symbol = entry->data;
@@ -2105,12 +2742,8 @@ static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_sym
  */
 KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list)
 {
-       struct kmod_file *file;
        struct kmod_elf *elf;
-       const char *path;
-       const void *mem;
        struct kmod_modversion *symbols;
-       size_t size;
        int i, count, ret = 0;
 
        if (mod == NULL || list == NULL)
@@ -2118,28 +2751,13 @@ KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod
 
        assert(*list == NULL);
 
-       path = kmod_module_get_path(mod);
-       if (path == NULL)
-               return -ENOENT;
-
-       file = kmod_file_open(path);
-       if (file == NULL)
+       elf = kmod_module_get_elf(mod);
+       if (elf == NULL)
                return -errno;
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
-
-       elf = kmod_elf_new(mem, size);
-       if (elf == NULL) {
-               ret = -errno;
-               goto elf_open_error;
-       }
-
        count = kmod_elf_get_dependency_symbols(elf, &symbols);
-       if (count < 0) {
-               ret = count;
-               goto get_strings_error;
-       }
+       if (count < 0)
+               return count;
 
        for (i = 0; i < count; i++) {
                struct kmod_module_dependency_symbol *mv;
@@ -2170,11 +2788,6 @@ KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod
 
 list_error:
        free(symbols);
-get_strings_error:
-       kmod_elf_unref(elf);
-elf_open_error:
-       kmod_file_unref(file);
-
        return ret;
 }
 
@@ -2191,7 +2804,7 @@ KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct km
 {
        struct kmod_module_dependency_symbol *dependency_symbol;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return NULL;
 
        dependency_symbol = entry->data;
@@ -2204,14 +2817,13 @@ KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct km
  *
  * Get the crc of a kmod module dependency_symbol.
  *
- * Returns: the crc of this kmod module dependency_symbol on success or NULL on
- * failure. The string is owned by the dependency_symbol, do not free it.
+ * Returns: the crc of this kmod module dependency_symbol if available, otherwise default to 0.
  */
 KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry)
 {
        struct kmod_module_dependency_symbol *dependency_symbol;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return 0;
 
        dependency_symbol = entry->data;
@@ -2231,7 +2843,7 @@ KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *e
 {
        struct kmod_module_dependency_symbol *dependency_symbol;
 
-       if (entry == NULL)
+       if (entry == NULL || entry->data == NULL)
                return 0;
 
        dependency_symbol = entry->data;