]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: Exec func on each netns
authorVadim Kochan <vadim4j@gmail.com>
Sun, 18 Jan 2015 14:10:17 +0000 (16:10 +0200)
committerStephen Hemminger <shemming@brocade.com>
Thu, 5 Feb 2015 18:28:19 +0000 (10:28 -0800)
Added possibility to run some func on each netns.

Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
include/namespace.h
include/utils.h
lib/namespace.c
lib/utils.c

index 52f7fbd7bb8c07db288dadc90a8235cbae396348..a2ac7dccd0e112efbe2ec4230dccc2604f394c2a 100644 (file)
@@ -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__ */
index e1fe7cfca715daa1cca3f79283662988697de99e..a8817d30a482ca88c386f0d08376763e97010026 100644 (file)
@@ -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__ */
index 65c1e3d7cd523eeadfac1b702a1320ee1531a2b5..c03a103a4f02549b4c9ee9d83d1a8fe1de09147e 100644 (file)
@@ -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;
+}
index f65ceaaf74032dcdf1fbdbbb1d6f11a0356bffe0..efebe189758f23d82a223017c67cc709237b8664 100644 (file)
@@ -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);
+}