1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
26 #include <linux/watchdog.h>
31 static int watchdog_fd = -1;
32 static usec_t watchdog_timeout = USEC_INFINITY;
34 static int update_timeout(void) {
40 if (watchdog_timeout == USEC_INFINITY)
42 else if (watchdog_timeout == 0) {
45 flags = WDIOS_DISABLECARD;
46 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
48 return log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
51 char buf[FORMAT_TIMESPAN_MAX];
53 sec = (int) ((watchdog_timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
54 r = ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec);
56 return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);
58 watchdog_timeout = (usec_t) sec * USEC_PER_SEC;
59 log_info("Set hardware watchdog to %s.", format_timespan(buf, sizeof(buf), watchdog_timeout, 0));
61 flags = WDIOS_ENABLECARD;
62 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
64 /* ENOTTY means the watchdog is always enabled so we're fine */
65 log_full(errno == ENOTTY ? LOG_DEBUG : LOG_WARNING,
66 "Failed to enable hardware watchdog: %m");
71 r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
73 return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
79 static int open_watchdog(void) {
80 struct watchdog_info ident;
85 watchdog_fd = open("/dev/watchdog", O_WRONLY|O_CLOEXEC);
89 if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &ident) >= 0)
90 log_info("Hardware watchdog '%s', version %x",
92 ident.firmware_version);
94 return update_timeout();
97 int watchdog_set_timeout(usec_t *usec) {
100 watchdog_timeout = *usec;
102 /* If we didn't open the watchdog yet and didn't get any
103 * explicit timeout value set, don't do anything */
104 if (watchdog_fd < 0 && watchdog_timeout == USEC_INFINITY)
110 r = update_timeout();
112 *usec = watchdog_timeout;
117 int watchdog_ping(void) {
120 if (watchdog_fd < 0) {
126 r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
128 return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
133 void watchdog_close(bool disarm) {
142 /* Explicitly disarm it */
143 flags = WDIOS_DISABLECARD;
144 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
146 log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
148 /* To be sure, use magic close logic, too */
150 static const char v = 'V';
152 if (write(watchdog_fd, &v, 1) > 0)
155 if (errno != EINTR) {
156 log_error_errno(errno, "Failed to disarm watchdog timer: %m");
162 watchdog_fd = safe_close(watchdog_fd);