]> 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 aaeb2c0d2f59d24ecf32d65b83afe5a63224c70d..da7e3d890055550b370dfea691852d356a02457b 100644 (file)
@@ -1,13 +1,13 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <errno.h>
 #include <getopt.h>
-#include <libkmod.h>
 #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"
@@ -17,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");
@@ -63,12 +62,13 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
 
 static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
         _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *pp = NULL;
         int r;
 
         assert(ctx);
         assert(path);
 
-        r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
+        r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp);
         if (r < 0) {
                 if (ignore_enoent && r == -ENOENT)
                         return 0;
@@ -76,29 +76,26 @@ static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent
                 return log_error_errno(r, "Failed to open %s: %m", path);
         }
 
-        log_debug("apply: %s", path);
+        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", path);
+                        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;
@@ -116,10 +113,9 @@ static int help(void) {
                "Loads statically configured kernel modules.\n\n"
                "  -h --help             Show this help\n"
                "     --version          Show package version\n"
-               "\nSee the %s for details.\n"
-               , program_invocation_short_name
-               , link
-        );
+               "\nSee the %s for details.\n",
+               program_invocation_short_name,
+               link);
 
         return 0;
 }
@@ -153,7 +149,7 @@ static int parse_argv(int argc, char *argv[]) {
                         return -EINVAL;
 
                 default:
-                        assert_not_reached("Unhandled option");
+                        assert_not_reached();
                 }
 
         return 1;
@@ -167,7 +163,7 @@ static int run(int argc, char *argv[]) {
         if (r <= 0)
                 return r;
 
-        log_setup_service();
+        log_setup();
 
         umask(0022);
 
@@ -176,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);
@@ -187,39 +181,25 @@ static int run(int argc, char *argv[]) {
         r = 0;
 
         if (argc > optind) {
-                int i;
-
-                for (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;