]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod-module: probe: add flag to stop loading on already loaded
authorLucas De Marchi <lucas.demarchi@profusion.mobi>
Mon, 30 Jan 2012 18:26:52 +0000 (16:26 -0200)
committerLucas De Marchi <lucas.demarchi@profusion.mobi>
Mon, 30 Jan 2012 22:05:34 +0000 (20:05 -0200)
It's not as simple as tell user to check if the module is loaded before
calling this function. Due to race conditions, module might not be
loaded before the function call, but fail later because another process
inserted it.

libkmod/libkmod-module.c
libkmod/libkmod.h

index e1a04ba910503ddcdbb9da50f52b1acd200dfe60..ff01cd89fb419202ec5d8fef034ea2993f28541e 100644 (file)
@@ -1168,6 +1168,11 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
                        if (state == KMOD_MODULE_LIVE ||
                                        state == KMOD_MODULE_COMING ||
                                        state == KMOD_MODULE_BUILTIN) {
+                               if (m == mod && (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) {
+                                       err = KMOD_PROBE_STOP_ON_ALREADY_LOADED;
+                                       break;
+                               }
+
                                DBG(mod->ctx, "Ignoring '%s': "
                                        "module already loaded\n", m->name);
                                free(options);
@@ -1179,11 +1184,21 @@ KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod,
                free(options);
 
                /*
-                * Ignore "already loaded" error. We need to check here
-                * because of race conditions. We checked first if module was
-                * already loaded but it may have been loaded between the
-                * check and the moment we try to insert it.
+                * Treat "already loaded" error. If we were told to stop on
+                * already loaded and the module being loaded is not a
+                * softdep, bail out. Otherwise, just ignore and continue.
+                *
+                * We need to check here because of race conditions. We
+                * checked first if module was already loaded but it may have
+                * been loaded between the check and the moment we try to
+                * insert it.
                 */
+               if (err == -EEXIST && m == mod &&
+                               (flags & KMOD_PROBE_STOP_ON_ALREADY_LOADED)) {
+                       err = KMOD_PROBE_STOP_ON_ALREADY_LOADED;
+                       break;
+               }
+
                if (err < 0 && err != -EEXIST &&
                                (flags & KMOD_PROBE_STOP_ON_DEP_FAILURE))
                        break;
index b37715007ea8989584983f6031219c3e77b37ba8..1873f1cc25909050cddc248ada60148d0641f335 100644 (file)
@@ -134,6 +134,7 @@ enum kmod_probe {
        KMOD_PROBE_STOP_ON_DEP_FAILURE = 0x0F,
        KMOD_PROBE_STOP_ON_COMMAND = 0x10,
        KMOD_PROBE_IGNORE_COMMAND = 0x20,
+       KMOD_PROBE_STOP_ON_ALREADY_LOADED = 0x30,
 };
 
 /*