]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Pass SVCNAME from enviromment to hooks so that a service hook
authorRoy Marples <roy@marples.name>
Tue, 9 Sep 2014 08:22:10 +0000 (08:22 +0000)
committerRoy Marples <roy@marples.name>
Tue, 9 Sep 2014 08:22:10 +0000 (08:22 +0000)
can know it's name (may not be dhcpcd).

README
script.c

diff --git a/README b/README
index fb6eb8acd4517f03c876a1bc73004ffec74948f6..3ee66833891dae047204dbf9e141d71f1912c03b 100644 (file)
--- a/README
+++ b/README
@@ -97,6 +97,20 @@ The configure program attempts to find hooks for systems you have installed.
 To add more simply
 ./configure -with-hook=ntp.conf
 
+Some system services expose the name of the service we are in,
+by default dhcpcd will pick SVCNAME from the environment.
+You can override this in CPPFLAGS+= -DSVCNAME="RC_SVCNAME".
+This is important because dhcpcd will scrub the environment aside from $PATH
+before running hooks.
+This variable could be used to facilitate service re-entry so this chain could
+happen in a custom OS hook:
+  dhcpcd service marked inactive && dhcpcd service starts
+  dependant services are not started because dhcpcd is inactive (not stopped)
+  dhcpcd hook tests $if_oneup && $if_ipwaited
+  if true, mark the dhcpcd service as started and then start dependencies
+  if false and the dhcpcd service was previously started, mark as inactive and
+     stop any dependant services.
+
 
 Compatibility
 -------------
index 7e420f2d5bcb2777fad28ac037cfde2a68e2ff29..db203f4f501fba04bf102fb824e02b5fdaeef8af 100644 (file)
--- a/script.c
+++ b/script.c
 #include "compat/posix_spawn.h"
 #endif
 
+/* Allow the OS to define another script env var name */
+#ifndef SVCNAME
+#define SVCNAME "SVCNAME"
+#endif
+
 #define DEFAULT_PATH   "PATH=/usr/bin:/usr/sbin:/bin:/sbin"
 
 static const char * const if_params[] = {
@@ -598,7 +603,7 @@ script_runreason(const struct interface *ifp, const char *reason)
 {
        char *argv[2];
        char **env = NULL, **ep;
-       char *path, *bigenv;
+       char *svcname, *path, *bigenv;
        size_t e, elen = 0;
        pid_t pid;
        int status = 0;
@@ -620,7 +625,9 @@ script_runreason(const struct interface *ifp, const char *reason)
                syslog(LOG_ERR, "%s: make_env: %m", ifp->name);
                return -1;
        }
-       ep = realloc(env, sizeof(char *) * (elen + 2));
+       /* Resize for PATH and SVCNAME */
+       svcname = getenv(SVCNAME);
+       ep = realloc(env, sizeof(char *) * (elen + 2 + (svcname ? 1 : 0)));
        if (ep == NULL) {
                elen = 0;
                goto out;
@@ -643,6 +650,16 @@ script_runreason(const struct interface *ifp, const char *reason)
                        goto out;
                }
        }
+       ep = env;
+       if (svcname) {
+               e = strlen(SVCNAME) + strlen(svcname) + 2;
+               env[++elen] = malloc(e);
+               if (env[elen] == NULL) {
+                       elen = 0;
+                       goto out;
+               }
+               snprintf(env[elen], e, "%s=%s", SVCNAME, svcname);
+       }
        env[++elen] = NULL;
 
        pid = exec_script(ifp->ctx, argv, env);