Convert built-in watchdog to a standalone mini watchdogd

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2018-07-09 23:49:04 +02:00
parent 43942ab513
commit 3a1ad67e92
4 changed files with 55 additions and 36 deletions
+5 -3
View File
@@ -218,9 +218,11 @@ AM_CONDITIONAL(INETD, [test "x$enable_inetd" = "xyes"])
AM_CONDITIONAL(WATCHDOGD, [test "x$enable_watchdog" = "xyes"])
# Override default libdir, used for plugins and rescue.conf
#pkglibdir=$libdir/finit
#AC_SUBST(pkglibdir)
#AC_EXPAND_DIR(pkglibdir, "$libdir/finit")
pkglibdir=$libdir/finit
AC_SUBST(pkglibexecdir)
AC_EXPAND_DIR(pkglibexecdir, "$libexecdir/finit")
AC_DEFINE_UNQUOTED(FINIT_LIBPATH_, "$pkglibexecdir", [Finit libexec path])
AC_EXPAND_DIR(plugin_path, "$libdir/finit/plugins")
AC_SUBST(plugin_path)
AC_DEFINE_UNQUOTED(PLUGIN_PATH, "$plugin_path", [Finit plugin path])
+9 -3
View File
@@ -10,6 +10,9 @@ endif
bin_PROGRAMS = logit
sbin_PROGRAMS = finit initctl reboot
if WATCHDOGD
pkglibexec_PROGRAMS = watchdogd
endif
logit_SOURCES = logit.c
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
@@ -36,9 +39,6 @@ pkginclude_HEADERS = cond.h finit.h helpers.h inetd.h log.h plugin.h svc.h
if INETD
finit_SOURCES += inetd.c inetd.h
endif
if WATCHDOGD
finit_SOURCES += watchdog.c watchdog.h
endif
finit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
finit_CFLAGS += $(lite_CFLAGS) $(uev_CFLAGS)
@@ -63,6 +63,12 @@ reboot_LDADD = $(lite_LIBS)
INIT_LNKS = telinit init
REBOOT_LNKS = shutdown halt poweroff suspend
if WATCHDOGD
watchdogd_SOURCES = watchdog.c watchdog.h
watchdogd_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
#log.c log.h
endif
# Hook in install to add finit and reboot symlink(s)
install-exec-hook:
@$(INSTALL_DATA) $(srcdir)/rescue.conf $(DESTDIR)$(pkglibdir)
+9 -3
View File
@@ -47,7 +47,6 @@
#include "tty.h"
#include "util.h"
#include "utmp-api.h"
#include "watchdog.h"
int wdogpid = 0; /* No watchdog by default */
int runlevel = 0; /* Bootstrap 'S' */
@@ -214,12 +213,18 @@ static void emergency_shell(void)
*/
static void finalize(void)
{
svc_t *svc;
/*
* Run startup scripts in the runparts directory, if any.
*/
if (runparts && fisdir(runparts) && !rescue)
run_parts(runparts, NULL);
svc = svc_find(FINIT_LIBPATH_ "watchdogd", 1);
if (svc && !wdogpid)
wdogpid = svc->pid;
/*
* Start all tasks/services in the configured runlevel
*/
@@ -412,9 +417,10 @@ int main(int argc, char* argv[])
}
/*
* Start built-in watchdog as soon as possible, if enabled
* Start bundled watchdogd as soon as possible, if enabled
*/
wdogpid = watchdog(argv[0]);
if (which(FINIT_LIBPATH_ "/watchdogd"))
service_register(SVC_TYPE_SERVICE, FINIT_LIBPATH_ "/watchdogd", global_rlimit, NULL);
/*
* Mount filesystems
+32 -27
View File
@@ -21,12 +21,18 @@
* SOFTWARE.
*/
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <linux/watchdog.h>
#include "finit.h"
#include "log.h"
#include "watchdog.h"
int running = 1;
@@ -51,6 +57,9 @@ static int init(char *progname, char *devnode)
signal(SIGTERM, sighandler);
signal(SIGPWR, sighandler);
openlog(&progname[1], LOG_CONS | LOG_PID, LOG_DAEMON);
syslog(LOG_INFO, "Finit v%s basic watchdogd starting ...", VERSION);
fd = open(devnode, O_WRONLY);
if (fd == -1)
return -1;
@@ -78,6 +87,7 @@ static int loop(int fd, int timeout)
/* External watchdogd wants to take over ... */
if (handover) {
syslog(LOG_INFO, "Handing over %s and exiting ...", WDT_DEVNODE);
ioctl(fd, WDIOC_KEEPALIVE, &dummy);
return !write(fd, "V", 1);
}
@@ -85,35 +95,30 @@ static int loop(int fd, int timeout)
return 0;
}
int watchdog(char *progname)
int main(int argc, char *argv[])
{
int pid;
int fd, ret;
pid = fork();
if (pid == 0) {
int fd, ret;
fd = init(progname, WDT_DEVNODE);
if (fd == -1) {
if (ENOENT != errno)
_pe("Failed connecting to watchdog %s", WDT_DEVNODE);
_exit(1);
}
ret = loop(fd, WDT_TIMEOUT);
while (!handover) {
/* Waiting for SIGTERM ... */
sleep(1);
/* Set lowest possible timeout on SIGTERM */
ioctl(fd, WDIOC_SETTIMEOUT, &shutdown);
}
close(fd);
_exit(ret);
fd = init(argv[0], WDT_DEVNODE);
if (fd == -1) {
if (ENOENT != errno)
syslog(LOG_CRIT, "Failed connecting to watchdog %s", WDT_DEVNODE);
return 1;
}
return pid;
ret = loop(fd, WDT_TIMEOUT);
while (!handover) {
syslog(LOG_ALERT, "System going down ...");
/* Waiting for SIGTERM ... */
sleep(1);
/* Set lowest possible timeout on SIGTERM */
ioctl(fd, WDIOC_SETTIMEOUT, &shutdown);
}
close(fd);
return ret;
}
/**