]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add private function kmod_module_new_from_alias()
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 13 Dec 2011 16:12:50 +0000 (14:12 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Tue, 13 Dec 2011 16:12:50 +0000 (14:12 -0200)
This function will create a new kmod_module() by calling
kmod_module_new_from_name(), but instead of given the module name, it
gives the alias. This way, the modules' hash will contain the alias
instead of the name. If module was created successfully, then it swap
the alias with the actual name.

The downside is that the structure is not shared anymore if we create a
kmod_module by alias and after by name. However, in modprobe's
configuration it's possible to have different options for different
aliases. In future we might want to create a way to share the common
part of the structure (then having only one instance as we had before).

libkmod/libkmod-module.c
libkmod/libkmod-private.h

index 0e71aea7424de220ef872e813864acfbece13e1e..074e368e83e9a0c283f91cdaecacb16096ba9336 100644 (file)
@@ -51,6 +51,7 @@ struct kmod_module {
        char *options;
        char *install_commands;
        char *remove_commands;
+       char *alias; /* only set if this module was created from an alias */
        int n_dep;
        int refcount;
        struct {
@@ -199,7 +200,7 @@ KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
        if (ctx == NULL || name == NULL)
                return -ENOENT;
 
-       modname_normalize(name, name_norm, &namelen);
+       alias_normalize(name, name_norm, &namelen);
 
        m = kmod_pool_get_module(ctx, name_norm);
        if (m != NULL) {
@@ -225,6 +226,35 @@ KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx,
        return 0;
 }
 
+int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias,
+                               const char *name, struct kmod_module **mod)
+{
+       int err;
+       struct kmod_module *m;
+
+       err = kmod_module_new_from_name(ctx, alias, mod);
+       if (err < 0)
+               return err;
+
+       m = *mod;
+
+       /* if module did not came from pool */
+       if (m->alias == NULL) {
+               m->alias = m->name;
+               m->name = strdup(name);
+               if (m->name == NULL)
+                       goto fail_oom;
+       }
+
+       return 0;
+
+fail_oom:
+       ERR(ctx, "out of memory\n");
+       kmod_module_unref(m);
+       *mod = NULL;
+       return err;
+}
+
 KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx,
                                                const char *path,
                                                struct kmod_module **mod)
@@ -304,6 +334,8 @@ KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod)
        free(mod->install_commands);
        free(mod->remove_commands);
        free(mod->path);
+       if (mod->alias != NULL)
+               free(mod->name);
        free(mod);
        return NULL;
 }
index a139792c95e6e1e7dfd9bb91104f2ee4f689d3c5..c22e805e7f77988a0e8493977c28064b7000694e 100644 (file)
@@ -104,6 +104,7 @@ const char *kmod_command_get_modname(const struct kmod_list *l) __attribute__((n
 
 
 /* libkmod-module.c */
+int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, const char *name, struct kmod_module **mod);
 char *modname_normalize(const char *modname, char buf[NAME_MAX], size_t *len)  __attribute__((nonnull(1, 2)));
 int kmod_module_parse_depline(struct kmod_module *mod, char *line) __attribute__((nonnull(1, 2)));