netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Denis Kirjanov <kirjanov@gmail.com>
To: stephen@networkplumber.org
Cc: netdev@vger.kernel.org, Denis Kirjanov <denis.kirjanov@suse.com>,
	Denis Kirjanov <dkirjanov@suse.de>
Subject: [PATCH iproute2] ifstat: make load_info() more verbose on error
Date: Fri, 26 Jan 2024 05:08:55 -0500	[thread overview]
Message-ID: <20240126100855.1004-1-dkirjanov@suse.de> (raw)

From: Denis Kirjanov <denis.kirjanov@suse.com>

convert frprintf calls to perror() so the caller
can see the reason of an error

Signed-off-by: Denis Kirjanov <dkirjanov@suse.de>
---
 misc/ifstat.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/misc/ifstat.c b/misc/ifstat.c
index f6f9ba50..721f4914 100644
--- a/misc/ifstat.c
+++ b/misc/ifstat.c
@@ -117,16 +117,20 @@ static int get_nlmsg_extended(struct nlmsghdr *m, void *arg)
 		return 0;
 
 	len -= NLMSG_LENGTH(sizeof(*ifsm));
-	if (len < 0)
+	if (len < 0) {
+		errno = EINVAL;
 		return -1;
+	}
 
 	parse_rtattr(tb, IFLA_STATS_MAX, IFLA_STATS_RTA(ifsm), len);
 	if (tb[filter_type] == NULL)
 		return 0;
 
 	n = malloc(sizeof(*n));
-	if (!n)
-		abort();
+	if (!n) {
+		errno = ENOMEM;
+		return -1;
+	}
 
 	n->ifindex = ifsm->ifindex;
 	n->name = strdup(ll_index_to_name(ifsm->ifindex));
@@ -161,8 +165,10 @@ static int get_nlmsg(struct nlmsghdr *m, void *arg)
 		return 0;
 
 	len -= NLMSG_LENGTH(sizeof(*ifi));
-	if (len < 0)
+	if (len < 0) {
+		errno = EINVAL;
 		return -1;
+	}
 
 	if (!(ifi->ifi_flags&IFF_UP))
 		return 0;
@@ -172,8 +178,10 @@ static int get_nlmsg(struct nlmsghdr *m, void *arg)
 		return 0;
 
 	n = malloc(sizeof(*n));
-	if (!n)
-		abort();
+	if (!n) {
+		errno = ENOMEM;
+		return -1;
+	}
 	n->ifindex = ifi->ifi_index;
 	n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
 	memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
@@ -204,7 +212,7 @@ static void load_info(void)
 		}
 
 		if (rtnl_dump_filter(&rth, get_nlmsg_extended, NULL) < 0) {
-			fprintf(stderr, "Dump terminated\n");
+			perror("Dump terminated\n");
 			exit(1);
 		}
 	} else {
@@ -214,7 +222,7 @@ static void load_info(void)
 		}
 
 		if (rtnl_dump_filter(&rth, get_nlmsg, NULL) < 0) {
-			fprintf(stderr, "Dump terminated\n");
+			perror("Dump terminated\n");
 			exit(1);
 		}
 	}
-- 
2.30.2


             reply	other threads:[~2024-01-26 10:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-26 10:08 Denis Kirjanov [this message]
2024-01-30 20:50 ` [PATCH iproute2] ifstat: make load_info() more verbose on error patchwork-bot+netdevbpf

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=20240126100855.1004-1-dkirjanov@suse.de \
    --to=kirjanov@gmail.com \
    --cc=denis.kirjanov@suse.com \
    --cc=dkirjanov@suse.de \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.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).