]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
watchdog: use /dev/watchdog0 only if it exists 24664/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 Sep 2022 19:55:35 +0000 (04:55 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 16 Sep 2022 13:34:37 +0000 (22:34 +0900)
Fixes #24661.

src/shared/watchdog.c

index 690493a698dd01d61ab57190d5a376906cdf19fb..8c8f1893f766ac44726b4a4e089221f2192c54f5 100644 (file)
@@ -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/<major>:<minor> 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);