core/watchdog: initialize variables only if they'll be used

This is not a hot path, but let's move the variables to the
minimal scope in which they are needed.

Also, all functions which call watchdog_open() check for fd < 0 first,
so change the conditional return into an assert.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-03-21 11:30:26 +01:00
parent 18284f5a1b
commit ab37e9cdb2
+7 -9
View File
@@ -312,8 +312,7 @@ static int watchdog_open(void) {
char **try_order;
int r;
if (watchdog_fd >= 0)
return 0;
assert(watchdog_fd < 0);
/* 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
@@ -446,15 +445,13 @@ usec_t watchdog_runtime_wait(void) {
usec_t ntime = now(CLOCK_BOOTTIME);
assert(ntime >= watchdog_last_ping);
return usec_sub_unsigned(watchdog_last_ping + (timeout / 2), ntime);
return usec_sub_unsigned(watchdog_last_ping + timeout/2, ntime);
}
return timeout / 2;
}
int watchdog_ping(void) {
usec_t ntime, timeout;
if (watchdog_timeout == 0)
return 0;
@@ -462,14 +459,15 @@ int watchdog_ping(void) {
/* open_watchdog() will automatically ping the device for us if necessary */
return watchdog_open();
ntime = now(CLOCK_BOOTTIME);
timeout = watchdog_calc_timeout();
/* Never ping earlier than watchdog_timeout/4 and try to ping
* by watchdog_timeout/2 plus scheduling latencies at the latest */
if (timestamp_is_set(watchdog_last_ping)) {
usec_t ntime = now(CLOCK_BOOTTIME),
timeout = watchdog_calc_timeout();
assert(ntime >= watchdog_last_ping);
if ((ntime - watchdog_last_ping) < (timeout / 4))
if (ntime - watchdog_last_ping < timeout/4)
return 0;
}