-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include <getopt.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"
#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");
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;
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;
"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;
}
return -EINVAL;
default:
- assert_not_reached("Unhandled option");
+ assert_not_reached();
}
return 1;
if (r <= 0)
return r;
- log_setup_service();
+ log_setup();
umask(0022);
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);
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;