From af9572c6d7288515623d2b85204cb97eb5c7b151 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Thu, 2 Feb 2012 11:07:33 -0500 Subject: [PATCH] lib/module: check initstate before inserting module This applies to both the high level probe_insert_module() and the underlying insert_module() functions. By checking module initstate prior to inserting a module, we can avoid a lot of needless work just to find out that the init_module call fails with EEXIST. This implements a helper function, module_is_inkernel, to return a boolean value describing if a module is live, coming, or builtin. --- libkmod/libkmod-module.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index e16dbd91..db1c55ff 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -98,6 +98,17 @@ static inline const char *path_join(const char *path, size_t prefixlen, return buf; } +static inline bool module_is_inkernel(struct kmod_module *mod) +{ + int state = kmod_module_get_initstate(mod); + if (state == KMOD_MODULE_LIVE || + state == KMOD_MODULE_COMING || + state == KMOD_MODULE_BUILTIN) + return true; + else + return false; +} + int kmod_module_parse_depline(struct kmod_module *mod, char *line) { struct kmod_ctx *ctx = mod->ctx; @@ -1121,6 +1132,13 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod, if (mod == NULL) return -ENOENT; + if (module_is_inkernel(mod)) { + if (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED) + return -EEXIST; + else + return 0; + } + err = flags & (KMOD_PROBE_APPLY_BLACKLIST | KMOD_PROBE_APPLY_BLACKLIST_ALL); if (err != 0) { @@ -1166,24 +1184,12 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod, err = module_do_install_commands(m, options, &cb); } else { - int state; - - if (flags & KMOD_PROBE_IGNORE_LOADED) - state = -1; - else - state = kmod_module_get_initstate(m); - - if (state == KMOD_MODULE_LIVE || - state == KMOD_MODULE_COMING || - state == KMOD_MODULE_BUILTIN) { - if (m == mod && (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) { - err = -EEXIST; - break; - } - + if (!(flags & KMOD_PROBE_IGNORE_LOADED) && module_is_inkernel(m)) { DBG(mod->ctx, "Ignoring module '%s': " "already loaded\n", m->name); err = 0; + free(options); + continue; } if (print_action != NULL) print_action(m, false, options ?: ""); -- 2.47.2