From a6b7ebaafa73205f4fc80dd307013fb714ebbd76 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 14 Sep 2022 04:55:35 +0900 Subject: watchdog: use /dev/watchdog0 only if it exists Fixes #24661. --- src/shared/watchdog.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index 690493a698..8c8f1893f7 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -314,7 +314,7 @@ static int update_timeout(void) { static int open_watchdog(void) { struct watchdog_info ident; - const char *fn; + char **try_order; int r; if (watchdog_fd >= 0) @@ -324,16 +324,25 @@ static int open_watchdog(void) { * 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; + try_order = !watchdog_device || PATH_IN_SET(watchdog_device, "/dev/watchdog", "/dev/watchdog0") ? + STRV_MAKE("/dev/watchdog0", "/dev/watchdog") : STRV_MAKE(watchdog_device); - r = free_and_strdup(&watchdog_device, fn); - if (r < 0) - return log_oom_debug(); + STRV_FOREACH(wd, try_order) { + watchdog_fd = open(*wd, O_WRONLY|O_CLOEXEC); + if (watchdog_fd >= 0) { + r = free_and_strdup(&watchdog_device, *wd); + if (r < 0) + return log_oom_debug(); + + break; + } + + if (errno != ENOENT) + return log_debug_errno(errno, "Failed to open watchdog device %s: %m", *wd); + } - watchdog_fd = open(watchdog_device, O_WRONLY|O_CLOEXEC); if (watchdog_fd < 0) - return log_debug_errno(errno, "Failed to open watchdog device %s, ignoring: %m", watchdog_device); + return log_debug_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to open watchdog device %s: %m", watchdog_device ?: "auto"); if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &ident) < 0) log_debug_errno(errno, "Hardware watchdog %s does not support WDIOC_GETSUPPORT ioctl, ignoring: %m", watchdog_device); -- cgit v1.2.3