]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use a local variable instead of the optind (#86)
authorPetr Gotthard <petr.gotthard@centrum.cz>
Mon, 25 Sep 2023 09:00:27 +0000 (11:00 +0200)
committerGitHub <noreply@github.com>
Mon, 25 Sep 2023 09:00:27 +0000 (10:00 +0100)
The optind get overwritten by reload_config(), so the reconf_reboot()
used a wrong argument count.

Signed-off-by: Petr Gotthard <petr.gotthard@centrum.cz>
src/dhcpcd.c

index cc5dfb584c4f0d8453b1bd6ef0ad6aa7d63e1f20..0cc62226a23db6dc36a6d237743eff7a9856c5bd 100644 (file)
@@ -1498,7 +1498,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd,
 {
        struct interface *ifp;
        unsigned long long opts;
-       int opt, oi, do_reboot, do_renew, af = AF_UNSPEC;
+       int opt, oi, oifind, do_reboot, do_renew, af = AF_UNSPEC;
        size_t len, l, nifaces;
        char *tmp, *p;
 
@@ -1514,7 +1514,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd,
                return control_queue(fd, UNCONST(fd->ctx->cffile),
                    strlen(fd->ctx->cffile) + 1);
        } else if (strcmp(*argv, "--getinterfaces") == 0) {
-               optind = argc = 0;
+               oifind = argc = 0;
                goto dumplease;
        } else if (strcmp(*argv, "--listen") == 0) {
                fd->flags |= FD_LISTEN;
@@ -1577,6 +1577,9 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd,
                }
        }
 
+       /* store the index; the optind will change when a getopt get called */
+       oifind = optind;
+
        if (opts & DHCPCD_DUMPLEASE) {
                ctx->options |= DHCPCD_DUMPLEASE;
 dumplease:
@@ -1584,11 +1587,11 @@ dumplease:
                TAILQ_FOREACH(ifp, ctx->ifaces, next) {
                        if (!ifp->active)
                                continue;
-                       for (oi = optind; oi < argc; oi++) {
+                       for (oi = oifind; oi < argc; oi++) {
                                if (strcmp(ifp->name, argv[oi]) == 0)
                                        break;
                        }
-                       if (optind == argc || oi < argc) {
+                       if (oifind == argc || oi < argc) {
                                opt = send_interface(NULL, ifp, af);
                                if (opt == -1)
                                        goto dumperr;
@@ -1600,11 +1603,11 @@ dumplease:
                TAILQ_FOREACH(ifp, ctx->ifaces, next) {
                        if (!ifp->active)
                                continue;
-                       for (oi = optind; oi < argc; oi++) {
+                       for (oi = oifind; oi < argc; oi++) {
                                if (strcmp(ifp->name, argv[oi]) == 0)
                                        break;
                        }
-                       if (optind == argc || oi < argc) {
+                       if (oifind == argc || oi < argc) {
                                if (send_interface(fd, ifp, af) == -1)
                                        goto dumperr;
                        }
@@ -1623,12 +1626,12 @@ dumperr:
        }
 
        if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) {
-               if (optind == argc) {
+               if (oifind == argc) {
                        stop_all_interfaces(ctx, opts);
                        eloop_exit(ctx->eloop, EXIT_SUCCESS);
                        return 0;
                }
-               for (oi = optind; oi < argc; oi++) {
+               for (oi = oifind; oi < argc; oi++) {
                        if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
                                continue;
                        if (!ifp->active)
@@ -1642,11 +1645,11 @@ dumperr:
        }
 
        if (do_renew) {
-               if (optind == argc) {
+               if (oifind == argc) {
                        dhcpcd_renew(ctx);
                        return 0;
                }
-               for (oi = optind; oi < argc; oi++) {
+               for (oi = oifind; oi < argc; oi++) {
                        if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
                                continue;
                        dhcpcd_ifrenew(ifp);
@@ -1656,7 +1659,7 @@ dumperr:
 
        reload_config(ctx);
        /* XXX: Respect initial commandline options? */
-       reconf_reboot(ctx, do_reboot, argc, argv, optind - 1);
+       reconf_reboot(ctx, do_reboot, argc, argv, oifind);
        return 0;
 }