]> git.proxmox.com Git - pve-ha-manager.git/blob - src/watchdog-mux.c
really activate softdog
[pve-ha-manager.git] / src / watchdog-mux.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <time.h>
8 #include <sys/ioctl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <sys/epoll.h>
14 #include <signal.h>
15 #include <sys/signalfd.h>
16
17 #include <linux/types.h>
18 #include <linux/watchdog.h>
19
20 #include <systemd/sd-daemon.h>
21
22 #define MY_SOCK_PATH "/run/watchdog-mux.sock"
23 #define WD_ACTIVE_MARKER "/run/watchdog-mux.active"
24
25 #define LISTEN_BACKLOG 32 /* set same value in watchdog-mux.socket */
26
27 #define MAX_EVENTS 10
28
29 #define WATCHDOG_DEV "/dev/watchdog"
30
31 int watchdog_fd = -1;
32 int watchdog_timeout = 10;
33 int client_watchdog_timeout = 60;
34 int update_watchdog = 1;
35
36 typedef struct {
37 int fd;
38 time_t time;
39 int magic_close;
40 } wd_client_t;
41
42 #define MAX_CLIENTS 100
43
44 static wd_client_t client_list[MAX_CLIENTS];
45
46 static wd_client_t *
47 alloc_client(int fd, time_t time)
48 {
49 int i;
50
51 for (i = 0; i < MAX_CLIENTS; i++) {
52 if (client_list[i].fd == 0) {
53 client_list[i].fd = fd;
54 client_list[i].time = time;
55 client_list[i].magic_close = 0;
56 return &client_list[i];
57 }
58 }
59
60 return NULL;
61 }
62
63 static void
64 free_client(wd_client_t *wd_client)
65 {
66 if (!wd_client)
67 return;
68
69 wd_client->time = 0;
70 wd_client->fd = 0;
71 wd_client->magic_close = 0;
72 }
73
74 static int
75 active_client_count(void)
76 {
77 int i, count = 0;
78
79 for (i = 0; i < MAX_CLIENTS; i++) {
80 if (client_list[i].fd != 0 && client_list[i].time != 0) {
81 count++;
82 }
83 }
84
85 return count;
86 }
87
88 static void
89 watchdog_close(void)
90 {
91 if (watchdog_fd != -1) {
92 if (write(watchdog_fd, "V", 1) == -1) {
93 perror("write magic watchdog close");
94 }
95 if (close(watchdog_fd) == -1) {
96 perror("write magic watchdog close");
97 }
98 }
99
100 watchdog_fd = -1;
101 }
102
103 int
104 main(void)
105 {
106 struct sockaddr_un my_addr, peer_addr;
107 socklen_t peer_addr_size;
108 struct epoll_event ev, events[MAX_EVENTS];
109 int socket_count, listen_sock, nfds, epollfd, sigfd;
110
111
112 struct stat fs;
113
114 if (stat(WD_ACTIVE_MARKER, &fs) == 0) {
115 fprintf(stderr, "watchdog active - unable to restart watchdog-mux\n");
116 exit(EXIT_FAILURE);
117 }
118
119 /* if you want to debug, set options in /lib/modprobe.d/aliases.conf
120 * options softdog soft_noboot=1
121 */
122 if (stat(WATCHDOG_DEV, &fs) == -1) {
123 system("modprobe -q softdog"); // load softdog by default
124 }
125
126 if ((watchdog_fd = open(WATCHDOG_DEV, O_WRONLY)) == -1) {
127 perror("watchdog open");
128 exit(EXIT_FAILURE);
129 }
130
131 if (ioctl(watchdog_fd, WDIOC_SETTIMEOUT, &watchdog_timeout) == -1) {
132 perror("watchdog set timeout");
133 watchdog_close();
134 exit(EXIT_FAILURE);
135 }
136
137 /* read and log watchdog identity */
138 struct watchdog_info wdinfo;
139 if (ioctl(watchdog_fd, WDIOC_GETSUPPORT, &wdinfo) == -1) {
140 perror("read watchdog info");
141 watchdog_close();
142 exit(EXIT_FAILURE);
143 }
144
145 wdinfo.identity[sizeof(wdinfo.identity) - 1] = 0; // just to be sure
146 fprintf(stderr, "Watchdog driver '%s', version %x\n",
147 wdinfo.identity, wdinfo.firmware_version);
148
149 socket_count = sd_listen_fds(0);
150
151 if (socket_count > 1) {
152
153 perror("too many file descriptors received.\n");
154 goto err;
155
156 } else if (socket_count == 1) {
157
158 listen_sock = SD_LISTEN_FDS_START + 0;
159
160 } else {
161
162 unlink(MY_SOCK_PATH);
163
164 listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
165 if (listen_sock == -1) {
166 perror("socket create");
167 exit(EXIT_FAILURE);
168 }
169
170 memset(&my_addr, 0, sizeof(struct sockaddr_un));
171 my_addr.sun_family = AF_UNIX;
172 strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) - 1);
173
174 if (bind(listen_sock, (struct sockaddr *) &my_addr,
175 sizeof(struct sockaddr_un)) == -1) {
176 perror("socket bind");
177 exit(EXIT_FAILURE);
178 }
179
180 if (listen(listen_sock, LISTEN_BACKLOG) == -1) {
181 perror("socket listen");
182 goto err;
183 }
184 }
185
186 epollfd = epoll_create(10);
187 if (epollfd == -1) {
188 perror("epoll_create");
189 goto err;
190 }
191
192 ev.events = EPOLLIN;
193 ev.data.ptr = alloc_client(listen_sock, 0);
194 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &ev) == -1) {
195 perror("epoll_ctl add listen_sock");
196 goto err;
197 }
198
199 sigset_t mask;
200 sigemptyset(&mask);
201 sigaddset(&mask, SIGINT);
202 sigaddset(&mask, SIGTERM);
203 sigaddset(&mask, SIGHUP);
204
205 sigprocmask(SIG_BLOCK, &mask, NULL);
206
207 if ((sigfd = signalfd(-1, &mask, SFD_NONBLOCK)) < 0) {
208 perror("unable to open signalfd");
209 goto err;
210 }
211
212 ev.events = EPOLLIN;
213 ev.data.ptr = alloc_client(sigfd, 0);
214 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sigfd, &ev) == -1) {
215 perror("epoll_ctl add sigfd");
216 goto err;
217 }
218
219 for (;;) {
220 nfds = epoll_wait(epollfd, events, MAX_EVENTS, 1000);
221 if (nfds == -1) {
222 if (errno == EINTR)
223 continue;
224
225 perror("epoll_pwait");
226 goto err;
227 }
228
229 if (nfds == 0) { // timeout
230
231 // check for timeouts
232 if (update_watchdog) {
233 int i;
234 time_t ctime = time(NULL);
235 for (i = 0; i < MAX_CLIENTS; i++) {
236 if (client_list[i].fd != 0 && client_list[i].time != 0 &&
237 ((ctime - client_list[i].time) > client_watchdog_timeout)) {
238 update_watchdog = 0;
239 fprintf(stderr, "client watchdog expired - disable watchdog updates\n");
240 }
241 }
242 }
243
244 if (update_watchdog) {
245 if (ioctl(watchdog_fd, WDIOC_KEEPALIVE, 0) == -1) {
246 perror("watchdog update failed");
247 }
248 }
249
250 continue;
251 }
252
253 if (!update_watchdog)
254 break;
255
256 int terminate = 0;
257
258 int n;
259 for (n = 0; n < nfds; ++n) {
260 wd_client_t *wd_client = events[n].data.ptr;
261 if (wd_client->fd == listen_sock) {
262 int conn_sock = accept(listen_sock, (struct sockaddr *) &peer_addr, &peer_addr_size);
263 if (conn_sock == -1) {
264 perror("accept");
265 goto err; // fixme
266 }
267 if (fcntl(conn_sock, F_SETFL, O_NONBLOCK) == -1) {
268 perror("setnonblocking");
269 goto err; // fixme
270 }
271
272 wd_client_t *new_client = alloc_client(conn_sock, time(NULL));
273 if (new_client == NULL) {
274 fprintf(stderr, "unable to alloc wd_client structure\n");
275 goto err; // fixme;
276 }
277
278 mkdir(WD_ACTIVE_MARKER, 0600);
279
280 ev.events = EPOLLIN;
281 ev.data.ptr = new_client;
282 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock, &ev) == -1) {
283 perror("epoll_ctl: add conn_sock");
284 goto err; // fixme
285 }
286 } else if (wd_client->fd == sigfd) {
287
288 /* signal handling */
289
290 int rv = 0;
291 struct signalfd_siginfo si;
292
293 if ((rv = read(sigfd, &si, sizeof(si))) && rv >= 0) {
294 if (si.ssi_signo == SIGHUP) {
295 perror("got SIGHUP - ignored");
296 } else {
297 terminate = 1;
298 fprintf(stderr, "got terminate request\n");
299 }
300 }
301
302 } else {
303 char buf[4096];
304 int cfd = wd_client->fd;
305
306 ssize_t bytes = read(cfd, buf, sizeof(buf));
307 if (bytes == -1) {
308 perror("read");
309 goto err; // fixme
310 } else if (bytes > 0) {
311 int i;
312 for (i = 0; i < bytes; i++) {
313 if (buf[i] == 'V') {
314 wd_client->magic_close = 1;
315 } else {
316 wd_client->magic_close = 0;
317 }
318 }
319 wd_client->time = time(NULL);
320 } else {
321 if (events[n].events & EPOLLHUP || events[n].events & EPOLLERR) {
322 //printf("GOT %016x event\n", events[n].events);
323 if (epoll_ctl(epollfd, EPOLL_CTL_DEL, cfd, NULL) == -1) {
324 perror("epoll_ctl: del conn_sock");
325 goto err; // fixme
326 }
327 if (close(cfd) == -1) {
328 perror("close conn_sock");
329 goto err; // fixme
330 }
331
332 if (!wd_client->magic_close) {
333 fprintf(stderr, "client did not stop watchdog - disable watchdog updates\n");
334 update_watchdog = 0;
335 } else {
336 free_client(wd_client);
337 }
338
339 if (!active_client_count()) {
340 rmdir(WD_ACTIVE_MARKER);
341 }
342 }
343 }
344 }
345 }
346 if (terminate)
347 break;
348 }
349
350 int active_count = active_client_count();
351 if (active_count > 0) {
352 fprintf(stderr, "exit watchdog-mux with active connections\n");
353 } else {
354 fprintf(stderr, "clean exit\n");
355 watchdog_close();
356 }
357
358 unlink(MY_SOCK_PATH);
359 exit(EXIT_SUCCESS);
360
361 err:
362 unlink(MY_SOCK_PATH);
363 exit(EXIT_FAILURE);
364 }