From 9404f8631ecc028c4e98879fbc67ccd2be09249f Mon Sep 17 00:00:00 2001
From: Martin Schwenke <martin@meltin.net>
Date: Sat, 24 Oct 2020 20:29:58 +1100
Subject: [PATCH] ctdb-daemon: Clean up socket bind/secure/listen

Obey the coding style, modernise debug macros, clean up whitespace.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Volker Lendecke <vl@samba.org>
---
 ctdb/server/ctdb_daemon.c | 42 +++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/ctdb/server/ctdb_daemon.c b/ctdb/server/ctdb_daemon.c
index 7ebb419bc1f..149f191b464 100644
--- a/ctdb/server/ctdb_daemon.c
+++ b/ctdb/server/ctdb_daemon.c
@@ -1168,9 +1168,9 @@ static void ctdb_accept_client(struct tevent_context *ev,
 
 
 /*
-  create a unix domain socket and bind it
-  return a file descriptor open on the socket 
-*/
+ * Create a unix domain socket, bind it, secure it and listen.  Return
+ * the file descriptor for the socket.
+ */
 static int ux_socket_bind(struct ctdb_context *ctdb)
 {
 	struct sockaddr_un addr = { .sun_family = AF_UNIX };
@@ -1191,38 +1191,46 @@ static int ux_socket_bind(struct ctdb_context *ctdb)
 
 	ret = set_blocking(ctdb->daemon.sd, false);
 	if (ret != 0) {
-		DEBUG(DEBUG_ERR,
-		      (__location__
-		       " failed to set socket non-blocking (%s)\n",
-		       strerror(errno)));
+		DBG_ERR("Failed to set socket non-blocking (%s)\n",
+			strerror(errno));
 		goto failed;
 	}
 
-	if (bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
-		DEBUG(DEBUG_CRIT,("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name));
+	ret = bind(ctdb->daemon.sd, (struct sockaddr *)&addr, sizeof(addr));
+	if (ret == -1) {
+		D_ERR("Unable to bind on ctdb socket '%s'\n", ctdb->daemon.name);
 		goto failed;
 	}
 
-	if (chown(ctdb->daemon.name, geteuid(), getegid()) != 0 ||
-	    chmod(ctdb->daemon.name, 0700) != 0) {
-		DEBUG(DEBUG_CRIT,("Unable to secure ctdb socket '%s', ctdb->daemon.name\n", ctdb->daemon.name));
+	ret = chown(ctdb->daemon.name, geteuid(), getegid());
+	if (ret != 0) {
+		D_ERR("Unable to secure (chown) ctdb socket '%s'\n",
+		      ctdb->daemon.name);
+		goto failed;
+	}
+
+	ret = chmod(ctdb->daemon.name, 0700);
+	if (ret != 0) {
+		D_ERR("Unable to secure (chmod) ctdb socket '%s'\n",
+		      ctdb->daemon.name);
 		goto failed;
 	}
 
 
-	if (listen(ctdb->daemon.sd, 100) != 0) {
-		DEBUG(DEBUG_CRIT,("Unable to listen on ctdb socket '%s'\n", ctdb->daemon.name));
+	ret = listen(ctdb->daemon.sd, 100);
+	if (ret != 0) {
+		D_ERR("Unable to listen on ctdb socket '%s'\n",
+		      ctdb->daemon.name);
 		goto failed;
 	}
 
-	DEBUG(DEBUG_NOTICE, ("Listening to ctdb socket %s\n",
-			     ctdb->daemon.name));
+	D_NOTICE("Listening to ctdb socket %s\n", ctdb->daemon.name);
 	return 0;
 
 failed:
 	close(ctdb->daemon.sd);
 	ctdb->daemon.sd = -1;
-	return -1;	
+	return -1;
 }
 
 static void initialise_node_flags (struct ctdb_context *ctdb)