linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lukasz Florczak <lukasz.florczak@linux.intel.com>
To: linux-raid@vger.kernel.org
Cc: jes@trained-monkey.org, colyli@suse.de
Subject: [PATCH] mdmon: Stop parsing duplicate options
Date: Fri, 13 May 2022 09:19:42 +0200	[thread overview]
Message-ID: <20220513071942.27850-1-lukasz.florczak@linux.intel.com> (raw)

Introduce new function is_duplicate_opt() to check if given option
was already used and prevent setting it again along with an error
message.

Move parsing above in_initrd() check to be able to detect --offroot
option duplicates.

Now help option is executed after parsing to prevent executing commands
like: 'mdmon --help --ndlksnlksajndfjksndafasj'.

Signed-off-by: Lukasz Florczak <lukasz.florczak@linux.intel.com>
---
 mdmon.c | 44 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 9 deletions(-)

diff --git a/mdmon.c b/mdmon.c
index c71e62c6..3da9043f 100644
--- a/mdmon.c
+++ b/mdmon.c
@@ -289,6 +289,15 @@ void usage(void)
 	exit(2);
 }
 
+static bool is_duplicate_opt(const int opt, const int set_val, const char *long_name)
+{
+	if (opt == set_val) {
+		pr_err("--%s option duplicated!\n", long_name);
+		return true;
+	}
+	return false;
+}
+
 static int mdmon(char *devnm, int must_fork, int takeover);
 
 int main(int argc, char *argv[])
@@ -300,6 +309,7 @@ int main(int argc, char *argv[])
 	int all = 0;
 	int takeover = 0;
 	int dofork = 1;
+	bool help = false;
 	static struct option options[] = {
 		{"all", 0, NULL, 'a'},
 		{"takeover", 0, NULL, 't'},
@@ -309,37 +319,50 @@ int main(int argc, char *argv[])
 		{NULL, 0, NULL, 0}
 	};
 
-	if (in_initrd()) {
-		/*
-		 * set first char of argv[0] to @. This is used by
-		 * systemd to signal that the task was launched from
-		 * initrd/initramfs and should be preserved during shutdown
-		 */
-		argv[0][0] = '@';
-	}
-
 	while ((opt = getopt_long(argc, argv, "thaF", options, NULL)) != -1) {
 		switch (opt) {
 		case 'a':
+			if (is_duplicate_opt(all, 1, "all"))
+				exit(1);
 			container_name = argv[optind-1];
 			all = 1;
 			break;
 		case 't':
+			if (is_duplicate_opt(takeover, 1, "takeover"))
+				exit(1);
 			takeover = 1;
 			break;
 		case 'F':
+			if (is_duplicate_opt(dofork, 0, "foreground"))
+				exit(1);
 			dofork = 0;
 			break;
 		case OffRootOpt:
+			if (is_duplicate_opt(argv[0][0], '@', "offroot"))
+				exit(1);
 			argv[0][0] = '@';
 			break;
 		case 'h':
+			if (is_duplicate_opt(help, true, "help"))
+				exit(1);
+			help = true;
+			break;
 		default:
 			usage();
 			break;
 		}
 	}
 
+
+	if (in_initrd()) {
+		/*
+		 * set first char of argv[0] to @. This is used by
+		 * systemd to signal that the task was launched from
+		 * initrd/initramfs and should be preserved during shutdown
+		 */
+		argv[0][0] = '@';
+	}
+
 	if (all == 0 && container_name == NULL) {
 		if (argv[optind])
 			container_name = argv[optind];
@@ -354,6 +377,9 @@ int main(int argc, char *argv[])
 	if (strcmp(container_name, "/proc/mdstat") == 0)
 		all = 1;
 
+	if (help)
+		usage();
+
 	if (all) {
 		struct mdstat_ent *mdstat, *e;
 		int container_len = strlen(container_name);
-- 
2.27.0


             reply	other threads:[~2022-05-13  7:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-13  7:19 Lukasz Florczak [this message]
2022-06-09 22:08 ` [PATCH] mdmon: Stop parsing duplicate options Jes Sorensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220513071942.27850-1-lukasz.florczak@linux.intel.com \
    --to=lukasz.florczak@linux.intel.com \
    --cc=colyli@suse.de \
    --cc=jes@trained-monkey.org \
    --cc=linux-raid@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).