]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
watchdog: make watchdog_ping() a NOP when the watchdog is disabled or closed
authorFranck Bui <fbui@suse.com>
Mon, 6 Sep 2021 06:47:08 +0000 (08:47 +0200)
committerFranck Bui <fbui@suse.com>
Wed, 15 Sep 2021 08:54:19 +0000 (10:54 +0200)
This patch allows watchdog_ping() to be used unconditionally regardless of
whether watchdog_set_timeout() or watchdog_close() has been previously called
or not and in both cases watchdog_ping() does nothing.

shutdown.c has been updated to cope with this change.

src/shared/watchdog.c
src/shutdown/shutdown.c

index 361d5837dc984b1af694ea349bc9fd809544189d..e0700b601395fcfe6b2ba3354424e52685f336c7 100644 (file)
@@ -136,6 +136,9 @@ int watchdog_ping(void) {
         usec_t ntime;
         int r;
 
+        if (!timestamp_is_set(watchdog_timeout))
+                return 0;
+
         ntime = now(clock_boottime_or_monotonic());
 
         /* Never ping earlier than watchdog_timeout/4 and try to ping
@@ -186,4 +189,8 @@ void watchdog_close(bool disarm) {
         }
 
         watchdog_fd = safe_close(watchdog_fd);
+
+        /* Once closed, pinging the device becomes a NOP and we request a new
+         * call to watchdog_set_timeout() to open the device again. */
+        watchdog_timeout = USEC_INFINITY;
 }
index 2ba1a21dae943ad833ca93844e43acac08157c3c..a2ba96bf7c032e338bf5f96a8aa3dcdb76f46a99 100644 (file)
@@ -308,9 +308,9 @@ static void bump_sysctl_printk_log_level(int min_level) {
 }
 
 int main(int argc, char *argv[]) {
-        bool need_umount, need_swapoff, need_loop_detach, need_dm_detach, need_md_detach, in_container, use_watchdog = false, can_initrd;
+        bool need_umount, need_swapoff, need_loop_detach, need_dm_detach, need_md_detach, in_container, can_initrd;
         _cleanup_free_ char *cgroup = NULL;
-        char *arguments[3], *watchdog_device;
+        char *arguments[3], *watchdog_device, *watchdog_usec;
         int cmd, r, umount_log_level = LOG_INFO;
         static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL};
 
@@ -370,7 +370,6 @@ int main(int argc, char *argv[]) {
                    LOG_TARGET_KMSG))
                 bump_sysctl_printk_log_level(LOG_WARNING);
 
-        use_watchdog = getenv("WATCHDOG_USEC");
         watchdog_device = getenv("WATCHDOG_DEVICE");
         if (watchdog_device) {
                 r = watchdog_set_device(watchdog_device);
@@ -379,6 +378,18 @@ int main(int argc, char *argv[]) {
                                           watchdog_device);
         }
 
+        watchdog_usec = getenv("WATCHDOG_USEC");
+        if (watchdog_usec) {
+                usec_t usec;
+
+                r = safe_atou64(watchdog_usec, &usec);
+                if (r < 0)
+                        log_warning_errno(r, "Failed to parse watchdog timeout '%s', ignoring: %m",
+                                          watchdog_usec);
+                else
+                        (void) watchdog_set_timeout(&usec);
+        }
+
         /* Lock us into memory */
         (void) mlockall(MCL_CURRENT|MCL_FUTURE);
 
@@ -409,8 +420,7 @@ int main(int argc, char *argv[]) {
         for (;;) {
                 bool changed = false;
 
-                if (use_watchdog)
-                        (void) watchdog_ping();
+                (void) watchdog_ping();
 
                 /* Let's trim the cgroup tree on each iteration so
                    that we leave an empty cgroup tree around, so that