All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shivaprasad G Bhat <sbhat@linux.ibm.com>
To: nvdimm@lists.linux.dev
Subject: [ndctl PATCH] monitor: Fix the monitor config file parsing
Date: Thu, 17 Mar 2022 15:02:41 +0530	[thread overview]
Message-ID: <164750955519.2000193.16903542741359443926.stgit@LAPTOP-TBQTPII8> (raw)

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 --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;
 }



                 reply	other threads:[~2022-03-17  9:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=164750955519.2000193.16903542741359443926.stgit@LAPTOP-TBQTPII8 \
    --to=sbhat@linux.ibm.com \
    --cc=nvdimm@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.