From: Vadim Kochan <vadim4j@gmail.com>
To: netdev@vger.kernel.org
Cc: Vadim Kochan <vadim4j@gmail.com>
Subject: [PATCH iproute2 v2 1/3] lib: Exec func on each netns
Date: Sun, 18 Jan 2015 16:10:17 +0200 [thread overview]
Message-ID: <1421590219-19365-2-git-send-email-vadim4j@gmail.com> (raw)
In-Reply-To: <1421590219-19365-1-git-send-email-vadim4j@gmail.com>
From: Vadim Kochan <vadim4j@gmail.com>
Added possibility to run some func on each netns.
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
---
include/namespace.h | 6 ++++++
include/utils.h | 4 ++++
lib/namespace.c | 22 ++++++++++++++++++++++
lib/utils.c | 28 ++++++++++++++++++++++++++++
4 files changed, 60 insertions(+)
diff --git a/include/namespace.h b/include/namespace.h
index b8c5cad..26898b0 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -43,5 +43,11 @@ static 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 e1fe7cf..a8817d3 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 65c1e3d..c03a103 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 f65ceaa..efebe18 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);
+}
--
2.1.3
next prev parent reply other threads:[~2015-01-18 14:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-01-18 14:10 [PATCH iproute2 v2 0/3] ip netns: Run over all netns Vadim Kochan
2015-01-18 14:10 ` Vadim Kochan [this message]
2015-01-18 14:10 ` [PATCH iproute2 v2 2/3] ip netns: Allow exec on each netns Vadim Kochan
2015-01-18 14:10 ` [PATCH iproute2 v2 3/3] ip netns: Delete all netns Vadim Kochan
2015-02-05 18:29 ` [PATCH iproute2 v2 0/3] ip netns: Run over " Stephen Hemminger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1421590219-19365-2-git-send-email-vadim4j@gmail.com \
--to=vadim4j@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).