]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/watchdog.c
tty-ask-password: Split out password sending
[thirdparty/systemd.git] / src / shared / watchdog.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2012 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <syslog.h>
26 #include <unistd.h>
27 #include <linux/watchdog.h>
28
29 #include "fd-util.h"
30 #include "log.h"
31 #include "time-util.h"
32 #include "watchdog.h"
33
34 static int watchdog_fd = -1;
35 static usec_t watchdog_timeout = USEC_INFINITY;
36
37 static int update_timeout(void) {
38 int r;
39
40 if (watchdog_fd < 0)
41 return 0;
42
43 if (watchdog_timeout == USEC_INFINITY)
44 return 0;
45 else if (watchdog_timeout == 0) {
46 int flags;
47
48 flags = WDIOS_DISABLECARD;
49 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
50 if (r < 0)
51 return log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
52 } else {
53 int sec, flags;
54 char buf[FORMAT_TIMESPAN_MAX];
55
56 sec = (int) ((watchdog_timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
57 r = ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec);
58 if (r < 0)
59 return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);
60
61 watchdog_timeout = (usec_t) sec * USEC_PER_SEC;
62 log_info("Set hardware watchdog to %s.", format_timespan(buf, sizeof(buf), watchdog_timeout, 0));
63
64 flags = WDIOS_ENABLECARD;
65 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
66 if (r < 0) {
67 /* ENOTTY means the watchdog is always enabled so we're fine */
68 log_full(errno == ENOTTY ? LOG_DEBUG : LOG_WARNING,
69 "Failed to enable hardware watchdog: %m");
70 if (errno != ENOTTY)
71 return -errno;
72 }
73
74 r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
75 if (r < 0)
76 return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
77 }
78
79 return 0;
80 }
81
82 static int open_watchdog(void) {
83 struct watchdog_info ident;
84
85 if (watchdog_fd >= 0)
86 return 0;
87
88 watchdog_fd = open("/dev/watchdog", O_WRONLY|O_CLOEXEC);
89 if (watchdog_fd < 0)
90 return -errno;
91
92 if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &ident) >= 0)
93 log_info("Hardware watchdog '%s', version %x",
94 ident.identity,
95 ident.firmware_version);
96
97 return update_timeout();
98 }
99
100 int watchdog_set_timeout(usec_t *usec) {
101 int r;
102
103 watchdog_timeout = *usec;
104
105 /* If we didn't open the watchdog yet and didn't get any
106 * explicit timeout value set, don't do anything */
107 if (watchdog_fd < 0 && watchdog_timeout == USEC_INFINITY)
108 return 0;
109
110 if (watchdog_fd < 0)
111 r = open_watchdog();
112 else
113 r = update_timeout();
114
115 *usec = watchdog_timeout;
116
117 return r;
118 }
119
120 int watchdog_ping(void) {
121 int r;
122
123 if (watchdog_fd < 0) {
124 r = open_watchdog();
125 if (r < 0)
126 return r;
127 }
128
129 r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
130 if (r < 0)
131 return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
132
133 return 0;
134 }
135
136 void watchdog_close(bool disarm) {
137 int r;
138
139 if (watchdog_fd < 0)
140 return;
141
142 if (disarm) {
143 int flags;
144
145 /* Explicitly disarm it */
146 flags = WDIOS_DISABLECARD;
147 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
148 if (r < 0)
149 log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
150
151 /* To be sure, use magic close logic, too */
152 for (;;) {
153 static const char v = 'V';
154
155 if (write(watchdog_fd, &v, 1) > 0)
156 break;
157
158 if (errno != EINTR) {
159 log_error_errno(errno, "Failed to disarm watchdog timer: %m");
160 break;
161 }
162 }
163 }
164
165 watchdog_fd = safe_close(watchdog_fd);
166 }