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

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