From 97f9e38c5b039b4320f30b69ba846e129b07f09d Mon Sep 17 00:00:00 2001
From: Adrien Nader <adrien@notk.org>
Date: Tue, 15 Feb 2022 18:26:07 +0100
Subject: [PATCH] [PATCH iproute2] ip: netns: warn about extra arguments to
 commands.

ip: netns: warn about extra arguments to commands.

A few days ago I mixed two commands and ended up running "ip netns add
foo netns ns1 type veth ...". The error is quite obvious: the first
"netns" should have been "link" but without even a warning message I
didn't check that command first.

Warn if ip-netns subcommands receive more arguments than expected. Print
a warning but continue since it is likely that some script work fine
despite passing extra arguments.
Some subcommands already checked for extra arguments and errored out;
their behaviour is kept.

Signed-off-by: Adrien Nader <adrien@notk.org>
---
 ip/ipnetns.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/ip/ipnetns.c b/ip/ipnetns.c
index 12035349..d298b70a 100644
--- a/ip/ipnetns.c
+++ b/ip/ipnetns.c
@@ -492,6 +492,10 @@ static int netns_list(int argc, char **argv)
 	DIR *dir;
 	int id;
 
+  if (argc > 0) {
+    fprintf(stderr, "Warning: netns list doesn't take arguments\n");
+  }
+
 	dir = opendir(NETNS_RUN_DIR);
 	if (!dir)
 		return 0;
@@ -589,7 +593,7 @@ static int netns_pids(int argc, char **argv)
 		goto out;
 	}
 	if (argc > 1) {
-		fprintf(stderr, "extra arguments specified\n");
+		fprintf(stderr, "Extra arguments specified\n");
 		goto out;
 	}
 
@@ -709,7 +713,7 @@ static int netns_identify(int argc, char **argv)
 	if (argc < 1) {
 		pidstr = "self";
 	} else if (argc > 1) {
-		fprintf(stderr, "extra arguments specified\n");
+		fprintf(stderr, "Extra arguments specified\n");
 		return -1;
 	} else {
 		pidstr = argv[0];
@@ -820,12 +824,19 @@ static int netns_add(int argc, char **argv, bool create)
 			fprintf(stderr, "No netns name specified\n");
 			return -1;
 		}
+		if (argc > 1) {
+			fprintf(stderr, "Warning: extra arguments after the netns name\n");
+		}
 	} else {
 		if (argc < 2) {
 			fprintf(stderr, "No netns name and PID specified\n");
 			return -1;
 		}
 
+		if (argc > 2) {
+			fprintf(stderr, "Warning: extra arguments after the netns name and PID\n");
+		}
+
 		if (get_s32(&pid, argv[1], 0) || !pid) {
 			fprintf(stderr, "Invalid PID: %s\n", argv[1]);
 			return -1;
@@ -979,6 +990,10 @@ static int netns_set(int argc, char **argv)
 		fprintf(stderr, "No nsid specified\n");
 		return -1;
 	}
+	if (argc > 2) {
+		fprintf(stderr, "Warning: extra arguments after the netns name and nsid\n");
+	}
+
 	name = argv[0];
 	/* If a negative nsid is specified the kernel will select the nsid. */
 	if (strcmp(argv[1], "auto") == 0)
@@ -1005,6 +1020,10 @@ static int netns_monitor(int argc, char **argv)
 	struct inotify_event *event;
 	int fd;
 
+	if (argc > 0) {
+		fprintf(stderr, "Warning: netns monitor doesn't take arguments\n");
+	}
+
 	fd = inotify_init();
 	if (fd < 0) {
 		fprintf(stderr, "inotify_init failed: %s\n",
-- 
2.34.1

