Skip to contentSkip to conversation

xenwatchdogd bugfixes and enhancements

PATCH In review [email protected] 2 participants 9 messages Apr 11, 2024 – Apr 22, 2024

lore
Share & tools
lore
#0 lore raw

v3 cover xenwatchdogd bugfixes and enhancements

From: Leigh Brown <[email protected]>

The primary intention of this patch series is to replace the pathologically bad behaviour of rebooting the domain if you run "xenwatchdogd -h". To that end, I have implemented comprehensive argument validation. This validation ensures you can't pass arguments that instantly reboot the domain or cause it to spin loop running sleep(0) repeatedly.

I added a couple of enhancements whilst working on the changes as they were easy enough. In particular, being able to run in the foreground, disarming the watchdog on exit, help and a manpage.

Changes since v2:
- Drop patches 1-3 from v2, as they have been applied.
- Tweak check for NULL character.
- Fix initial implementation of parse_secs() not to use optind.
- Updated copyright notice.
- Better patch message for enhancements patch.
- Addressed feedback on manpage.

Changes since v1:
- Add Signed-off-by on every patch.
- Make global variables static.

Leigh Brown (4):
tools/misc: xenwatchdogd: add parse_secs()
tools/misc: xenwatchdogd enhancements
tools/misc: Add xenwatchdogd.c copyright notice
docs/man: Add xenwatchdog manual page

 docs/man/xenwatchdogd.8.pod |  55 ++++++++++++++++
 tools/misc/xenwatchdogd.c   | 121 +++++++++++++++++++++++++++++++-----
 2 files changed, 161 insertions(+), 15 deletions(-)
 create mode 100644 docs/man/xenwatchdogd.8.pod
Signature · 1 line
2.39.2
#1 lore raw

PATCH v3 1/4 tools/misc: xenwatchdogd: add parse_secs()

From: Leigh Brown <[email protected]>

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(-)
tools/misc/xenwatchdogd.c +15 -8
index 112b706357..9fa772e49f 100644
@@ -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)
+	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[1], "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[2], "sleep");
 
     if (signal(SIGHUP, &catch_exit) == SIG_ERR)
 	err(EXIT_FAILURE, "signal");
Signature · 1 line
2.39.2
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] tools/misc: xenwatchdogd: add parse_secs()

Quoted 7 lines · On Thu, Apr 11, 2024 at 07:20:20PM +0100, [email protected] wrote:

From: Leigh Brown <[email protected]>

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.

Thanks,

Signature · 1 line
Anthony PERARD
#3 lore raw

PATCH v3 2/4 tools/misc: xenwatchdogd enhancements

In reply to [email protected]

From: Leigh Brown <[email protected]>

Add usage() function, the ability to run in the foreground, and the ability to disarm the watchdog timer when exiting.

Add enhanced parameter parsing and validation, making use of getopt_long(). Check the number of parameters are correct, the timeout is at least two seconds (to allow a minimum sleep time of one second), and that the sleep time is at least one and less than the watchdog timeout.

With these changes, the daemon will no longer instantly reboot the domain if you enter a zero timeout (or non-numeric parameter), and prevent the daemon consuming 100% of a CPU due to zero sleep time.

---
 tools/misc/xenwatchdogd.c | 94 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 84 insertions(+), 10 deletions(-)
tools/misc/xenwatchdogd.c +84 -10
index 9fa772e49f..a16d1efc13 100644
@@ -10,6 +10,11 @@
 #include <signal.h>
 #include <stdio.h>
 #include <stdbool.h>
+#include <getopt.h>
+
+#define WDOG_MIN_TIMEOUT 2
+#define WDOG_MIN_SLEEP 1
+#define WDOG_EXIT_TIMEOUT 300
 
 static xc_interface *h;
 static volatile bool safeexit = false;
@@ -49,6 +54,26 @@ static void catch_usr1(int sig)
     done = true;
 }
 
+static void __attribute__((noreturn)) usage(int exit_code)
+{
+    FILE *out = exit_code ? stderr : stdout;
+
+    fprintf(out,
+	"Usage: xenwatchdog [OPTION]... <timeout> [<sleep>]\n"
+	"  -h, --help\t\tDisplay this help text and exit.\n"
+	"  -F, --foreground\tRun in foreground.\n"
+	"  -x, --safe-exit\tDisable watchdog on orderly exit.\n"
+	"\t\t\tNote: default is to set a %d second timeout on exit.\n\n"
+	"  timeout\t\tInteger seconds to arm the watchdog each time.\n"
+	"\t\t\tNote: minimum timeout is %d seconds.\n\n"
+	"  sleep\t\t\tInteger seconds to sleep between arming the watchdog.\n"
+	"\t\t\tNote: sleep must be at least %d and less than timeout.\n"
+	"\t\t\tIf not specified then set to half the timeout.\n",
+	WDOG_EXIT_TIMEOUT, WDOG_MIN_TIMEOUT, WDOG_MIN_SLEEP
+	);
+    exit(exit_code);
+}
+
 static int parse_secs(const char *arg, const char *what)
 {
     char *endptr;
@@ -66,22 +91,71 @@ int main(int argc, char **argv)
     int id;
     int t, s;
     int ret;
+    bool daemon = true;
+
+    for ( ;; )
+    {
+	int option_index = 0, c;
+	static const struct option long_options[] =
+	{
+	    { "help", no_argument, NULL, 'h' },
+	    { "foreground", no_argument, NULL, 'F' },
+	    { "safe-exit", no_argument, NULL, 'x' },
+	    { NULL, 0, NULL, 0 },
+	};
+
+	c = getopt_long(argc, argv, "hFxD", long_options, &option_index);
+	if (c == -1)
+	    break;
+
+	switch (c)
+	{
+	case 'h':
+	    usage(EXIT_SUCCESS);
+
+	case 'F':
+	    daemon = false;
+	    break;
+
+	case 'x':
+	    safeexit = true;
+	    break;
+
+	default:
+	    usage(EXIT_FAILURE);
+	}
+    }
 
-    if (argc < 2)
-	errx(EXIT_FAILURE, "usage: %s <timeout> <sleep>", argv[0]);
+    if (argc - optind < 1)
+	errx(EXIT_FAILURE, "timeout must be specified");
+
+    if (argc - optind > 2)
+	errx(EXIT_FAILURE, "too many arguments");
+
+    t = parse_secs(argv[optind], "timeout");
+    if (t < WDOG_MIN_TIMEOUT)
+	errx(EXIT_FAILURE, "Error: timeout must be at least %d seconds",
+			   WDOG_MIN_TIMEOUT);
+
+    ++optind;
+    if (optind < argc) {
+	s = parse_secs(argv[optind], "sleep");
+	if (s < WDOG_MIN_SLEEP)
+	    errx(EXIT_FAILURE, "Error: sleep must be no less than %d",
+			       WDOG_MIN_SLEEP);
+	if (s >= t)
+	    errx(EXIT_FAILURE, "Error: sleep must be less than timeout");
+    }
+    else
+	s = t / 2;
 
-    daemonize();
+    if (daemon)
+	daemonize();
 
     h = xc_interface_open(NULL, NULL, 0);
     if (h == NULL)
 	err(EXIT_FAILURE, "xc_interface_open");
 
-    t = parse_secs(argv[1], "timeout");
-
-    s = t / 2;
-    if (argc == 3)
-	s = parse_secs(argv[2], "sleep");
-
     if (signal(SIGHUP, &catch_exit) == SIG_ERR)
 	err(EXIT_FAILURE, "signal");
     if (signal(SIGINT, &catch_exit) == SIG_ERR)
@@ -105,6 +179,6 @@ int main(int argc, char **argv)
     }
 
     // Zero seconds timeout will disarm the watchdog timer
-    xc_watchdog(h, id, safeexit ? 0 : 300);
+    xc_watchdog(h, id, safeexit ? 0 : WDOG_EXIT_TIMEOUT);
     return 0;
 }
Signature · 1 line
2.39.2
Reviewed-by on v3 2/4
#4 lore raw

Re: [PATCH v3 2/4] tools/misc: xenwatchdogd enhancements

Quoted 11 lines · On Thu, Apr 11, 2024 at 07:20:21PM +0100, [email protected] wrote:

From: Leigh Brown <[email protected]>

Add usage() function, the ability to run in the foreground, and the ability to disarm the watchdog timer when exiting.

Add enhanced parameter parsing and validation, making use of getopt_long(). Check the number of parameters are correct, the timeout is at least two seconds (to allow a minimum sleep time of one second), and that the sleep time is at least one and less than the watchdog timeout.

Quoted 6 lines

With these changes, the daemon will no longer instantly reboot the domain if you enter a zero timeout (or non-numeric parameter), and prevent the daemon consuming 100% of a CPU due to zero sleep time.

Thanks,

Signature · 1 line
Anthony PERARD
#5 lore raw

PATCH v3 3/4 tools/misc: Add xenwatchdogd.c copyright notice

In reply to [email protected]

From: Leigh Brown <[email protected]>

Add copyright notice and description of the program.

---
 tools/misc/xenwatchdogd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/misc/xenwatchdogd.c +10 -0
index a16d1efc13..2884ca6ca9 100644
@@ -1,3 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * xenwatchdogd.c
+ *
+ * Watchdog based on Xen hypercall watchdog interface.
+ *
+ * Copyright 2010 Citrix Ltd
+ * Copyright 2024 Leigh Brown <[email protected]>
+ *
+ */
 
 #include <err.h>
 #include <limits.h>
Signature · 1 line
2.39.2
Acked-by on v3 3/4
#6 lore raw

Re: [PATCH v3 3/4] tools/misc: Add xenwatchdogd.c copyright notice

Quoted 15 lines · On Thu, Apr 11, 2024 at 07:20:22PM +0100, [email protected] wrote:

From: Leigh Brown <[email protected]>

Add copyright notice and description of the program.

---
 tools/misc/xenwatchdogd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/misc/xenwatchdogd.c +0 -0
index a16d1efc13..2884ca6ca9 100644

tools/misc/xenwatchdogd.c line 10

Quoted 10 lines
tools/misc/xenwatchdogd.c +10 -0
@@ -1,3 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * xenwatchdogd.c
+ *
+ * Watchdog based on Xen hypercall watchdog interface.
+ *
+ * Copyright 2010 Citrix Ltd
+ * Copyright 2024 Leigh Brown <[email protected]>
+ *
+ */

Thanks,

Signature · 1 line
Anthony PERARD
#7 lore raw

PATCH v3 4/4 docs/man: Add xenwatchdog manual page

In reply to [email protected]

From: Leigh Brown <[email protected]>

Add a manual page for xenwatchdogd.

---
 docs/man/xenwatchdogd.8.pod | 55 +++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 docs/man/xenwatchdogd.8.pod
docs/man/xenwatchdogd.8.pod new file +55 -0
new file mode 100644
index 0000000000..b03ed53ee6
@@ -0,0 +1,55 @@
+=head1 NAME
+
+xenwatchdogd - Xen hypercall based watchdog daemon
+
+=head1 SYNOPSIS
+
+B<xenwatchdogd> [ I<OPTIONS> ] <I<TIMEOUT>> [ <I<SLEEP>> ]
+
+=head1 DESCRIPTION
+
+B<xenwatchdogd> arms the Xen watchdog timer to I<TIMEOUT> every I<SLEEP>
+seconds. If the xenwatchdogd process dies or is delayed for more than
+I<TIMEOUT> seconds, then Xen will take the B<on_watchdog> action
+specified in the domain configuration (see xl.cfg(5)). If B<xenwatchdogd>
+is running in dom0, the whole system will reboot.
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<-h>, B<--help>
+
+Display a help message.
+
+=item B<-F>, B<--foreground>
+
+Run in the foreground. The default behaviour is to daemonize.
+
+=item B<-x>, B<--safe-exit>
+
+Disable watchdog on orderly exit. The default behaviour is to arm the
+watchdog to 300 seconds to allow time for the domain to shutdown.  See 
+also the B<SIGNALS> section.
+
+=item B<timeout>
+
+The number of seconds to arm the Xen watchdog timer. This must be set to
+a minimum of two.
+
+=item B<sleep>
+
+The number of seconds to sleep in between calls to arm the Xen watchdog
+timer. This must be at least one second, and less than the I<timeout>
+value. If not specified, it defaults to half the I<timeout> value.
+
+=back
+
+=head1 SIGNALS
+
+B<SIGUSR1> Will cause the program to disarm the watchdog timer and exit,
+regardless of whether the B<--safe-exit> option was passed.
+
+=head1 AUTHOR
+
+Citrix Ltd and other contributors.
Signature · 1 line
2.39.2