]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
netns: add JSON support
authorStephen Hemminger <sthemmin@microsoft.com>
Thu, 8 Mar 2018 16:39:10 +0000 (08:39 -0800)
committerDavid Ahern <dsahern@gmail.com>
Thu, 8 Mar 2018 17:53:11 +0000 (09:53 -0800)
Basic support for JSON output when showing network namespaces.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
ip/ipnetns.c

index 631794b8a3412a4b3de76d244a1e3908ff975c34..e06100f4ad2d2896705c76ff43167965d8c09975 100644 (file)
@@ -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;
 }