All of lore.kernel.org
 help / color / mirror / Atom feed
From: leigh@solinno.co.uk
To: xen-devel@lists.xenproject.org
Cc: andrew.cooper3@citrix.com, anthony.perard@citrix.com,
	slack@rabbit.lu, Leigh Brown <leigh@solinno.co.uk>
Subject: [PATCH 4/6] tools/misc: xenwatchdogd: add parse_secs()
Date: Wed, 27 Mar 2024 18:13:51 +0000	[thread overview]
Message-ID: <20240327181353.10951-5-leigh@solinno.co.uk> (raw)
In-Reply-To: <20240327181353.10951-1-leigh@solinno.co.uk>

From: Leigh Brown <leigh@solinno.co.uk>

Create a new parse_secs() function to parse the timeout and sleep
parameters. This ensures that non-numeric parameters are not
accidentally treated as numbers.
---
 tools/misc/xenwatchdogd.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/tools/misc/xenwatchdogd.c b/tools/misc/xenwatchdogd.c
index 224753e824..26bfdef3db 100644
--- a/tools/misc/xenwatchdogd.c
+++ b/tools/misc/xenwatchdogd.c
@@ -49,6 +49,18 @@ static void catch_usr1(int sig)
     done = true;
 }
 
+static int parse_secs(const char *arg, const char *what)
+{
+    char *endptr;
+    unsigned long val;
+
+    val = strtoul(arg, &endptr, 0);
+    if (val > INT_MAX || *endptr != 0)
+	errx(EXIT_FAILURE, "invalid %s: '%s'", what, arg);
+
+    return val;
+}
+
 int main(int argc, char **argv)
 {
     int id;
@@ -64,16 +76,11 @@ int main(int argc, char **argv)
     if (h == NULL)
 	err(EXIT_FAILURE, "xc_interface_open");
 
-    t = strtoul(argv[1], NULL, 0);
-    if (t == ULONG_MAX)
-	err(EXIT_FAILURE, "strtoul");
+    t = parse_secs(argv[optind], "timeout");
 
     s = t / 2;
-    if (argc == 3) {
-	s = strtoul(argv[2], NULL, 0);
-	if (s == ULONG_MAX)
-	    err(EXIT_FAILURE, "strtoul");
-    }
+    if (argc == 3)
+	s = parse_secs(argv[optind], "sleep");
 
     if (signal(SIGHUP, &catch_exit) == SIG_ERR)
 	err(EXIT_FAILURE, "signal");
-- 
2.39.2



  parent reply	other threads:[~2024-03-27 18:14 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-26  5:15 [PATCH] tools/misc: fix xenwatchdogd invocation zithro / Cyril Rébert
2024-03-26  8:19 ` Jan Beulich
2024-03-27 18:13   ` [PATCH 0/6] xenwatchdogd enhancements leigh
2024-03-27 18:13     ` [PATCH 1/6] tools/misc: xenwatchdogd: use EXIT_* constants leigh
2024-03-27 18:13     ` [PATCH 2/6] tools/misc: rework xenwatchdogd signal handling leigh
2024-03-28  9:31       ` Jan Beulich
2024-03-28  9:43         ` Jan Beulich
2024-03-27 18:13     ` [PATCH 3/6] tools/misc: xenwatchdogd: make functions static leigh
2024-03-28  9:37       ` Jan Beulich
2024-03-27 18:13     ` leigh [this message]
2024-03-27 18:13     ` [PATCH 5/6] tools/misc: xenwatchdogd enhancements leigh
2024-03-27 18:13     ` [PATCH 6/6] docs/man: Add xenwatchdog manual page leigh

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=20240327181353.10951-5-leigh@solinno.co.uk \
    --to=leigh@solinno.co.uk \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@citrix.com \
    --cc=slack@rabbit.lu \
    --cc=xen-devel@lists.xenproject.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.