From: David Ahern Add netns_get_fd_pid - verifies string is an integer, then returns an fd to /proc/$pid/ns/net. Signed-off-by: David Ahern --- include/namespace.h | 1 + lib/namespace.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/namespace.h b/include/namespace.h index 98f4af59..6a6fa438 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -52,6 +52,7 @@ static inline int setns(int fd, int nstype) int netns_switch(char *netns); int netns_get_fd(const char *netns); +int netns_get_fd_pid(const char *pidstr); int netns_foreach(int (*func)(char *nsname, void *arg), void *arg); struct netns_func { diff --git a/lib/namespace.c b/lib/namespace.c index 74b7e7ca..f391c694 100644 --- a/lib/namespace.c +++ b/lib/namespace.c @@ -114,6 +114,19 @@ int netns_get_fd(const char *name) return open(path, O_RDONLY); } +int netns_get_fd_pid(const char *pidstr) +{ + char path[PATH_MAX]; + int pid; + + /* make sure it is an integer */ + if (get_integer(&pid, pidstr, 0) < 0) + return -1; + + snprintf(path, sizeof(path), "/proc/%s/ns/net", pidstr); + return open(path, O_RDONLY); +} + int netns_foreach(int (*func)(char *nsname, void *arg), void *arg) { DIR *dir; -- 2.50.1 (Apple Git-155)