netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Serhey Popovych <serhe.popovych@gmail.com>
To: netdev@vger.kernel.org
Subject: [PATCH iproute2-next 1/4] utils: Introduce do_each_proc_net_dev() helper
Date: Wed, 31 Jan 2018 21:49:46 +0200	[thread overview]
Message-ID: <1517428189-29279-2-git-send-email-serhe.popovych@gmail.com> (raw)
In-Reply-To: <1517428189-29279-1-git-send-email-serhe.popovych@gmail.com>

It is natural for ip(8) tool to access /proc/net/dev for various
information and there at least two places implementing same iteration
over lines in this file: iptunnel.c and ip6tunnel.c.

To unify interface and avoid code duplication introduce helper that
reads line from /proc/net/dev, passes it to given callback function
that may return following three states to control do_each_proc_net_dev()
behaviour:

  o PND_ERROR - stop reading lines and return -1
  o PND_OK    - stop reading lines and return 0
  o PND_NEXT  - continue by reading next line.

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
---
 include/utils.h |   10 ++++++++++
 lib/utils.c     |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/include/utils.h b/include/utils.h
index 0394268..819b0ca 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -285,6 +285,16 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
 int do_each_netns(int (*func)(char *nsname, void *arg), void *arg,
 		bool show_label);
 
+typedef enum {
+	PND_ERROR = -1,
+	PND_OK    = 0,
+	PND_NEXT  = 1
+} pnd_result_t;
+
+typedef pnd_result_t (pnd_func_t)(char *name, char *stats, void *arg);
+
+int do_each_proc_net_dev(pnd_func_t f, void *arg);
+
 char *int_to_str(int val, char *buf);
 int get_guid(__u64 *guid, const char *arg);
 int get_real_family(int rtm_type, int rtm_family);
diff --git a/lib/utils.c b/lib/utils.c
index 8e15625..4f142d8 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1375,6 +1375,57 @@ int do_each_netns(int (*func)(char *nsname, void *arg), void *arg,
 	return netns_foreach(on_netns, &nsf);
 }
 
+int do_each_proc_net_dev(pnd_func_t f, void *arg)
+{
+	static const char pnd[] = "/proc/net/dev";
+	char buf[512];
+	int err = -1;
+	FILE *fp;
+
+	fp = fopen(pnd, "r");
+	if (!fp) {
+		perror("fopen");
+		return -1;
+	}
+
+	/* skip two lines at the begenning of the file */
+	if (!fgets(buf, sizeof(buf), fp) ||
+	    !fgets(buf, sizeof(buf), fp)) {
+		perror("fgets");
+		goto end;
+	}
+
+	while (fgets(buf, sizeof(buf), fp)) {
+		char *stats, *name = NULL;
+		pnd_result_t result;
+
+		buf[sizeof(buf) - 1] = '\0';
+		stats = strchr(buf, ':');
+		if (stats) {
+			*stats++ = '\0';
+			if (!check_ifname(buf))
+				name = buf;
+		}
+		if (!name) {
+			fprintf(stderr,
+				"Wrong format for \"%s\". Giving up.\n", pnd);
+			goto end;
+		}
+
+		result = f(name, stats, arg);
+		if (result == PND_NEXT)
+			continue;
+		if (result == PND_OK)
+			break;
+		goto end;
+	}
+	if (feof(fp))
+		err = 0;
+end:
+	fclose(fp);
+	return err;
+}
+
 char *int_to_str(int val, char *buf)
 {
 	sprintf(buf, "%d", val);
-- 
1.7.10.4

  reply	other threads:[~2018-01-31 19:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-31 19:49 [PATCH iproute2-next 0/4] ip: Introduce and use helper to read /proc/net/dev Serhey Popovych
2018-01-31 19:49 ` Serhey Popovych [this message]
2018-01-31 19:49 ` [PATCH iproute2-next 2/4] iptunnel: Use do_each_proc_net_dev() Serhey Popovych
2018-01-31 19:49 ` [PATCH iproute2-next 3/4] ip6tunnel: " Serhey Popovych
2018-01-31 19:49 ` [PATCH iproute2-next 4/4] tuntap: " Serhey Popovych
2018-01-31 23:04 ` [PATCH iproute2-next 0/4] ip: Introduce and use helper to read /proc/net/dev Stephen Hemminger
2018-02-01  3:17   ` David Ahern
2018-02-01 19:49   ` Serhey Popovych

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=1517428189-29279-2-git-send-email-serhe.popovych@gmail.com \
    --to=serhe.popovych@gmail.com \
    --cc=netdev@vger.kernel.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).