diff mbox series

[ndctl] monitor: Fix the monitor config file parsing

Message ID 164750955519.2000193.16903542741359443926.stgit@LAPTOP-TBQTPII8 (mailing list archive)
State Accepted
Commit 318375a08b794acf4bb63333ecf3819928f1c9e5
Headers show
Series [ndctl] monitor: Fix the monitor config file parsing | expand

Commit Message

Shivaprasad G Bhat March 17, 2022, 9:32 a.m. UTC
Presently, ndctl monitor is not parsing both the default and user specified
config files. The behaviour is quitely masked with the recent iniparser
parsing code without any signs until you remove the /etc/ndctl.conf.d
directory when the command fails as,

 #ndctl monitor -d nmem0
 iniparser: cannot open /etc/ndctl.conf.d

The error is coming from the libiniparser when the monitor parser is
initialised with MONITOR_CALLBACK type with parse_monitor_config() as the
callback. The configs->key is set to the NDCTL_CONF_FILE for this.
The parse_config_file() compares the filename with the key before
calling the custom callback. The current code calls the
parse_config_prefix() with either the default directory path or the
custom config filepath(i.e -c <XYZ>) while the configs->key is set to
the NDCTL_CONF_FILE. Since both these strings don't match the
NDCTL_CONF_FILE, parse_monitor_config() is not called at all and instead
generic iniparser code path is taken.

The patch sets the config key to the correct filename before the calls to
parse_config_prefix().

The previous behaviour for missing monitor.conf file in the default path
was to ignore and continue. The current callback parse_monitor_config()
reports an error as e889fa5e removed the chunk taking care of this case.

So the patch gets the "current" config directory and checks if the default
monitor.conf file exists, dont attempt to parse if the file is missing.

Fixes: e889fa5ef7ff ("ndctl, monitor: refator monitor for supporting multiple config files")
Signed-off-by: Shivaprasad G Bhat <sbhat@linux.ibm.com>
Signed-off-by: Vaibhav Jain <vaibhav@linux.ibm.com>
---
 ndctl/monitor.c |   29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 3e6a425..54678d6 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -14,6 +14,7 @@ 
 #include <ndctl/ndctl.h>
 #include <ndctl/libndctl.h>
 #include <sys/epoll.h>
+#include <sys/stat.h>
 #define BUF_SIZE 2048
 
 /* reuse the core log helpers for the monitor logger */
@@ -588,7 +589,7 @@  int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx)
 		"ndctl monitor [<options>]",
 		NULL
 	};
-	const struct config configs[] = {
+	struct config configs[] = {
 		CONF_MONITOR(NDCTL_CONF_FILE, parse_monitor_config),
 		CONF_STR("core:bus", &param.bus, NULL),
 		CONF_STR("core:region", &param.region, NULL),
@@ -604,7 +605,9 @@  int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx)
 	const char *prefix = "./", *ndctl_configs;
 	struct ndctl_filter_ctx fctx = { 0 };
 	struct monitor_filter_arg mfa = { 0 };
-	int i, rc;
+	int i, rc = 0;
+	struct stat st;
+	char *path = NULL;
 
 	argc = parse_options_prefix(argc, argv, prefix, options, u, 0);
 	for (i = 0; i < argc; i++) {
@@ -622,14 +625,20 @@  int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx)
 		monitor.ctx.log_priority = LOG_INFO;
 
 	ndctl_configs = ndctl_get_config_path(ctx);
-	if (monitor.configs)
+	if (!monitor.configs && ndctl_configs) {
+		rc = asprintf(&path, "%s/monitor.conf", ndctl_configs);
+		if (rc < 0)
+			goto out;
+
+		if (stat(path, &st) == 0)
+			monitor.configs = path;
+	}
+	if (monitor.configs) {
+		configs[0].key = monitor.configs;
 		rc = parse_configs_prefix(monitor.configs, prefix, configs);
-	else if (ndctl_configs)
-		rc = parse_configs_prefix(ndctl_configs, prefix, configs);
-	else
-		rc = 0;
-	if (rc)
-		goto out;
+		if (rc)
+			goto out;
+	}
 
 	if (monitor.log) {
 		if (strncmp(monitor.log, "./", 2) != 0)
@@ -687,5 +696,7 @@  int cmd_monitor(int argc, const char **argv, struct ndctl_ctx *ctx)
 out:
 	if (monitor.log_file)
 		fclose(monitor.log_file);
+	if (path)
+		free(path);
 	return rc;
 }