From: Roy Marples Date: Tue, 9 Sep 2014 08:22:10 +0000 (+0000) Subject: Pass SVCNAME from enviromment to hooks so that a service hook X-Git-Tag: v6.4.4~35 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bac1d910f1f0448292ad8f1b1c4f269a1f2a785e;p=thirdparty%2Fdhcpcd.git Pass SVCNAME from enviromment to hooks so that a service hook can know it's name (may not be dhcpcd). --- diff --git a/README b/README index fb6eb8ac..3ee66833 100644 --- 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 ------------- diff --git a/script.c b/script.c index 7e420f2d..db203f4f 100644 --- a/script.c +++ b/script.c @@ -57,6 +57,11 @@ #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);