Patch uses flock to ensure that only one instance of corosync is running.

Regards,
  Honza
commit fd9aaa81d3f4da5d8e368ddbc4de3168ff3d7450
Author: Jan Friesse <[email protected]>
Date:   Thu Jul 22 17:42:14 2010 +0200

    Ability to run only one instance of corosync
    
    This patch uses flock to allow only one instance of corosync running.

diff --git a/trunk/exec/main.c b/trunk/exec/main.c
index 4ddb5f2..9e08ede 100644
--- a/trunk/exec/main.c
+++ b/trunk/exec/main.c
@@ -38,6 +38,7 @@
 #include <pthread.h>
 #include <assert.h>
 #include <sys/types.h>
+#include <sys/file.h>
 #include <sys/poll.h>
 #include <sys/uio.h>
 #include <sys/mman.h>
@@ -1367,6 +1368,41 @@ static void main_service_ready (void)
 
 }
 
+static enum e_ais_done corosync_flock (void)
+{
+	enum e_ais_done err;
+	int lf;
+	int res;
+
+	err = AIS_DONE_EXIT;
+
+	lf = open (LOCALSTATEDIR"/lock/corosync", O_RDWR | O_CREAT, 0640);
+	if (lf == -1) {
+		log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't create lock file.\n");
+		return (AIS_DONE_AQUIRE_LOCK);
+	}
+
+retry_flock:
+	res = flock (lf, LOCK_EX | LOCK_NB);
+	if (res == -1) {
+		switch (errno) {
+		case EINTR:
+			goto retry_flock;
+			break;
+		case EWOULDBLOCK:
+			log_printf (LOGSYS_LEVEL_ERROR, "Another Corosync instance is already running.\n");
+			err = AIS_DONE_ALREADY_RUNNING;
+			break;
+		default:
+			log_printf (LOGSYS_LEVEL_ERROR, "Corosync Executive couldn't aquire lock.\n");
+			err = AIS_DONE_AQUIRE_LOCK;
+			break;
+		}
+	}
+
+	return (err);
+}
+
 int main (int argc, char **argv, char **envp)
 {
 	const char *error_string;
@@ -1386,6 +1422,7 @@ int main (int argc, char **argv, char **envp)
 	struct stat stat_out;
 	char corosync_lib_dir[PATH_MAX];
 	hdb_handle_t object_runtime_handle;
+	enum e_ais_done flock_err;
 
 #if defined(HAVE_PTHREAD_SPIN_LOCK)
 	pthread_spin_init (&serialize_spin, 0);
@@ -1436,6 +1473,10 @@ int main (int argc, char **argv, char **envp)
 	log_printf (LOGSYS_LEVEL_NOTICE, "Corosync Cluster Engine ('%s'): started and ready to provide service.\n", VERSION);
 	log_printf (LOGSYS_LEVEL_INFO, "Corosync built-in features:" PACKAGE_FEATURES "\n");
 
+	if ((flock_err = corosync_flock ()) != AIS_DONE_EXIT) {
+		corosync_exit_error (flock_err);
+	}
+
 	(void)signal (SIGINT, sigintr_handler);
 	(void)signal (SIGUSR2, sigusr2_handler);
 	(void)signal (SIGSEGV, sigsegv_handler);
diff --git a/trunk/exec/util.h b/trunk/exec/util.h
index 7b95536..ed3529c 100644
--- a/trunk/exec/util.h
+++ b/trunk/exec/util.h
@@ -60,7 +60,9 @@ enum e_ais_done {
 	AIS_DONE_INIT_SERVICES = 13,
 	AIS_DONE_OUT_OF_MEMORY = 14,
 	AIS_DONE_FATAL_ERR = 15,
-	AIS_DONE_DIR_NOT_PRESENT = 16
+	AIS_DONE_DIR_NOT_PRESENT = 16,
+	AIS_DONE_AQUIRE_LOCK = 17,
+	AIS_DONE_ALREADY_RUNNING = 18,
 };
 
 /*
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to