From 760ad715c2e9387831365ffb935bf7259c575354 Mon Sep 17 00:00:00 2001
From: Daniel Salzman <daniel.salzman@nic.cz>
Date: Mon, 20 Mar 2017 15:02:09 +0100
Subject: [PATCH] ctl: remove running from status with arguments, fix snprintf
 err return

---
 configure.ac               | 12 ++++----
 src/knot/ctl/commands.c    | 57 ++++++++++++++++++++++++++------------
 src/utils/knotc/commands.c | 16 +++++++----
 3 files changed, 55 insertions(+), 30 deletions(-)

diff --git a/configure.ac b/configure.ac
index 6b39c0132b..5b9dd4252b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -536,8 +536,7 @@ AM_CONDITIONAL([HAVE_SPHINXBUILD], test "$SPHINXBUILD" != "false")
 AM_CONDITIONAL([HAVE_PDFLATEX], test "$PDFLATEX" != "false")
 AM_CONDITIONAL([HAVE_MAKEINFO], test "$MAKEINFO" != "false")
 
-result_msg_base="
-  $PACKAGE $VERSION
+result_msg_base="  $PACKAGE $VERSION
 
     Target:   $host_os $host_cpu
     Compiler: ${CC}
@@ -573,11 +572,12 @@ result_msg_base="
     Dnstap support:      ${opt_dnstap}
     Code coverage:       ${enable_code_coverage}
     Bash completions:    ${bash_completions_output}
-    PKCS #11 support:    ${enable_pkcs11}
-"
+    PKCS #11 support:    ${enable_pkcs11}"
+
+result_msg_esc=$(echo -n "$result_msg_base" | sed '$!s/$/\\n/' | tr -d '\n')
+result_msg_add="
+$result_msg_base
 
-result_msg_esc=$(echo -n "$result_msg_base" | sed 's/$/\\n/' | tr -d '\n')
-result_msg_add="$result_msg_base
   Continue with 'make' command
 "
 
diff --git a/src/knot/ctl/commands.c b/src/knot/ctl/commands.c
index 621cc2c0de..e2e2864a46 100644
--- a/src/knot/ctl/commands.c
+++ b/src/knot/ctl/commands.c
@@ -948,7 +948,7 @@ static int send_stats_ctr(mod_ctr_t *ctr, ctl_args_t *args, knot_ctl_data_t *dat
 	if (ctr->count == 1) {
 		int ret = snprintf(value, sizeof(value), "%"PRIu64, ctr->counter);
 		if (ret <= 0 || ret >= sizeof(value)) {
-			return ret;
+			return KNOT_ESPACE;
 		}
 
 		(*data)[KNOT_CTL_IDX_ID] = NULL;
@@ -980,13 +980,13 @@ static int send_stats_ctr(mod_ctr_t *ctr, ctl_args_t *args, knot_ctl_data_t *dat
 				ret = snprintf(index, sizeof(index), "%u", i);
 			}
 			if (ret <= 0 || ret >= sizeof(index)) {
-				return ret;
+				return KNOT_ESPACE;
 			}
 
-			ret = snprintf(value, sizeof(value),  "%"PRIu64,
+			ret = snprintf(value, sizeof(value), "%"PRIu64,
 			               ctr->counters[i]);
 			if (ret <= 0 || ret >= sizeof(value)) {
-				return ret;
+				return KNOT_ESPACE;
 			}
 
 			(*data)[KNOT_CTL_IDX_ID] = index;
@@ -1125,27 +1125,47 @@ static int ctl_zone(ctl_args_t *args, ctl_cmd_t cmd)
 	}
 }
 
+static int server_status(ctl_args_t *args)
+{
+	const char *type = args->data[KNOT_CTL_IDX_TYPE];
+
+	if (type == NULL || strlen(type) == 0) {
+		return KNOT_EOK;
+	}
+
+	char buff[2048] = "";
+
+	int ret;
+	if (strcasecmp(type, "version") == 0) {
+		ret = snprintf(buff, sizeof(buff), "Version: %s", PACKAGE_VERSION);
+	} else if (strcasecmp(type, "workers") == 0) {
+		ret = snprintf(buff, sizeof(buff), "UDP workers: %zu, TCP workers %zu, "
+		               "background workers: %zu", conf_udp_threads(conf()),
+		               conf_tcp_threads(conf()), conf_bg_threads(conf()));
+	} else if (strcasecmp(type, "configure") == 0) {
+		ret = snprintf(buff, sizeof(buff), "%s", CONFIGURE_SUMMARY);
+	} else {
+		return KNOT_EINVAL;
+	}
+	if (ret <= 0 || ret >= sizeof(buff)) {
+		return KNOT_ESPACE;
+	}
+
+	args->data[KNOT_CTL_IDX_DATA] = buff;
+
+	return knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &args->data);
+}
+
 static int ctl_server(ctl_args_t *args, ctl_cmd_t cmd)
 {
 	int ret = KNOT_EOK;
-	char outbuf[2048] = { 0 };
 
 	switch (cmd) {
 	case CTL_STATUS:
-		ret = KNOT_EOK;
-		if (strcasecmp(args->data[KNOT_CTL_IDX_DATA], "version") == 0) {
-			snprintf(outbuf, sizeof(outbuf), "Version: %s", PACKAGE_VERSION);
-		} else if (strcasecmp(args->data[KNOT_CTL_IDX_DATA], "workers") == 0) {
-			snprintf(outbuf, sizeof(outbuf), "UDP workers: %zu, TCP workers %zu, "
-				 "background workers: %zu", conf_udp_threads(conf()),
-				 conf_tcp_threads(conf()), conf_bg_threads(conf()));
-		} else if (strcasecmp(args->data[KNOT_CTL_IDX_DATA], "configure") == 0) {
-			snprintf(outbuf, sizeof(outbuf), "%s", CONFIGURE_SUMMARY);
-		} else if (args->data[KNOT_CTL_IDX_DATA][0] != '\0') {
-			return KNOT_EINVAL;
+		ret = server_status(args);
+		if (ret != KNOT_EOK) {
+			send_error(args, knot_strerror(ret));
 		}
-		args->data[KNOT_CTL_IDX_DATA] = outbuf;
-		ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &args->data);
 		break;
 	case CTL_STOP:
 		ret = KNOT_CTL_ESTOP;
@@ -1196,6 +1216,7 @@ static int ctl_stats(ctl_args_t *args, ctl_cmd_t cmd)
 			int ret = snprintf(value, sizeof(value), "%"PRIu64,
 			                   i->val(args->server));
 			if (ret <= 0 || ret >= sizeof(value)) {
+				ret = KNOT_ESPACE;
 				send_error(args, knot_strerror(ret));
 				return ret;
 			}
diff --git a/src/utils/knotc/commands.c b/src/utils/knotc/commands.c
index 43cefe261c..b48c6a4244 100644
--- a/src/utils/knotc/commands.c
+++ b/src/utils/knotc/commands.c
@@ -203,11 +203,15 @@ static void format_data(ctl_cmd_t cmd, knot_ctl_type_t data_type,
 
 	switch (cmd) {
 	case CTL_STATUS:
-		if (data_type == KNOT_CTL_TYPE_DATA &&
-		    value != NULL && *value != '\0') {
-			printf("%s\n", value);
+		if (error != NULL) {
+			printf("error: (%s)%s%s", error,
+			       (type != NULL) ? " "  : "",
+			       (type != NULL) ? type : "");
+		} else if (value != NULL) {
+			printf("%s", value);
+			*empty = false;
 		}
-		// FALLTHROUGH
+		break;
 	case CTL_STOP:
 	case CTL_RELOAD:
 	case CTL_CONF_BEGIN:
@@ -324,7 +328,7 @@ static void format_block(ctl_cmd_t cmd, bool failed, bool empty)
 {
 	switch (cmd) {
 	case CTL_STATUS:
-		printf("%s\n", failed ? "" : "Running");
+		printf("%s\n", (failed || !empty) ? "" : "Running");
 		break;
 	case CTL_STOP:
 		printf("%s\n", failed ? "" : "Stopped");
@@ -416,7 +420,7 @@ static int cmd_ctl(cmd_args_t *args)
 	knot_ctl_data_t data = {
 		[KNOT_CTL_IDX_CMD] = ctl_cmd_to_str(args->desc->cmd),
 		[KNOT_CTL_IDX_FLAGS] = args->force ? CTL_FLAG_FORCE : NULL,
-		[KNOT_CTL_IDX_DATA] = (args->argc > 0 ? args->argv[0] : "")
+		[KNOT_CTL_IDX_TYPE] = args->argc > 0 ? args->argv[0] : NULL
 	};
 
 	// Send the command.
-- 
GitLab