]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/watchdog-mux.c
11 #include <sys/socket.h>
13 #include <sys/epoll.h>
15 #include <sys/signalfd.h>
17 #include <linux/types.h>
18 #include <linux/watchdog.h>
20 #include <systemd/sd-daemon.h>
22 #define WD_SOCK_PATH "/run/watchdog-mux.sock"
23 #define WD_ACTIVE_MARKER "/run/watchdog-mux.active"
25 #define LISTEN_BACKLOG 32 /* set same value in watchdog-mux.socket */
29 #define WATCHDOG_DEV "/dev/watchdog"
32 int watchdog_timeout
= 10;
33 int client_watchdog_timeout
= 60;
34 int update_watchdog
= 1;
42 #define MAX_CLIENTS 100
44 static wd_client_t client_list
[MAX_CLIENTS
];
47 alloc_client(int fd
, time_t time
)
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
];
64 free_client(wd_client_t
*wd_client
)
71 wd_client
->magic_close
= 0;
75 active_client_count(void)
79 for (i
= 0; i
< MAX_CLIENTS
; i
++) {
80 if (client_list
[i
].fd
!= 0 && client_list
[i
].time
!= 0) {
91 if (watchdog_fd
!= -1) {
92 if (write(watchdog_fd
, "V", 1) == -1) {
93 perror("write magic watchdog close");
95 if (close(watchdog_fd
) == -1) {
96 perror("write magic watchdog close");
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 int unlink_socket
= 0;
114 if (stat(WD_ACTIVE_MARKER
, &fs
) == 0) {
115 fprintf(stderr
, "watchdog active - unable to restart watchdog-mux\n");
119 /* if you want to debug, set options in /lib/modprobe.d/aliases.conf
120 * options softdog soft_noboot=1
122 if (stat(WATCHDOG_DEV
, &fs
) == -1) {
123 system("modprobe -q softdog"); // load softdog by default
126 if ((watchdog_fd
= open(WATCHDOG_DEV
, O_WRONLY
)) == -1) {
127 perror("watchdog open");
131 if (ioctl(watchdog_fd
, WDIOC_SETTIMEOUT
, &watchdog_timeout
) == -1) {
132 perror("watchdog set timeout");
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");
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
);
149 socket_count
= sd_listen_fds(0);
151 if (socket_count
> 1) {
153 perror("too many file descriptors received.\n");
156 } else if (socket_count
== 1) {
158 listen_sock
= SD_LISTEN_FDS_START
+ 0;
164 unlink(WD_SOCK_PATH
);
166 listen_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
167 if (listen_sock
== -1) {
168 perror("socket create");
172 memset(&my_addr
, 0, sizeof(struct sockaddr_un
));
173 my_addr
.sun_family
= AF_UNIX
;
174 strncpy(my_addr
.sun_path
, WD_SOCK_PATH
, sizeof(my_addr
.sun_path
) - 1);
176 if (bind(listen_sock
, (struct sockaddr
*) &my_addr
,
177 sizeof(struct sockaddr_un
)) == -1) {
178 perror("socket bind");
182 if (listen(listen_sock
, LISTEN_BACKLOG
) == -1) {
183 perror("socket listen");
188 epollfd
= epoll_create(10);
190 perror("epoll_create");
195 ev
.data
.ptr
= alloc_client(listen_sock
, 0);
196 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, listen_sock
, &ev
) == -1) {
197 perror("epoll_ctl add listen_sock");
203 sigaddset(&mask
, SIGINT
);
204 sigaddset(&mask
, SIGTERM
);
205 sigaddset(&mask
, SIGHUP
);
207 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
209 if ((sigfd
= signalfd(-1, &mask
, SFD_NONBLOCK
)) < 0) {
210 perror("unable to open signalfd");
215 ev
.data
.ptr
= alloc_client(sigfd
, 0);
216 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, sigfd
, &ev
) == -1) {
217 perror("epoll_ctl add sigfd");
222 nfds
= epoll_wait(epollfd
, events
, MAX_EVENTS
, 1000);
227 perror("epoll_pwait");
231 if (nfds
== 0) { // timeout
233 // check for timeouts
234 if (update_watchdog
) {
236 time_t ctime
= time(NULL
);
237 for (i
= 0; i
< MAX_CLIENTS
; i
++) {
238 if (client_list
[i
].fd
!= 0 && client_list
[i
].time
!= 0 &&
239 ((ctime
- client_list
[i
].time
) > client_watchdog_timeout
)) {
241 fprintf(stderr
, "client watchdog expired - disable watchdog updates\n");
246 if (update_watchdog
) {
247 if (ioctl(watchdog_fd
, WDIOC_KEEPALIVE
, 0) == -1) {
248 perror("watchdog update failed");
255 if (!update_watchdog
)
261 for (n
= 0; n
< nfds
; ++n
) {
262 wd_client_t
*wd_client
= events
[n
].data
.ptr
;
263 if (wd_client
->fd
== listen_sock
) {
264 int conn_sock
= accept(listen_sock
, (struct sockaddr
*) &peer_addr
, &peer_addr_size
);
265 if (conn_sock
== -1) {
269 if (fcntl(conn_sock
, F_SETFL
, O_NONBLOCK
) == -1) {
270 perror("setnonblocking");
274 wd_client_t
*new_client
= alloc_client(conn_sock
, time(NULL
));
275 if (new_client
== NULL
) {
276 fprintf(stderr
, "unable to alloc wd_client structure\n");
280 mkdir(WD_ACTIVE_MARKER
, 0600);
283 ev
.data
.ptr
= new_client
;
284 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, conn_sock
, &ev
) == -1) {
285 perror("epoll_ctl: add conn_sock");
288 } else if (wd_client
->fd
== sigfd
) {
290 /* signal handling */
293 struct signalfd_siginfo si
;
295 if ((rv
= read(sigfd
, &si
, sizeof(si
))) && rv
>= 0) {
296 if (si
.ssi_signo
== SIGHUP
) {
297 perror("got SIGHUP - ignored");
300 fprintf(stderr
, "got terminate request\n");
306 int cfd
= wd_client
->fd
;
308 ssize_t bytes
= read(cfd
, buf
, sizeof(buf
));
312 } else if (bytes
> 0) {
314 for (i
= 0; i
< bytes
; i
++) {
316 wd_client
->magic_close
= 1;
318 wd_client
->magic_close
= 0;
321 wd_client
->time
= time(NULL
);
323 if (events
[n
].events
& EPOLLHUP
|| events
[n
].events
& EPOLLERR
) {
324 //printf("GOT %016x event\n", events[n].events);
325 if (epoll_ctl(epollfd
, EPOLL_CTL_DEL
, cfd
, NULL
) == -1) {
326 perror("epoll_ctl: del conn_sock");
329 if (close(cfd
) == -1) {
330 perror("close conn_sock");
334 if (!wd_client
->magic_close
) {
335 fprintf(stderr
, "client did not stop watchdog - disable watchdog updates\n");
338 free_client(wd_client
);
341 if (!active_client_count()) {
342 rmdir(WD_ACTIVE_MARKER
);
352 int active_count
= active_client_count();
353 if (active_count
> 0) {
354 fprintf(stderr
, "exit watchdog-mux with active connections\n");
356 fprintf(stderr
, "clean exit\n");
361 unlink(WD_SOCK_PATH
);
367 unlink(WD_SOCK_PATH
);