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