From: Lucas De Marchi Date: Tue, 13 Dec 2011 16:12:50 +0000 (-0200) Subject: Add private function kmod_module_new_from_alias() X-Git-Tag: v1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6ad5f2636249bc1ceeab81325284686815aa7fbe;p=thirdparty%2Fkmod.git Add private function kmod_module_new_from_alias() 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). --- diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 0e71aea7..074e368e 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -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; } diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index a139792c..c22e805e 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -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)));