]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
modules-load: use RET_GATHER, update error handling
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 9 Jul 2023 19:35:33 +0000 (13:35 -0600)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 13 Jul 2023 09:40:15 +0000 (11:40 +0200)
We would try to return the first error, but conf_files_list_nulstr()
failure would terminate the app immediately. So let's just return the
error from conf_files_list_nulstr(), and the first error encountered for
other failures after which we continue.

src/modules-load/modules-load.c

index 8e7f7a5ebae044eade763a27d2db414339524f36..4adeef3afbddcf974a490219a40a60ab6375007b 100644 (file)
@@ -97,8 +97,7 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent
                 k = module_load_and_warn(ctx, l, true);
                 if (k == -ENOENT)
                         continue;
-                if (k < 0 && r >= 0)
-                        r = k;
+                RET_GATHER(r, k);
         }
 
         return r;
@@ -186,11 +185,8 @@ static int run(int argc, char *argv[]) {
         r = 0;
 
         if (argc > optind) {
-                for (int i = optind; i < argc; i++) {
-                        k = apply_file(ctx, argv[i], false);
-                        if (k < 0 && r == 0)
-                                r = k;
-                }
+                for (int i = optind; i < argc; i++)
+                        RET_GATHER(r, apply_file(ctx, argv[i], false));
 
         } else {
                 _cleanup_strv_free_ char **files = NULL;
@@ -199,23 +195,15 @@ static int run(int argc, char *argv[]) {
                         k = module_load_and_warn(ctx, *i, true);
                         if (k == -ENOENT)
                                 continue;
-                        if (k < 0 && r == 0)
-                                r = k;
+                        RET_GATHER(r, k);
                 }
 
                 k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
-                if (k < 0) {
-                        log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
-                        if (r == 0)
-                                r = k;
-                        return r;
-                }
+                if (k < 0)
+                        return log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
 
-                STRV_FOREACH(fn, files) {
-                        k = apply_file(ctx, *fn, true);
-                        if (k < 0 && r == 0)
-                                r = k;
-                }
+                STRV_FOREACH(fn, files)
+                        RET_GATHER(r, apply_file(ctx, *fn, true));
         }
 
         return r;