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

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