]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/watchdog-mux.c
9 #include <sys/socket.h>
11 #include <sys/epoll.h>
13 #include <linux/types.h>
14 #include <linux/watchdog.h>
16 #include <systemd/sd-daemon.h>
18 #define MY_SOCK_PATH "/run/watchdog-mux.sock"
19 #define LISTEN_BACKLOG 50
22 #define WATCHDOG_DEV "/dev/watchdog"
25 int watchdog_timeout
= 20;
30 if (watchdog_fd
!= -1) {
31 if (write(watchdog_fd
, "V", 1) == -1) {
32 perror("write magic watchdog close");
34 if (close(watchdog_fd
) == -1) {
35 perror("write magic watchdog close");
45 struct sockaddr_un my_addr
, peer_addr
;
46 socklen_t peer_addr_size
;
47 struct epoll_event ev
, events
[MAX_EVENTS
];
48 int socket_count
, listen_sock
, nfds
, epollfd
;
51 if (stat(WATCHDOG_DEV
, &fs
) == -1) {
52 system("modprobe -q softdog soft_noboot=1"); // fixme
55 if ((watchdog_fd
= open(WATCHDOG_DEV
, O_WRONLY
)) == -1) {
56 perror("watchdog open");
60 if (ioctl(watchdog_fd
, WDIOC_SETTIMEOUT
, &watchdog_timeout
) == -1) {
61 perror("watchdog set timeout");
66 /* read and log watchdog identity */
67 struct watchdog_info wdinfo
;
68 if (ioctl(watchdog_fd
, WDIOC_GETSUPPORT
, &wdinfo
) == -1) {
69 perror("read watchdog info");
74 wdinfo
.identity
[sizeof(wdinfo
.identity
) - 1] = 0; // just to be sure
75 fprintf(stderr
, "Watchdog driver '%s', version %x\n",
76 wdinfo
.identity
, wdinfo
.firmware_version
);
78 socket_count
= sd_listen_fds(0);
80 if (socket_count
> 1) {
82 perror("too many file descriptors received.\n");
85 } else if (socket_count
== 1) {
87 listen_sock
= SD_LISTEN_FDS_START
+ 0;
93 listen_sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
94 if (listen_sock
== -1) {
95 perror("socket create");
99 memset(&my_addr
, 0, sizeof(struct sockaddr_un
));
100 my_addr
.sun_family
= AF_UNIX
;
101 strncpy(my_addr
.sun_path
, MY_SOCK_PATH
, sizeof(my_addr
.sun_path
) - 1);
103 if (bind(listen_sock
, (struct sockaddr
*) &my_addr
,
104 sizeof(struct sockaddr_un
)) == -1) {
105 perror("socket bind");
109 if (listen(listen_sock
, LISTEN_BACKLOG
) == -1) {
110 perror("socket listen");
115 epollfd
= epoll_create(10);
117 perror("epoll_create");
122 ev
.data
.fd
= listen_sock
;
123 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, listen_sock
, &ev
) == -1) {
124 perror("epoll_ctl: listen_sock");
129 nfds
= epoll_wait(epollfd
, events
, MAX_EVENTS
, -1); //fixme: timeout
131 perror("epoll_pwait");
136 for (n
= 0; n
< nfds
; ++n
) {
137 if (events
[n
].data
.fd
== listen_sock
) {
138 int conn_sock
= accept(listen_sock
, (struct sockaddr
*) &peer_addr
, &peer_addr_size
);
139 if (conn_sock
== -1) {
143 if (fcntl(conn_sock
, F_SETFL
, O_NONBLOCK
) == -1) {
144 perror("setnonblocking");
149 ev
.data
.fd
= conn_sock
;
150 if (epoll_ctl(epollfd
, EPOLL_CTL_ADD
, conn_sock
, &ev
) == -1) {
151 perror("epoll_ctl: add conn_sock");
156 int cfd
= events
[n
].data
.fd
;
158 ssize_t bytes
= read(cfd
, buf
, sizeof(buf
));
162 } else if (bytes
> 0) {
163 printf("GOT %zd bytes\n", bytes
);
165 if (events
[n
].events
& EPOLLHUP
|| events
[n
].events
& EPOLLERR
) {
166 printf("GOT %016x event\n", events
[n
].events
);
167 if (epoll_ctl(epollfd
, EPOLL_CTL_DEL
, cfd
, NULL
) == -1) {
168 perror("epoll_ctl: del conn_sock");
171 if (close(cfd
) == -1) {
172 perror("close conn_sock");
186 unlink(MY_SOCK_PATH
);
190 unlink(MY_SOCK_PATH
);