]> git.ipfire.org Git - thirdparty/kmod.git/blobdiff - libkmod/libkmod.c
Use sorted configuration files in precedence order
[thirdparty/kmod.git] / libkmod / libkmod.c
index 3abdca987212ba4d52b4c14d713596ca1e184d13..b519b90345e4c1aeb76d10815148394ee96a97b7 100644 (file)
@@ -5,7 +5,8 @@
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation version 2.1.
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -116,7 +117,7 @@ const char *kmod_get_dirname(const struct kmod_ctx *ctx)
  * @ctx: kmod library context
  *
  * Retrieve stored data pointer from library context. This might be useful
- * to access from callbacks like a custom logging function.
+ * to access from callbacks.
  *
  * Returns: stored userdata
  */
@@ -187,12 +188,13 @@ static char *get_kernel_release(const char *dirname)
  * release the resources of the kmod library context.
  *
  * @dirname: what to consider as linux module's directory, if NULL
- *           defaults to /lib/modules/`uname -r`
+ *           defaults to /lib/modules/`uname -r`.
  * @config_paths: ordered array of paths (directories or files) where
- *               to load user-defined configuration parameters such as
- *               alias, blacklists, commands (install, remove). If
- *               NULL defaults to /run/modprobe.d, /etc/modprobe.d and
- *               /lib/modprobe.d.  This array must be null terminated.
+ *                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
+ *                /lib/modprobe.d. Give an empty vector if configuration should
+ *                not be read. This array must be null terminated.
  *
  * Returns: a new kmod library context
  */
@@ -267,7 +269,6 @@ 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.
- *
  */
 KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
 {
@@ -297,7 +298,6 @@ KMOD_EXPORT struct kmod_ctx *kmod_unref(struct kmod_ctx *ctx)
  * The built-in logging writes to stderr. It can be
  * overridden by a custom function, to plug log messages
  * into the user's logging functionality.
- *
  */
 KMOD_EXPORT void kmod_set_log_fn(struct kmod_ctx *ctx,
                                        void (*log_fn)(void *data,
@@ -342,33 +342,31 @@ KMOD_EXPORT void kmod_set_log_priority(struct kmod_ctx *ctx, int priority)
 }
 
 struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx,
-                                                       const char *name)
+                                                       const char *key)
 {
        struct kmod_module *mod;
 
-       mod = kmod_hash_find(ctx->modules_by_name, name);
+       mod = kmod_hash_find(ctx->modules_by_name, key);
 
-       DBG(ctx, "get module name='%s' found=%p\n", name, mod);
+       DBG(ctx, "get module name='%s' found=%p\n", key, mod);
 
        return mod;
 }
 
-void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod)
+void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod,
+                                                       const char *key)
 {
-       const char *name = kmod_module_get_name(mod);
-
-       DBG(ctx, "add %p name='%s'\n", mod, name);
+       DBG(ctx, "add %p key='%s'\n", mod, key);
 
-       kmod_hash_add(ctx->modules_by_name, name, mod);
+       kmod_hash_add(ctx->modules_by_name, key, mod);
 }
 
-void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod)
+void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod,
+                                                       const char *key)
 {
-       const char *name = kmod_module_get_name(mod);
+       DBG(ctx, "del %p key='%s'\n", mod, key);
 
-       DBG(ctx, "del %p name='%s'\n", mod, name);
-
-       kmod_hash_del(ctx->modules_by_name, name);
+       kmod_hash_del(ctx->modules_by_name, key);
 }
 
 static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
@@ -401,10 +399,10 @@ static int kmod_lookup_alias_from_alias_bin(struct kmod_ctx *ctx,
                index_file_close(idx);
        }
 
-       for (realname = realnames; realname; realname = realnames->next) {
+       for (realname = realnames; realname; realname = realname->next) {
                struct kmod_module *mod;
 
-               err = kmod_module_new_from_name(ctx, realname->value, &mod);
+               err = kmod_module_new_from_alias(ctx, name, realname->value, &mod);
                if (err < 0) {
                        ERR(ctx, "%s\n", strerror(-err));
                        goto fail;
@@ -516,7 +514,8 @@ int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name,
                if (fnmatch(aliasname, name, 0) == 0) {
                        struct kmod_module *mod;
 
-                       err = kmod_module_new_from_name(ctx, modname, &mod);
+                       err = kmod_module_new_from_alias(ctx, aliasname,
+                                                               modname, &mod);
                        if (err < 0) {
                                ERR(ctx, "%s\n", strerror(-err));
                                goto fail;
@@ -534,10 +533,87 @@ fail:
        return err;
 }
 
+int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name,
+                                               struct kmod_list **list)
+{
+       struct kmod_config *config = ctx->config;
+       struct kmod_list *l, *node;
+       int err, nmatch = 0;
+
+       kmod_list_foreach(l, config->install_commands) {
+               const char *modname = kmod_command_get_modname(l);
+
+               if (streq(modname, name)) {
+                       const char *cmd = kmod_command_get_command(l);
+                       struct kmod_module *mod;
+
+                       err = kmod_module_new_from_name(ctx, modname, &mod);
+                       if (err < 0) {
+                               ERR(ctx, "%s\n", strerror(-err));
+                               return err;
+                       }
+
+                       node = kmod_list_append(*list, mod);
+                       if (node == NULL) {
+                               ERR(ctx, "out of memory\n");
+                               return -ENOMEM;
+                       }
+
+                       *list = node;
+                       nmatch = 1;
+
+                       kmod_module_set_install_commands(mod, cmd);
+
+                       /*
+                        * match only the first one, like modprobe from
+                        * module-init-tools does
+                        */
+                       break;
+               }
+       }
+
+       if (nmatch)
+               return nmatch;
+
+       kmod_list_foreach(l, config->remove_commands) {
+               const char *modname = kmod_command_get_modname(l);
+
+               if (streq(modname, name)) {
+                       const char *cmd = kmod_command_get_command(l);
+                       struct kmod_module *mod;
+
+                       err = kmod_module_new_from_name(ctx, modname, &mod);
+                       if (err < 0) {
+                               ERR(ctx, "%s\n", strerror(-err));
+                               return err;
+                       }
+
+                       node = kmod_list_append(*list, mod);
+                       if (node == NULL) {
+                               ERR(ctx, "out of memory\n");
+                               return -ENOMEM;
+                       }
+
+                       *list = node;
+                       nmatch = 1;
+
+                       kmod_module_set_remove_commands(mod, cmd);
+
+                       /*
+                        * match only the first one, like modprobe from
+                        * module-init-tools does
+                        */
+                       break;
+               }
+       }
+
+       return nmatch;
+}
+
 /**
  * kmod_module_get_filtered_blacklist:
  * @ctx: kmod library context
- * @input: list to be filtered with blacklist
+ * @input: list of kmod_module to be filtered with blacklist
  * @output: where to save the new list
  *
  * Given a list @input, this function filter it out with config's blacklist
@@ -590,35 +666,41 @@ fail:
        return -ENOMEM;
 }
 
+/**
+ * kmod_load_resources:
+ * @ctx: kmod library context
+ *
+ * Load indexes and keep them open in @ctx. This way it's faster to lookup
+ * information within the indexes. If this function is not called before a
+ * search, the necessary index is always opened and closed.
+ *
+ * If user will do more than one or two lookups, insertions, deletions, most
+ * likely it's good to call this function first. Particularly in a daemon like
+ * udev that on bootup issues hundreds of calls to lookup the index, calling
+ * this function will speedup the searches.
+ *
+ * Returns: 0 on success or < 0 otherwise.
+ */
 KMOD_EXPORT int kmod_load_resources(struct kmod_ctx *ctx)
 {
-       char path[PATH_MAX];
        size_t i;
 
        if (ctx == NULL)
                return -ENOENT;
 
        for (i = 0; i < ARRAY_SIZE(index_files); i++) {
-               if (ctx->indexes[i] == NULL) {
-                       const char *fn = index_files[i];
-                       size_t fnlen = strlen(fn);
-                       const char *prefix = "";
-                       const char *suffix = "";
-
-                       if (fn[0] != '/')
-                               prefix = ctx->dirname;
-
-                       if (fnlen < 4 || !streq(fn + fnlen - 4, ".bin"))
-                               suffix = ".bin";
+               char path[PATH_MAX];
 
-                       snprintf(path, sizeof(path), "%s/%s%s",
-                                prefix, fn, suffix);
-                       fn = path;
-
-                       ctx->indexes[i] = index_mm_open(ctx, fn, true);
-                       if (ctx->indexes[i] == NULL)
-                               goto fail;
+               if (ctx->indexes[i] != NULL) {
+                       INFO(ctx, "Index %s already loaded\n", index_files[i]);
+                       continue;
                }
+
+               snprintf(path, sizeof(path), "%s/%s.bin", ctx->dirname,
+                                                       index_files[i]);
+               ctx->indexes[i] = index_mm_open(ctx, path, true);
+               if (ctx->indexes[i] == NULL)
+                       goto fail;
        }
 
        return 0;
@@ -628,6 +710,21 @@ fail:
        return -ENOMEM;
 }
 
+/**
+ * kmod_unload_resources:
+ * @ctx: kmod library context
+ *
+ * Unload all the indexes. This will free the resources to maintain the index
+ * open and all subsequent searches will need to open and close the index.
+ *
+ * User is free to call kmod_load_resources() and kmod_unload_resources() as
+ * many times as wanted during the lifecycle of @ctx. For example, if a daemon
+ * knows that when starting up it will lookup a lot of modules, it could call
+ * kmod_load_resources() and after the first burst of searches is gone, it
+ * could free the resources by calling kmod_unload_resources().
+ *
+ * Returns: 0 on success or < 0 otherwise.
+ */
 KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
 {
        size_t i;
@@ -643,95 +740,6 @@ KMOD_EXPORT void kmod_unload_resources(struct kmod_ctx *ctx)
        }
 }
 
-KMOD_EXPORT int kmod_resolve_alias_options(struct kmod_ctx *ctx,
-                                               const char *given_alias,
-                                               char **options)
-{
-       struct kmod_list *modules = NULL, *l;
-       char alias[NAME_MAX];
-       char *opts = NULL;
-       size_t optslen = 0;
-       int err;
-
-       if (ctx == NULL || options == NULL)
-               return -ENOENT;
-
-       modname_normalize(given_alias, alias, NULL);
-
-       err = kmod_module_new_from_lookup(ctx, alias, &modules);
-       if (err >= 0) {
-               kmod_list_foreach(l, modules) {
-                       const char *str = kmod_module_get_options(l->data);
-                       size_t len;
-                       void *tmp;
-
-                       if (str == NULL)
-                               continue;
-                       len = strlen(str);
-
-                       tmp = realloc(opts, optslen + len + 2);
-                       if (tmp == NULL)
-                               goto failed;
-                       opts = tmp;
-                       if (optslen > 0) {
-                               opts[optslen] = ' ';
-                               optslen++;
-                       }
-                       memcpy(opts + optslen, str, len);
-                       optslen += len;
-                       opts[optslen] = '\0';
-               }
-       }
-
-       kmod_list_foreach(l, ctx->config->options) {
-               const struct kmod_list *ml;
-               const char *modname = kmod_option_get_modname(l);
-               const char *str;
-               bool already_done = false;
-               size_t len;
-               void *tmp;
-
-               if (fnmatch(modname, alias, 0) != 0)
-                       continue;
-
-               kmod_list_foreach(ml, modules) {
-                       const char *mln = kmod_module_get_name(ml->data);
-                       if (fnmatch(modname, mln, 0) == 0) {
-                               already_done = true;
-                               break;
-                       }
-               }
-               if (already_done)
-                       continue;
-
-               str = kmod_option_get_options(l);
-               len = strlen(str);
-               tmp = realloc(opts, optslen + len + 2);
-               if (tmp == NULL)
-                       goto failed;
-               opts = tmp;
-               if (optslen > 0) {
-                       opts[optslen] = ' ';
-                       optslen++;
-               }
-               memcpy(opts + optslen, str, len);
-               optslen += len;
-               opts[optslen] = '\0';
-       }
-
-       DBG(ctx, "alias=%s  options='%s'\n", alias, opts);
-       kmod_module_unref_list(modules);
-       *options = opts;
-       return 0;
-
-failed:
-       kmod_module_unref_list(modules);
-       free(opts);
-       ERR(ctx, "out of memory\n");
-       *options = NULL;
-       return -ENOMEM;
-}
-
 const struct kmod_list *kmod_get_options(const struct kmod_ctx *ctx)
 {
        return ctx->config->options;
@@ -746,3 +754,8 @@ 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;
+}