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

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