From 9bf60d21f3460cc4deb14839563c320d4bbcf6fa Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Mon, 19 Dec 2011 02:18:14 -0200 Subject: [PATCH] kmod_modprobe: fix handling of remove commands The check for remove/install commands must be before the ignore_loaded check because we will actually run something instead of removing/inserting a module and the modname might not correspond to a real module. Otherwise a fake module like "remove removeme echo 'bla'" would not work. This also keeps compatibility with modprobe. --- tools/kmod-modprobe.c | 89 ++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/tools/kmod-modprobe.c b/tools/kmod-modprobe.c index 2ea8d08..c14baa5 100644 --- a/tools/kmod-modprobe.c +++ b/tools/kmod-modprobe.c @@ -417,27 +417,6 @@ static int rmmod_do(struct kmod_module *mod) struct kmod_list *pre = NULL, *post = NULL; int err; - if (!ignore_loaded) { - int state = kmod_module_get_initstate(mod); - if (state == KMOD_MODULE_BUILTIN) { - LOG("Module %s is builtin.\n", modname); - return -ENOENT; - } else if (state != KMOD_MODULE_LIVE) { - if (first_time) { - LOG("Module %s is not in kernel.\n", modname); - return -ENOENT; - } else - return 0; - } - } - - /* not in original modprobe -r, but helpful */ - if (remove_dependencies) { - err = rmmod_do_dependencies(mod); - if (err < 0) - return err; - } - if (!ignore_commands) { const char *cmd; @@ -463,6 +442,28 @@ static int rmmod_do(struct kmod_module *mod) } if (!ignore_loaded) { + int state = kmod_module_get_initstate(mod); + + if (state == KMOD_MODULE_BUILTIN) { + LOG("Module %s is builtin.\n", modname); + return -ENOENT; + } else if (state != KMOD_MODULE_LIVE) { + if (first_time) { + LOG("Module %s is not in kernel.\n", modname); + return -ENOENT; + } else + return 0; + } + } + + /* not in original modprobe -r, but helpful */ + if (remove_dependencies) { + err = rmmod_do_dependencies(mod); + if (err < 0) + return err; + } + + if (!ignore_loaded) { int usage = kmod_module_get_refcnt(mod); if (usage > 0) { LOG("Module %s is in use.\n", modname); @@ -698,29 +699,6 @@ static int insmod_do(struct kmod_module *mod, const char *extra_opts) char *opts = NULL; int err; - if (!ignore_loaded) { - int state = kmod_module_get_initstate(mod); - - if (state == KMOD_MODULE_BUILTIN) { - if (first_time) { - LOG("Module %s already in kernel (builtin).\n", - modname); - return -EEXIST; - } - return 0; - } else if (state == KMOD_MODULE_LIVE) { - if (first_time) { - LOG("Module %s already in kernel.\n", modname); - return -EEXIST; - } - return 0; - } - } - - err = insmod_do_dependencies(mod); - if (err < 0) - return err; - if (!ignore_commands) { const char *cmd; @@ -745,6 +723,29 @@ static int insmod_do(struct kmod_module *mod, const char *extra_opts) } } + if (!ignore_loaded) { + int state = kmod_module_get_initstate(mod); + + if (state == KMOD_MODULE_BUILTIN) { + if (first_time) { + LOG("Module %s already in kernel (builtin).\n", + modname); + return -EEXIST; + } + return 0; + } else if (state == KMOD_MODULE_LIVE) { + if (first_time) { + LOG("Module %s already in kernel.\n", modname); + return -EEXIST; + } + return 0; + } + } + + err = insmod_do_dependencies(mod); + if (err < 0) + return err; + if (conf_opts || extra_opts) { if (conf_opts == NULL) opts = strdup(extra_opts); -- 2.7.4