]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/modules-load/modules-load.c
tree-wide: add clickable man page link to all --help texts
[thirdparty/systemd.git] / src / modules-load / modules-load.c
... / ...
CommitLineData
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 "module-util.h"
16#include "proc-cmdline.h"
17#include "string-util.h"
18#include "strv.h"
19#include "terminal-util.h"
20#include "util.h"
21
22static char **arg_proc_cmdline_modules = NULL;
23
24static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
25
26static 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
34static 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
47static 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
63static 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, ignoring: %m", path);
76 }
77
78 log_debug("apply: %s", path);
79 for (;;) {
80 char line[LINE_MAX], *l;
81 int k;
82
83 if (!fgets(line, sizeof(line), f)) {
84 if (feof(f))
85 break;
86
87 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
88 }
89
90 l = strstrip(line);
91 if (!*l)
92 continue;
93 if (strchr(COMMENTS "\n", *l))
94 continue;
95
96 k = module_load_and_warn(ctx, l, true);
97 if (k < 0 && r == 0)
98 r = k;
99 }
100
101 return r;
102}
103
104static int help(void) {
105 _cleanup_free_ char *link = NULL;
106 int r;
107
108 r = terminal_urlify_man("systemd-modules-load.service", "8", &link);
109 if (r < 0)
110 return log_oom();
111
112 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
113 "Loads statically configured kernel modules.\n\n"
114 " -h --help Show this help\n"
115 " --version Show package version\n"
116 "\nSee the %s for details.\n"
117 , program_invocation_short_name
118 , link
119 );
120
121 return 0;
122}
123
124static int parse_argv(int argc, char *argv[]) {
125
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
143 switch (c) {
144
145 case 'h':
146 return help();
147
148 case ARG_VERSION:
149 return version();
150
151 case '?':
152 return -EINVAL;
153
154 default:
155 assert_not_reached("Unhandled option");
156 }
157
158 return 1;
159}
160
161int main(int argc, char *argv[]) {
162 int r, k;
163 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
164
165 r = parse_argv(argc, argv);
166 if (r <= 0)
167 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
168
169 log_set_target(LOG_TARGET_AUTO);
170 log_parse_environment();
171 log_open();
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 goto finish;
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 < 0 && r == 0)
206 r = k;
207 }
208
209 k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
210 if (k < 0) {
211 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
212 if (r == 0)
213 r = k;
214 goto finish;
215 }
216
217 STRV_FOREACH(fn, files) {
218 k = apply_file(ctx, *fn, true);
219 if (k < 0 && r == 0)
220 r = k;
221 }
222 }
223
224finish:
225 strv_free(arg_proc_cmdline_modules);
226
227 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
228}