]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/modules-load/modules-load.c
Merge pull request #30633 from mrc0mmand/cocci-shenanigans
[thirdparty/systemd.git] / src / modules-load / modules-load.c
index b2a41a7e755025f67b7604b3acd1992ecfd1824f..da7e3d890055550b370dfea691852d356a02457b 100644 (file)
@@ -5,8 +5,9 @@
 #include <limits.h>
 #include <sys/stat.h>
 
+#include "build.h"
 #include "conf-files.h"
-#include "def.h"
+#include "constants.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "log.h"
@@ -16,7 +17,6 @@
 #include "proc-cmdline.h"
 #include "string-util.h"
 #include "strv.h"
-#include "util.h"
 
 static char **arg_proc_cmdline_modules = NULL;
 static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
@@ -79,26 +79,23 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent
         log_debug("apply: %s", pp);
         for (;;) {
                 _cleanup_free_ char *line = NULL;
-                char *l;
                 int k;
 
-                k = read_line(f, LONG_LINE_MAX, &line);
+                k = read_stripped_line(f, LONG_LINE_MAX, &line);
                 if (k < 0)
                         return log_error_errno(k, "Failed to read file '%s': %m", pp);
                 if (k == 0)
                         break;
 
-                l = strstrip(line);
-                if (isempty(l))
+                if (isempty(line))
                         continue;
-                if (strchr(COMMENTS, *l))
+                if (strchr(COMMENTS, *line))
                         continue;
 
-                k = module_load_and_warn(ctx, l, true);
+                k = module_load_and_warn(ctx, line, true);
                 if (k == -ENOENT)
                         continue;
-                if (k < 0 && r >= 0)
-                        r = k;
+                RET_GATHER(r, k);
         }
 
         return r;
@@ -152,7 +149,7 @@ static int parse_argv(int argc, char *argv[]) {
                         return -EINVAL;
 
                 default:
-                        assert_not_reached("Unhandled option");
+                        assert_not_reached();
                 }
 
         return 1;
@@ -175,10 +172,8 @@ static int run(int argc, char *argv[]) {
                 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
 
         ctx = kmod_new(NULL, NULL);
-        if (!ctx) {
-                log_error("Failed to allocate memory for kmod.");
-                return -ENOMEM;
-        }
+        if (!ctx)
+                return log_oom();
 
         kmod_load_resources(ctx);
         kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
@@ -186,37 +181,25 @@ 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;
-                char **fn, **i;
 
                 STRV_FOREACH(i, arg_proc_cmdline_modules) {
                         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;