2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-11-16 22:09:36 +01:00
|
|
|
#include <sys/ioctl.h>
|
2015-12-03 21:13:37 +01:00
|
|
|
#include <syslog.h>
|
2012-04-05 22:08:10 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
#include "errno-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "log.h"
|
2017-12-08 18:26:30 +01:00
|
|
|
#include "string-util.h"
|
2015-12-03 21:13:37 +01:00
|
|
|
#include "time-util.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "watchdog.h"
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
static int watchdog_fd = -1;
|
2017-12-08 18:26:30 +01:00
|
|
|
static char *watchdog_device = NULL;
|
2014-07-29 12:23:31 +02:00
|
|
|
static usec_t watchdog_timeout = USEC_INFINITY;
|
2020-04-02 09:10:55 +02:00
|
|
|
static usec_t watchdog_last_ping = USEC_INFINITY;
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
static int update_timeout(void) {
|
|
|
|
if (watchdog_fd < 0)
|
|
|
|
return 0;
|
2014-07-29 12:23:31 +02:00
|
|
|
if (watchdog_timeout == USEC_INFINITY)
|
2012-04-05 22:08:10 +02:00
|
|
|
return 0;
|
2020-10-27 09:57:28 +01:00
|
|
|
|
|
|
|
if (watchdog_timeout == 0) {
|
2012-04-05 22:08:10 +02:00
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = WDIOS_DISABLECARD;
|
2020-10-27 09:57:28 +01:00
|
|
|
if (ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags) < 0)
|
2014-11-28 19:57:32 +01:00
|
|
|
return log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
|
2012-04-05 22:08:10 +02:00
|
|
|
} else {
|
|
|
|
char buf[FORMAT_TIMESPAN_MAX];
|
2020-10-27 09:57:28 +01:00
|
|
|
int sec, flags;
|
|
|
|
usec_t t;
|
2012-04-05 22:08:10 +02:00
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
t = DIV_ROUND_UP(watchdog_timeout, USEC_PER_SEC);
|
|
|
|
sec = (int) t >= INT_MAX ? INT_MAX : t; /* Saturate */
|
|
|
|
if (ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec) < 0)
|
2014-11-28 19:57:32 +01:00
|
|
|
return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
watchdog_timeout = (usec_t) sec * USEC_PER_SEC;
|
2013-04-04 02:56:56 +02:00
|
|
|
log_info("Set hardware watchdog to %s.", format_timespan(buf, sizeof(buf), watchdog_timeout, 0));
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
flags = WDIOS_ENABLECARD;
|
2020-10-27 09:57:28 +01:00
|
|
|
if (ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags) < 0) {
|
2015-06-17 18:57:39 +02:00
|
|
|
/* ENOTTY means the watchdog is always enabled so we're fine */
|
2020-10-27 09:57:28 +01:00
|
|
|
log_full(ERRNO_IS_NOT_SUPPORTED(errno) ? LOG_DEBUG : LOG_WARNING,
|
2015-06-17 18:57:39 +02:00
|
|
|
"Failed to enable hardware watchdog: %m");
|
2020-10-27 09:57:28 +01:00
|
|
|
if (!ERRNO_IS_NOT_SUPPORTED(errno))
|
2015-06-17 18:57:39 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
2012-04-05 22:08:10 +02:00
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
if (ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0) < 0)
|
2014-11-28 19:57:32 +01:00
|
|
|
return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
|
2020-04-02 09:10:55 +02:00
|
|
|
|
|
|
|
watchdog_last_ping = now(clock_boottime_or_monotonic());
|
2012-04-05 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_watchdog(void) {
|
|
|
|
struct watchdog_info ident;
|
2020-10-27 09:57:28 +01:00
|
|
|
const char *fn;
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
if (watchdog_fd >= 0)
|
|
|
|
return 0;
|
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
fn = watchdog_device ?: "/dev/watchdog";
|
|
|
|
watchdog_fd = open(fn, O_WRONLY|O_CLOEXEC);
|
2012-04-05 22:08:10 +02:00
|
|
|
if (watchdog_fd < 0)
|
2020-10-27 09:57:28 +01:00
|
|
|
return log_debug_errno(errno, "Failed to open watchdog device %s: %m", fn);
|
2012-04-05 22:08:10 +02:00
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &ident) < 0)
|
|
|
|
log_debug_errno(errno, "Hardware watchdog %s does not support WDIOC_GETSUPPORT ioctl: %m", fn);
|
|
|
|
else
|
|
|
|
log_info("Using hardware watchdog '%s', version %x, device %s",
|
2012-04-05 22:08:10 +02:00
|
|
|
ident.identity,
|
2020-10-27 09:57:28 +01:00
|
|
|
ident.firmware_version,
|
|
|
|
fn);
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
return update_timeout();
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:26:30 +01:00
|
|
|
int watchdog_set_device(char *path) {
|
2019-08-20 20:24:47 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
r = free_and_strdup(&watchdog_device, path);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (r > 0) /* watchdog_device changed */
|
|
|
|
watchdog_fd = safe_close(watchdog_fd);
|
|
|
|
|
|
|
|
return r;
|
2017-12-08 18:26:30 +01:00
|
|
|
}
|
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
int watchdog_set_timeout(usec_t *usec) {
|
2012-04-06 21:37:26 +02:00
|
|
|
int r;
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
watchdog_timeout = *usec;
|
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
/* If we didn't open the watchdog yet and didn't get any explicit timeout value set, don't do
|
|
|
|
* anything */
|
2014-07-29 12:23:31 +02:00
|
|
|
if (watchdog_fd < 0 && watchdog_timeout == USEC_INFINITY)
|
2012-04-05 22:08:10 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (watchdog_fd < 0)
|
2012-04-06 21:37:26 +02:00
|
|
|
r = open_watchdog();
|
2012-04-05 22:08:10 +02:00
|
|
|
else
|
2012-04-06 21:37:26 +02:00
|
|
|
r = update_timeout();
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
*usec = watchdog_timeout;
|
2012-04-06 21:37:26 +02:00
|
|
|
return r;
|
2012-04-05 22:08:10 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 09:10:55 +02:00
|
|
|
usec_t watchdog_runtime_wait(void) {
|
2020-10-27 09:57:28 +01:00
|
|
|
usec_t rtwait, ntime;
|
2020-04-02 09:10:55 +02:00
|
|
|
|
|
|
|
if (!timestamp_is_set(watchdog_timeout))
|
|
|
|
return USEC_INFINITY;
|
|
|
|
|
2020-04-21 20:46:53 +02:00
|
|
|
/* Sleep half the watchdog timeout since the last successful ping at most */
|
2020-04-02 09:10:55 +02:00
|
|
|
if (timestamp_is_set(watchdog_last_ping)) {
|
|
|
|
ntime = now(clock_boottime_or_monotonic());
|
|
|
|
assert(ntime >= watchdog_last_ping);
|
|
|
|
rtwait = usec_sub_unsigned(watchdog_last_ping + (watchdog_timeout / 2), ntime);
|
|
|
|
} else
|
|
|
|
rtwait = watchdog_timeout / 2;
|
|
|
|
|
|
|
|
return rtwait;
|
|
|
|
}
|
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
int watchdog_ping(void) {
|
2020-04-02 09:10:55 +02:00
|
|
|
usec_t ntime;
|
2012-04-05 22:08:10 +02:00
|
|
|
int r;
|
|
|
|
|
2020-04-02 09:10:55 +02:00
|
|
|
ntime = now(clock_boottime_or_monotonic());
|
|
|
|
|
|
|
|
/* Never ping earlier than watchdog_timeout/4 and try to ping
|
|
|
|
* by watchdog_timeout/2 plus scheduling latencies the latest */
|
|
|
|
if (timestamp_is_set(watchdog_last_ping)) {
|
|
|
|
assert(ntime >= watchdog_last_ping);
|
|
|
|
if ((ntime - watchdog_last_ping) < (watchdog_timeout / 4))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-05 22:08:10 +02:00
|
|
|
if (watchdog_fd < 0) {
|
|
|
|
r = open_watchdog();
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-10-27 09:57:28 +01:00
|
|
|
if (ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0) < 0)
|
2014-11-28 19:57:32 +01:00
|
|
|
return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
|
2012-04-05 22:08:10 +02:00
|
|
|
|
2020-04-02 09:10:55 +02:00
|
|
|
watchdog_last_ping = ntime;
|
2012-04-05 22:08:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void watchdog_close(bool disarm) {
|
|
|
|
if (watchdog_fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (disarm) {
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* Explicitly disarm it */
|
|
|
|
flags = WDIOS_DISABLECARD;
|
2020-10-27 09:57:28 +01:00
|
|
|
if (ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags) < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
|
2012-04-05 22:08:10 +02:00
|
|
|
|
|
|
|
/* To be sure, use magic close logic, too */
|
|
|
|
for (;;) {
|
|
|
|
static const char v = 'V';
|
|
|
|
|
|
|
|
if (write(watchdog_fd, &v, 1) > 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (errno != EINTR) {
|
2014-11-28 19:29:59 +01:00
|
|
|
log_error_errno(errno, "Failed to disarm watchdog timer: %m");
|
2012-04-05 22:08:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
watchdog_fd = safe_close(watchdog_fd);
|
2012-04-05 22:08:10 +02:00
|
|
|
}
|