[Ocfs2-tools-devel] [PATCH 2/2] o2hbmonitor: add semaphore
Srinivas Eeda
srinivas.eeda at oracle.com
Fri Nov 12 16:22:33 PST 2010
add semaphore to limit to one o2hbmonitor per node
Signed-off-by: Srinivas Eeda <srinivas.eeda at oracle.com>
---
o2monitor/o2hbmonitor.c | 39 +++++++++++++++++++++++++++++++++++++++
1 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/o2monitor/o2hbmonitor.c b/o2monitor/o2hbmonitor.c
index f01da76..d7cc92c 100644
--- a/o2monitor/o2hbmonitor.c
+++ b/o2monitor/o2hbmonitor.c
@@ -43,6 +43,8 @@
#include <libgen.h>
#include <syslog.h>
#include <errno.h>
+#include <sys/ipc.h>
+#include <sys/sem.h>
#define SYS_CONFIG_DIR "/sys/kernel/config"
#define O2HB_CLUSTER_DIR SYS_CONFIG_DIR"/cluster"
@@ -61,6 +63,8 @@
#define SLOW_POLL_IN_SECS 10
#define FAST_POLL_IN_SECS 2
+#define O2HB_SEM_MAGIC_KEY 0x4A32594B
+
char *progname;
int interactive;
int warn_threshold_percent;
@@ -300,6 +304,32 @@ static void monitor(void)
}
}
+static int getlock(void)
+{
+ int semid, vals[]={0};
+ struct sembuf trylock[] = {{0, 0, SEM_UNDO|IPC_NOWAIT},
+ {0, 1, SEM_UNDO|IPC_NOWAIT}};
+
+ semid = semget(O2HB_SEM_MAGIC_KEY, 1, 0);
+ if (semid < 0) {
+ printf("creating sem\n");
+ semid = semget(O2HB_SEM_MAGIC_KEY, 1,
+ IPC_CREAT|IPC_EXCL|S_IRUSR);
+ if (semid < 0)
+ goto out;
+ semctl(semid, 0, SETALL, vals);
+ if (semop(semid, trylock, 2) < 0)
+ goto out;
+ else
+ return 0;
+ }
+ if (semop(semid, trylock, 2) < 0)
+ goto out;
+ return 0;
+out:
+ return errno;
+}
+
static void usage(void)
{
fprintf(stderr, "usage: %s [-w percent] -[ivV]\n", progname);
@@ -360,6 +390,15 @@ int main(int argc, char **argv)
}
openlog(progname, LOG_CONS|LOG_NDELAY, LOG_DAEMON);
+ if ((ret = getlock())) {
+ if (ret == EAGAIN)
+ syslog(LOG_WARNING, "o2hbmonitor already running\n");
+ else
+ syslog(LOG_WARNING, "o2hbmonitor failed to start\n");
+ closelog();
+ return ret;
+ }
+
monitor();
closelog();
--
1.5.6.5
More information about the Ocfs2-tools-devel
mailing list