From 3a1ad67e920e594968029c79668e930ff02bd6dd Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 8 Jul 2018 19:21:49 +0200 Subject: [PATCH] Convert built-in watchdog to a standalone mini watchdogd Signed-off-by: Joachim Nilsson --- configure.ac | 8 ++++--- src/Makefile.am | 12 +++++++--- src/finit.c | 12 +++++++--- src/watchdog.c | 59 +++++++++++++++++++++++++++---------------------- 4 files changed, 55 insertions(+), 36 deletions(-) diff --git a/configure.ac b/configure.ac index 8a2fc124..0e4ecf3a 100644 --- a/configure.ac +++ b/configure.ac @@ -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]) diff --git a/src/Makefile.am b/src/Makefile.am index 0d947efc..7880cc63 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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) diff --git a/src/finit.c b/src/finit.c index e62bcb8d..5ab8891f 100644 --- a/src/finit.c +++ b/src/finit.c @@ -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 diff --git a/src/watchdog.c b/src/watchdog.c index 2c1e4ff1..54145e70 100644 --- a/src/watchdog.c +++ b/src/watchdog.c @@ -21,12 +21,18 @@ * SOFTWARE. */ +#include +#include +#include +#include +#include +#include +#include #include +#include #include #include -#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; } /**