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