]> 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 d6d081092a62c3582f45abe7d75313b2d2155d38..889f26479a987ccb6c32e7a90a80080ae475beb7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libkmod - interface to kernel module operations
  *
- * Copyright (C) 2011-2012  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 <sys/wait.h>
-#include <string.h>
-#include <fnmatch.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:
  *
@@ -60,6 +70,7 @@ struct kmod_module {
        const char *install_commands;   /* owned by kmod_config */
        const char *remove_commands;    /* owned by kmod_config */
        char *alias; /* only set if this module was created from an alias */
+       struct kmod_file *file;
        int n_dep;
        int refcount;
        struct {
@@ -69,6 +80,13 @@ struct kmod_module {
                bool remove_commands : 1;
        } init;
 
+       /*
+        * 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
@@ -80,6 +98,13 @@ struct kmod_module {
         * 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,
@@ -98,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;
@@ -140,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,6 +216,26 @@ 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:
  *
@@ -223,10 +279,8 @@ static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
                keylen = namelen + aliaslen + 1;
 
        m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1));
-       if (m == NULL) {
-               free(m);
+       if (m == NULL)
                return -ENOMEM;
-       }
 
        memset(m, 0, sizeof(*m));
 
@@ -258,7 +312,7 @@ static int kmod_module_new(struct kmod_ctx *ctx, const char *key,
  *
  * 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.
@@ -382,8 +436,10 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
        }
 
        err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m);
-       if (err < 0)
+       if (err < 0) {
+               free(abspath);
                return err;
+       }
 
        m->path = abspath;
        *mod = m;
@@ -413,6 +469,10 @@ KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
 
        kmod_pool_del_module(mod->ctx, mod, mod->hashkey);
        kmod_module_unref_list(mod->dep);
+
+       if (mod->file)
+               kmod_file_unref(mod->file);
+
        kmod_unref(mod->ctx);
        free(mod->options);
        free(mod->path);
@@ -515,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;
@@ -533,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)
 {
@@ -550,8 +613,10 @@ KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list)
  * @input: list of kmod_module to be filtered with blacklist
  * @output: where to save the new list
  *
+ * 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
- * ans save it in @output.
+ * and save it in @output.
  *
  * Returns: 0 on success or < 0 otherwise. @output is saved with the updated
  * list.
@@ -560,49 +625,7 @@ KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx,
                                                const struct kmod_list *input,
                                                struct kmod_list **output)
 {
-       const struct kmod_list *li;
-       const struct kmod_list *blacklist;
-
-       if (ctx == NULL || output == NULL)
-               return -ENOENT;
-
-       *output = NULL;
-       if (input == NULL)
-               return 0;
-
-       blacklist = kmod_get_blacklists(ctx);
-       kmod_list_foreach(li, input) {
-               struct kmod_module *mod = li->data;
-               const struct kmod_list *lb;
-               struct kmod_list *node;
-               bool filtered = false;
-
-               kmod_list_foreach(lb, blacklist) {
-                       const char *name = lb->data;
-
-                       if (streq(name, mod->name)) {
-                               filtered = true;
-                               break;
-                       }
-               }
-
-               if (filtered)
-                       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;
+       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)
@@ -632,9 +655,8 @@ static const struct kmod_list *module_get_dependencies_noref(const struct kmod_m
  * The result is cached in @mod, so subsequent calls to this function will
  * return the already searched list of modules.
  *
- * 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: 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)
 {
@@ -743,7 +765,11 @@ 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
+ * @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.
  *
@@ -757,8 +783,9 @@ KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod,
        if (mod == NULL)
                return -ENOENT;
 
-       /* Filter out other flags */
-       flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT);
+       /* Filter out other flags and force ONONBLOCK */
+       flags &= KMOD_REMOVE_FORCE;
+       flags |= KMOD_REMOVE_NOWAIT;
 
        err = delete_module(mod->name, flags);
        if (err != 0) {
@@ -775,7 +802,9 @@ 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.
+ * 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,
@@ -791,8 +820,7 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
        int err;
        const void *mem;
        off_t size;
-       struct kmod_file *file;
-       struct kmod_elf *elf = NULL;
+       struct kmod_elf *elf;
        const char *path;
        const char *args = options ? options : "";
 
@@ -802,23 +830,35 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
        path = kmod_module_get_path(mod);
        if (path == NULL) {
                ERR(mod->ctx, "could not find module by name='%s'\n", mod->name);
-               return -ENOSYS;
+               return -ENOENT;
        }
 
-       file = kmod_file_open(mod->ctx, path);
-       if (file == NULL) {
-               err = -errno;
-               return err;
+       if (!mod->file) {
+               mod->file = kmod_file_open(mod->ctx, path);
+               if (mod->file == NULL) {
+                       err = -errno;
+                       return err;
+               }
        }
 
-       size = kmod_file_get_size(file);
-       mem = kmod_file_get_contents(file);
+       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_elf_new(mem, size);
+               elf = kmod_file_get_elf(mod->file);
                if (elf == NULL) {
                        err = -errno;
-                       goto elf_failed;
+                       return err;
                }
 
                if (flags & KMOD_INSERT_FORCE_MODVERSION) {
@@ -834,26 +874,25 @@ KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod,
                }
 
                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);
        }
-
-       if (elf != NULL)
-               kmod_elf_unref(elf);
-elf_failed:
-       kmod_file_unref(file);
-
        return err;
 }
 
 static bool module_is_blacklisted(struct kmod_module *mod)
 {
        struct kmod_ctx *ctx = mod->ctx;
-       const struct kmod_list *bl = kmod_get_blacklists(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) {
@@ -866,6 +905,63 @@ static bool module_is_blacklisted(struct kmod_module *mod)
        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)
 {
@@ -898,7 +994,8 @@ static int module_do_install_commands(struct kmod_module *mod,
                                        struct probe_insert_cb *cb)
 {
        const char *command = kmod_module_get_install_commands(mod);
-       char *p, *cmd;
+       char *p;
+       _cleanup_free_ char *cmd;
        int err;
        size_t cmdlen, options_len, varlen;
 
@@ -921,10 +1018,9 @@ static int module_do_install_commands(struct kmod_module *mod,
                size_t slen = cmdlen - varlen + options_len;
                char *suffix = p + varlen;
                char *s = malloc(slen + 1);
-               if (s == NULL) {
-                       free(cmd);
+               if (!s)
                        return -ENOMEM;
-               }
+
                memcpy(s, cmd, p - cmd);
                memcpy(s + prefixlen, options, options_len);
                memcpy(s + prefixlen + options_len, suffix, suffixlen);
@@ -940,8 +1036,6 @@ static int module_do_install_commands(struct kmod_module *mod,
        else
                err = command_do(mod, "install", cmd);
 
-       free(cmd);
-
        return err;
 }
 
@@ -972,6 +1066,7 @@ static char *module_options_concat(const char *opt, const char *xopt)
 }
 
 static int __kmod_module_get_probe_list(struct kmod_module *mod,
+                                               bool required,
                                                bool ignorecmd,
                                                struct kmod_list **list);
 
@@ -984,13 +1079,14 @@ static int __kmod_module_fill_softdep(struct kmod_module *mod,
 
        err = kmod_module_get_softdeps(mod, &pre, &post);
        if (err < 0) {
-               ERR(mod->ctx, "could not get softdep: %s", strerror(-err));
+               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, list);
+               err = __kmod_module_get_probe_list(m, false, false, list);
                if (err < 0)
                        goto fail;
        }
@@ -1002,12 +1098,11 @@ static int __kmod_module_fill_softdep(struct kmod_module *mod,
                goto fail;
        }
        *list = l;
-       mod->visited = true;
        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, list);
+               err = __kmod_module_get_probe_list(m, false, false, list);
                if (err < 0)
                        goto fail;
        }
@@ -1021,6 +1116,7 @@ fail:
 
 /* re-entrant */
 static int __kmod_module_get_probe_list(struct kmod_module *mod,
+                                               bool required,
                                                bool ignorecmd,
                                                struct kmod_list **list)
 {
@@ -1032,8 +1128,22 @@ static int __kmod_module_get_probe_list(struct kmod_module *mod,
                                                                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);
@@ -1071,8 +1181,9 @@ static int kmod_module_get_probe_list(struct kmod_module *mod,
         * 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 = __kmod_module_get_probe_list(mod, ignorecmd, list);
+       err = __kmod_module_get_probe_list(mod, true, ignorecmd, list);
        if (err < 0) {
                kmod_module_unref_list(*list);
                *list = NULL;
@@ -1085,7 +1196,25 @@ static int kmod_module_get_probe_list(struct kmod_module *mod,
  * 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.
+ * 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.
@@ -1097,11 +1226,10 @@ static int kmod_module_get_probe_list(struct kmod_module *mod,
  * Insert a module in Linux kernel resolving dependencies, soft dependencies,
  * install commands and applying blacklist.
  *
- * If @run_install is NULL, and the flag KMOD_PROBE_STOP_ON_COMMANDS is not
- * given, 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.
+ * 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.
@@ -1122,9 +1250,23 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
        if (mod == NULL)
                return -ENOENT;
 
-       err = flags & (KMOD_PROBE_APPLY_BLACKLIST |
-                                       KMOD_PROBE_APPLY_BLACKLIST_ALL);
-       if (err != 0) {
+       if (!(flags & KMOD_PROBE_IGNORE_LOADED)
+                                       && module_is_inkernel(mod)) {
+               if (flags & KMOD_PROBE_FAIL_ON_LOADED)
+                       return -EEXIST;
+               else
+                       return 0;
+       }
+
+       /*
+        * 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;
        }
@@ -1137,8 +1279,8 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
        if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) {
                struct kmod_list *filtered = NULL;
 
-               err = kmod_module_get_filtered_blacklist(mod->ctx,
-                                                       list, &filtered);
+               err = kmod_module_apply_filter(mod->ctx,
+                               KMOD_FILTER_BLACKLIST, list, &filtered);
                if (err < 0)
                        return err;
 
@@ -1156,18 +1298,20 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
                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 = module_options_concat(moptions,
+               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;
+               }
+
+               options = module_options_concat(moptions,
                                        m == mod ? extra_options : NULL);
 
                if (cmd != NULL && !m->ignorecmd) {
-                       if (flags & KMOD_PROBE_STOP_ON_COMMAND) {
-                               DBG(mod->ctx, "Stopping on '%s': "
-                                       "install command\n", m->name);
-                               err = KMOD_PROBE_STOP_ON_COMMAND;
-                               free(options);
-                               break;
-                       }
-
                        if (print_action != NULL)
                                print_action(m, true, options ?: "");
 
@@ -1175,25 +1319,6 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
                                err = module_do_install_commands(m, options,
                                                                        &cb);
                } else {
-                       int state;
-
-                       if (flags & KMOD_PROBE_IGNORE_LOADED)
-                               state = -1;
-                       else
-                               state = kmod_module_get_initstate(m);
-
-                       if (state == KMOD_MODULE_LIVE ||
-                                       state == KMOD_MODULE_COMING ||
-                                       state == KMOD_MODULE_BUILTIN) {
-                               if (m == mod && (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) {
-                                       err = KMOD_PROBE_STOP_ON_ALREADY_LOADED;
-                                       break;
-                               }
-
-                               DBG(mod->ctx, "Ignoring module '%s': "
-                                               "already loaded\n", m->name);
-                               err = 0;
-                       }
                        if (print_action != NULL)
                                print_action(m, false, options ?: "");
 
@@ -1204,10 +1329,11 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
 
                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, bail out. Otherwise, just ignore and continue.
+                * 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
@@ -1215,12 +1341,16 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
                 * insert it.
                 */
                if (err == -EEXIST && m == mod &&
-                               (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) {
-                       err = KMOD_PROBE_STOP_ON_ALREADY_LOADED;
+                               (flags & KMOD_PROBE_FAIL_ON_LOADED))
                        break;
-               }
 
-               if (err < 0 && err != -EEXIST)
+               /*
+                * Ignore errors from softdeps
+                */
+               if (err == -EEXIST || !m->required)
+                       err = 0;
+
+               else if (err < 0)
                        break;
        }
 
@@ -1247,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;
@@ -1321,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)
@@ -1393,7 +1525,8 @@ 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, *ctx_softdeps;
+       const struct kmod_list *l;
+       const struct kmod_config *config;
 
        if (mod == NULL || pre == NULL || post == NULL)
                return -ENOENT;
@@ -1401,9 +1534,9 @@ KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod,
        assert(*pre == NULL);
        assert(*post == NULL);
 
-       ctx_softdeps = kmod_get_softdeps(mod->ctx);
+       config = kmod_get_config(mod->ctx);
 
-       kmod_list_foreach(l, ctx_softdeps) {
+       kmod_list_foreach(l, config->softdeps) {
                const char *modname = kmod_softdep_get_name(l);
                const char * const *array;
                unsigned count;
@@ -1448,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)
@@ -1496,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
@@ -1528,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);
@@ -1544,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);
@@ -1563,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;
+       }
 }
 
 /**
@@ -1584,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)
 {
@@ -1594,6 +1736,10 @@ 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);
@@ -1607,7 +1753,7 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod)
                        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",
@@ -1638,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) {
@@ -1686,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;
 }
 
@@ -1698,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)
 {
@@ -1713,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;
        }
@@ -1742,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);
@@ -1756,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;
                }
 
@@ -1831,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;
 
@@ -1848,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;
                }
 
@@ -1880,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) {
@@ -1893,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) {
@@ -1972,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[];
@@ -1986,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);
@@ -1999,6 +2177,79 @@ 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
@@ -2009,7 +2260,9 @@ static void kmod_module_info_free(struct kmod_module_info *info)
  *
  * 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().
  *
@@ -2017,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(mod->ctx, 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;
@@ -2064,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;
 }
 
@@ -2195,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)
@@ -2208,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(mod->ctx, 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;
@@ -2258,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.
@@ -2279,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;
@@ -2292,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;
@@ -2360,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)
@@ -2373,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(mod->ctx, 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;
@@ -2423,11 +2643,6 @@ 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;
 }
 
@@ -2444,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;
@@ -2457,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;
@@ -2528,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)
@@ -2541,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(mod->ctx, 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;
@@ -2593,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;
 }
 
@@ -2614,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;
@@ -2627,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;
@@ -2654,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;