From 36d79a90c28ac9d655ccd8261b3ac0b497a5aec1 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Sun, 14 Jul 2013 11:56:32 +0200 Subject: the cloexec logic of the watchdog was broken Signed-off-by: John Crispin --- main.c | 2 +- state.c | 7 +++---- watchdog.c | 12 ++++++++---- watchdog.h | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index a1b4fdb..49d85f4 100644 --- a/main.c +++ b/main.c @@ -40,7 +40,7 @@ static int main_procd_init(int argc, char **argv) procd_signal_preinit(); procd_early(); debug_init(); - watchdog_init(); + watchdog_init(1); system("/sbin/kmodloader /etc/modules-boot.d/"); uloop_init(); hotplug("/etc/hotplug-preinit.json"); diff --git a/state.c b/state.c index 71890a2..9cde905 100644 --- a/state.c +++ b/state.c @@ -40,15 +40,14 @@ static void state_enter(void) switch (state) { case STATE_EARLY: LOG("- early -\n"); - watchdog_init(); + watchdog_init(0); hotplug("/etc/hotplug.json"); procd_coldplug(); break; case STATE_INIT: - // check if the wdt appeared during coldplug - if (!watchdog_fd()) - watchdog_init(); + // try to reopen incase the wdt was not available before coldplug + watchdog_init(0); LOG("- init -\n"); log_init(); procd_connect_ubus(); diff --git a/watchdog.c b/watchdog.c index dc54308..d927c53 100644 --- a/watchdog.c +++ b/watchdog.c @@ -91,24 +91,28 @@ char* watchdog_fd(void) return fd_buf; } -void watchdog_init(void) +void watchdog_init(int preinit) { char *env = getenv("WDTFD"); + if (wdt_fd >= 0) + return; wdt_timeout.cb = watchdog_timeout_cb; if (env) { DEBUG(1, "Watchdog handover: fd=%s\n", env); wdt_fd = atoi(env); unsetenv("WDTFD"); - fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC); } else { wdt_fd = open("/dev/watchdog", O_WRONLY); - if ((getpid() != 1) && (wdt_fd >= 0)) - fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC); } + if (wdt_fd < 0) return; + + if (!preinit) + fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC); + LOG("- watchdog -\n"); watchdog_timeout(30); watchdog_timeout_cb(&wdt_timeout); diff --git a/watchdog.h b/watchdog.h index 66037b1..cebbc33 100644 --- a/watchdog.h +++ b/watchdog.h @@ -15,7 +15,7 @@ #ifndef __PROCD_WATCHDOG_H #define __PROCD_WATCHDOG_H -void watchdog_init(void); +void watchdog_init(int preinit); char* watchdog_fd(void); int watchdog_timeout(int timeout); int watchdog_frequency(int frequency); -- cgit v1.2.3