]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
More fixes to dhcp dumping leases.
authorRoy Marples <roy@marples.name>
Tue, 11 Mar 2014 22:37:10 +0000 (22:37 +0000)
committerRoy Marples <roy@marples.name>
Tue, 11 Mar 2014 22:37:10 +0000 (22:37 +0000)
When running dump and test hooks, exit early to allow as non root.

dhcp.c
dhcp.h
dhcp6.c
dhcpcd-hooks/01-test
dhcpcd-hooks/02-dump
dhcpcd.c

diff --git a/dhcp.c b/dhcp.c
index 6221db7ac38397944c03c24d9deb7df851e1e56f..de56dfde496101843bff26ae33a6f26a5cbf7310 100644 (file)
--- 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 ff9411bfecabedf8756229633ef3d8aa226dce72..d9d925e2c441ce15bbecd19184920f9f2a0bcc04 100644 (file)
--- 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 ecfef26bd1e6a17b18a9f6feb5d87381cd9fb3d8..83ab4b38d7498aca85f8e2cef481bce624164424 100644 (file)
--- 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;
index b850039592fffb5c82d49796adf81850599efc90..6e662019d11e16bc24b4f85b8a378a326e5acdff 100644 (file)
@@ -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
index cbb46eae9ec62ef8d89791a006a2c203c272a181..97eab7a5b7a103607251c3079233667688af3d85 100644 (file)
@@ -2,4 +2,5 @@
 
 if [ "$reason" = "DUMP" ]; then
        set | sed -ne 's/^new_//p' | sort
+       exit 0
 fi
index 8ccb89108ec50c16f528257588a027921d560648..64758e5897bf55c79abd219323b8abebc858e01e 100644 (file)
--- 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;
        }