]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
tools/rmmod: rework control flow
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 9 Oct 2024 15:26:25 +0000 (16:26 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 15 Oct 2024 18:37:11 +0000 (13:37 -0500)
Currently the control flow we use is a bit tricky:

 - we use a goto within a loop, to avoid one function call
 - we increment r, assuming it cannot wrap around

In practise these are not bugs, although make the code more convoluted
than needed. While in here, scope the local err to where it's used.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/180
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
tools/rmmod.c

index ed985a8cd3e69bb9652536eb814a6c57cd0cca48..5dd7aa3a443c2168490cb68b0a5fab10dfbf839a 100644 (file)
@@ -96,7 +96,7 @@ static int do_rmmod(int argc, char *argv[])
        int verbose = LOG_ERR;
        int use_syslog = 0;
        int flags = 0;
-       int i, err, r = 0;
+       int i, r = 0;
 
        for (;;) {
                int c, idx = 0;
@@ -148,6 +148,8 @@ static int do_rmmod(int argc, char *argv[])
                struct kmod_module *mod;
                const char *arg = argv[i];
                struct stat st;
+               int err;
+
                if (stat(arg, &st) == 0)
                        err = kmod_module_new_from_path(ctx, arg, &mod);
                else
@@ -160,16 +162,17 @@ static int do_rmmod(int argc, char *argv[])
                }
 
                if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
-                       r++;
-                       goto next;
+                       r = EXIT_FAILURE;
+                       kmod_module_unref(mod);
+                       continue;
                }
 
                err = kmod_module_remove_module(mod, flags);
                if (err < 0) {
                        ERR("could not remove module %s: %s\n", arg, strerror(-err));
-                       r++;
+                       r = EXIT_FAILURE;
                }
-next:
+
                kmod_module_unref(mod);
        }