+#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <systemd/sd-daemon.h>
-#define MY_SOCK_PATH "/run/watchdog-mux.sock"
+#define WD_SOCK_PATH "/run/watchdog-mux.sock"
#define WD_ACTIVE_MARKER "/run/watchdog-mux.active"
#define LISTEN_BACKLOG 32 /* set same value in watchdog-mux.socket */
socklen_t peer_addr_size;
struct epoll_event ev, events[MAX_EVENTS];
int socket_count, listen_sock, nfds, epollfd, sigfd;
-
+ int unlink_socket = 0;
struct stat fs;
fprintf(stderr, "watchdog active - unable to restart watchdog-mux\n");
exit(EXIT_FAILURE);
}
-
+
+ /* if you want to debug, set options in /lib/modprobe.d/aliases.conf
+ * options softdog soft_noboot=1
+ */
if (stat(WATCHDOG_DEV, &fs) == -1) {
- system("modprobe -q softdog soft_noboot=1"); // fixme
+ char *wd_module = getenv("WATCHDOG_MODULE");
+ if (wd_module) {
+ char *cmd = NULL;
+ if ((asprintf(&cmd, "modprobe -q %s", wd_module) == -1)) {
+ perror("assemble modprobe command failed");
+ exit(EXIT_FAILURE);
+ }
+ system(cmd);
+ } else {
+ system("modprobe -q softdog"); // load softdog by default
+ }
}
if ((watchdog_fd = open(WATCHDOG_DEV, O_WRONLY)) == -1) {
wdinfo.identity, wdinfo.firmware_version);
socket_count = sd_listen_fds(0);
-
+
if (socket_count > 1) {
perror("too many file descriptors received.\n");
} else if (socket_count == 1) {
listen_sock = SD_LISTEN_FDS_START + 0;
-
+
} else {
- unlink(MY_SOCK_PATH);
+ unlink_socket = 1;
+
+ unlink(WD_SOCK_PATH);
listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (listen_sock == -1) {
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
- strncpy(my_addr.sun_path, MY_SOCK_PATH, sizeof(my_addr.sun_path) - 1);
+ strncpy(my_addr.sun_path, WD_SOCK_PATH, sizeof(my_addr.sun_path) - 1);
if (bind(listen_sock, (struct sockaddr *) &my_addr,
sizeof(struct sockaddr_un)) == -1) {
watchdog_close();
}
- unlink(MY_SOCK_PATH);
+ if (unlink_socket)
+ unlink(WD_SOCK_PATH);
+
exit(EXIT_SUCCESS);
err:
- unlink(MY_SOCK_PATH);
+ if (unlink_socket)
+ unlink(WD_SOCK_PATH);
+
exit(EXIT_FAILURE);
}