]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/modules-load/modules-load.c
Merge pull request #13867 from keszybz/man-condition
[thirdparty/systemd.git] / src / modules-load / modules-load.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <libkmod.h>
6 #include <limits.h>
7 #include <string.h>
8 #include <sys/stat.h>
9
10 #include "conf-files.h"
11 #include "def.h"
12 #include "fd-util.h"
13 #include "fileio.h"
14 #include "log.h"
15 #include "main-func.h"
16 #include "module-util.h"
17 #include "pretty-print.h"
18 #include "proc-cmdline.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "util.h"
22
23 static char **arg_proc_cmdline_modules = NULL;
24 static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
25
26 STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep);
27
28 static void systemd_kmod_log(void *data, int priority, const char *file, int line,
29 const char *fn, const char *format, va_list args) {
30
31 DISABLE_WARNING_FORMAT_NONLITERAL;
32 log_internalv(priority, 0, file, line, fn, format, args);
33 REENABLE_WARNING;
34 }
35
36 static int add_modules(const char *p) {
37 _cleanup_strv_free_ char **k = NULL;
38
39 k = strv_split(p, ",");
40 if (!k)
41 return log_oom();
42
43 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
44 return log_oom();
45
46 return 0;
47 }
48
49 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
50 int r;
51
52 if (proc_cmdline_key_streq(key, "modules_load")) {
53
54 if (proc_cmdline_value_missing(key, value))
55 return 0;
56
57 r = add_modules(value);
58 if (r < 0)
59 return r;
60 }
61
62 return 0;
63 }
64
65 static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
66 _cleanup_fclose_ FILE *f = NULL;
67 int r;
68
69 assert(ctx);
70 assert(path);
71
72 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
73 if (r < 0) {
74 if (ignore_enoent && r == -ENOENT)
75 return 0;
76
77 return log_error_errno(r, "Failed to open %s: %m", path);
78 }
79
80 log_debug("apply: %s", path);
81 for (;;) {
82 _cleanup_free_ char *line = NULL;
83 char *l;
84 int k;
85
86 k = read_line(f, LONG_LINE_MAX, &line);
87 if (k < 0)
88 return log_error_errno(k, "Failed to read file '%s': %m", path);
89 if (k == 0)
90 break;
91
92 l = strstrip(line);
93 if (isempty(l))
94 continue;
95 if (strchr(COMMENTS, *l))
96 continue;
97
98 k = module_load_and_warn(ctx, l, true);
99 if (k == -ENOENT)
100 continue;
101 if (k < 0 && r >= 0)
102 r = k;
103 }
104
105 return r;
106 }
107
108 static int help(void) {
109 _cleanup_free_ char *link = NULL;
110 int r;
111
112 r = terminal_urlify_man("systemd-modules-load.service", "8", &link);
113 if (r < 0)
114 return log_oom();
115
116 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
117 "Loads statically configured kernel modules.\n\n"
118 " -h --help Show this help\n"
119 " --version Show package version\n"
120 "\nSee the %s for details.\n"
121 , program_invocation_short_name
122 , link
123 );
124
125 return 0;
126 }
127
128 static int parse_argv(int argc, char *argv[]) {
129 enum {
130 ARG_VERSION = 0x100,
131 };
132
133 static const struct option options[] = {
134 { "help", no_argument, NULL, 'h' },
135 { "version", no_argument, NULL, ARG_VERSION },
136 {}
137 };
138
139 int c;
140
141 assert(argc >= 0);
142 assert(argv);
143
144 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
145 switch (c) {
146
147 case 'h':
148 return help();
149
150 case ARG_VERSION:
151 return version();
152
153 case '?':
154 return -EINVAL;
155
156 default:
157 assert_not_reached("Unhandled option");
158 }
159
160 return 1;
161 }
162
163 static int run(int argc, char *argv[]) {
164 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
165 int r, k;
166
167 r = parse_argv(argc, argv);
168 if (r <= 0)
169 return r;
170
171 log_setup_service();
172
173 umask(0022);
174
175 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
176 if (r < 0)
177 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
178
179 ctx = kmod_new(NULL, NULL);
180 if (!ctx) {
181 log_error("Failed to allocate memory for kmod.");
182 return -ENOMEM;
183 }
184
185 kmod_load_resources(ctx);
186 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
187
188 r = 0;
189
190 if (argc > optind) {
191 int i;
192
193 for (i = optind; i < argc; i++) {
194 k = apply_file(ctx, argv[i], false);
195 if (k < 0 && r == 0)
196 r = k;
197 }
198
199 } else {
200 _cleanup_strv_free_ char **files = NULL;
201 char **fn, **i;
202
203 STRV_FOREACH(i, arg_proc_cmdline_modules) {
204 k = module_load_and_warn(ctx, *i, true);
205 if (k == -ENOENT)
206 continue;
207 if (k < 0 && r == 0)
208 r = k;
209 }
210
211 k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
212 if (k < 0) {
213 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
214 if (r == 0)
215 r = k;
216 return r;
217 }
218
219 STRV_FOREACH(fn, files) {
220 k = apply_file(ctx, *fn, true);
221 if (k < 0 && r == 0)
222 r = k;
223 }
224 }
225
226 return r;
227 }
228
229 DEFINE_MAIN_FUNCTION(run);