From e4c98db335673dcbd7cff3add516db92861302b2 Mon Sep 17 00:00:00 2001 From: "Edward A. James" Date: Fri, 8 Dec 2017 11:26:30 -0600 Subject: [PATCH] watchdog: allow a device path to be specified Currently systemd hardcodes the use of /dev/watchdog. This is a legacy chardev that points to watchdog0 in the system. Modify the watchdog API to allow a different device path to be passed and stored. Opening the watchdog defaults to /dev/watchdog, maintaining existing behavior. --- src/shared/watchdog.c | 9 ++++++++- src/shared/watchdog.h | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index a6a356dbd0..b0a422da84 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -27,10 +27,12 @@ #include "fd-util.h" #include "log.h" +#include "string-util.h" #include "time-util.h" #include "watchdog.h" static int watchdog_fd = -1; +static char *watchdog_device = NULL; static usec_t watchdog_timeout = USEC_INFINITY; static int update_timeout(void) { @@ -84,7 +86,8 @@ static int open_watchdog(void) { if (watchdog_fd >= 0) return 0; - watchdog_fd = open("/dev/watchdog", O_WRONLY|O_CLOEXEC); + watchdog_fd = open(watchdog_device ?: "/dev/watchdog", + O_WRONLY|O_CLOEXEC); if (watchdog_fd < 0) return -errno; @@ -96,6 +99,10 @@ static int open_watchdog(void) { return update_timeout(); } +int watchdog_set_device(char *path) { + return free_and_strdup(&watchdog_device, path); +} + int watchdog_set_timeout(usec_t *usec) { int r; diff --git a/src/shared/watchdog.h b/src/shared/watchdog.h index 8c17e7e1dc..5694338db3 100644 --- a/src/shared/watchdog.h +++ b/src/shared/watchdog.h @@ -25,6 +25,11 @@ #include "time-util.h" #include "util.h" +int watchdog_set_device(char *path); int watchdog_set_timeout(usec_t *usec); int watchdog_ping(void); void watchdog_close(bool disarm); + +static inline void watchdog_free_device(void) { + (void) watchdog_set_device(NULL); +}