Lookup for commands in kmod_module_new_from_lookup()
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Fri, 16 Dec 2011 05:57:12 +0000 (03:57 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Sat, 17 Dec 2011 21:41:35 +0000 (19:41 -0200)
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"'

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

diff --git a/TODO b/TODO
index 5cead32..76ce804 100644 (file)
--- 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
index e901b76..dd340ee 100644 (file)
@@ -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
index 50f3b66..c992129 100644 (file)
@@ -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;
index 7e14239..c87e95c 100644 (file)
@@ -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