]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/modules-load/modules-load.c
util-lib: various improvements to kernel command line parsing
[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 (proc_cmdline_key_streq(key, "modules_load")) {
66
67 if (proc_cmdline_value_missing(key, value))
68 return 0;
69
70 r = add_modules(value);
71 if (r < 0)
72 return r;
73 }
74
75 return 0;
76 }
77
78 static int load_module(struct kmod_ctx *ctx, const char *m) {
79 const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
80 struct kmod_list *itr, *modlist = NULL;
81 int r = 0;
82
83 log_debug("load: %s", m);
84
85 r = kmod_module_new_from_lookup(ctx, m, &modlist);
86 if (r < 0)
87 return log_error_errno(r, "Failed to lookup alias '%s': %m", m);
88
89 if (!modlist) {
90 log_error("Failed to find module '%s'", m);
91 return -ENOENT;
92 }
93
94 kmod_list_foreach(itr, modlist) {
95 struct kmod_module *mod;
96 int state, err;
97
98 mod = kmod_module_get_module(itr);
99 state = kmod_module_get_initstate(mod);
100
101 switch (state) {
102 case KMOD_MODULE_BUILTIN:
103 log_info("Module '%s' is builtin", kmod_module_get_name(mod));
104 break;
105
106 case KMOD_MODULE_LIVE:
107 log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
108 break;
109
110 default:
111 err = kmod_module_probe_insert_module(mod, probe_flags,
112 NULL, NULL, NULL, NULL);
113
114 if (err == 0)
115 log_info("Inserted module '%s'", kmod_module_get_name(mod));
116 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
117 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
118 else {
119 log_error_errno(err, "Failed to insert '%s': %m", kmod_module_get_name(mod));
120 r = err;
121 }
122 }
123
124 kmod_module_unref(mod);
125 }
126
127 kmod_module_unref_list(modlist);
128
129 return r;
130 }
131
132 static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
133 _cleanup_fclose_ FILE *f = NULL;
134 int r;
135
136 assert(ctx);
137 assert(path);
138
139 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
140 if (r < 0) {
141 if (ignore_enoent && r == -ENOENT)
142 return 0;
143
144 return log_error_errno(r, "Failed to open %s, ignoring: %m", path);
145 }
146
147 log_debug("apply: %s", path);
148 for (;;) {
149 char line[LINE_MAX], *l;
150 int k;
151
152 if (!fgets(line, sizeof(line), f)) {
153 if (feof(f))
154 break;
155
156 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
157 }
158
159 l = strstrip(line);
160 if (!*l)
161 continue;
162 if (strchr(COMMENTS "\n", *l))
163 continue;
164
165 k = load_module(ctx, l);
166 if (k < 0 && r == 0)
167 r = k;
168 }
169
170 return r;
171 }
172
173 static void help(void) {
174 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
175 "Loads statically configured kernel modules.\n\n"
176 " -h --help Show this help\n"
177 " --version Show package version\n",
178 program_invocation_short_name);
179 }
180
181 static int parse_argv(int argc, char *argv[]) {
182
183 enum {
184 ARG_VERSION = 0x100,
185 };
186
187 static const struct option options[] = {
188 { "help", no_argument, NULL, 'h' },
189 { "version", no_argument, NULL, ARG_VERSION },
190 {}
191 };
192
193 int c;
194
195 assert(argc >= 0);
196 assert(argv);
197
198 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
199
200 switch (c) {
201
202 case 'h':
203 help();
204 return 0;
205
206 case ARG_VERSION:
207 return version();
208
209 case '?':
210 return -EINVAL;
211
212 default:
213 assert_not_reached("Unhandled option");
214 }
215
216 return 1;
217 }
218
219 int main(int argc, char *argv[]) {
220 int r, k;
221 struct kmod_ctx *ctx;
222
223 r = parse_argv(argc, argv);
224 if (r <= 0)
225 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
226
227 log_set_target(LOG_TARGET_AUTO);
228 log_parse_environment();
229 log_open();
230
231 umask(0022);
232
233 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
234 if (r < 0)
235 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
236
237 ctx = kmod_new(NULL, NULL);
238 if (!ctx) {
239 log_error("Failed to allocate memory for kmod.");
240 goto finish;
241 }
242
243 kmod_load_resources(ctx);
244 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
245
246 r = 0;
247
248 if (argc > optind) {
249 int i;
250
251 for (i = optind; i < argc; i++) {
252 k = apply_file(ctx, argv[i], false);
253 if (k < 0 && r == 0)
254 r = k;
255 }
256
257 } else {
258 _cleanup_strv_free_ char **files = NULL;
259 char **fn, **i;
260
261 STRV_FOREACH(i, arg_proc_cmdline_modules) {
262 k = load_module(ctx, *i);
263 if (k < 0 && r == 0)
264 r = k;
265 }
266
267 k = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
268 if (k < 0) {
269 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
270 if (r == 0)
271 r = k;
272 goto finish;
273 }
274
275 STRV_FOREACH(fn, files) {
276 k = apply_file(ctx, *fn, true);
277 if (k < 0 && r == 0)
278 r = k;
279 }
280 }
281
282 finish:
283 kmod_unref(ctx);
284 strv_free(arg_proc_cmdline_modules);
285
286 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
287 }