Blame SOURCES/0013-mdmon-Stop-parsing-duplicate-options.patch

c0f891
From 1066ab83dbe9a4cc20f7db44a40aa2cbb9d5eed6 Mon Sep 17 00:00:00 2001
c0f891
From: Lukasz Florczak <lukasz.florczak@linux.intel.com>
c0f891
Date: Fri, 13 May 2022 09:19:42 +0200
37f2b0
Subject: [PATCH 13/83] mdmon: Stop parsing duplicate options
c0f891
c0f891
Introduce new function is_duplicate_opt() to check if given option
c0f891
was already used and prevent setting it again along with an error
c0f891
message.
c0f891
c0f891
Move parsing above in_initrd() check to be able to detect --offroot
c0f891
option duplicates.
c0f891
c0f891
Now help option is executed after parsing to prevent executing commands
c0f891
like: 'mdmon --help --ndlksnlksajndfjksndafasj'.
c0f891
c0f891
Signed-off-by: Lukasz Florczak <lukasz.florczak@linux.intel.com>
c0f891
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
c0f891
---
c0f891
 mdmon.c | 44 +++++++++++++++++++++++++++++++++++---------
c0f891
 1 file changed, 35 insertions(+), 9 deletions(-)
c0f891
c0f891
diff --git a/mdmon.c b/mdmon.c
c0f891
index 5570574b..c057da63 100644
c0f891
--- a/mdmon.c
c0f891
+++ b/mdmon.c
c0f891
@@ -288,6 +288,15 @@ void usage(void)
c0f891
 	exit(2);
c0f891
 }
c0f891
 
c0f891
+static bool is_duplicate_opt(const int opt, const int set_val, const char *long_name)
c0f891
+{
c0f891
+	if (opt == set_val) {
c0f891
+		pr_err("--%s option duplicated!\n", long_name);
c0f891
+		return true;
c0f891
+	}
c0f891
+	return false;
c0f891
+}
c0f891
+
c0f891
 static int mdmon(char *devnm, int must_fork, int takeover);
c0f891
 
c0f891
 int main(int argc, char *argv[])
c0f891
@@ -299,6 +308,7 @@ int main(int argc, char *argv[])
c0f891
 	int all = 0;
c0f891
 	int takeover = 0;
c0f891
 	int dofork = 1;
c0f891
+	bool help = false;
c0f891
 	static struct option options[] = {
c0f891
 		{"all", 0, NULL, 'a'},
c0f891
 		{"takeover", 0, NULL, 't'},
c0f891
@@ -308,37 +318,50 @@ int main(int argc, char *argv[])
c0f891
 		{NULL, 0, NULL, 0}
c0f891
 	};
c0f891
 
c0f891
-	if (in_initrd()) {
c0f891
-		/*
c0f891
-		 * set first char of argv[0] to @. This is used by
c0f891
-		 * systemd to signal that the task was launched from
c0f891
-		 * initrd/initramfs and should be preserved during shutdown
c0f891
-		 */
c0f891
-		argv[0][0] = '@';
c0f891
-	}
c0f891
-
c0f891
 	while ((opt = getopt_long(argc, argv, "thaF", options, NULL)) != -1) {
c0f891
 		switch (opt) {
c0f891
 		case 'a':
c0f891
+			if (is_duplicate_opt(all, 1, "all"))
c0f891
+				exit(1);
c0f891
 			container_name = argv[optind-1];
c0f891
 			all = 1;
c0f891
 			break;
c0f891
 		case 't':
c0f891
+			if (is_duplicate_opt(takeover, 1, "takeover"))
c0f891
+				exit(1);
c0f891
 			takeover = 1;
c0f891
 			break;
c0f891
 		case 'F':
c0f891
+			if (is_duplicate_opt(dofork, 0, "foreground"))
c0f891
+				exit(1);
c0f891
 			dofork = 0;
c0f891
 			break;
c0f891
 		case OffRootOpt:
c0f891
+			if (is_duplicate_opt(argv[0][0], '@', "offroot"))
c0f891
+				exit(1);
c0f891
 			argv[0][0] = '@';
c0f891
 			break;
c0f891
 		case 'h':
c0f891
+			if (is_duplicate_opt(help, true, "help"))
c0f891
+				exit(1);
c0f891
+			help = true;
c0f891
+			break;
c0f891
 		default:
c0f891
 			usage();
c0f891
 			break;
c0f891
 		}
c0f891
 	}
c0f891
 
c0f891
+
c0f891
+	if (in_initrd()) {
c0f891
+		/*
c0f891
+		 * set first char of argv[0] to @. This is used by
c0f891
+		 * systemd to signal that the task was launched from
c0f891
+		 * initrd/initramfs and should be preserved during shutdown
c0f891
+		 */
c0f891
+		argv[0][0] = '@';
c0f891
+	}
c0f891
+
c0f891
 	if (all == 0 && container_name == NULL) {
c0f891
 		if (argv[optind])
c0f891
 			container_name = argv[optind];
c0f891
@@ -353,6 +376,9 @@ int main(int argc, char *argv[])
c0f891
 	if (strcmp(container_name, "/proc/mdstat") == 0)
c0f891
 		all = 1;
c0f891
 
c0f891
+	if (help)
c0f891
+		usage();
c0f891
+
c0f891
 	if (all) {
c0f891
 		struct mdstat_ent *mdstat, *e;
c0f891
 		int container_len = strlen(container_name);
c0f891
-- 
37f2b0
2.38.1
c0f891