]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - libkmod/libkmod.c
libkmod: Fix documentation on config precedence order
[thirdparty/kmod.git] / libkmod / libkmod.c
index 4990d3e5faf7d57f6a275271caf5fe5acce89f82..25655b95492d2ec8c82541015c7e8a4a02abdae2 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 <stdio.h>
-#include <stdlib.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <limits.h>
-#include <unistd.h>
+#include <assert.h>
+#include <ctype.h>
 #include <errno.h>
 #include <fnmatch.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
-#include <sys/utsname.h>
+#include <unistd.h>
 #include <sys/stat.h>
+#include <sys/utsname.h>
+
+#include <shared/hash.h>
+#include <shared/util.h>
 
 #include "libkmod.h"
-#include "libkmod-private.h"
+#include "libkmod-internal.h"
 #include "libkmod-index.h"
 
 #define KMOD_HASH_SIZE (256)
 #define KMOD_LRU_MAX (128)
-#define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_SYMBOL + 1
+#define _KMOD_INDEX_MODULES_SIZE KMOD_INDEX_MODULES_BUILTIN + 1
 
 /**
  * SECTION:libkmod
@@ -54,12 +57,14 @@ static struct _index_files {
        [KMOD_INDEX_MODULES_DEP] = { .fn = "modules.dep", .prefix = "" },
        [KMOD_INDEX_MODULES_ALIAS] = { .fn = "modules.alias", .prefix = "alias " },
        [KMOD_INDEX_MODULES_SYMBOL] = { .fn = "modules.symbols", .prefix = "alias "},
+       [KMOD_INDEX_MODULES_BUILTIN_ALIAS] = { .fn = "modules.builtin.alias", .prefix = "" },
+       [KMOD_INDEX_MODULES_BUILTIN] = { .fn = "modules.builtin", .prefix = ""},
 };
 
 static const char *default_config_paths[] = {
-       "/run/modprobe.d",
        SYSCONFDIR "/modprobe.d",
-       ROOTPREFIX "/lib/modprobe.d",
+       "/run/modprobe.d",
+       "/lib/modprobe.d",
        NULL
 };
 
@@ -97,6 +102,7 @@ void kmod_log(const struct kmod_ctx *ctx,
        va_end(args);
 }
 
+_printf_format_(6, 0)
 static void log_filep(void *data,
                        int priority, const char *file, int line,
                        const char *fn, const char *format, va_list args)
@@ -141,7 +147,15 @@ static void log_filep(void *data,
        vfprintf(fp, format, args);
 }
 
-const char *kmod_get_dirname(const struct kmod_ctx *ctx)
+
+/**
+ * kmod_get_dirname:
+ * @ctx: kmod library context
+ *
+ * Retrieve the absolute path used for linux modules in this context. The path
+ * is computed from the arguments to kmod_new().
+ */
+KMOD_EXPORT const char *kmod_get_dirname(const struct kmod_ctx *ctx)
 {
        return ctx->dirname;
 }
@@ -193,7 +207,7 @@ static int log_priority(const char *priority)
        return 0;
 }
 
-static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules";
+static const char *dirname_default_prefix = "/lib/modules";
 
 static char *get_kernel_release(const char *dirname)
 {
@@ -215,16 +229,15 @@ static char *get_kernel_release(const char *dirname)
 /**
  * kmod_new:
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to $rootprefix/lib/modules/`uname -r`. If it's relative,
- *           it's treated as relative to current the current working
- *           directory. Otherwise, give an absolute dirname.
+ *           defaults to /lib/modules/`uname -r`. If it's relative,
+ *           it's treated as relative to the current working directory.
+ *           Otherwise, give an absolute dirname.
  * @config_paths: ordered array of paths (directories or files) where
  *                to load from user-defined configuration parameters such as
  *                alias, blacklists, commands (install, remove). If
- *                NULL defaults to /run/modprobe.d, /etc/modprobe.d and
- *                $rootprefix/lib/modprobe.d. Give an empty vector if
- *                configuration should not be read. This array must be null
- *                terminated.
+ *                NULL defaults to /etc/modprobe.d, /run/modprobe.d and
+ *                /lib/modprobe.d. Give an empty vector if configuration should
+ *                not be read. This array must be null terminated.
  *
  * Create kmod library context. This reads the kmod configuration
  * and fills in the default values.
@@ -253,7 +266,7 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname,
        ctx->dirname = get_kernel_release(dirname);
 
        /* environment overwrites config */
-       env = getenv("KMOD_LOG");
+       env = secure_getenv("KMOD_LOG");
        if (env != NULL)
                kmod_set_log_priority(ctx, log_priority(env));
 
@@ -305,6 +318,8 @@ KMOD_EXPORT struct kmod_ctx *kmod_ref(struct kmod_ctx *ctx)
  *
  * Drop a reference of the kmod library context. If the refcount
  * reaches zero, the resources of the context will be released.
+ *
+ * Returns: the passed kmod library context or NULL if it's freed
  */
 KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
 {
@@ -455,6 +470,7 @@ static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
 
 fail:
        *list = kmod_list_remove_n_latest(*list, nmatch);
+       index_values_free(realnames);
        return err;
 
 }
@@ -476,6 +492,99 @@ int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name,
                                                                name, list);
 }
 
+static char *lookup_builtin_file(struct kmod_ctx *ctx, const char *name)
+{
+       char *line;
+
+       if (ctx->indexes[KMOD_INDEX_MODULES_BUILTIN]) {
+               DBG(ctx, "use mmaped index '%s' modname=%s\n",
+                               index_files[KMOD_INDEX_MODULES_BUILTIN].fn,
+                               name);
+               line = index_mm_search(ctx->indexes[KMOD_INDEX_MODULES_BUILTIN],
+                                                                       name);
+       } else {
+               struct index_file *idx;
+               char fn[PATH_MAX];
+
+               snprintf(fn, sizeof(fn), "%s/%s.bin", ctx->dirname,
+                               index_files[KMOD_INDEX_MODULES_BUILTIN].fn);
+               DBG(ctx, "file=%s modname=%s\n", fn, name);
+
+               idx = index_file_open(fn);
+               if (idx == NULL) {
+                       DBG(ctx, "could not open builtin file '%s'\n", fn);
+                       return NULL;
+               }
+
+               line = index_search(idx, name);
+               index_file_close(idx);
+       }
+
+       return line;
+}
+
+int kmod_lookup_alias_from_kernel_builtin_file(struct kmod_ctx *ctx,
+                                               const char *name,
+                                               struct kmod_list **list)
+{
+       struct kmod_list *l;
+       int ret;
+
+       assert(*list == NULL);
+
+       ret = kmod_lookup_alias_from_alias_bin(ctx,
+                                              KMOD_INDEX_MODULES_BUILTIN_ALIAS,
+                                              name, list);
+
+       kmod_list_foreach(l, *list) {
+               struct kmod_module *mod = l->data;
+               kmod_module_set_builtin(mod, true);
+       }
+
+       return ret;
+}
+
+int kmod_lookup_alias_from_builtin_file(struct kmod_ctx *ctx, const char *name,
+                                               struct kmod_list **list)
+{
+       char *line;
+       int err = 0;
+
+       assert(*list == NULL);
+
+       line = lookup_builtin_file(ctx, name);
+       if (line != NULL) {
+               struct kmod_module *mod;
+
+               err = kmod_module_new_from_name(ctx, name, &mod);
+               if (err < 0) {
+                       ERR(ctx, "Could not create module from name %s: %s\n",
+                                                       name, strerror(-err));
+                       goto finish;
+               }
+
+               /* already mark it as builtin since it's being created from
+                * this index */
+               kmod_module_set_builtin(mod, true);
+               *list = kmod_list_append(*list, mod);
+               if (*list == NULL)
+                       err = -ENOMEM;
+       }
+
+finish:
+       free(line);
+       return err;
+}
+
+bool kmod_lookup_alias_is_builtin(struct kmod_ctx *ctx, const char *name)
+{
+       _cleanup_free_ char *line;
+
+       line = lookup_builtin_file(ctx, name);
+
+       return line != NULL;
+}
+
 char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
 {
        struct index_file *idx;
@@ -496,7 +605,7 @@ char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name)
 
        idx = index_file_open(fn);
        if (idx == NULL) {
-               ERR(ctx, "could not open moddep file '%s'\n", fn);
+               DBG(ctx, "could not open moddep file '%s'\n", fn);
                return NULL;
        }
 
@@ -663,6 +772,16 @@ void kmod_set_modules_visited(struct kmod_ctx *ctx, bool visited)
                kmod_module_set_visited((struct kmod_module *)v, visited);
 }
 
+void kmod_set_modules_required(struct kmod_ctx *ctx, bool required)
+{
+       struct hash_iter iter;
+       const void *v;
+
+       hash_iter_init(ctx->modules_by_name, &iter);
+       while (hash_iter_next(&iter, NULL, &v))
+               kmod_module_set_required((struct kmod_module *)v, required);
+}
+
 static bool is_cache_invalid(const char *path, unsigned long long stamp)
 {
        struct stat st;
@@ -736,6 +855,7 @@ KMOD_EXPORT int kmod_validate_resources(struct kmod_ctx *ctx)
  */
 KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
 {
+       int ret = 0;
        size_t i;
 
        if (ctx == NULL)
@@ -752,17 +872,25 @@ KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
 
                snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
                                                        index_files[i].fn);
-               ctx->indexes[i] = index_mm_open(ctx, path, true,
-                                               &ctx->indexes_stamp[i]);
-               if (ctx->indexes[i] == NULL)
-                       goto fail;
+               ret = index_mm_open(ctx, path, &ctx->indexes_stamp[i],
+                                   &ctx->indexes[i]);
+
+               /*
+                * modules.builtin.alias are considered optional since it's
+                * recently added and older installations may not have it;
+                * we allow failing for any reason
+                */
+               if (ret) {
+                       if (i != KMOD_INDEX_MODULES_BUILTIN_ALIAS)
+                               break;
+                       ret = 0;
+               }
        }
 
-       return 0;
+       if (ret)
+               kmod_unload_resources(ctx);
 
-fail:
-       kmod_unload_resources(ctx);
-       return -ENOMEM;
+       return ret;
 }
 
 /**
@@ -799,7 +927,11 @@ KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
 /**
  * kmod_dump_index:
  * @ctx: kmod library context
- * @type: index to dump
+ * @type: index to dump, valid indexes are
+ * KMOD_INDEX_MODULES_DEP: index of module dependencies;
+ * KMOD_INDEX_MODULES_ALIAS: index of module aliases;
+ * KMOD_INDEX_MODULES_SYMBOL: index of symbol aliases;
+ * KMOD_INDEX_MODULES_BUILTIN: index of builtin module.
  * @fd: file descriptor to dump index to
  *
  * Dump index to file descriptor. Note that this function doesn't use stdio.h
@@ -841,32 +973,7 @@ KMOD_EXPORT int kmod_dump_index(struct kmod_ctx *ctx, enum kmod_index type,
        return 0;
 }
 
-const struct kmod_list *kmod_get_blacklists(const struct kmod_ctx *ctx)
-{
-       return ctx->config->blacklists;
-}
-
-const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx)
-{
-       return ctx->config->options;
-}
-
-const struct kmod_list *kmod_get_install_commands(const struct kmod_ctx *ctx)
-{
-       return ctx->config->install_commands;
-}
-
-const struct kmod_list *kmod_get_remove_commands(const struct kmod_ctx *ctx)
-{
-       return ctx->config->remove_commands;
-}
-
-const struct kmod_list *kmod_get_softdeps(const struct kmod_ctx *ctx)
-{
-       return ctx->config->softdeps;
-}
-
-const struct kmod_list *kmod_get_aliases(const struct kmod_ctx *ctx)
+const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx)
 {
-       return ctx->config->aliases;
+       return ctx->config;
 }