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