From 7636e72b1575d8544e470cb94654605013815a6d Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 1 Dec 2011 17:56:03 -0200 Subject: [PATCH] Add dependency list to kmod_module Dependency list is created from line as defined in modules.dep. --- libkmod/libkmod-module.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++- libkmod/libkmod-private.h | 3 +++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index f864e56..bd39154 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -17,6 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include #include @@ -44,6 +45,11 @@ struct kmod_module { int refcount; const char *path; const char *name; + struct kmod_list *dep; + + struct { + bool dep : 1; + } init; }; static char *path_to_modname(const char *path, bool alloc) @@ -75,6 +81,51 @@ static const char *get_modname(struct kmod_module *mod) return mod->name; } +int kmod_module_parse_dep(struct kmod_module *mod, char *line) +{ + struct kmod_ctx *ctx = mod->ctx; + struct kmod_list *list = NULL; + char *p, *saveptr; + int err, n = 0; + + assert(!mod->init.dep && mod->dep == NULL); + mod->init.dep = true; + + p = strchr(line, ':'); + if (p == NULL) + return 0; + + p++; + + for (p = strtok_r(p, " \t", &saveptr); p != NULL; + p = strtok_r(NULL, " \t", &saveptr)) { + const char *modname = path_to_modname(p, false); + struct kmod_module *mod; + + err = kmod_module_new_from_name(ctx, modname, &mod); + if (err < 0) { + ERR(ctx, "ctx=%p modname=%s error=%s\n", + ctx, modname, strerror(-err)); + goto fail; + } + + DBG(ctx, "add dep: %s\n", modname); + + list = kmod_list_append(list, mod); + n++; + } + + DBG(ctx, "%d dependencies for %s\n", n, mod->name); + + mod->dep = list; + return n; + +fail: + kmod_module_unref_list(list); + mod->init.dep = false; + return err; +} + KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, const char *name, struct kmod_module **mod) @@ -137,6 +188,7 @@ KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) DBG(mod->ctx, "kmod_module %p released\n", mod); + kmod_module_unref_list(mod->dep); kmod_unref(mod->ctx); free((char *) mod->path); free((char *) mod->name); @@ -166,7 +218,6 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx, const char *alias, struct kmod_list **list) { - int err; if (ctx == NULL || alias == NULL) diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 417a24a..1d32e8b 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -72,6 +72,9 @@ void kmod_free_config(struct kmod_ctx *ctx, struct kmod_config *config); const char *kmod_alias_get_name(const struct kmod_list *l); const char *kmod_alias_get_modname(const struct kmod_list *l); +/* libkmod-module.c */ +int kmod_module_parse_dep(struct kmod_module *mod, char *line); + /* util functions */ char *getline_wrapped(FILE *fp, unsigned int *linenum); char *underscores(struct kmod_ctx *ctx, char *s); -- 2.7.4