libkmod-module: probe: add flag to stop loading on already loaded

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.
diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c
index e1a04ba..ff01cd8 100644
--- a/libkmod/libkmod-module.c
+++ b/libkmod/libkmod-module.c
@@ -1168,6 +1168,11 @@
 			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 @@
 		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;