]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/watchdog.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/ioctl.h>
26 #include <linux/watchdog.h>
30 #include "time-util.h"
33 static int watchdog_fd
= -1;
34 static usec_t watchdog_timeout
= USEC_INFINITY
;
36 static int update_timeout(void) {
42 if (watchdog_timeout
== USEC_INFINITY
)
44 else if (watchdog_timeout
== 0) {
47 flags
= WDIOS_DISABLECARD
;
48 r
= ioctl(watchdog_fd
, WDIOC_SETOPTIONS
, &flags
);
50 return log_warning_errno(errno
, "Failed to disable hardware watchdog: %m");
53 char buf
[FORMAT_TIMESPAN_MAX
];
55 sec
= (int) ((watchdog_timeout
+ USEC_PER_SEC
- 1) / USEC_PER_SEC
);
56 r
= ioctl(watchdog_fd
, WDIOC_SETTIMEOUT
, &sec
);
58 return log_warning_errno(errno
, "Failed to set timeout to %is: %m", sec
);
60 watchdog_timeout
= (usec_t
) sec
* USEC_PER_SEC
;
61 log_info("Set hardware watchdog to %s.", format_timespan(buf
, sizeof(buf
), watchdog_timeout
, 0));
63 flags
= WDIOS_ENABLECARD
;
64 r
= ioctl(watchdog_fd
, WDIOC_SETOPTIONS
, &flags
);
66 /* ENOTTY means the watchdog is always enabled so we're fine */
67 log_full(errno
== ENOTTY
? LOG_DEBUG
: LOG_WARNING
,
68 "Failed to enable hardware watchdog: %m");
73 r
= ioctl(watchdog_fd
, WDIOC_KEEPALIVE
, 0);
75 return log_warning_errno(errno
, "Failed to ping hardware watchdog: %m");
81 static int open_watchdog(void) {
82 struct watchdog_info ident
;
87 watchdog_fd
= open("/dev/watchdog", O_WRONLY
|O_CLOEXEC
);
91 if (ioctl(watchdog_fd
, WDIOC_GETSUPPORT
, &ident
) >= 0)
92 log_info("Hardware watchdog '%s', version %x",
94 ident
.firmware_version
);
96 return update_timeout();
99 int watchdog_set_timeout(usec_t
*usec
) {
102 watchdog_timeout
= *usec
;
104 /* If we didn't open the watchdog yet and didn't get any
105 * explicit timeout value set, don't do anything */
106 if (watchdog_fd
< 0 && watchdog_timeout
== USEC_INFINITY
)
112 r
= update_timeout();
114 *usec
= watchdog_timeout
;
119 int watchdog_ping(void) {
122 if (watchdog_fd
< 0) {
128 r
= ioctl(watchdog_fd
, WDIOC_KEEPALIVE
, 0);
130 return log_warning_errno(errno
, "Failed to ping hardware watchdog: %m");
135 void watchdog_close(bool disarm
) {
144 /* Explicitly disarm it */
145 flags
= WDIOS_DISABLECARD
;
146 r
= ioctl(watchdog_fd
, WDIOC_SETOPTIONS
, &flags
);
148 log_warning_errno(errno
, "Failed to disable hardware watchdog: %m");
150 /* To be sure, use magic close logic, too */
152 static const char v
= 'V';
154 if (write(watchdog_fd
, &v
, 1) > 0)
157 if (errno
!= EINTR
) {
158 log_error_errno(errno
, "Failed to disarm watchdog timer: %m");
164 watchdog_fd
= safe_close(watchdog_fd
);