diff options
| author | Roy Marples <roy@marples.name> | 2008-05-13 11:21:19 +0000 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2008-05-13 11:21:19 +0000 |
| commit | 2151620deb3287e809d847bcaf30c461fae049b7 (patch) | |
| tree | 5623bdc54d7d0da3d91a94a2aa3544de1d7e8b66 /configure.c | |
| parent | 1b3bc4775a960dbf65ffe3f540d63d122311f9c4 (diff) | |
| download | dhcpcd-2151620deb3287e809d847bcaf30c461fae049b7.tar.xz | |
We should make our own env instead of using setenv/unsetenv so we work better with vfork systems.
Diffstat (limited to 'configure.c')
| -rw-r--r-- | configure.c | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/configure.c b/configure.c index 1551f874..004a7224 100644 --- a/configure.c +++ b/configure.c @@ -45,11 +45,16 @@ #include "net.h" #include "signal.h" +#define DEFAULT_PATH "PATH=/usr/bin:/usr/sbin:/bin:/sbin" + int exec_script(const char *script, const char *iface, const char *reason, const struct dhcp_message *dhcpn, const struct dhcp_message *dhcpo) { char *const argv[2] = { (char *)script, NULL }; + char **env = NULL, **ep; + char *path; + ssize_t e, elen; int ret = 0; pid_t pid; int status = 0; @@ -58,6 +63,38 @@ exec_script(const char *script, const char *iface, const char *reason, logger(LOG_DEBUG, "exec `%s'", script); + /* Make our env */ + elen = 3; + env = xmalloc(sizeof(char *) * (elen + 1)); + path = getenv("PATH"); + if (path) { + e = strlen("PATH") + strlen(path) + 2; + env[0] = xmalloc(e); + snprintf(env[0], e, "PATH=%s", path); + } else + env[0] = xstrdup(DEFAULT_PATH); + e = strlen("interface") + strlen(iface) + 2; + env[1] = xmalloc(e); + snprintf(env[1], e, "interface=%s", iface); + e = strlen("reason") + strlen(reason) + 2; + env[2] = xmalloc(e); + snprintf(env[2], e, "reason=%s", reason); + if (dhcpo) { + e = configure_env(NULL, NULL, dhcpo); + if (e > 0) { + env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + elen += configure_env(env + elen, "old", dhcpo); + } + } + if (dhcpn) { + e = configure_env(NULL, NULL, dhcpn); + if (e > 0) { + env = xrealloc(env, sizeof(char *) * (elen + e + 1)); + elen += configure_env(env + elen, "new", dhcpn); + } + } + env[elen] = '\0'; + /* OK, we need to block signals */ sigfillset(&full); sigprocmask(SIG_SETMASK, &full, &old); @@ -79,13 +116,7 @@ exec_script(const char *script, const char *iface, const char *reason, signal_reset(); #endif sigprocmask(SIG_SETMASK, &old, NULL); - if (dhcpo) - configure_env("old", dhcpo); - if (dhcpn) - configure_env("new", dhcpn); - setenv("interface", iface, 1); - setenv("reason", reason, 1); - execvp(script, argv); + execve(script, argv, env); logger(LOG_ERR, "%s: %s", script, strerror(errno)); _exit(111); /* NOTREACHED */ @@ -107,6 +138,12 @@ exec_script(const char *script, const char *iface, const char *reason, } } + /* Cleanup */ + ep = env; + while (*ep) + free(*ep++); + free(env); + return status; } |
