locking: Use separate locking helper binary for locking
authorAmitay Isaacs <amitay@gmail.com>
Tue, 30 Apr 2013 05:07:49 +0000 (15:07 +1000)
committerAmitay Isaacs <amitay@gmail.com>
Thu, 23 May 2013 23:06:40 +0000 (09:06 +1000)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Makefile.in
server/ctdb_lock.c
tests/scripts/integration.bash

index 71c3f3186e4bf6f073547b80553dec8d79b719d1..1bbec6f6ccd174e83b4386180bbe4ed08f805752 100755 (executable)
@@ -51,7 +51,7 @@ PMDA_DEST_DIR = /var/lib/pcp/pmdas
 CFLAGS=@CPPFLAGS@ -g -I$(srcdir)/include -Iinclude -Ilib -Ilib/util -I$(srcdir) \
        $(TALLOC_CFLAGS) $(TEVENT_CFLAGS) $(TDB_CFLAGS) -I@libreplacedir@ \
        -DVARDIR=\"$(localstatedir)\" -DETCDIR=\"$(etcdir)\" \
-       -DLOGDIR=\"$(logdir)\" \
+       -DLOGDIR=\"$(logdir)\" -DBINDIR=\"$(bindir)\" \
        -DSOCKPATH=\"$(sockpath)\" \
        -DUSE_MMAP=1 -DTEVENT_DEPRECATED_QUIET=1 @CFLAGS@ -Wno-format-zero-length $(POPT_CFLAGS) \
        -fPIC
index 8e2f40dd4e0c053511800f37b8fc0418e49c3880..9469835397b70bf5484466a979e642edd7b280ec 100644 (file)
@@ -808,7 +808,10 @@ static void ctdb_lock_schedule(struct ctdb_context *ctdb)
 {
        struct lock_context *lock_ctx, *next_ctx;
        int ret;
-       pid_t parent;
+       TALLOC_CTX *tmp_ctx;
+       const char *helper = BINDIR "/ctdb_lock_helper";
+       const char *prog;
+       char **args;
 
        if (ctdb->lock_num_current >= MAX_LOCK_PROCESSES_PER_DB) {
                return;
@@ -850,41 +853,60 @@ static void ctdb_lock_schedule(struct ctdb_context *ctdb)
                return;
        }
 
-       parent = getpid();
+       set_close_on_exec(lock_ctx->fd[0]);
+
+       /* Create data for child process */
+       tmp_ctx = talloc_new(lock_ctx);
+       if (tmp_ctx == NULL) {
+               DEBUG(DEBUG_ERR, ("Failed to allocate memory for helper args\n"));
+               close(lock_ctx->fd[0]);
+               close(lock_ctx->fd[1]);
+               return;
+       }
+
+       /* Create arguments for lock helper */
+       args = lock_helper_args(tmp_ctx, lock_ctx, lock_ctx->fd[1]);
+       if (args == NULL) {
+               DEBUG(DEBUG_ERR, ("Failed to create lock helper args\n"));
+               close(lock_ctx->fd[0]);
+               close(lock_ctx->fd[1]);
+               talloc_free(tmp_ctx);
+               return;
+       }
+
+       prog = getenv("CTDB_LOCK_HELPER");
+       if (prog == NULL) {
+               prog = helper;
+       }
+
        lock_ctx->child = ctdb_fork(ctdb);
 
        if (lock_ctx->child == (pid_t)-1) {
                DEBUG(DEBUG_ERR, ("Failed to create a child in ctdb_lock_schedule\n"));
                close(lock_ctx->fd[0]);
                close(lock_ctx->fd[1]);
+               talloc_free(tmp_ctx);
                return;
        }
 
+
        /* Child process */
        if (lock_ctx->child == 0) {
-               char c;
-               close(lock_ctx->fd[0]);
-               debug_extra = lock_child_log_prefix(lock_ctx);
-               if (ctdb_lock_item(lock_ctx)) {
-                       c = 0;
-               } else {
-                       c = 1;
+               ret = execv(prog, args);
+               if (ret < 0) {
+                       DEBUG(DEBUG_ERR, ("Failed to execute helper %s (%d, %s)\n",
+                                         prog, errno, strerror(errno)));
                }
-               write(lock_ctx->fd[1], &c, 1);
-
-               /* Hang around, but if parent dies, terminate */
-               while (kill(parent, 0) == 0 || errno != ESRCH) {
-                       sleep(5);
-               }
-               _exit(0);
+               _exit(1);
        }
 
        /* Parent process */
        close(lock_ctx->fd[1]);
-       set_close_on_exec(lock_ctx->fd[0]);
 
        talloc_set_destructor(lock_ctx, ctdb_lock_context_destructor);
 
+       talloc_free(tmp_ctx);
+
        /* Set up timeout handler */
        lock_ctx->ttimer = tevent_add_timer(ctdb->ev,
                                            lock_ctx,
index 2ec827c14f9d630ca7013490ec79bea54e899004..429df69de1363c53d474eb363c2f210439f6b403 100644 (file)
@@ -14,6 +14,7 @@ if [ -n "$TEST_LOCAL_DAEMONS" ] ; then
     # Otherwise CTDB need to be installed on all nodes.
     if [ -n "$ctdb_dir" -a -d "${ctdb_dir}/bin" ] ; then
        PATH="${ctdb_dir}/bin:${PATH}"
+        export CTDB_LOCK_HELPER="${ctdb_dir}/bin/ctdb_lock_helper"
     fi
 
     export CTDB_NODES="${TEST_VAR_DIR}/nodes.txt"