From fd5acc3e026d9f36474f33fbb6bba9076a9d7385 Mon Sep 17 00:00:00 2001
From: Libor Peltan <libor.peltan@nic.cz>
Date: Wed, 8 Mar 2017 16:28:12 +0100
Subject: [PATCH] ctl: status: added options: version, workers, configure

...to display either real Knot running version, numbers of workers, or configure options
---
 configure.ac               | 48 ++++++++++++++++++++++++--------------
 doc/man/knotc.8in          |  6 ++++-
 doc/man_knotc.rst          |  6 ++++-
 src/knot/ctl/commands.c    | 14 +++++++++++
 src/utils/knotc/commands.c | 12 +++++++---
 5 files changed, 63 insertions(+), 23 deletions(-)

diff --git a/configure.ac b/configure.ac
index c1c6a49fa4..6b39c0132b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -9,6 +9,7 @@ m4_define([knot_VERSION_EXTRA],)dnl
 m4_define([knot_PKG_VERSION],[knot_VERSION_MAJOR.knot_VERSION_MINOR.knot_VERSION_PATCH]knot_VERSION_EXTRA)dnl
 
 AC_INIT([knot], knot_PKG_VERSION, [knot-dns@labs.nic.cz])
+configure_params="$*"
 AM_INIT_AUTOMAKE([gnits subdir-objects no-dist-gzip dist-xz -Wall -Werror])
 AM_SILENT_RULES([yes])
 AC_CONFIG_SRCDIR([src/knot])
@@ -26,6 +27,10 @@ AC_CONFIG_FILES([src/libknot/version.h
                  src/zscanner/version.h
                  src/dnssec/lib/dnssec/version.h])
 
+# Store ./configure parameters into macro
+AC_DEFINE_UNQUOTED([CONFIGURE_PARAMS],["$configure_params"],[Params passed to configure])
+AC_DEFINE_UNQUOTED([CONFIGURE_CFLAGS],["$CFLAGS"],[Passed CFLAGS from environment])
+
 # Updating version info
 # https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
 AC_SUBST([libknot_VERSION_INFO],["-version-info 5:0:0"])
@@ -531,23 +536,7 @@ AM_CONDITIONAL([HAVE_SPHINXBUILD], test "$SPHINXBUILD" != "false")
 AM_CONDITIONAL([HAVE_PDFLATEX], test "$PDFLATEX" != "false")
 AM_CONDITIONAL([HAVE_MAKEINFO], test "$MAKEINFO" != "false")
 
-AC_CONFIG_FILES([Makefile
-                 doc/Makefile
-                 libtap/Makefile
-                 tests/Makefile
-                 tests-fuzz/Makefile
-                 samples/Makefile
-                 python/Makefile
-                 src/Makefile
-                 src/contrib/dnstap/Makefile
-                 src/dnssec/Makefile
-                 src/dnssec/tests/Makefile
-                 src/zscanner/Makefile
-                 ])
-
-AC_OUTPUT
-
-AC_MSG_RESULT([
+result_msg_base="
   $PACKAGE $VERSION
 
     Target:   $host_os $host_cpu
@@ -585,6 +574,29 @@ AC_MSG_RESULT([
     Code coverage:       ${enable_code_coverage}
     Bash completions:    ${bash_completions_output}
     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
   Continue with 'make' command
-])
+"
+
+AC_DEFINE_UNQUOTED([CONFIGURE_SUMMARY],["$result_msg_esc"],[Configure summary])
+
+AC_CONFIG_FILES([Makefile
+                 doc/Makefile
+                 libtap/Makefile
+                 tests/Makefile
+                 tests-fuzz/Makefile
+                 samples/Makefile
+                 python/Makefile
+                 src/Makefile
+                 src/contrib/dnstap/Makefile
+                 src/dnssec/Makefile
+                 src/dnssec/tests/Makefile
+                 src/zscanner/Makefile
+                 ])
+
+AC_OUTPUT
+
+AC_MSG_RESULT([$result_msg_add])
diff --git a/doc/man/knotc.8in b/doc/man/knotc.8in
index adc8ed6faa..983830753b 100644
--- a/doc/man/knotc.8in
+++ b/doc/man/knotc.8in
@@ -68,8 +68,10 @@ Print the program version.
 .SS Actions
 .INDENT 0.0
 .TP
-\fBstatus\fP
+\fBstatus\fP [\fIdetail\fP]
 Check if the server is running.
+Moreover display either the running knotd version, numbers of worker threads,
+or Knot DNS buid (configure) summary, if the parameter is specified.
 .TP
 \fBstop\fP
 Stop the server if running.
@@ -194,6 +196,8 @@ Use \fB@\fP \fIowner\fP to denote the zone name.
 .sp
 Type \fIitem\fP parameter in the form of \fIsection\fP[\fB[\fP\fIid\fP\fB]\fP][\fB\&.\fP\fIname\fP].
 .sp
+The \fIdetail\fP option for \fBstatus\fP can be one of words: version, workers, configure.
+.sp
 (*) indicates a local operation which requires a configuration.
 .SS Interactive mode
 .sp
diff --git a/doc/man_knotc.rst b/doc/man_knotc.rst
index 845b76f43e..1d8577fd9c 100644
--- a/doc/man_knotc.rst
+++ b/doc/man_knotc.rst
@@ -45,8 +45,10 @@ Parameters
 Actions
 .......
 
-**status**
+**status** [*detail*]
   Check if the server is running.
+  Moreover display either the running knotd version, numbers of worker threads,
+  or Knot DNS buid (configure) summary, if the parameter is specified.
 
 **stop**
   Stop the server if running.
@@ -172,6 +174,8 @@ Use **@** *owner* to denote the zone name.
 
 Type *item* parameter in the form of *section*\ [**[**\ *id*\ **]**\ ][**.**\ *name*].
 
+The *detail* option for **status** can be one of words: version, workers, configure.
+
 (*) indicates a local operation which requires a configuration.
 
 Interactive mode
diff --git a/src/knot/ctl/commands.c b/src/knot/ctl/commands.c
index 91a0ef6709..621cc2c0de 100644
--- a/src/knot/ctl/commands.c
+++ b/src/knot/ctl/commands.c
@@ -1128,10 +1128,24 @@ static int ctl_zone(ctl_args_t *args, ctl_cmd_t cmd)
 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;
+		}
+		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;
diff --git a/src/utils/knotc/commands.c b/src/utils/knotc/commands.c
index c88ed73fac..43cefe261c 100644
--- a/src/utils/knotc/commands.c
+++ b/src/utils/knotc/commands.c
@@ -203,6 +203,11 @@ 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);
+		}
+		// FALLTHROUGH
 	case CTL_STOP:
 	case CTL_RELOAD:
 	case CTL_CONF_BEGIN:
@@ -403,14 +408,15 @@ static int ctl_receive(cmd_args_t *args)
 
 static int cmd_ctl(cmd_args_t *args)
 {
-	int ret = check_args(args, 0, 0);
+	int ret = check_args(args, 0, (args->desc->cmd == CTL_STATUS ? 1 : 0));
 	if (ret != KNOT_EOK) {
 		return ret;
 	}
 
 	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_FLAGS] = args->force ? CTL_FLAG_FORCE : NULL,
+		[KNOT_CTL_IDX_DATA] = (args->argc > 0 ? args->argv[0] : "")
 	};
 
 	// Send the command.
@@ -957,7 +963,7 @@ static int cmd_conf_ctl(cmd_args_t *args)
 const cmd_desc_t cmd_table[] = {
 	{ CMD_EXIT,            NULL,              CTL_NONE },
 
-	{ CMD_STATUS,          cmd_ctl,           CTL_STATUS },
+	{ CMD_STATUS,          cmd_ctl,           CTL_STATUS,          CMD_FOPT_DATA},
 	{ CMD_STOP,            cmd_ctl,           CTL_STOP },
 	{ CMD_RELOAD,          cmd_ctl,           CTL_RELOAD },
 	{ CMD_STATS,           cmd_stats_ctl,     CTL_STATS },
-- 
GitLab