chiark / gitweb /
zsh-completion: Do not interpret escape sequences in _filter_units_by_property
[elogind.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 <sys/ioctl.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <linux/watchdog.h>
27
28 #include "watchdog.h"
29 #include "log.h"
30
31 static int watchdog_fd = -1;
32 static usec_t watchdog_timeout = USEC_INFINITY;
33
34 static int update_timeout(void) {
35         int r;
36
37         if (watchdog_fd < 0)
38                 return 0;
39
40         if (watchdog_timeout == USEC_INFINITY)
41                 return 0;
42         else if (watchdog_timeout == 0) {
43                 int flags;
44
45                 flags = WDIOS_DISABLECARD;
46                 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
47                 if (r < 0)
48                         return log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
49         } else {
50                 int sec, flags;
51                 char buf[FORMAT_TIMESPAN_MAX];
52
53                 sec = (int) ((watchdog_timeout + USEC_PER_SEC - 1) / USEC_PER_SEC);
54                 r = ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &sec);
55                 if (r < 0)
56                         return log_warning_errno(errno, "Failed to set timeout to %is: %m", sec);
57
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));
60
61                 flags = WDIOS_ENABLECARD;
62                 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
63                 if (r < 0)
64                         return log_warning_errno(errno, "Failed to enable hardware watchdog: %m");
65
66                 r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
67                 if (r < 0)
68                         return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
69         }
70
71         return 0;
72 }
73
74 static int open_watchdog(void) {
75         struct watchdog_info ident;
76
77         if (watchdog_fd >= 0)
78                 return 0;
79
80         watchdog_fd = open("/dev/watchdog", O_WRONLY|O_CLOEXEC);
81         if (watchdog_fd < 0)
82                 return -errno;
83
84         if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &ident) >= 0)
85                 log_info("Hardware watchdog '%s', version %x",
86                          ident.identity,
87                          ident.firmware_version);
88
89         return update_timeout();
90 }
91
92 int watchdog_set_timeout(usec_t *usec) {
93         int r;
94
95         watchdog_timeout = *usec;
96
97         /* If we didn't open the watchdog yet and didn't get any
98          * explicit timeout value set, don't do anything */
99         if (watchdog_fd < 0 && watchdog_timeout == USEC_INFINITY)
100                 return 0;
101
102         if (watchdog_fd < 0)
103                 r = open_watchdog();
104         else
105                 r = update_timeout();
106
107         *usec = watchdog_timeout;
108
109         return r;
110 }
111
112 int watchdog_ping(void) {
113         int r;
114
115         if (watchdog_fd < 0) {
116                 r = open_watchdog();
117                 if (r < 0)
118                         return r;
119         }
120
121         r = ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0);
122         if (r < 0)
123                 return log_warning_errno(errno, "Failed to ping hardware watchdog: %m");
124
125         return 0;
126 }
127
128 void watchdog_close(bool disarm) {
129         int r;
130
131         if (watchdog_fd < 0)
132                 return;
133
134         if (disarm) {
135                 int flags;
136
137                 /* Explicitly disarm it */
138                 flags = WDIOS_DISABLECARD;
139                 r = ioctl(watchdog_fd, WDIOC_SETOPTIONS, &flags);
140                 if (r < 0)
141                         log_warning_errno(errno, "Failed to disable hardware watchdog: %m");
142
143                 /* To be sure, use magic close logic, too */
144                 for (;;) {
145                         static const char v = 'V';
146
147                         if (write(watchdog_fd, &v, 1) > 0)
148                                 break;
149
150                         if (errno != EINTR) {
151                                 log_error_errno(errno, "Failed to disarm watchdog timer: %m");
152                                 break;
153                         }
154                 }
155         }
156
157         watchdog_fd = safe_close(watchdog_fd);
158 }