netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrey Vagin <avagin@openvz.org>
To: netdev@vger.kernel.org
Cc: Pavel Emelyanov <xemul@parallels.com>,
	Andrey Vagin <avagin@openvz.org>,
	Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 1/4] ss: handle socket diag request in a separate function
Date: Thu, 21 Mar 2013 13:33:23 +0400	[thread overview]
Message-ID: <1363858406-1489-2-git-send-email-avagin@openvz.org> (raw)
In-Reply-To: <1363858406-1489-1-git-send-email-avagin@openvz.org>

It will be reused to show netlink sockets

Cc: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Andrey Vagin <avagin@openvz.org>
---
 misc/ss.c | 49 +++++++++++++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 20 deletions(-)

diff --git a/misc/ss.c b/misc/ss.c
index da9d32c..bde344a 100644
--- a/misc/ss.c
+++ b/misc/ss.c
@@ -2206,31 +2206,17 @@ static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f)
 	return 0;
 }
 
-static int unix_show_netlink(struct filter *f, FILE *dump_fp)
+static int handle_netlink_request(struct filter *f, FILE *dump_fp,
+				  struct nlmsghdr *req, size_t size,
+				  int (* show_one_sock)(struct nlmsghdr *nlh, struct filter *f))
 {
 	int fd;
-	struct {
-		struct nlmsghdr nlh;
-		struct unix_diag_req r;
-	} req;
 	char	buf[8192];
 
 	if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
 		return -1;
 
-	memset(&req, 0, sizeof(req));
-	req.nlh.nlmsg_len = sizeof(req);
-	req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
-	req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
-	req.nlh.nlmsg_seq = 123456;
-
-	req.r.sdiag_family = AF_UNIX;
-	req.r.udiag_states = f->states;
-	req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
-	if (show_mem)
-		req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
-
-	if (send(fd, &req, sizeof(req), 0) < 0) {
+	if (send(fd, req, size, 0) < 0) {
 		close(fd);
 		return -1;
 	}
@@ -2275,13 +2261,13 @@ static int unix_show_netlink(struct filter *f, FILE *dump_fp)
 				} else {
 					errno = -err->error;
 					if (errno != ENOENT)
-						fprintf(stderr, "UDIAG answers %d\n", errno);
+						fprintf(stderr, "DIAG answers %d\n", errno);
 				}
 				close(fd);
 				return -1;
 			}
 			if (!dump_fp) {
-				err = unix_show_sock(h, f);
+				err = show_one_sock(h, f);
 				if (err < 0) {
 					close(fd);
 					return err;
@@ -2303,6 +2289,29 @@ close_it:
 	return 0;
 }
 
+static int unix_show_netlink(struct filter *f, FILE *dump_fp)
+{
+	struct {
+		struct nlmsghdr nlh;
+		struct unix_diag_req r;
+	} req;
+
+	memset(&req, 0, sizeof(req));
+	req.nlh.nlmsg_len = sizeof(req);
+	req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
+	req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
+	req.nlh.nlmsg_seq = 123456;
+
+	req.r.sdiag_family = AF_UNIX;
+	req.r.udiag_states = f->states;
+	req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
+	if (show_mem)
+		req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
+
+	return handle_netlink_request(f, dump_fp, &req.nlh,
+					sizeof(req), unix_show_sock);
+}
+
 static int unix_show(struct filter *f)
 {
 	FILE *fp;
-- 
1.7.11.7

  reply	other threads:[~2013-03-21  9:35 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-21  9:21 [PATCH 0/2] netlink: implement socket diag for netlink sockets Andrey Vagin
2013-03-21  9:21 ` [PATCH 1/2] net: prepare netlink code for netlink diag Andrey Vagin
2013-03-21  9:21 ` [PATCH 2/2] netlink: Diag core and basic socket info dumping Andrey Vagin
2013-03-21 11:46   ` Pavel Emelyanov
2013-03-21 12:52   ` Thomas Graf
2013-03-21 14:31     ` Andrew Vagin
2013-03-21 14:41       ` Thomas Graf
2013-03-21  9:33 ` [PATCH 0/4] ss: Get netlink sockets info via sock-diag Andrey Vagin
2013-03-21  9:33   ` Andrey Vagin [this message]
2013-03-21  9:33   ` [PATCH 2/4] ss: create a function to print info about netlink sockets Andrey Vagin
2013-03-21  9:33   ` [PATCH 3/4] ss: show destination address for " Andrey Vagin
2013-03-21  9:33   ` [PATCH 4/4] ss: Get netlink sockets info via sock-diag Andrey Vagin
2013-03-21 17:00   ` [PATCH 0/4] " Stephen Hemminger
  -- strict thread matches above, loose matches on Subject: below --
2013-06-05  8:41 [PATCH 0/4] ss: Get netlink sockets info via sock-diag (v2) Andrey Vagin
2013-06-05  8:41 ` [PATCH 1/4] ss: handle socket diag request in a separate function Andrey Vagin

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=1363858406-1489-2-git-send-email-avagin@openvz.org \
    --to=avagin@openvz.org \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=xemul@parallels.com \
    /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).