From a73b7b1b0e1b4469d95ac4b2a13a031499a9e76a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 18 Oct 2021 11:17:11 +0200 Subject: [PATCH 1/4] watchdog: fix error code handling --- src/shared/watchdog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index 4de6003039..8a551fb5c8 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -107,7 +107,7 @@ static int update_timeout(void) { if (watchdog_timeout == USEC_INFINITY) { r = watchdog_get_timeout(); if (r < 0) - return log_error_errno(errno, "Failed to query watchdog HW timeout: %m"); + return log_error_errno(r, "Failed to query watchdog HW timeout: %m"); } r = watchdog_set_enable(true); From 59bcac0b1a48c7821d335bc52b9098da9e125932 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 18 Oct 2021 11:21:42 +0200 Subject: [PATCH 2/4] watchdog: always prefer /dev/watchdog0 over /dev/watchdog --- man/systemd-system.conf.xml | 4 ++-- src/shared/watchdog.c | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml index 2307a8f5c9..3805a010e2 100644 --- a/man/systemd-system.conf.xml +++ b/man/systemd-system.conf.xml @@ -144,7 +144,7 @@ depending on hardware capabilities). If RuntimeWatchdogSec= is set to a non-zero value, the watchdog hardware - (/dev/watchdog or the path specified with WatchdogDevice= or + (/dev/watchdog0 or the path specified with WatchdogDevice= or the kernel option systemd.watchdog-device=) will be programmed to automatically reboot the system if it is not contacted within the specified timeout interval. The system manager will ensure to contact it at least once in half the specified timeout interval. This feature requires @@ -182,7 +182,7 @@ Configure the hardware watchdog device that the runtime and shutdown watchdog timers will open and use. Defaults - to /dev/watchdog. This setting has no + to /dev/watchdog0. This setting has no effect if a hardware watchdog is not available. diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index 8a551fb5c8..c23a4cbac4 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -10,6 +10,7 @@ #include "errno-util.h" #include "fd-util.h" #include "log.h" +#include "path-util.h" #include "string-util.h" #include "time-util.h" #include "watchdog.h" @@ -127,7 +128,13 @@ static int open_watchdog(void) { if (watchdog_fd >= 0) return 0; - fn = watchdog_device ?: "/dev/watchdog"; + /* Let's prefer new-style /dev/watchdog0 (i.e. kernel 3.5+) over classic /dev/watchdog. The former + * has the benefit that we can easily find the matching directory in sysfs from it, as the relevant + * sysfs attributes can only be found via /sys/dev/char/: if the new-style device + * major/minor is used, not the old-style. */ + fn = !watchdog_device || path_equal(watchdog_device, "/dev/watchdog") ? + "/dev/watchdog0" : watchdog_device; + watchdog_fd = open(fn, O_WRONLY|O_CLOEXEC); if (watchdog_fd < 0) return log_debug_errno(errno, "Failed to open watchdog device %s, ignoring: %m", fn); From ef52fa5bd1bef763e8bac82abf040cb695827dac Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 18 Oct 2021 11:26:49 +0200 Subject: [PATCH 3/4] watchdog: drop unnecessary variable --- src/shared/watchdog.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index c23a4cbac4..4317d90b24 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -22,12 +22,10 @@ static usec_t watchdog_last_ping = USEC_INFINITY; static int watchdog_set_enable(bool enable) { int flags = enable ? WDIOS_ENABLECARD : WDIOS_DISABLECARD; - int r; assert(watchdog_fd >= 0); - r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags); - if (r < 0) { + if (ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags) < 0) { if (!enable) return log_warning_errno(errno, "Failed to disable hardware watchdog, ignoring: %m"); From d9752ca196853fa17d9b58e496f8c3668a9bc099 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 18 Oct 2021 11:26:58 +0200 Subject: [PATCH 4/4] watchdog: fix fd validity check --- src/shared/watchdog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index 4317d90b24..3f4c72826d 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -42,7 +42,7 @@ static int watchdog_set_enable(bool enable) { static int watchdog_get_timeout(void) { int sec = 0; - assert(watchdog_fd > 0); + assert(watchdog_fd >= 0); if (ioctl(watchdog_fd, WDIOC_GETTIMEOUT, &sec) < 0) return -errno;