ip netns: Delete all netns
Allow delete all namespace names by:
$ ip -all netns del
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 59a9321..e4038ea 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -23,7 +23,7 @@
{
fprintf(stderr, "Usage: ip netns list\n");
fprintf(stderr, " ip netns add NAME\n");
- fprintf(stderr, " ip netns delete NAME\n");
+ fprintf(stderr, " ip [-all] netns delete [NAME]\n");
fprintf(stderr, " ip netns identify [PID]\n");
fprintf(stderr, " ip netns pids NAME\n");
fprintf(stderr, " ip [-all] netns exec [NAME] cmd ...\n");
@@ -259,18 +259,11 @@
}
-static int netns_delete(int argc, char **argv)
+static int on_netns_del(char *nsname, void *arg)
{
- const char *name;
char netns_path[MAXPATHLEN];
- if (argc < 1) {
- fprintf(stderr, "No netns name specified\n");
- return -1;
- }
-
- name = argv[0];
- snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
+ snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, nsname);
umount2(netns_path, MNT_DETACH);
if (unlink(netns_path) < 0) {
fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
@@ -280,6 +273,19 @@
return 0;
}
+static int netns_delete(int argc, char **argv)
+{
+ if (argc < 1 && !do_all) {
+ fprintf(stderr, "No netns name specified\n");
+ return -1;
+ }
+
+ if (do_all)
+ return netns_foreach(on_netns_del, NULL);
+
+ return on_netns_del(argv[0], NULL);
+}
+
static int create_netns_dir(void)
{
/* Create the base netns directory if it doesn't exist */