modules-load.c revision 4b462d1a28461b302586b117736ef288fba1012f
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt/***
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt This file is part of systemd.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt Copyright 2010 Lennart Poettering
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt systemd is free software; you can redistribute it and/or modify it
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt under the terms of the GNU Lesser General Public License as published by
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt the Free Software Foundation; either version 2.1 of the License, or
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt (at your option) any later version.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt systemd is distributed in the hope that it will be useful, but
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt WITHOUT ANY WARRANTY; without even the implied warranty of
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt Lesser General Public License for more details.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt You should have received a copy of the GNU Lesser General Public License
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt along with systemd; If not, see <http://www.gnu.org/licenses/>.
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt***/
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <unistd.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <fcntl.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <errno.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <string.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <sys/stat.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <limits.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <dirent.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <getopt.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include <libkmod.h>
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include "log.h"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include "util.h"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include "strv.h"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include "conf-files.h"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include "fileio.h"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#include "build.h"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic char **arg_proc_cmdline_modules = NULL;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic const char conf_file_dirs[] =
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "/etc/modules-load.d\0"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "/run/modules-load.d\0"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "/usr/local/lib/modules-load.d\0"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "/usr/lib/modules-load.d\0"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#ifdef HAVE_SPLIT_USR
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt "/lib/modules-load.d\0"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#endif
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#pragma GCC diagnostic push
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#pragma GCC diagnostic ignored "-Wformat-nonliteral"
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic void systemd_kmod_log(void *data, int priority, const char *file, int line,
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt const char *fn, const char *format, va_list args) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_metav(priority, file, line, fn, format, args);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt#pragma GCC diagnostic pop
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int add_modules(const char *p) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _cleanup_strv_free_ char **k = NULL;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt k = strv_split(p, ",");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (!k)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return log_oom();
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (strv_extend_strv(&arg_proc_cmdline_modules, k) < 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return log_oom();
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return 0;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int parse_proc_cmdline_word(const char *word) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (startswith(word, "modules-load=")) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = add_modules(word + 13);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (r < 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt } else if (startswith(word, "rd.modules-load=")) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (in_initrd()) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = add_modules(word + 16);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (r < 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return 0;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int load_module(struct kmod_ctx *ctx, const char *m) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt struct kmod_list *itr, *modlist = NULL;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int r = 0;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_debug("load: %s", m);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = kmod_module_new_from_lookup(ctx, m, &modlist);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (r < 0) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_error("Failed to lookup alias '%s': %s", m, strerror(-r));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (!modlist) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_error("Failed to find module '%s'", m);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return -ENOENT;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt kmod_list_foreach(itr, modlist) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt struct kmod_module *mod;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int state, err;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt mod = kmod_module_get_module(itr);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt state = kmod_module_get_initstate(mod);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt switch (state) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt case KMOD_MODULE_BUILTIN:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_info("Module '%s' is builtin", kmod_module_get_name(mod));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt break;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt case KMOD_MODULE_LIVE:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt break;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt default:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt err = kmod_module_probe_insert_module(mod, probe_flags,
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt NULL, NULL, NULL, NULL);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (err == 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_info("Inserted module '%s'", kmod_module_get_name(mod));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt else if (err == KMOD_PROBE_APPLY_BLACKLIST)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt else {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_error("Failed to insert '%s': %s", kmod_module_get_name(mod),
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt strerror(-err));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = err;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt kmod_module_unref(mod);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt kmod_module_unref_list(modlist);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtstatic int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt _cleanup_fclose_ FILE *f = NULL;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(ctx);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(path);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = search_and_fopen_nulstr(path, "re", conf_file_dirs, &f);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (r < 0) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (ignore_enoent && r == -ENOENT)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return 0;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error("Failed to open %s, ignoring: %s", path, strerror(-r));
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return r;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_debug("apply: %s", path);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt for (;;) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt char line[LINE_MAX], *l;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int k;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (!fgets(line, sizeof(line), f)) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (feof(f))
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt break;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error("Failed to read file '%s', ignoring: %m", path);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return -errno;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt l = strstrip(line);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (!*l)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt continue;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (strchr(COMMENTS "\n", *l))
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt continue;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt k = load_module(ctx, l);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (k < 0 && r == 0)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = k;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return r;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int help(void) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt "Loads statically configured kernel modules.\n\n"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt " -h --help Show this help\n"
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt " --version Show package version\n",
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt program_invocation_short_name);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return 0;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtstatic int parse_argv(int argc, char *argv[]) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt enum {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt ARG_VERSION = 0x100,
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt };
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt static const struct option options[] = {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt { "help", no_argument, NULL, 'h' },
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt { "version", no_argument, NULL, ARG_VERSION },
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt {}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt };
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int c;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(argc >= 0);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt assert(argv);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt switch (c) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt case 'h':
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return help();
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt case ARG_VERSION:
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt puts(PACKAGE_STRING);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt puts(SYSTEMD_FEATURES);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return 0;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt case '?':
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return -EINVAL;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt default:
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt assert_not_reached("Unhandled option");
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt }
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return 1;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt}
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidtint main(int argc, char *argv[]) {
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt int r, k;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt struct kmod_ctx *ctx;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt r = parse_argv(argc, argv);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (r <= 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_set_target(LOG_TARGET_AUTO);
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt log_parse_environment();
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_open();
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt umask(0022);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt if (parse_proc_cmdline(parse_proc_cmdline_word) < 0)
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt return EXIT_FAILURE;
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt
32a4456cc252689f51f383d150d34ed636bfec4dMichal Schmidt ctx = kmod_new(NULL, NULL);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (!ctx) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error("Failed to allocate memory for kmod.");
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt goto finish;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt kmod_load_resources(ctx);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = 0;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (argc > optind) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt int i;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt for (i = optind; i < argc; i++) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt k = apply_file(ctx, argv[i], false);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (k < 0 && r == 0)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = k;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt } else {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt _cleanup_free_ char **files = NULL;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt char **fn, **i;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt STRV_FOREACH(i, arg_proc_cmdline_modules) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt k = load_module(ctx, *i);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (k < 0 && r == 0)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = k;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt k = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (k < 0) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt log_error("Failed to enumerate modules-load.d files: %s", strerror(-k));
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (r == 0)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = k;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt goto finish;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt STRV_FOREACH(fn, files) {
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt k = apply_file(ctx, *fn, true);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt if (k < 0 && r == 0)
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt r = k;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt }
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidtfinish:
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt kmod_unref(ctx);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt strv_free(arg_proc_cmdline_modules);
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt}
9ba81d5a61b7c992a1d2e5e02f334b8e2a0b0c22Michal Schmidt