From: Lucas De Marchi Date: Fri, 16 Dec 2011 05:57:12 +0000 (-0200) Subject: Lookup for commands in kmod_module_new_from_lookup() X-Git-Tag: v2~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4fc55236815514fe44cc9258a1b42af1383f5a1;p=thirdparty%2Fkmod.git Lookup for commands in kmod_module_new_from_lookup() Install and remove commands are now properly treated on lookup. Example config file: $ ./test/test-lookup installme libkmod version 1 Alias: 'installme' Modules matching: installme install commands: 'echo "this is a install message"' $ ./test/test-lookup removeme libkmod version 1 Alias: 'removeme' Modules matching: removeme remove commands: 'echo "this is a remove message"' --- diff --git a/TODO b/TODO index 5cead320..76ce8049 100644 --- a/TODO +++ b/TODO @@ -13,10 +13,6 @@ Features: * provide ELF manipulation to implement modinfo -* Add lookup for install commands in kmod_module_new_from_lookup() - -* Add lookup for remove commands - * Add functions to dump configuration * Add functions list all modules known by modules.dep diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index e901b767..dd340eee 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -482,7 +482,8 @@ KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx, err = kmod_lookup_alias_from_symbols_file(ctx, alias, list); CHECK_ERR_AND_FINISH(err, fail, list, finish); -// TODO: add lookup for install commands here. + err = kmod_lookup_alias_from_commands(ctx, alias, list); + CHECK_ERR_AND_FINISH(err, fail, list, finish); err = kmod_lookup_alias_from_aliases_file(ctx, alias, list); CHECK_ERR_AND_FINISH(err, fail, list, finish); @@ -849,6 +850,12 @@ KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_modul return mod->install_commands; } +void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) +{ + mod->init.install_commands = true; + mod->install_commands = cmd; +} + /** * kmod_module_get_remove_commands: * @mod: kmod module @@ -896,6 +903,12 @@ KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module return mod->remove_commands; } +void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) +{ + mod->init.remove_commands = true; + mod->remove_commands = cmd; +} + /** * SECTION:libkmod-loaded * @short_description: currently loaded modules diff --git a/libkmod/libkmod-private.h b/libkmod/libkmod-private.h index 50f3b66f..c9921298 100644 --- a/libkmod/libkmod-private.h +++ b/libkmod/libkmod-private.h @@ -74,6 +74,7 @@ int kmod_lookup_alias_from_config(struct kmod_ctx *ctx, const char *name, struct int kmod_lookup_alias_from_symbols_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list) __attribute__((nonnull(1, 2, 3))); int kmod_lookup_alias_from_aliases_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list) __attribute__((nonnull(1, 2, 3))); int kmod_lookup_alias_from_moddep_file(struct kmod_ctx *ctx, const char *name, struct kmod_list **list) __attribute__((nonnull(1, 2, 3))); +int kmod_lookup_alias_from_commands(struct kmod_ctx *ctx, const char *name, struct kmod_list **list) __attribute__((nonnull(1, 2, 3))); char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name) __attribute__((nonnull(1,2))); @@ -109,6 +110,8 @@ const char *kmod_command_get_modname(const struct kmod_list *l) __attribute__((n 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))); +void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) __attribute__((nonnull(1))); +void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) __attribute__((nonnull(1))); /* libkmod-hash.c */ struct kmod_hash; diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 7e14239d..c87e95c0 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -533,6 +533,83 @@ 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