From db32a2bce54b9618fe247b33d6de81bd5f7a3b62 Mon Sep 17 00:00:00 2001
From: Martin Schwenke <martin@meltin.net>
Date: Tue, 9 Dec 2014 14:07:20 +1100
Subject: [PATCH] ctdb-recoverd: New function ctdb_recovery_unlock()

Unlock the recovery lock file.  This way knowledge of the file
descriptor isn't sprinkled throughout the code.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
---
 ctdb/include/ctdb_private.h |  1 +
 ctdb/server/ctdb_recover.c  |  9 +++++++++
 ctdb/server/ctdb_recoverd.c | 24 +++++-------------------
 3 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/ctdb/include/ctdb_private.h b/ctdb/include/ctdb_private.h
index 03130d66029..19fab572a3b 100644
--- a/ctdb/include/ctdb_private.h
+++ b/ctdb/include/ctdb_private.h
@@ -1261,6 +1261,7 @@ void set_close_on_exec(int fd);
 
 bool ctdb_recovery_have_lock(struct ctdb_context *ctdb);
 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep);
+void ctdb_recovery_unlock(struct ctdb_context *ctdb);
 
 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file);
 
diff --git a/ctdb/server/ctdb_recover.c b/ctdb/server/ctdb_recover.c
index 1f1e50aa292..a4d84fd1a75 100644
--- a/ctdb/server/ctdb_recover.c
+++ b/ctdb/server/ctdb_recover.c
@@ -784,6 +784,15 @@ bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep)
 	return true;
 }
 
+void ctdb_recovery_unlock(struct ctdb_context *ctdb)
+{
+	if (ctdb->recovery_lock_fd != -1) {
+		DEBUG(DEBUG_NOTICE, ("Releasing recovery lock\n"));
+		close(ctdb->recovery_lock_fd);
+		ctdb->recovery_lock_fd = -1;
+	}
+}
+
 /*
   delete a record as part of the vacuum process
   only delete if we are not lmaster or dmaster, and our rsn is <= the provided rsn
diff --git a/ctdb/server/ctdb_recoverd.c b/ctdb/server/ctdb_recoverd.c
index 3d7c43240ad..ba42663a576 100644
--- a/ctdb/server/ctdb_recoverd.c
+++ b/ctdb/server/ctdb_recoverd.c
@@ -2688,9 +2688,7 @@ static void election_handler(struct ctdb_context *ctdb, uint64_t srvid,
 		/* Release the recovery lock file */
 		if (em->pnn != ctdb->pnn &&
 		    ctdb_recovery_have_lock(ctdb)) {
-			DEBUG(DEBUG_NOTICE, ("Release the recovery lock\n"));
-			close(ctdb->recovery_lock_fd);
-			ctdb->recovery_lock_fd = -1;
+			ctdb_recovery_unlock(ctdb);
 			unban_all_nodes(ctdb);
 		}
 	}
@@ -3478,10 +3476,7 @@ static int update_recovery_lock_file(struct ctdb_context *ctdb)
 			DEBUG(DEBUG_ERR,("Reclock file disabled\n"));
 			talloc_free(ctdb->recovery_lock_file);
 			ctdb->recovery_lock_file = NULL;
-			if (ctdb->recovery_lock_fd != -1) {
-				close(ctdb->recovery_lock_fd);
-				ctdb->recovery_lock_fd = -1;
-			}
+			ctdb_recovery_unlock(ctdb);
 		}
 		talloc_free(tmp_ctx);
 		return 0;
@@ -3489,10 +3484,7 @@ static int update_recovery_lock_file(struct ctdb_context *ctdb)
 
 	if (ctdb->recovery_lock_file == NULL) {
 		ctdb->recovery_lock_file = talloc_strdup(ctdb, reclockfile);
-		if (ctdb->recovery_lock_fd != -1) {
-			close(ctdb->recovery_lock_fd);
-			ctdb->recovery_lock_fd = -1;
-		}
+		ctdb_recovery_unlock(ctdb);
 		talloc_free(tmp_ctx);
 		return 0;
 	}
@@ -3505,10 +3497,7 @@ static int update_recovery_lock_file(struct ctdb_context *ctdb)
 
 	talloc_free(ctdb->recovery_lock_file);
 	ctdb->recovery_lock_file = talloc_strdup(ctdb, reclockfile);
-	if (ctdb->recovery_lock_fd != -1) {
-		close(ctdb->recovery_lock_fd);
-		ctdb->recovery_lock_fd = -1;
-	}
+	ctdb_recovery_unlock(ctdb);
 
 	talloc_free(tmp_ctx);
 	return 0;
@@ -3575,10 +3564,7 @@ static void main_loop(struct ctdb_context *ctdb, struct ctdb_recoverd *rec,
 	   we close the file
 	*/
         if (ctdb->recovery_lock_file == NULL) {
-		if (ctdb->recovery_lock_fd != -1) {
-			close(ctdb->recovery_lock_fd);
-			ctdb->recovery_lock_fd = -1;
-		}
+		ctdb_recovery_unlock(ctdb);
 	}
 
 	pnn = ctdb_get_pnn(ctdb);