From: Lucas De Marchi Date: Fri, 16 Dec 2011 05:33:26 +0000 (-0200) Subject: kmod_module: do not find more than the first command X-Git-Tag: v2~57 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60f6760e73cac29c04c3568109dd2df92758eda7;p=thirdparty%2Fkmod.git kmod_module: do not find more than the first command modprobe from module-init-tools does not use more than one install/remove command, it just stops on the first one. Test modprobe.conf: install bla echo "this is a message" install bla echo "this is a message" $ modprobe bla this is a message $ Install and remove commands are already a legacy thing we need to carry, but let's not extend it so people do not start doing crazy things. With this patch we are breaking on the first element we find in the configuration. May be we can add a warning later when parsing the config that install commands are duplicated. --- diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 80da684b..e901b767 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -50,8 +50,8 @@ struct kmod_module { char *path; struct kmod_list *dep; char *options; - char *install_commands; - char *remove_commands; + const char *install_commands; /* owned by kmod_config */ + const char *remove_commands; /* owned by kmod_config */ char *alias; /* only set if this module was created from an alias */ int n_dep; int refcount; @@ -396,8 +396,6 @@ KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) kmod_module_unref_list(mod->dep); kmod_unref(mod->ctx); free(mod->options); - free(mod->install_commands); - free(mod->remove_commands); free(mod->path); free(mod); return NULL; @@ -827,52 +825,28 @@ KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_modul /* lazy init */ struct kmod_module *m = (struct kmod_module *)mod; const struct kmod_list *l, *ctx_install_commands; - char *cmds = NULL; - size_t cmdslen = 0; ctx_install_commands = kmod_get_install_commands(mod->ctx); kmod_list_foreach(l, ctx_install_commands) { const char *modname = kmod_command_get_modname(l); - const char *str; - size_t len; - void *tmp; if (strcmp(modname, mod->name) != 0) continue; - str = kmod_command_get_command(l); - len = strlen(str); - if (len < 1) - continue; - - tmp = realloc(cmds, cmdslen + len + 2); - if (tmp == NULL) { - free(cmds); - goto failed; - } - - cmds = tmp; + m->install_commands = kmod_command_get_command(l); - if (cmdslen > 0) { - cmds[cmdslen] = ';'; - cmdslen++; - } - - memcpy(cmds + cmdslen, str, len); - cmdslen += len; - cmds[cmdslen] = '\0'; + /* + * find only the first command, as modprobe from + * module-init-tools does + */ + break; } m->init.install_commands = true; - m->install_commands = cmds; } return mod->install_commands; - -failed: - ERR(mod->ctx, "out of memory\n"); - return NULL; } /** @@ -898,52 +872,28 @@ KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module /* lazy init */ struct kmod_module *m = (struct kmod_module *)mod; const struct kmod_list *l, *ctx_remove_commands; - char *cmds = NULL; - size_t cmdslen = 0; ctx_remove_commands = kmod_get_remove_commands(mod->ctx); kmod_list_foreach(l, ctx_remove_commands) { const char *modname = kmod_command_get_modname(l); - const char *str; - size_t len; - void *tmp; if (strcmp(modname, mod->name) != 0) continue; - str = kmod_command_get_command(l); - len = strlen(str); - if (len < 1) - continue; - - tmp = realloc(cmds, cmdslen + len + 2); - if (tmp == NULL) { - free(cmds); - goto failed; - } - - cmds = tmp; + m->remove_commands = kmod_command_get_command(l); - if (cmdslen > 0) { - cmds[cmdslen] = ';'; - cmdslen++; - } - - memcpy(cmds + cmdslen, str, len); - cmdslen += len; - cmds[cmdslen] = '\0'; + /* + * find only the first command, as modprobe from + * module-init-tools does + */ + break; } m->init.remove_commands = true; - m->remove_commands = cmds; } return mod->remove_commands; - -failed: - ERR(mod->ctx, "out of memory\n"); - return NULL; } /**