From: Vishal Verma <vishal.l.verma@intel.com>
To: linux-nvdimm@lists.01.org
Subject: [ndctl PATCH 4/4] ndctl, monitor: improve error reporting throughout monitor.c
Date: Thu, 19 Jul 2018 16:59:58 -0600 [thread overview]
Message-ID: <20180719225958.6814-5-vishal.l.verma@intel.com> (raw)
In-Reply-To: <20180719225958.6814-1-vishal.l.verma@intel.com>
In several places in the ndctl monitor, we were losing useful error
information (from 'errno' for example), and just returning a simple '1'
or '-1'. Fix these to capture and propagate the correct errors
everywhere.
In the case of notify_dimm_event(), don't error out for failures of
json_object_new_*. Follow the precedent of util/json.c and only add the
object to its parent if the 'new' function was successful.
Cc: QI Fuli <qi.fuli@jp.fujitsu.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
ndctl/monitor.c | 58 +++++++++++++++++++++++----------------------------------
1 file changed, 23 insertions(+), 35 deletions(-)
diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 7fc2aaa..dbad7aa 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -174,45 +174,30 @@ static int notify_dimm_event(struct monitor_dimm *mdimm)
jmsg = json_object_new_object();
if (!jmsg) {
fail("\n");
- return -1;
+ return -ENOMEM;
}
clock_gettime(CLOCK_REALTIME, &ts);
sprintf(timestamp, "%10ld.%09ld", ts.tv_sec, ts.tv_nsec);
jobj = json_object_new_string(timestamp);
- if (!jobj) {
- fail("\n");
- return -1;
- }
- json_object_object_add(jmsg, "timestamp", jobj);
+ if (jobj)
+ json_object_object_add(jmsg, "timestamp", jobj);
jobj = json_object_new_int(getpid());
- if (!jobj) {
- fail("\n");
- return -1;
- }
- json_object_object_add(jmsg, "pid", jobj);
+ if (jobj)
+ json_object_object_add(jmsg, "pid", jobj);
jobj = dimm_event_to_json(mdimm);
- if (!jobj) {
- fail("\n");
- return -1;
- }
- json_object_object_add(jmsg, "event", jobj);
+ if (jobj)
+ json_object_object_add(jmsg, "event", jobj);
jdimm = util_dimm_to_json(mdimm->dimm, 0);
- if (!jdimm) {
- fail("\n");
- return -1;
- }
- json_object_object_add(jmsg, "dimm", jdimm);
+ if (jdimm)
+ json_object_object_add(jmsg, "dimm", jdimm);
jobj = util_dimm_health_to_json(mdimm->dimm);
- if (!jobj) {
- fail("\n");
- return -1;
- }
- json_object_object_add(jdimm, "health", jobj);
+ if (jobj)
+ json_object_object_add(jdimm, "health", jobj);
if (monitor.human)
notice(ctx, "%s\n", json_object_to_json_string_ext(jmsg,
@@ -403,9 +388,12 @@ static int monitor_event(struct ndctl_ctx *ctx,
for (i = 0; i < nfds; i++) {
mdimm = events[i].data.ptr;
if (util_dimm_event_filter(mdimm, monitor.event_flags)) {
- if (notify_dimm_event(mdimm))
+ rc = notify_dimm_event(mdimm);
+ if (rc) {
fail("%s: notify dimm event failed\n",
ndctl_dimm_get_devname(mdimm->dimm));
+ goto out;
+ }
}
rc = pread(mdimm->health_eventfd, &buf, sizeof(buf), 0);
if (rc < 0) {
@@ -601,7 +589,7 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
const char *prefix = "./";
struct util_filter_ctx fctx = { 0 };
struct monitor_filter_arg mfa = { 0 };
- int i;
+ int i, rc;
argc = parse_options_prefix(argc, argv, prefix, options, u, 0);
for (i = 0; i < argc; i++) {
@@ -614,7 +602,8 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
ndctl_set_log_fn((struct ndctl_ctx *)ctx, log_standard);
ndctl_set_log_priority((struct ndctl_ctx *)ctx, LOG_NOTICE);
- if (read_config_file((struct ndctl_ctx *)ctx, &monitor, ¶m))
+ rc = read_config_file((struct ndctl_ctx *)ctx, &monitor, ¶m);
+ if (rc)
goto out;
if (monitor.log) {
@@ -648,18 +637,17 @@ int cmd_monitor(int argc, const char **argv, void *ctx)
mfa.maxfd_dimm = -1;
mfa.flags = 0;
- if (util_filter_walk(ctx, &fctx, ¶m))
+ rc = util_filter_walk(ctx, &fctx, ¶m);
+ if (rc)
goto out;
if (!mfa.num_dimm) {
err((struct ndctl_ctx *)ctx, "no dimms to monitor\n");
+ rc = -ENXIO;
goto out;
}
- if (monitor_event(ctx, &mfa))
- goto out;
-
- return 0;
+ rc = monitor_event(ctx, &mfa);
out:
- return 1;
+ return rc;
}
--
2.14.4
_______________________________________________
Linux-nvdimm mailing list
Linux-nvdimm@lists.01.org
https://lists.01.org/mailman/listinfo/linux-nvdimm
next prev parent reply other threads:[~2018-07-19 23:00 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-19 22:59 [ndctl PATCH 0/4] misc fixups for ndctl-monitor Vishal Verma
2018-07-19 22:59 ` [ndctl PATCH 1/4] ndctl, monitor: Add a config-file section to the man page Vishal Verma
2018-07-19 22:59 ` [ndctl PATCH 2/4] ndctl, monitor: fix memory leak in read_config_file Vishal Verma
2018-07-19 22:59 ` [ndctl PATCH 3/4] ndctl, monitor: Fix memory leak in monitor_event Vishal Verma
2018-07-19 22:59 ` Vishal Verma [this message]
2018-07-19 23:59 ` [ndctl PATCH 0/4] misc fixups for ndctl-monitor Qi, Fuli
2018-07-20 3:26 ` Vishal Verma
2018-07-20 4:10 ` Qi, Fuli
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=20180719225958.6814-5-vishal.l.verma@intel.com \
--to=vishal.l.verma@intel.com \
--cc=linux-nvdimm@lists.01.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).