2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "conf-files.h"
32 #include "proc-cmdline.h"
33 #include "string-util.h"
37 static char **arg_proc_cmdline_modules
= NULL
;
39 static const char conf_file_dirs
[] = CONF_PATHS_NULSTR("modules-load.d");
41 static void systemd_kmod_log(void *data
, int priority
, const char *file
, int line
,
42 const char *fn
, const char *format
, va_list args
) {
44 DISABLE_WARNING_FORMAT_NONLITERAL
;
45 log_internalv(priority
, 0, file
, line
, fn
, format
, args
);
49 static int add_modules(const char *p
) {
50 _cleanup_strv_free_
char **k
= NULL
;
52 k
= strv_split(p
, ",");
56 if (strv_extend_strv(&arg_proc_cmdline_modules
, k
, true) < 0)
62 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
65 if (STR_IN_SET(key
, "modules-load", "rd.modules-load") && value
) {
66 r
= add_modules(value
);
74 static int load_module(struct kmod_ctx
*ctx
, const char *m
) {
75 const int probe_flags
= KMOD_PROBE_APPLY_BLACKLIST
;
76 struct kmod_list
*itr
, *modlist
= NULL
;
79 log_debug("load: %s", m
);
81 r
= kmod_module_new_from_lookup(ctx
, m
, &modlist
);
83 return log_error_errno(r
, "Failed to lookup alias '%s': %m", m
);
86 log_error("Failed to find module '%s'", m
);
90 kmod_list_foreach(itr
, modlist
) {
91 struct kmod_module
*mod
;
94 mod
= kmod_module_get_module(itr
);
95 state
= kmod_module_get_initstate(mod
);
98 case KMOD_MODULE_BUILTIN
:
99 log_info("Module '%s' is builtin", kmod_module_get_name(mod
));
102 case KMOD_MODULE_LIVE
:
103 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod
));
107 err
= kmod_module_probe_insert_module(mod
, probe_flags
,
108 NULL
, NULL
, NULL
, NULL
);
111 log_info("Inserted module '%s'", kmod_module_get_name(mod
));
112 else if (err
== KMOD_PROBE_APPLY_BLACKLIST
)
113 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod
));
115 log_error_errno(err
, "Failed to insert '%s': %m", kmod_module_get_name(mod
));
120 kmod_module_unref(mod
);
123 kmod_module_unref_list(modlist
);
128 static int apply_file(struct kmod_ctx
*ctx
, const char *path
, bool ignore_enoent
) {
129 _cleanup_fclose_
FILE *f
= NULL
;
135 r
= search_and_fopen_nulstr(path
, "re", NULL
, conf_file_dirs
, &f
);
137 if (ignore_enoent
&& r
== -ENOENT
)
140 return log_error_errno(r
, "Failed to open %s, ignoring: %m", path
);
143 log_debug("apply: %s", path
);
145 char line
[LINE_MAX
], *l
;
148 if (!fgets(line
, sizeof(line
), f
)) {
152 return log_error_errno(errno
, "Failed to read file '%s', ignoring: %m", path
);
158 if (strchr(COMMENTS
"\n", *l
))
161 k
= load_module(ctx
, l
);
169 static void help(void) {
170 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
171 "Loads statically configured kernel modules.\n\n"
172 " -h --help Show this help\n"
173 " --version Show package version\n",
174 program_invocation_short_name
);
177 static int parse_argv(int argc
, char *argv
[]) {
183 static const struct option options
[] = {
184 { "help", no_argument
, NULL
, 'h' },
185 { "version", no_argument
, NULL
, ARG_VERSION
},
194 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
209 assert_not_reached("Unhandled option");
215 int main(int argc
, char *argv
[]) {
217 struct kmod_ctx
*ctx
;
219 r
= parse_argv(argc
, argv
);
221 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
223 log_set_target(LOG_TARGET_AUTO
);
224 log_parse_environment();
229 r
= parse_proc_cmdline(parse_proc_cmdline_item
, NULL
);
231 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
233 ctx
= kmod_new(NULL
, NULL
);
235 log_error("Failed to allocate memory for kmod.");
239 kmod_load_resources(ctx
);
240 kmod_set_log_fn(ctx
, systemd_kmod_log
, NULL
);
247 for (i
= optind
; i
< argc
; i
++) {
248 k
= apply_file(ctx
, argv
[i
], false);
254 _cleanup_strv_free_
char **files
= NULL
;
257 STRV_FOREACH(i
, arg_proc_cmdline_modules
) {
258 k
= load_module(ctx
, *i
);
263 k
= conf_files_list_nulstr(&files
, ".conf", NULL
, conf_file_dirs
);
265 log_error_errno(k
, "Failed to enumerate modules-load.d files: %m");
271 STRV_FOREACH(fn
, files
) {
272 k
= apply_file(ctx
, *fn
, true);
280 strv_free(arg_proc_cmdline_modules
);
282 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;