netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2-next] netns: add JSON support
@ 2018-03-08 16:39 Stephen Hemminger
  2018-03-08 17:55 ` David Ahern
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Hemminger @ 2018-03-08 16:39 UTC (permalink / raw)
  To: netdev; +Cc: Stephen Hemminger, Stephen Hemminger

From: Stephen Hemminger <sthemmin@microsoft.com>

Basic support for JSON output when showing network namespaces.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/ipnetns.c | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 631794b8a341..e06100f4ad2d 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -22,6 +22,7 @@
 #include "list.h"
 #include "ip_common.h"
 #include "namespace.h"
+#include "json_print.h"
 
 static int usage(void)
 {
@@ -293,26 +294,30 @@ int print_nsid(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
 		return -1;
 	}
 
+	open_json_object(NULL);
 	if (n->nlmsg_type == RTM_DELNSID)
-		fprintf(fp, "Deleted ");
+		print_bool(PRINT_ANY, "deleted", "Deleted ", true);
 
 	nsid = rta_getattr_u32(tb[NETNSA_NSID]);
-	fprintf(fp, "nsid %u ", nsid);
+	print_uint(PRINT_ANY, "nsid", "nsid %u ", nsid);
 
 	c = netns_map_get_by_nsid(nsid);
 	if (c != NULL) {
-		fprintf(fp, "(iproute2 netns name: %s)", c->name);
+		print_string(PRINT_ANY, "name",
+			     "(iproute2 netns name: %s)", c->name);
 		netns_map_del(c);
 	}
 
 	/* During 'ip monitor nsid', no chance to have new nsid in cache. */
 	if (c == NULL && n->nlmsg_type == RTM_NEWNSID)
 		if (netns_get_name(nsid, name) == 0) {
-			fprintf(fp, "(iproute2 netns name: %s)", name);
+			print_string(PRINT_ANY, "name",
+				     "(iproute2 netns name: %s)", name);
 			netns_map_add(nsid, name);
 		}
 
-	fprintf(fp, "\n");
+	print_string(PRINT_FP, NULL, "\n", NULL);
+	close_json_object();
 	fflush(fp);
 	return 0;
 }
@@ -329,10 +334,14 @@ static int netns_list_id(int argc, char **argv)
 		perror("Cannot send dump request");
 		exit(1);
 	}
+
+	new_json_obj(json);
 	if (rtnl_dump_filter(&rth, print_nsid, stdout) < 0) {
+		delete_json_obj();
 		fprintf(stderr, "Dump terminated\n");
 		exit(1);
 	}
+	delete_json_obj();
 	return 0;
 }
 
@@ -346,20 +355,27 @@ static int netns_list(int argc, char **argv)
 	if (!dir)
 		return 0;
 
+	new_json_obj(json);
 	while ((entry = readdir(dir)) != NULL) {
 		if (strcmp(entry->d_name, ".") == 0)
 			continue;
 		if (strcmp(entry->d_name, "..") == 0)
 			continue;
-		printf("%s", entry->d_name);
+
+		open_json_object(NULL);
+		print_string(PRINT_ANY, "name",
+			     "%s", entry->d_name);
 		if (ipnetns_have_nsid()) {
 			id = get_netnsid_from_name(entry->d_name);
 			if (id >= 0)
-				printf(" (id: %d)", id);
+				print_uint(PRINT_ANY, "id",
+					   " (id: %d)", id);
 		}
-		printf("\n");
+		print_string(PRINT_FP, NULL, "\n", NULL);
+		close_json_object();
 	}
 	closedir(dir);
+	delete_json_obj();
 	return 0;
 }
 
-- 
2.16.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH iproute2-next] netns: add JSON support
  2018-03-08 16:39 [PATCH iproute2-next] netns: add JSON support Stephen Hemminger
@ 2018-03-08 17:55 ` David Ahern
  0 siblings, 0 replies; 2+ messages in thread
From: David Ahern @ 2018-03-08 17:55 UTC (permalink / raw)
  To: Stephen Hemminger, netdev; +Cc: Stephen Hemminger

On 3/8/18 9:39 AM, Stephen Hemminger wrote:
> From: Stephen Hemminger <sthemmin@microsoft.com>
> 
> Basic support for JSON output when showing network namespaces.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  ip/ipnetns.c | 32 ++++++++++++++++++++++++--------
>  1 file changed, 24 insertions(+), 8 deletions(-)
> 

Applied to iproute2-next.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-03-08 17:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-08 16:39 [PATCH iproute2-next] netns: add JSON support Stephen Hemminger
2018-03-08 17:55 ` David Ahern

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).