From: Roy Marples Date: Tue, 11 Mar 2014 22:37:10 +0000 (+0000) Subject: More fixes to dhcp dumping leases. X-Git-Tag: v6.3.2~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9cf6e1039b06dabb8ffa7a6e08548fb4ed8781d6;p=thirdparty%2Fdhcpcd.git More fixes to dhcp dumping leases. When running dump and test hooks, exit early to allow as non root. --- diff --git a/dhcp.c b/dhcp.c index 6221db7a..de56dfde 100644 --- a/dhcp.c +++ b/dhcp.c @@ -2678,34 +2678,32 @@ dhcp_open(struct interface *ifp) } int -dhcp_dump(const char *ifname) +dhcp_dump(struct dhcpcd_ctx *ctx, const char *ifname) { - struct dhcpcd_ctx ctx; struct interface *ifp; struct dhcp_state *state; - int r; - ifp = NULL; + if (ctx->ifaces == NULL) { + ctx->ifaces = malloc(sizeof(*ctx->ifaces)); + if (ctx->ifaces == NULL) + return -1; + TAILQ_INIT(ctx->ifaces); + } state = NULL; - memset(&ctx, 0, sizeof(ctx)); - ctx.ifaces = malloc(sizeof(*ctx.ifaces)); - if (ctx.ifaces == NULL) - goto eexit; - TAILQ_INIT(ctx.ifaces); ifp = calloc(1, sizeof(*ifp)); if (ifp == NULL) goto eexit; - ifp->ctx = &ctx; - TAILQ_INSERT_HEAD(ctx.ifaces, ifp, next); + ifp->ctx = ctx; + TAILQ_INSERT_HEAD(ctx->ifaces, ifp, next); ifp->if_data[IF_DATA_DHCP] = state = calloc(1, sizeof(*state)); if (state == NULL) goto eexit; ifp->options = calloc(1, sizeof(*ifp->options)); if (ifp->options == NULL) goto eexit; + strlcpy(ifp->name, ifname, sizeof(ifp->name)); snprintf(state->leasefile, sizeof(state->leasefile), LEASEFILE, ifp->name); - strlcpy(ifp->options->script, SCRIPT, sizeof(ifp->options->script)); state->new = read_lease(ifp); if (state->new == NULL && errno == ENOENT) { strlcpy(state->leasefile, ifname, sizeof(state->leasefile)); @@ -2714,28 +2712,14 @@ dhcp_dump(const char *ifname) if (state->new == NULL) { if (errno == ENOENT) syslog(LOG_ERR, "%s: no lease to dump", ifname); - r = -1; - goto cexit; + return -1; } state->reason = "DUMP"; - r = script_runreason(ifp, state->reason); - goto cexit; + return script_runreason(ifp, state->reason); eexit: syslog(LOG_ERR, "%s: %m", __func__); - r = -1; - -cexit: - if (state) { - free(state->new); - free(state); - } - if (ifp) { - free(ifp->options); - free(ifp); - } - free(ctx.ifaces); - return r; + return -1; } void diff --git a/dhcp.h b/dhcp.h index ff9411bf..d9d925e2 100644 --- a/dhcp.h +++ b/dhcp.h @@ -285,7 +285,7 @@ void dhcp_bind(void *); void dhcp_reboot_newopts(struct interface *, int); void dhcp_close(struct interface *); void dhcp_free(struct interface *); -int dhcp_dump(const char *); +int dhcp_dump(struct dhcpcd_ctx *, const char *); #else #define dhcp_printoptions #define dhcp_drop(a, b) diff --git a/dhcp6.c b/dhcp6.c index ecfef26b..83ab4b38 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -2648,7 +2648,8 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason) struct dhcpcd_ctx *ctx; unsigned long long options; - eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); + if (ifp->ctx->eloop) + eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); if (ifp->options) options = ifp->options->options; diff --git a/dhcpcd-hooks/01-test b/dhcpcd-hooks/01-test index b8500395..6e662019 100644 --- a/dhcpcd-hooks/01-test +++ b/dhcpcd-hooks/01-test @@ -4,4 +4,5 @@ if [ "$reason" = "TEST" ]; then set | grep "^\(interface\|pid\|reason\|skip_hooks\)=" | sort set | grep "^if\(carrier\|flags\|mtu\|wireless\)=" | sort set | grep "^\(new_\|old_\|ra_count=\|ra[0-9]*_\)" | sort + exit 0 fi diff --git a/dhcpcd-hooks/02-dump b/dhcpcd-hooks/02-dump index cbb46eae..97eab7a5 100644 --- a/dhcpcd-hooks/02-dump +++ b/dhcpcd-hooks/02-dump @@ -2,4 +2,5 @@ if [ "$reason" = "DUMP" ]; then set | sed -ne 's/^new_//p' | sort + exit 0 fi diff --git a/dhcpcd.c b/dhcpcd.c index 8ccb8910..64758e58 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -1275,7 +1275,7 @@ main(int argc, char **argv) syslog(LOG_ERR, "dumplease requires an interface"); goto exit_failure; } - if (dhcp_dump(argv[optind]) == -1) + if (dhcp_dump(&ctx, argv[optind]) == -1) goto exit_failure; goto exit_success; }