[PATCH 5/5] initd: Convert the watchdog_fd return value from char* to int

Michael Jones mike at meshplusplus.com
Tue Sep 29 12:22:24 EDT 2020


This change improves the frequently called path of determining
if the watchdog is alive when responding to ubus transactions
at the expense of complicating the less frequently called code
of transitioning to the upgraded binary, and transitioning from
pre-init to procd.

Signed-off-by: Michael Jones <mike at meshplusplus.com>
---
 initd/preinit.c | 11 ++++++++---
 system.c        |  2 +-
 sysupgrade.c    |  8 +++++---
 watchdog.c      | 11 ++---------
 watchdog.h      |  6 +++---
 5 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/initd/preinit.c b/initd/preinit.c
index 9dfe5c1..255ceeb 100644
--- a/initd/preinit.c
+++ b/initd/preinit.c
@@ -90,7 +90,7 @@ fail:
 static void
 spawn_procd(struct uloop_process *proc, int ret)
 {
-    char *wdt_fd = watchdog_fd();
+    int wdt_fd = watchdog_fd();
     char *argv[] = { "/sbin/procd", NULL};
     char dbg[2];
 
@@ -104,8 +104,13 @@ spawn_procd(struct uloop_process *proc, int ret)
     check_sysupgrade();
 
     DEBUG(2, "Exec to real procd now\n");
-    if (wdt_fd)
-        setenv("WDTFD", wdt_fd, 1);
+
+    if (wdt_fd >= 0) {
+        char fd_buf[12];
+        snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
+        setenv("WDTFD", fd_buf, 1);
+    }
+
     check_dbglvl();
     if (debug > 0) {
         snprintf(dbg, 2, "%d", debug);
diff --git a/system.c b/system.c
index ef7943a..caae0ec 100644
--- a/system.c
+++ b/system.c
@@ -378,7 +378,7 @@ static int watchdog_set(struct ubus_context *ctx,
struct ubus_object *obj,
     if (tb[WDT_STOP])
         watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
 
-    if (watchdog_fd() == NULL)
+    if (watchdog_fd() < 0)
         status = "offline";
     else if (watchdog_get_stopped())
         status = "stopped";
diff --git a/sysupgrade.c b/sysupgrade.c
index fc588b0..20e1ef0 100644
--- a/sysupgrade.c
+++ b/sysupgrade.c
@@ -29,7 +29,7 @@ void sysupgrade_exec_upgraded(const char *prefix, char
*path,
                   const char *backup, char *command,
                   struct blob_attr *options)
 {
-    char *wdt_fd = watchdog_fd();
+    int wdt_fd = watchdog_fd();
     char *argv[] = { "/sbin/upgraded", NULL, NULL, NULL};
     struct blob_attr *option;
     int rem;
@@ -44,9 +44,11 @@ void sysupgrade_exec_upgraded(const char *prefix,
char *path,
     argv[1] = path;
     argv[2] = command;
 
-    if (wdt_fd) {
+    if (wdt_fd >= 0) {
+        char fd_buf[12];
+        snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
         watchdog_set_cloexec(false);
-        setenv("WDTFD", wdt_fd, 1);
+        setenv("WDTFD", fd_buf, 1);
     }
 
     if (backup)
diff --git a/watchdog.c b/watchdog.c
index ac5b656..3c565d2 100644
--- a/watchdog.c
+++ b/watchdog.c
@@ -146,16 +146,9 @@ int watchdog_frequency(int frequency)
     return wdt_frequency;
 }
 
-char* watchdog_fd(void)
+int watchdog_fd(void)
 {
-    static char fd_buf[12];
-
-    if (wdt_fd < 0)
-        return NULL;
-
-    snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
-
-    return fd_buf;
+    return wdt_fd;
 }
 
 void watchdog_init(int preinit)
diff --git a/watchdog.h b/watchdog.h
index 73c75d5..3c35d9a 100644
--- a/watchdog.h
+++ b/watchdog.h
@@ -19,7 +19,7 @@
 
 #ifndef DISABLE_INIT
 void watchdog_init(int preinit);
-char* watchdog_fd(void);
+int watchdog_fd(void);
 int watchdog_timeout(int timeout);
 int watchdog_frequency(int frequency);
 void watchdog_set_magicclose(bool val);
@@ -33,9 +33,9 @@ static inline void watchdog_init(int preinit)
 {
 }
 
-static inline char* watchdog_fd(void)
+static inline int watchdog_fd(void)
 {
-    return "";
+    return -1;
 }
 
 static inline int watchdog_timeout(int timeout)
-- 
2.26.2




More information about the openwrt-devel mailing list