From: Roy Marples Date: Sun, 13 Apr 2008 11:27:41 +0000 (+0000) Subject: As we're only dealing with dhcpcd.sh now, the exec_cmd function doesn't need to be... X-Git-Tag: v4.0.2~504 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e1dcde7a8c6452bd02de7728924d21b4f0d985c4;p=thirdparty%2Fdhcpcd.git As we're only dealing with dhcpcd.sh now, the exec_cmd function doesn't need to be so complex. --- diff --git a/client.c b/client.c index 087ba1f0..10c37f44 100644 --- a/client.c +++ b/client.c @@ -835,7 +835,7 @@ handle_timeout(struct if_state *state, const struct options *options) IN_LINKLOCAL(ntohl(lease->addr.s_addr))) logger(LOG_WARNING, "using IPV4LL address %s", inet_ntoa(lease->addr)); - if (configure(iface, state->dhcp, lease, options, 1) == -1 && + if (configure(iface, state->dhcp, lease, options, 1) != 0 && !(state->options & DHCPCD_DAEMONISED)) return -1; @@ -930,9 +930,10 @@ handle_timeout(struct if_state *state, const struct options *options) } static int -handle_dhcp(struct if_state *state, struct dhcp_message *dhcp, const struct options *options) +handle_dhcp(struct if_state *state, struct dhcp_message **dhcpp, const struct options *options) { struct timespec ts; + struct dhcp_message *dhcp = *dhcpp; struct interface *iface = state->interface; struct dhcp_lease *lease = &state->lease; char *addr; @@ -951,7 +952,6 @@ handle_dhcp(struct if_state *state, struct dhcp_message *dhcp, const struct opti addr = get_option_string(dhcp, DHCP_MESSAGE); logger(LOG_INFO, "received NAK: %s", addr); free(addr); - free(dhcp); state->state = STATE_INIT; state->timeout = 0; state->xid = 0; @@ -1063,6 +1063,7 @@ handle_dhcp(struct if_state *state, struct dhcp_message *dhcp, const struct opti if (state->dhcp) free(state->dhcp); state->dhcp = dhcp; + *dhcpp = NULL; if (options->options & DHCPCD_INFORM) { if (options->request_address.s_addr != 0) @@ -1133,13 +1134,12 @@ handle_dhcp(struct if_state *state, struct dhcp_message *dhcp, const struct opti } state->xid = 0; - - if (configure(iface, dhcp, &state->lease, options, 1) == -1 && - !(state->options & DHCPCD_DAEMONISED)) + if (configure(iface, dhcp, &state->lease, options, 1) != 0) return -1; - if (!(state->options & DHCPCD_DAEMONISED) - && state->options & DHCPCD_DAEMONISE) { + if (!(state->options & DHCPCD_DAEMONISED) && + state->options & DHCPCD_DAEMONISE) + { switch (daemonise(state->pidfd)) { case 0: state->options |= DHCPCD_DAEMONISED; @@ -1188,15 +1188,11 @@ handle_packet(struct if_state *state, const struct options *options) dhcp->xid, state->xid); continue; } - if (handle_dhcp(state, dhcp, options) == 0) + if (handle_dhcp(state, &dhcp, options) == 0) return 0; } while (state->buffer_pos != 0); - - if (state->options & DHCPCD_FORKED) - return -1; - free(dhcp); - return 0; + return -1; } int diff --git a/configure.c b/configure.c index 4eea2ce4..eca2a27c 100644 --- a/configure.c +++ b/configure.c @@ -33,7 +33,6 @@ #include #include -#include #include #include @@ -46,12 +45,10 @@ #include "net.h" #include "signal.h" -int -exec_cmd(const char *cmd, const char *args, ...) +static int +exec_script(const char *cmd, const char *arg1, const char *arg2) { - va_list va; - char **argv; - int n = 1; + char *const argv[4] = { (char *)cmd, (char *)arg1, (char *)arg2, NULL}; int ret = 0; pid_t pid; pid_t wpid; @@ -59,19 +56,7 @@ exec_cmd(const char *cmd, const char *args, ...) sigset_t full; sigset_t old; - va_start(va, args); - while (va_arg(va, char *) != NULL) - n++; - va_end(va); - argv = xmalloc(sizeof(char *) * (n + 2)); - - va_start(va, args); - n = 2; - argv[0] = (char *)cmd; - argv[1] = (char *)args; - while ((argv[n] = va_arg(va, char *)) != NULL) - n++; - va_end(va); + logger(LOG_DEBUG, "exec `%s' `%s' `%s'", cmd, arg1, arg2); /* OK, we need to block signals */ sigfillset(&full); @@ -86,17 +71,16 @@ exec_cmd(const char *cmd, const char *args, ...) switch (pid) { case -1: - logger(LOG_ERR, "vfork: %s", strerror(errno)); + logger(LOG_ERR, "fork: %s", strerror(errno)); ret = -1; break; case 0: #ifndef THERE_IS_NO_FORK signal_reset(); #endif - sigprocmask (SIG_SETMASK, &old, NULL); - if (execvp(cmd, argv) && errno != ENOENT) - logger (LOG_ERR, "error executing \"%s\": %s", - cmd, strerror(errno)); + sigprocmask(SIG_SETMASK, &old, NULL); + execvp(cmd, argv); + logger(LOG_ERR, "%s: %s", cmd, strerror(errno)); _exit(111); /* NOTREACHED */ } @@ -107,7 +91,6 @@ exec_cmd(const char *cmd, const char *args, ...) /* Restore our signals */ sigprocmask(SIG_SETMASK, &old, NULL); - free(argv); /* Wait for the script to finish */ do { @@ -120,27 +103,9 @@ exec_cmd(const char *cmd, const char *args, ...) ret = WEXITSTATUS(status); else ret = -1; - if (ret != 0) - logger(LOG_ERR, "%s exited non zero", cmd); return ret; } -/* IMPORTANT: Ensure that the last parameter is NULL when calling */ -static void -exec_script(const char *script, _unused const char *infofile, const char *arg) -{ - struct stat buf; - - if (stat(script, &buf) == -1) { - if (strcmp(script, DEFAULT_SCRIPT) != 0) - logger(LOG_ERR, "`%s': %s", script, strerror(ENOENT)); - return; - } - - logger(LOG_DEBUG, "exec \"%s\" \"%s\" \"%s\"", script, infofile, arg); - exec_cmd(script, infofile, arg, (char *)NULL); -} - static struct rt * reverse_routes(struct rt *routes) { @@ -550,7 +515,6 @@ configure(struct interface *iface, const struct dhcp_message *dhcp, } exec_script(options->script, iface->infofile, "down"); - return 0; } @@ -607,6 +571,5 @@ configure(struct interface *iface, const struct dhcp_message *dhcp, logger(LOG_ERR, "write_lease: %s", strerror(errno)); exec_script(options->script, iface->infofile, up ? "new" : "up"); - return 0; } diff --git a/configure.h b/configure.h index ec563107..61a89431 100644 --- a/configure.h +++ b/configure.h @@ -32,7 +32,6 @@ #include "dhcp.h" #include "net.h" -int exec_cmd(const char *, const char *, ...); int write_info(const struct interface *, const struct dhcp_message *, const struct dhcp_lease *, const struct options *, int); int configure(struct interface *, const struct dhcp_message *,