diff options
author | Vadim Kochan <vadim4j@gmail.com> | 2015-01-18 16:10:17 +0200 |
---|---|---|
committer | Stephen Hemminger <shemming@brocade.com> | 2015-02-05 10:28:19 -0800 |
commit | e998e118ddc3a0ab1b325ad7ed4abe59b83e684a (patch) | |
tree | 31b1bd53f6e6a0d33e2669b741a552585d907a14 | |
parent | 4c7d75de95f5f20ab106460cfe86218b2dd87a77 (diff) |
lib: Exec func on each netns
Added possibility to run some func on each netns.
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
-rw-r--r-- | include/namespace.h | 6 | ||||
-rw-r--r-- | include/utils.h | 4 | ||||
-rw-r--r-- | lib/namespace.c | 22 | ||||
-rw-r--r-- | lib/utils.c | 28 |
4 files changed, 60 insertions, 0 deletions
diff --git a/include/namespace.h b/include/namespace.h index 52f7fbd7..a2ac7dcc 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -44,5 +44,11 @@ static inline int setns(int fd, int nstype) extern int netns_switch(char *netns); extern int netns_get_fd(const char *netns); +extern int netns_foreach(int (*func)(char *nsname, void *arg), void *arg); + +struct netns_func { + int (*func)(char *nsname, void *arg); + void *arg; +}; #endif /* __NAMESPACE_H__ */ diff --git a/include/utils.h b/include/utils.h index e1fe7cfc..a8817d30 100644 --- a/include/utils.h +++ b/include/utils.h @@ -5,6 +5,7 @@ #include <asm/types.h> #include <resolv.h> #include <stdlib.h> +#include <stdbool.h> #include "libnetlink.h" #include "ll_map.h" @@ -162,4 +163,7 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req, char **name, char **type, char **link, char **dev, int *group, int *index); +extern int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, + bool show_label); + #endif /* __UTILS_H__ */ diff --git a/lib/namespace.c b/lib/namespace.c index 65c1e3d7..c03a103a 100644 --- a/lib/namespace.c +++ b/lib/namespace.c @@ -99,3 +99,25 @@ int netns_get_fd(const char *name) } return open(path, O_RDONLY); } + +int netns_foreach(int (*func)(char *nsname, void *arg), void *arg) +{ + DIR *dir; + struct dirent *entry; + + dir = opendir(NETNS_RUN_DIR); + if (!dir) + return -1; + + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") == 0) + continue; + if (strcmp(entry->d_name, "..") == 0) + continue; + if (func(entry->d_name, arg)) + break; + } + + closedir(dir); + return 0; +} diff --git a/lib/utils.c b/lib/utils.c index f65ceaaf..efebe189 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -31,6 +31,7 @@ #include "utils.h" +#include "namespace.h" int timestamp_short = 0; @@ -878,3 +879,30 @@ void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n) tstr[strlen(tstr)-1] = 0; fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs); } + +static int on_netns(char *nsname, void *arg) +{ + struct netns_func *f = arg; + + if (netns_switch(nsname)) + return -1; + + return f->func(nsname, f->arg); +} + +static int on_netns_label(char *nsname, void *arg) +{ + printf("\nnetns: %s\n", nsname); + return on_netns(nsname, arg); +} + +int do_each_netns(int (*func)(char *nsname, void *arg), void *arg, + bool show_label) +{ + struct netns_func nsf = { .func = func, .arg = arg }; + + if (show_label) + return netns_foreach(on_netns_label, &nsf); + + return netns_foreach(on_netns, &nsf); +} |