Blob Blame History Raw
From 8301cbafed191f30656a22876941cc7c9189b623 Mon Sep 17 00:00:00 2001
From: Klaus Wenninger <klaus.wenninger@aon.at>
Date: Thu, 31 Jan 2019 14:42:01 +0100
Subject: [PATCH] Refactor: fail earlier on invalid servants

---
 src/sbd-inquisitor.c | 51 ++++++++++++++++++++++++++++++++-------------------
 src/sbd-md.c         |  7 +------
 src/sbd.h            |  2 +-
 3 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/src/sbd-inquisitor.c b/src/sbd-inquisitor.c
index 8e0bc87..9be6c99 100644
--- a/src/sbd-inquisitor.c
+++ b/src/sbd-inquisitor.c
@@ -42,19 +42,36 @@ void recruit_servant(const char *devname, pid_t pid)
 	struct servants_list_item *newbie;
 
 	if (lookup_servant_by_dev(devname)) {
-		cl_log(LOG_DEBUG, "Servant %s already exists", devname);
-		return;
+	    cl_log(LOG_DEBUG, "Servant %s already exists", devname);
+	    return;
 	}
 
 	newbie = malloc(sizeof(*newbie));
-	if (!newbie) {
-		fprintf(stderr, "malloc failed in recruit_servant.\n");
-		exit(1);
+	if (newbie) {
+	    memset(newbie, 0, sizeof(*newbie));
+	    newbie->devname = strdup(devname);
+	    newbie->pid = pid;
+	    newbie->first_start = 1;
+	}
+	if (!newbie || !newbie->devname) {
+	    fprintf(stderr, "heap allocation failed in recruit_servant.\n");
+	    exit(1);
+	}
+
+	/* some sanity-check on our newbie */
+	if (sbd_is_disk(newbie)) {
+	    cl_log(LOG_INFO, "Monitoring %s", devname);
+	    disk_count++;
+	} else if (sbd_is_pcmk(newbie) || sbd_is_cluster(newbie)) {
+	    /* alive just after pcmk and cluster servants have shown up */
+	    newbie->outdated = 1;
+	} else {
+	    /* toss our newbie */
+	    cl_log(LOG_ERR, "Refusing to recruit unrecognized servant %s", devname);
+	    free((void *) newbie->devname);
+	    free(newbie);
+	    return;
 	}
-	memset(newbie, 0, sizeof(*newbie));
-	newbie->devname = strdup(devname);
-	newbie->pid = pid;
-	newbie->first_start = 1;
 
 	if (!s) {
 		servants_leader = newbie;
@@ -65,12 +82,6 @@ void recruit_servant(const char *devname, pid_t pid)
 	}
 
 	servant_count++;
-        if(sbd_is_disk(newbie)) {
-            cl_log(LOG_INFO, "Monitoring %s", devname);
-            disk_count++;
-        } else {
-            newbie->outdated = 1;
-        }
 }
 
 int assign_servant(const char* devname, functionp_t functionp, int mode, const void* argp)
@@ -148,7 +159,7 @@ void servant_start(struct servants_list_item *s)
 	if (sbd_is_disk(s)) {
 #if SUPPORT_SHARED_DISK
 		DBGLOG(LOG_INFO, "Starting servant for device %s", s->devname);
-		s->pid = assign_servant(s->devname, servant, start_mode, s);
+		s->pid = assign_servant(s->devname, servant_md, start_mode, s);
 #else
                 cl_log(LOG_ERR, "Shared disk functionality not supported");
                 return;
@@ -785,12 +796,14 @@ parse_device_line(const char *line)
 
             if (lpc > last) {
                 entry = calloc(1, 1 + lpc - last);
+                if (!entry) {
+                    fprintf(stderr, "heap allocation failed parsing device-line.\n");
+                    exit(1);
+                }
                 rc = sscanf(line + last, "%[^;]", entry);
             }
 
-            if (entry == NULL) {
-                /* Skip */
-            } else if (rc != 1) {
+            if (rc != 1) {
                 cl_log(LOG_WARNING, "Could not parse (%d %d): %s", last, lpc, line + last);
             } else {
                 cl_log(LOG_DEBUG, "Adding '%s'", entry);
diff --git a/src/sbd-md.c b/src/sbd-md.c
index 579d273..ba2c34d 100644
--- a/src/sbd-md.c
+++ b/src/sbd-md.c
@@ -1031,7 +1031,7 @@ static int servant_check_timeout_inconsistent(struct sector_header_s *hdr)
 	return 0;
 }
 
-int servant(const char *diskname, int mode, const void* argp)
+int servant_md(const char *diskname, int mode, const void* argp)
 {
 	struct sector_mbox_s *s_mbox = NULL;
 	struct sector_node_s *s_node = NULL;
@@ -1046,11 +1046,6 @@ int servant(const char *diskname, int mode, const void* argp)
 	char uuid[37];
 	const struct servants_list_item *s = argp;
 
-	if (!diskname) {
-		cl_log(LOG_ERR, "Empty disk name %s.", diskname);
-		return -1;
-	}
-
 	cl_log(LOG_INFO, "Servant starting for device %s", diskname);
 
 	/* Block most of the signals */
diff --git a/src/sbd.h b/src/sbd.h
index 386c85c..6fe07f9 100644
--- a/src/sbd.h
+++ b/src/sbd.h
@@ -175,7 +175,7 @@ int ping_via_slots(const char *name, struct servants_list_item *servants);
 int dump_headers(struct servants_list_item *servants);
 unsigned long get_first_msgwait(struct servants_list_item *servants);
 int messenger(const char *name, const char *msg, struct servants_list_item *servants);
-int servant(const char *diskname, int mode, const void* argp);
+int servant_md(const char *diskname, int mode, const void* argp);
 #endif
 
 int servant_pcmk(const char *diskname, int mode, const void* argp);
-- 
1.8.3.1