Skip to content

Commit 5bbf2db

Browse files
committed
watchdog: make watchdog_ping() a NOP when the watchdog is disabled or closed
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.
1 parent 1266329 commit 5bbf2db

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/shared/watchdog.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ int watchdog_ping(void) {
136136
usec_t ntime;
137137
int r;
138138

139+
if (!timestamp_is_set(watchdog_timeout))
140+
return 0;
141+
139142
ntime = now(clock_boottime_or_monotonic());
140143

141144
/* Never ping earlier than watchdog_timeout/4 and try to ping
@@ -186,4 +189,8 @@ void watchdog_close(bool disarm) {
186189
}
187190

188191
watchdog_fd = safe_close(watchdog_fd);
192+
193+
/* Once closed, pinging the device becomes a NOP and we request a new
194+
* call to watchdog_set_timeout() to open the device again. */
195+
watchdog_timeout = USEC_INFINITY;
189196
}

src/shutdown/shutdown.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ static void bump_sysctl_printk_log_level(int min_level) {
308308
}
309309

310310
int main(int argc, char *argv[]) {
311-
bool need_umount, need_swapoff, need_loop_detach, need_dm_detach, need_md_detach, in_container, use_watchdog = false, can_initrd;
311+
bool need_umount, need_swapoff, need_loop_detach, need_dm_detach, need_md_detach, in_container, can_initrd;
312312
_cleanup_free_ char *cgroup = NULL;
313-
char *arguments[3], *watchdog_device;
313+
char *arguments[3], *watchdog_device, *watchdog_usec;
314314
int cmd, r, umount_log_level = LOG_INFO;
315315
static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL};
316316

@@ -370,7 +370,6 @@ int main(int argc, char *argv[]) {
370370
LOG_TARGET_KMSG))
371371
bump_sysctl_printk_log_level(LOG_WARNING);
372372

373-
use_watchdog = getenv("WATCHDOG_USEC");
374373
watchdog_device = getenv("WATCHDOG_DEVICE");
375374
if (watchdog_device) {
376375
r = watchdog_set_device(watchdog_device);
@@ -379,6 +378,18 @@ int main(int argc, char *argv[]) {
379378
watchdog_device);
380379
}
381380

381+
watchdog_usec = getenv("WATCHDOG_USEC");
382+
if (watchdog_usec) {
383+
usec_t usec;
384+
385+
r = safe_atou64(watchdog_usec, &usec);
386+
if (r < 0)
387+
log_warning_errno(r, "Failed to parse watchdog timeout '%s', ignoring: %m",
388+
watchdog_usec);
389+
else
390+
(void) watchdog_set_timeout(&usec);
391+
}
392+
382393
/* Lock us into memory */
383394
(void) mlockall(MCL_CURRENT|MCL_FUTURE);
384395

@@ -409,8 +420,7 @@ int main(int argc, char *argv[]) {
409420
for (;;) {
410421
bool changed = false;
411422

412-
if (use_watchdog)
413-
(void) watchdog_ping();
423+
(void) watchdog_ping();
414424

415425
/* Let's trim the cgroup tree on each iteration so
416426
that we leave an empty cgroup tree around, so that

0 commit comments

Comments
 (0)