Move guts of netns_identify into a standalone function that returns the netns name in a given buffer.
Signed-off-by: David Ahern <d...@cumulusnetworks.com> --- ip/ip_common.h | 1 + ip/ipnetns.c | 47 +++++++++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/ip/ip_common.h b/ip/ip_common.h index ab6a83431fd6..e8642a184f39 100644 --- a/ip/ip_common.h +++ b/ip/ip_common.h @@ -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); diff --git a/ip/ipnetns.c b/ip/ipnetns.c index 8201b94a1620..0b0378ab6560 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -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]; -- 2.1.4