]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/watchdog-mux.c
10 #include <sys/socket.h>
12 #include <sys/epoll.h>
14 #include <linux/types.h>
15 #include <linux/watchdog.h>
17 #include <systemd/sd-daemon.h>
19 #define MY_SOCK_PATH "/run/watchdog-mux.sock"
20 #define LISTEN_BACKLOG 50
23 #define WATCHDOG_DEV "/dev/watchdog"
26 int watchdog_timeout
= 20;
34 #define MAX_CLIENTS 100
36 static wd_client_t client_list
[MAX_CLIENTS
];
43 for (i
= 0; i
< MAX_CLIENTS
; i
++) {
44 if (client_list
[i
].fd
== 0) {
45 memset(&client_list
[i
], 1, sizeof(wd_client_t
));
46 client_list
[i
].fd
= fd
;
47 return &client_list
[i
];
55 free_client(wd_client_t
*wd_client
)
66 if (watchdog_fd
!= -1) {
67 if (write(watchdog_fd
, "V", 1) == -1) {
68 perror("write magic watchdog close");
70 if (close(watchdog_fd
) == -1) {
71 perror("write magic watchdog close");
81 struct sockaddr_un my_addr
, peer_addr
;
82 socklen_t peer_addr_size
;
83 struct epoll_event ev
, events
[MAX_EVENTS
];
84 int socket_count
, listen_sock
, nfds
, epollfd
;
87 if (stat(WATCHDOG_DEV
, &fs
) == -1) {
88 system("modprobe -q softdog soft_noboot=1"); // fixme
91 if ((watchdog_fd
= open(WATCHDOG_DEV
, O_WRONLY
)) == -1) {
92 perror("watchdog open");
96 if (ioctl(watchdog_fd
, WDIOC_SETTIMEOUT
, &watchdog_timeout
) == -1) {
97 perror("watchdog set timeout");
102 /* read and log watchdog identity */
103 struct watchdog_info wdinfo
;
104 if (ioctl(watchdog_fd
, WDIOC_GETSUPPORT
, &wdinfo
) == -1) {
105 perror("read watchdog info");
110 wdinfo
.identity
[sizeof(wdinfo
.identity
) - 1] = 0; // just to be sure
111 fprintf(stderr
, "Watchdog driver '%s', version %x\n",
112 wdinfo
.identity
, wdinfo
.firmware_version
);
114 socket_count
= sd_listen_fds(0);
116 if (socket_count
> 1) {
118 perror("too many file descriptors received.\n");
121 } else if (socket_count
== 1) {
123 listen_sock
= SD_LISTEN_FDS_START
+ 0;
127 unlink(MY_SOCK_PATH
);
129 listen_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
130 if (listen_sock
== -1) {
131 perror("socket create");
135 memset(&my_addr
, 0, sizeof(struct sockaddr_un
));
136 my_addr
.sun_family
= AF_UNIX
;
137 strncpy(my_addr
.sun_path
, MY_SOCK_PATH
, sizeof(my_addr
.sun_path
) - 1);
139 if (bind(listen_sock
, (struct sockaddr
*) &my_addr
,
140 sizeof(struct sockaddr_un
)) == -1) {
141 perror("socket bind");
145 if (listen(listen_sock
, LISTEN_BACKLOG
) == -1) {
146 perror("socket listen");
151 epollfd
= epoll_create(10);
153 perror("epoll_create");
158 ev
.data
.ptr
= alloc_client(listen_sock
);
159 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, listen_sock
, &ev
) == -1) {
160 perror("epoll_ctl: listen_sock");
165 nfds
= epoll_wait(epollfd
, events
, MAX_EVENTS
, 1000);
170 perror("epoll_pwait");
174 if (nfds
== 0) { // timeout
176 if (ioctl(watchdog_fd
, WDIOC_KEEPALIVE
, 0) == -1) {
177 perror("watchdog update failed");
184 for (n
= 0; n
< nfds
; ++n
) {
185 wd_client_t
*wd_client
= events
[n
].data
.ptr
;
186 if (wd_client
->fd
== listen_sock
) {
187 int conn_sock
= accept(listen_sock
, (struct sockaddr
*) &peer_addr
, &peer_addr_size
);
188 if (conn_sock
== -1) {
192 if (fcntl(conn_sock
, F_SETFL
, O_NONBLOCK
) == -1) {
193 perror("setnonblocking");
197 wd_client_t
*new_client
= alloc_client(conn_sock
);
198 if (new_client
== NULL
) {
199 fprintf(stderr
, "unable to alloc wd_client structure\n");
203 ev
.data
.ptr
= new_client
;
204 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, conn_sock
, &ev
) == -1) {
205 perror("epoll_ctl: add conn_sock");
210 int cfd
= wd_client
->fd
;
212 ssize_t bytes
= read(cfd
, buf
, sizeof(buf
));
216 } else if (bytes
> 0) {
217 fprintf(stderr
, "GOT %zd bytes\n", bytes
);
219 if (events
[n
].events
& EPOLLHUP
|| events
[n
].events
& EPOLLERR
) {
220 printf("GOT %016x event\n", events
[n
].events
);
221 if (epoll_ctl(epollfd
, EPOLL_CTL_DEL
, cfd
, NULL
) == -1) {
222 perror("epoll_ctl: del conn_sock");
225 if (close(cfd
) == -1) {
226 perror("close conn_sock");
229 fprintf(stderr
, "close client connection\n");
230 free_client(wd_client
);
242 unlink(MY_SOCK_PATH
);
246 unlink(MY_SOCK_PATH
);