]> git.ipfire.org Git - thirdparty/systemd.git/blob - 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
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
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.
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
14 Lesser General Public License for more details.
15
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/>.
18 ***/
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <libkmod.h>
23 #include <limits.h>
24 #include <string.h>
25 #include <sys/stat.h>
26
27 #include "conf-files.h"
28 #include "def.h"
29 #include "fd-util.h"
30 #include "fileio.h"
31 #include "log.h"
32 #include "proc-cmdline.h"
33 #include "string-util.h"
34 #include "strv.h"
35 #include "util.h"
36
37 static char **arg_proc_cmdline_modules = NULL;
38
39 static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
40
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) {
43
44 DISABLE_WARNING_FORMAT_NONLITERAL;
45 log_internalv(priority, 0, file, line, fn, format, args);
46 REENABLE_WARNING;
47 }
48
49 static int add_modules(const char *p) {
50 _cleanup_strv_free_ char **k = NULL;
51
52 k = strv_split(p, ",");
53 if (!k)
54 return log_oom();
55
56 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
57 return log_oom();
58
59 return 0;
60 }
61
62 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
63 int r;
64
65 if (streq(key, "modules-load") && value) {
66 r = add_modules(value);
67 if (r < 0)
68 return r;
69 }
70
71 return 0;
72 }
73
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;
77 int r = 0;
78
79 log_debug("load: %s", m);
80
81 r = kmod_module_new_from_lookup(ctx, m, &modlist);
82 if (r < 0)
83 return log_error_errno(r, "Failed to lookup alias '%s': %m", m);
84
85 if (!modlist) {
86 log_error("Failed to find module '%s'", m);
87 return -ENOENT;
88 }
89
90 kmod_list_foreach(itr, modlist) {
91 struct kmod_module *mod;
92 int state, err;
93
94 mod = kmod_module_get_module(itr);
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:
103 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
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 {
115 log_error_errno(err, "Failed to insert '%s': %m", kmod_module_get_name(mod));
116 r = err;
117 }
118 }
119
120 kmod_module_unref(mod);
121 }
122
123 kmod_module_unref_list(modlist);
124
125 return r;
126 }
127
128 static 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
135 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
136 if (r < 0) {
137 if (ignore_enoent && r == -ENOENT)
138 return 0;
139
140 return log_error_errno(r, "Failed to open %s, ignoring: %m", path);
141 }
142
143 log_debug("apply: %s", path);
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
152 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
153 }
154
155 l = strstrip(line);
156 if (!*l)
157 continue;
158 if (strchr(COMMENTS "\n", *l))
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
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);
175 }
176
177 static int parse_argv(int argc, char *argv[]) {
178
179 enum {
180 ARG_VERSION = 0x100,
181 };
182
183 static const struct option options[] = {
184 { "help", no_argument, NULL, 'h' },
185 { "version", no_argument, NULL, ARG_VERSION },
186 {}
187 };
188
189 int c;
190
191 assert(argc >= 0);
192 assert(argv);
193
194 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
195
196 switch (c) {
197
198 case 'h':
199 help();
200 return 0;
201
202 case ARG_VERSION:
203 return version();
204
205 case '?':
206 return -EINVAL;
207
208 default:
209 assert_not_reached("Unhandled option");
210 }
211
212 return 1;
213 }
214
215 int main(int argc, char *argv[]) {
216 int r, k;
217 struct kmod_ctx *ctx;
218
219 r = parse_argv(argc, argv);
220 if (r <= 0)
221 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
222
223 log_set_target(LOG_TARGET_AUTO);
224 log_parse_environment();
225 log_open();
226
227 umask(0022);
228
229 r = parse_proc_cmdline(parse_proc_cmdline_item, NULL, true);
230 if (r < 0)
231 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
232
233 ctx = kmod_new(NULL, NULL);
234 if (!ctx) {
235 log_error("Failed to allocate memory for kmod.");
236 goto finish;
237 }
238
239 kmod_load_resources(ctx);
240 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
241
242 r = 0;
243
244 if (argc > optind) {
245 int i;
246
247 for (i = optind; i < argc; i++) {
248 k = apply_file(ctx, argv[i], false);
249 if (k < 0 && r == 0)
250 r = k;
251 }
252
253 } else {
254 _cleanup_strv_free_ char **files = NULL;
255 char **fn, **i;
256
257 STRV_FOREACH(i, arg_proc_cmdline_modules) {
258 k = load_module(ctx, *i);
259 if (k < 0 && r == 0)
260 r = k;
261 }
262
263 k = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
264 if (k < 0) {
265 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
266 if (r == 0)
267 r = k;
268 goto finish;
269 }
270
271 STRV_FOREACH(fn, files) {
272 k = apply_file(ctx, *fn, true);
273 if (k < 0 && r == 0)
274 r = k;
275 }
276 }
277
278 finish:
279 kmod_unref(ctx);
280 strv_free(arg_proc_cmdline_modules);
281
282 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
283 }