]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/modules-load/modules-load.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / modules-load / modules-load.c
CommitLineData
b2423f1f
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
b2423f1f
LP
9 (at your option) any later version.
10
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
5430f7f2 14 Lesser General Public License for more details.
b2423f1f 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
b2423f1f
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
b2423f1f 20#include <errno.h>
3f6fd1ba 21#include <getopt.h>
07630cea 22#include <libkmod.h>
3f6fd1ba 23#include <limits.h>
b2423f1f
LP
24#include <string.h>
25#include <sys/stat.h>
b2423f1f 26
3f6fd1ba 27#include "conf-files.h"
a0f29c76 28#include "def.h"
3ffd4af2 29#include "fd-util.h"
0d39fa9c 30#include "fileio.h"
b2423f1f 31#include "log.h"
232ac0d6 32#include "module-util.h"
4e731273 33#include "proc-cmdline.h"
07630cea 34#include "string-util.h"
b2423f1f 35#include "strv.h"
3f6fd1ba 36#include "util.h"
03658d4f
LP
37
38static char **arg_proc_cmdline_modules = NULL;
b2423f1f 39
75eb6154 40static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
fabe5c0e 41
83684a35 42static void systemd_kmod_log(void *data, int priority, const char *file, int line,
f168c273 43 const char *fn, const char *format, va_list args) {
bcfce235
LP
44
45 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 46 log_internalv(priority, 0, file, line, fn, format, args);
bcfce235 47 REENABLE_WARNING;
83684a35
TG
48}
49
03658d4f 50static int add_modules(const char *p) {
fabe5c0e 51 _cleanup_strv_free_ char **k = NULL;
03658d4f
LP
52
53 k = strv_split(p, ",");
0d0f0c50
SL
54 if (!k)
55 return log_oom();
03658d4f 56
e287086b 57 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
0d0f0c50 58 return log_oom();
03658d4f 59
03658d4f
LP
60 return 0;
61}
62
96287a49 63static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 64 int r;
03658d4f 65
1d84ad94
LP
66 if (proc_cmdline_key_streq(key, "modules_load")) {
67
68 if (proc_cmdline_value_missing(key, value))
69 return 0;
70
059cb385 71 r = add_modules(value);
141a79f4
ZJS
72 if (r < 0)
73 return r;
03658d4f
LP
74 }
75
c007bb1b 76 return 0;
03658d4f
LP
77}
78
79static int load_module(struct kmod_ctx *ctx, const char *m) {
27fda47f 80 const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
232ac0d6
ZJS
81 struct kmod_list *itr;
82 _cleanup_(kmod_module_unref_listp) struct kmod_list *modlist = NULL;
03658d4f
LP
83 int r = 0;
84
9f6445e3 85 log_debug("load: %s", m);
03658d4f
LP
86
87 r = kmod_module_new_from_lookup(ctx, m, &modlist);
f647962d
MS
88 if (r < 0)
89 return log_error_errno(r, "Failed to lookup alias '%s': %m", m);
03658d4f 90
27fda47f
MS
91 if (!modlist) {
92 log_error("Failed to find module '%s'", m);
8f9c0b4c 93 return -ENOENT;
27fda47f
MS
94 }
95
03658d4f 96 kmod_list_foreach(itr, modlist) {
232ac0d6 97 _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL;
27fda47f 98 int state, err;
03658d4f
LP
99
100 mod = kmod_module_get_module(itr);
27fda47f
MS
101 state = kmod_module_get_initstate(mod);
102
103 switch (state) {
104 case KMOD_MODULE_BUILTIN:
105 log_info("Module '%s' is builtin", kmod_module_get_name(mod));
106 break;
107
108 case KMOD_MODULE_LIVE:
b56c267f 109 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
27fda47f
MS
110 break;
111
112 default:
113 err = kmod_module_probe_insert_module(mod, probe_flags,
114 NULL, NULL, NULL, NULL);
115
116 if (err == 0)
117 log_info("Inserted module '%s'", kmod_module_get_name(mod));
118 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
119 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
120 else {
2c3f0bb2 121 assert(err < 0);
6cbb0af1
ZJS
122
123 log_full_errno(err == ENODEV ? LOG_NOTICE :
124 err == ENOENT ? LOG_WARNING :
125 LOG_ERR,
126 err,
127 "Failed to insert '%s': %m",
128 kmod_module_get_name(mod));
129 if (!IN_SET(err, ENODEV, ENOENT))
130 r = err;
27fda47f 131 }
03658d4f 132 }
03658d4f
LP
133 }
134
03658d4f
LP
135 return r;
136}
137
fabe5c0e
LP
138static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
139 _cleanup_fclose_ FILE *f = NULL;
140 int r;
141
142 assert(ctx);
143 assert(path);
144
4cf7ea55 145 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
fabe5c0e
LP
146 if (r < 0) {
147 if (ignore_enoent && r == -ENOENT)
148 return 0;
149
8d3d7072 150 return log_error_errno(r, "Failed to open %s, ignoring: %m", path);
fabe5c0e
LP
151 }
152
9f6445e3 153 log_debug("apply: %s", path);
fabe5c0e
LP
154 for (;;) {
155 char line[LINE_MAX], *l;
156 int k;
157
158 if (!fgets(line, sizeof(line), f)) {
159 if (feof(f))
160 break;
161
e1427b13 162 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
fabe5c0e
LP
163 }
164
165 l = strstrip(line);
166 if (!*l)
167 continue;
d3b6d0c2 168 if (strchr(COMMENTS "\n", *l))
fabe5c0e
LP
169 continue;
170
171 k = load_module(ctx, l);
172 if (k < 0 && r == 0)
173 r = k;
174 }
175
176 return r;
177}
178
601185b4 179static void help(void) {
fabe5c0e
LP
180 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
181 "Loads statically configured kernel modules.\n\n"
eb9da376
LP
182 " -h --help Show this help\n"
183 " --version Show package version\n",
fabe5c0e 184 program_invocation_short_name);
fabe5c0e
LP
185}
186
187static int parse_argv(int argc, char *argv[]) {
188
eb9da376
LP
189 enum {
190 ARG_VERSION = 0x100,
191 };
192
fabe5c0e
LP
193 static const struct option options[] = {
194 { "help", no_argument, NULL, 'h' },
eb9da376
LP
195 { "version", no_argument, NULL, ARG_VERSION },
196 {}
fabe5c0e
LP
197 };
198
199 int c;
200
201 assert(argc >= 0);
202 assert(argv);
203
601185b4 204 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
205
206 switch (c) {
207
208 case 'h':
601185b4
ZJS
209 help();
210 return 0;
eb9da376
LP
211
212 case ARG_VERSION:
3f6fd1ba 213 return version();
fabe5c0e
LP
214
215 case '?':
216 return -EINVAL;
217
218 default:
eb9da376 219 assert_not_reached("Unhandled option");
fabe5c0e 220 }
fabe5c0e
LP
221
222 return 1;
223}
224
b2423f1f 225int main(int argc, char *argv[]) {
fabe5c0e 226 int r, k;
232ac0d6 227 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
b2423f1f 228
fabe5c0e
LP
229 r = parse_argv(argc, argv);
230 if (r <= 0)
231 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
b2423f1f 232
4cfa2c99 233 log_set_target(LOG_TARGET_AUTO);
b2423f1f
LP
234 log_parse_environment();
235 log_open();
236
4c12626c
LP
237 umask(0022);
238
1d84ad94 239 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
b5884878 240 if (r < 0)
da927ba9 241 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
03658d4f 242
4e2075ce
LP
243 ctx = kmod_new(NULL, NULL);
244 if (!ctx) {
83684a35 245 log_error("Failed to allocate memory for kmod.");
b2423f1f
LP
246 goto finish;
247 }
248
83684a35 249 kmod_load_resources(ctx);
83684a35 250 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
b2423f1f 251
fabe5c0e 252 r = 0;
03658d4f 253
fabe5c0e
LP
254 if (argc > optind) {
255 int i;
b2423f1f 256
fabe5c0e
LP
257 for (i = optind; i < argc; i++) {
258 k = apply_file(ctx, argv[i], false);
259 if (k < 0 && r == 0)
260 r = k;
b2423f1f
LP
261 }
262
fabe5c0e 263 } else {
4df32778 264 _cleanup_strv_free_ char **files = NULL;
fabe5c0e 265 char **fn, **i;
b2423f1f 266
fabe5c0e
LP
267 STRV_FOREACH(i, arg_proc_cmdline_modules) {
268 k = load_module(ctx, *i);
b857193b
LP
269 if (k < 0 && r == 0)
270 r = k;
b2423f1f
LP
271 }
272
b5084605 273 k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
4b462d1a 274 if (k < 0) {
da927ba9 275 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
4b462d1a
LP
276 if (r == 0)
277 r = k;
fabe5c0e 278 goto finish;
b2423f1f
LP
279 }
280
fabe5c0e
LP
281 STRV_FOREACH(fn, files) {
282 k = apply_file(ctx, *fn, true);
283 if (k < 0 && r == 0)
284 r = k;
285 }
b2423f1f
LP
286 }
287
b2423f1f 288finish:
03658d4f 289 strv_free(arg_proc_cmdline_modules);
b2423f1f 290
fabe5c0e 291 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
b2423f1f 292}