All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicolas Dichtel <nicolas.dichtel@6wind.com>
To: shemminger@vyatta.com
Cc: netdev@vger.kernel.org, vadim4j@gmail.com, jbenc@redhat.com,
	Nicolas Dichtel <nicolas.dichtel@6wind.com>
Subject: [PATCH iproute2 3/3] ipnetns: add a runtime check for RTM_GETNSID support
Date: Mon, 13 Apr 2015 10:23:05 +0200	[thread overview]
Message-ID: <1428913385-4331-3-git-send-email-nicolas.dichtel@6wind.com> (raw)
In-Reply-To: <1428913385-4331-1-git-send-email-nicolas.dichtel@6wind.com>

The goal of this patch is to test during the runtime if the command RTM_GETNSID
is supported by the kernel.

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 ip/ipnetns.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 53 insertions(+), 3 deletions(-)

diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 5a213dcf46cd..12f6b90de89a 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -34,6 +34,54 @@ static int usage(void)
 	exit(-1);
 }
 
+static int have_rtnl_getnsid = -1;
+
+static int ipnetns_accept_msg(const struct sockaddr_nl *who,
+			      struct nlmsghdr *n, void *arg)
+{
+	struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
+
+	if (n->nlmsg_type == NLMSG_ERROR &&
+	    (err->error == -EOPNOTSUPP || err->error == -EINVAL))
+		have_rtnl_getnsid = 0;
+	else
+		have_rtnl_getnsid = 1;
+	return -1;
+}
+
+static int ipnetns_have_nsid(void)
+{
+	struct {
+		struct nlmsghdr n;
+		struct rtgenmsg g;
+		char            buf[1024];
+	} req;
+	int fd;
+
+	if (have_rtnl_getnsid < 0) {
+		memset(&req, 0, sizeof(req));
+		req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
+		req.n.nlmsg_flags = NLM_F_REQUEST;
+		req.n.nlmsg_type = RTM_GETNSID;
+		req.g.rtgen_family = AF_UNSPEC;
+
+		fd = open("/proc/self/ns/net", O_RDONLY);
+		if (fd < 0)
+			return fd;
+
+		addattr32(&req.n, 1024, NETNSA_FD, fd);
+
+		if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
+			perror("request send failed");
+			exit(1);
+		}
+		rtnl_listen(&rth, ipnetns_accept_msg, NULL);
+		close(fd);
+	}
+
+	return have_rtnl_getnsid;
+}
+
 static int get_netnsid_from_name(const char *name)
 {
 	struct {
@@ -95,9 +143,11 @@ static int netns_list(int argc, char **argv)
 		if (strcmp(entry->d_name, "..") == 0)
 			continue;
 		printf("%s", entry->d_name);
-		id = get_netnsid_from_name(entry->d_name);
-		if (id >= 0)
-			printf(" (id: %d)", id);
+		if (ipnetns_have_nsid()) {
+			id = get_netnsid_from_name(entry->d_name);
+			if (id >= 0)
+				printf(" (id: %d)", id);
+		}
 		printf("\n");
 	}
 	closedir(dir);
-- 
2.2.2

  parent reply	other threads:[~2015-04-13  8:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-08 19:42 [PATCH iproute2] configure: add missing INCLUDE to netnsid detection Jiri Benc
2015-04-08 19:40 ` Vadim Kochan
2015-04-10 16:17   ` Nicolas Dichtel
2015-04-13  8:23     ` [PATCH iproute2 1/3] Revert "configure: add missing INCLUDE to netnsid detection" Nicolas Dichtel
2015-04-13  8:23       ` [PATCH iproute2 2/3] Revert "ip netns: Fix rtnl error while print netns list" Nicolas Dichtel
2015-04-13  8:23       ` Nicolas Dichtel [this message]
2015-04-13  8:34         ` [PATCH v2 iproute2 1/3] Revert "configure: add missing INCLUDE to netnsid detection" Nicolas Dichtel
2015-04-13  8:34           ` [PATCH v2 iproute2 2/3] Revert "ip netns: Fix rtnl error while print netns list" Nicolas Dichtel
2015-04-13  8:34           ` [PATCH v2 iproute2 3/3] ipnetns: add a runtime check for RTM_GETNSID support Nicolas Dichtel
     [not found]       ` <f5aa0068662f470e988a01e3b92450f1@HQ1WP-EXMB12.corp.brocade.com>
2015-04-13 15:53         ` [PATCH " Stephen Hemminger
2015-04-10 20:25 ` [PATCH iproute2] configure: add missing INCLUDE to netnsid detection Stephen Hemminger

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=1428913385-4331-3-git-send-email-nicolas.dichtel@6wind.com \
    --to=nicolas.dichtel@6wind.com \
    --cc=jbenc@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=shemminger@vyatta.com \
    --cc=vadim4j@gmail.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 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.