]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/modules-load/modules-load.c
tree-wide: make parse_proc_cmdline() strip "rd." prefix automatically
[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"
4e731273 32#include "proc-cmdline.h"
07630cea 33#include "string-util.h"
b2423f1f 34#include "strv.h"
3f6fd1ba 35#include "util.h"
03658d4f
LP
36
37static char **arg_proc_cmdline_modules = NULL;
b2423f1f 38
75eb6154 39static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
fabe5c0e 40
83684a35 41static void systemd_kmod_log(void *data, int priority, const char *file, int line,
f168c273 42 const char *fn, const char *format, va_list args) {
bcfce235
LP
43
44 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 45 log_internalv(priority, 0, file, line, fn, format, args);
bcfce235 46 REENABLE_WARNING;
83684a35
TG
47}
48
03658d4f 49static int add_modules(const char *p) {
fabe5c0e 50 _cleanup_strv_free_ char **k = NULL;
03658d4f
LP
51
52 k = strv_split(p, ",");
0d0f0c50
SL
53 if (!k)
54 return log_oom();
03658d4f 55
e287086b 56 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
0d0f0c50 57 return log_oom();
03658d4f 58
03658d4f
LP
59 return 0;
60}
61
96287a49 62static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 63 int r;
03658d4f 64
d7f69e16 65 if (streq(key, "modules-load") && value) {
059cb385 66 r = add_modules(value);
141a79f4
ZJS
67 if (r < 0)
68 return r;
03658d4f
LP
69 }
70
c007bb1b 71 return 0;
03658d4f
LP
72}
73
74static int load_module(struct kmod_ctx *ctx, const char *m) {
27fda47f 75 const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
03658d4f
LP
76 struct kmod_list *itr, *modlist = NULL;
77 int r = 0;
78
9f6445e3 79 log_debug("load: %s", m);
03658d4f
LP
80
81 r = kmod_module_new_from_lookup(ctx, m, &modlist);
f647962d
MS
82 if (r < 0)
83 return log_error_errno(r, "Failed to lookup alias '%s': %m", m);
03658d4f 84
27fda47f
MS
85 if (!modlist) {
86 log_error("Failed to find module '%s'", m);
8f9c0b4c 87 return -ENOENT;
27fda47f
MS
88 }
89
03658d4f
LP
90 kmod_list_foreach(itr, modlist) {
91 struct kmod_module *mod;
27fda47f 92 int state, err;
03658d4f
LP
93
94 mod = kmod_module_get_module(itr);
27fda47f
MS
95 state = kmod_module_get_initstate(mod);
96
97 switch (state) {
98 case KMOD_MODULE_BUILTIN:
99 log_info("Module '%s' is builtin", kmod_module_get_name(mod));
100 break;
101
102 case KMOD_MODULE_LIVE:
b56c267f 103 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
27fda47f
MS
104 break;
105
106 default:
107 err = kmod_module_probe_insert_module(mod, probe_flags,
108 NULL, NULL, NULL, NULL);
109
110 if (err == 0)
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));
114 else {
c33b3297 115 log_error_errno(err, "Failed to insert '%s': %m", kmod_module_get_name(mod));
27fda47f
MS
116 r = err;
117 }
03658d4f
LP
118 }
119
120 kmod_module_unref(mod);
121 }
122
123 kmod_module_unref_list(modlist);
124
125 return r;
126}
127
fabe5c0e
LP
128static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
129 _cleanup_fclose_ FILE *f = NULL;
130 int r;
131
132 assert(ctx);
133 assert(path);
134
4cf7ea55 135 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
fabe5c0e
LP
136 if (r < 0) {
137 if (ignore_enoent && r == -ENOENT)
138 return 0;
139
8d3d7072 140 return log_error_errno(r, "Failed to open %s, ignoring: %m", path);
fabe5c0e
LP
141 }
142
9f6445e3 143 log_debug("apply: %s", path);
fabe5c0e
LP
144 for (;;) {
145 char line[LINE_MAX], *l;
146 int k;
147
148 if (!fgets(line, sizeof(line), f)) {
149 if (feof(f))
150 break;
151
e1427b13 152 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
fabe5c0e
LP
153 }
154
155 l = strstrip(line);
156 if (!*l)
157 continue;
d3b6d0c2 158 if (strchr(COMMENTS "\n", *l))
fabe5c0e
LP
159 continue;
160
161 k = load_module(ctx, l);
162 if (k < 0 && r == 0)
163 r = k;
164 }
165
166 return r;
167}
168
601185b4 169static void help(void) {
fabe5c0e
LP
170 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
171 "Loads statically configured kernel modules.\n\n"
eb9da376
LP
172 " -h --help Show this help\n"
173 " --version Show package version\n",
fabe5c0e 174 program_invocation_short_name);
fabe5c0e
LP
175}
176
177static int parse_argv(int argc, char *argv[]) {
178
eb9da376
LP
179 enum {
180 ARG_VERSION = 0x100,
181 };
182
fabe5c0e
LP
183 static const struct option options[] = {
184 { "help", no_argument, NULL, 'h' },
eb9da376
LP
185 { "version", no_argument, NULL, ARG_VERSION },
186 {}
fabe5c0e
LP
187 };
188
189 int c;
190
191 assert(argc >= 0);
192 assert(argv);
193
601185b4 194 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
195
196 switch (c) {
197
198 case 'h':
601185b4
ZJS
199 help();
200 return 0;
eb9da376
LP
201
202 case ARG_VERSION:
3f6fd1ba 203 return version();
fabe5c0e
LP
204
205 case '?':
206 return -EINVAL;
207
208 default:
eb9da376 209 assert_not_reached("Unhandled option");
fabe5c0e 210 }
fabe5c0e
LP
211
212 return 1;
213}
214
b2423f1f 215int main(int argc, char *argv[]) {
fabe5c0e 216 int r, k;
83684a35 217 struct kmod_ctx *ctx;
b2423f1f 218
fabe5c0e
LP
219 r = parse_argv(argc, argv);
220 if (r <= 0)
221 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
b2423f1f 222
4cfa2c99 223 log_set_target(LOG_TARGET_AUTO);
b2423f1f
LP
224 log_parse_environment();
225 log_open();
226
4c12626c
LP
227 umask(0022);
228
d7f69e16 229 r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
b5884878 230 if (r < 0)
da927ba9 231 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
03658d4f 232
4e2075ce
LP
233 ctx = kmod_new(NULL, NULL);
234 if (!ctx) {
83684a35 235 log_error("Failed to allocate memory for kmod.");
b2423f1f
LP
236 goto finish;
237 }
238
83684a35 239 kmod_load_resources(ctx);
83684a35 240 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
b2423f1f 241
fabe5c0e 242 r = 0;
03658d4f 243
fabe5c0e
LP
244 if (argc > optind) {
245 int i;
b2423f1f 246
fabe5c0e
LP
247 for (i = optind; i < argc; i++) {
248 k = apply_file(ctx, argv[i], false);
249 if (k < 0 && r == 0)
250 r = k;
b2423f1f
LP
251 }
252
fabe5c0e 253 } else {
4df32778 254 _cleanup_strv_free_ char **files = NULL;
fabe5c0e 255 char **fn, **i;
b2423f1f 256
fabe5c0e
LP
257 STRV_FOREACH(i, arg_proc_cmdline_modules) {
258 k = load_module(ctx, *i);
b857193b
LP
259 if (k < 0 && r == 0)
260 r = k;
b2423f1f
LP
261 }
262
4b462d1a
LP
263 k = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
264 if (k < 0) {
da927ba9 265 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
4b462d1a
LP
266 if (r == 0)
267 r = k;
fabe5c0e 268 goto finish;
b2423f1f
LP
269 }
270
fabe5c0e
LP
271 STRV_FOREACH(fn, files) {
272 k = apply_file(ctx, *fn, true);
273 if (k < 0 && r == 0)
274 r = k;
275 }
b2423f1f
LP
276 }
277
b2423f1f 278finish:
83684a35 279 kmod_unref(ctx);
03658d4f 280 strv_free(arg_proc_cmdline_modules);
b2423f1f 281
fabe5c0e 282 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
b2423f1f 283}