]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip netns: refactor netns_identify
authorDavid Ahern <dsa@cumulusnetworks.com>
Thu, 16 Feb 2017 16:58:56 +0000 (08:58 -0800)
committerStephen Hemminger <sthemmin@microsoft.com>
Fri, 17 Feb 2017 23:33:24 +0000 (15:33 -0800)
Move guts of netns_identify into a standalone function that returns
the netns name in a given buffer.

Signed-off-by: David Ahern <dsa@cumulusnetworks.com>
ip/ip_common.h
ip/ipnetns.c

index ab6a83431fd68939d3258928c0628c16fa674a49..e8642a184f393b438f4b1ae756b3a2d03f709a5d 100644 (file)
@@ -59,6 +59,7 @@ int do_ipnetconf(int argc, char **argv);
 int do_iptoken(int argc, char **argv);
 int do_ipvrf(int argc, char **argv);
 void vrf_reset(void);
+int netns_identify_pid(const char *pidstr, char *name, int len);
 
 int iplink_get(unsigned int flags, char *name, __u32 filt_mask);
 
index 8201b94a1620991bfac511d1238771f8c495b023..0b0378ab6560c6152f17909a2eea95127f2f02c8 100644 (file)
@@ -468,28 +468,15 @@ static int netns_pids(int argc, char **argv)
 
 }
 
-static int netns_identify(int argc, char **argv)
+int netns_identify_pid(const char *pidstr, char *name, int len)
 {
-       const char *pidstr;
        char net_path[PATH_MAX];
        int netns;
        struct stat netst;
        DIR *dir;
        struct dirent *entry;
 
-       if (argc < 1) {
-               pidstr = "self";
-       } else if (argc > 1) {
-               fprintf(stderr, "extra arguments specified\n");
-               return -1;
-       } else {
-               pidstr = argv[0];
-               if (!is_pid(pidstr)) {
-                       fprintf(stderr, "Specified string '%s' is not a pid\n",
-                                       pidstr);
-                       return -1;
-               }
-       }
+       name[0] = '\0';
 
        snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
        netns = open(net_path, O_RDONLY);
@@ -531,7 +518,8 @@ static int netns_identify(int argc, char **argv)
 
                if ((st.st_dev == netst.st_dev) &&
                    (st.st_ino == netst.st_ino)) {
-                       printf("%s\n", entry->d_name);
+                       strncpy(name, entry->d_name, len - 1);
+                       name[len - 1] = '\0';
                }
        }
        closedir(dir);
@@ -539,6 +527,33 @@ static int netns_identify(int argc, char **argv)
 
 }
 
+static int netns_identify(int argc, char **argv)
+{
+       const char *pidstr;
+       char name[256];
+       int rc;
+
+       if (argc < 1) {
+               pidstr = "self";
+       } else if (argc > 1) {
+               fprintf(stderr, "extra arguments specified\n");
+               return -1;
+       } else {
+               pidstr = argv[0];
+               if (!is_pid(pidstr)) {
+                       fprintf(stderr, "Specified string '%s' is not a pid\n",
+                                       pidstr);
+                       return -1;
+               }
+       }
+
+       rc = netns_identify_pid(pidstr, name, sizeof(name));
+       if (!rc)
+               printf("%s\n", name);
+
+       return rc;
+}
+
 static int on_netns_del(char *nsname, void *arg)
 {
        char netns_path[PATH_MAX];