]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b2423f1f 2
b2423f1f 3#include <errno.h>
3f6fd1ba 4#include <getopt.h>
07630cea 5#include <libkmod.h>
3f6fd1ba 6#include <limits.h>
b2423f1f
LP
7#include <string.h>
8#include <sys/stat.h>
b2423f1f 9
3f6fd1ba 10#include "conf-files.h"
a0f29c76 11#include "def.h"
3ffd4af2 12#include "fd-util.h"
0d39fa9c 13#include "fileio.h"
b2423f1f 14#include "log.h"
232ac0d6 15#include "module-util.h"
4e731273 16#include "proc-cmdline.h"
07630cea 17#include "string-util.h"
b2423f1f 18#include "strv.h"
37ec0fdd 19#include "terminal-util.h"
3f6fd1ba 20#include "util.h"
03658d4f
LP
21
22static char **arg_proc_cmdline_modules = NULL;
b2423f1f 23
75eb6154 24static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
fabe5c0e 25
83684a35 26static void systemd_kmod_log(void *data, int priority, const char *file, int line,
f168c273 27 const char *fn, const char *format, va_list args) {
bcfce235
LP
28
29 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 30 log_internalv(priority, 0, file, line, fn, format, args);
bcfce235 31 REENABLE_WARNING;
83684a35
TG
32}
33
03658d4f 34static int add_modules(const char *p) {
fabe5c0e 35 _cleanup_strv_free_ char **k = NULL;
03658d4f
LP
36
37 k = strv_split(p, ",");
0d0f0c50
SL
38 if (!k)
39 return log_oom();
03658d4f 40
e287086b 41 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
0d0f0c50 42 return log_oom();
03658d4f 43
03658d4f
LP
44 return 0;
45}
46
96287a49 47static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 48 int r;
03658d4f 49
1d84ad94
LP
50 if (proc_cmdline_key_streq(key, "modules_load")) {
51
52 if (proc_cmdline_value_missing(key, value))
53 return 0;
54
059cb385 55 r = add_modules(value);
141a79f4
ZJS
56 if (r < 0)
57 return r;
03658d4f
LP
58 }
59
c007bb1b 60 return 0;
03658d4f
LP
61}
62
fabe5c0e
LP
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
4cf7ea55 70 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
fabe5c0e
LP
71 if (r < 0) {
72 if (ignore_enoent && r == -ENOENT)
73 return 0;
74
8d3d7072 75 return log_error_errno(r, "Failed to open %s, ignoring: %m", path);
fabe5c0e
LP
76 }
77
9f6445e3 78 log_debug("apply: %s", path);
fabe5c0e
LP
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
e1427b13 87 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
fabe5c0e
LP
88 }
89
90 l = strstrip(line);
91 if (!*l)
92 continue;
d3b6d0c2 93 if (strchr(COMMENTS "\n", *l))
fabe5c0e
LP
94 continue;
95
c3ad9786 96 k = module_load_and_warn(ctx, l, true);
fabe5c0e
LP
97 if (k < 0 && r == 0)
98 r = k;
99 }
100
101 return r;
102}
103
37ec0fdd
LP
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
fabe5c0e
LP
112 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
113 "Loads statically configured kernel modules.\n\n"
eb9da376 114 " -h --help Show this help\n"
37ec0fdd
LP
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;
fabe5c0e
LP
122}
123
124static int parse_argv(int argc, char *argv[]) {
125
eb9da376
LP
126 enum {
127 ARG_VERSION = 0x100,
128 };
129
fabe5c0e
LP
130 static const struct option options[] = {
131 { "help", no_argument, NULL, 'h' },
eb9da376
LP
132 { "version", no_argument, NULL, ARG_VERSION },
133 {}
fabe5c0e
LP
134 };
135
136 int c;
137
138 assert(argc >= 0);
139 assert(argv);
140
601185b4 141 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
142
143 switch (c) {
144
145 case 'h':
37ec0fdd 146 return help();
eb9da376
LP
147
148 case ARG_VERSION:
3f6fd1ba 149 return version();
fabe5c0e
LP
150
151 case '?':
152 return -EINVAL;
153
154 default:
eb9da376 155 assert_not_reached("Unhandled option");
fabe5c0e 156 }
fabe5c0e
LP
157
158 return 1;
159}
160
b2423f1f 161int main(int argc, char *argv[]) {
fabe5c0e 162 int r, k;
232ac0d6 163 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
b2423f1f 164
fabe5c0e
LP
165 r = parse_argv(argc, argv);
166 if (r <= 0)
167 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
b2423f1f 168
4cfa2c99 169 log_set_target(LOG_TARGET_AUTO);
b2423f1f
LP
170 log_parse_environment();
171 log_open();
172
4c12626c
LP
173 umask(0022);
174
1d84ad94 175 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
b5884878 176 if (r < 0)
da927ba9 177 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
03658d4f 178
4e2075ce
LP
179 ctx = kmod_new(NULL, NULL);
180 if (!ctx) {
83684a35 181 log_error("Failed to allocate memory for kmod.");
b2423f1f
LP
182 goto finish;
183 }
184
83684a35 185 kmod_load_resources(ctx);
83684a35 186 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
b2423f1f 187
fabe5c0e 188 r = 0;
03658d4f 189
fabe5c0e
LP
190 if (argc > optind) {
191 int i;
b2423f1f 192
fabe5c0e
LP
193 for (i = optind; i < argc; i++) {
194 k = apply_file(ctx, argv[i], false);
195 if (k < 0 && r == 0)
196 r = k;
b2423f1f
LP
197 }
198
fabe5c0e 199 } else {
4df32778 200 _cleanup_strv_free_ char **files = NULL;
fabe5c0e 201 char **fn, **i;
b2423f1f 202
fabe5c0e 203 STRV_FOREACH(i, arg_proc_cmdline_modules) {
c3ad9786 204 k = module_load_and_warn(ctx, *i, true);
b857193b
LP
205 if (k < 0 && r == 0)
206 r = k;
b2423f1f
LP
207 }
208
b5084605 209 k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
4b462d1a 210 if (k < 0) {
da927ba9 211 log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
4b462d1a
LP
212 if (r == 0)
213 r = k;
fabe5c0e 214 goto finish;
b2423f1f
LP
215 }
216
fabe5c0e
LP
217 STRV_FOREACH(fn, files) {
218 k = apply_file(ctx, *fn, true);
219 if (k < 0 && r == 0)
220 r = k;
221 }
b2423f1f
LP
222 }
223
b2423f1f 224finish:
03658d4f 225 strv_free(arg_proc_cmdline_modules);
b2423f1f 226
fabe5c0e 227 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
b2423f1f 228}