]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/modules-load/modules-load.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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"
c3d6fb26 15#include "main-func.h"
232ac0d6 16#include "module-util.h"
294bf0c3 17#include "pretty-print.h"
4e731273 18#include "proc-cmdline.h"
07630cea 19#include "string-util.h"
b2423f1f 20#include "strv.h"
3f6fd1ba 21#include "util.h"
03658d4f
LP
22
23static char **arg_proc_cmdline_modules = NULL;
75eb6154 24static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
fabe5c0e 25
c3d6fb26
YW
26STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep);
27
83684a35 28static void systemd_kmod_log(void *data, int priority, const char *file, int line,
f168c273 29 const char *fn, const char *format, va_list args) {
bcfce235
LP
30
31 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 32 log_internalv(priority, 0, file, line, fn, format, args);
bcfce235 33 REENABLE_WARNING;
83684a35
TG
34}
35
03658d4f 36static int add_modules(const char *p) {
fabe5c0e 37 _cleanup_strv_free_ char **k = NULL;
03658d4f
LP
38
39 k = strv_split(p, ",");
0d0f0c50
SL
40 if (!k)
41 return log_oom();
03658d4f 42
e287086b 43 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
0d0f0c50 44 return log_oom();
03658d4f 45
03658d4f
LP
46 return 0;
47}
48
96287a49 49static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
74df0fca 50 int r;
03658d4f 51
1d84ad94
LP
52 if (proc_cmdline_key_streq(key, "modules_load")) {
53
54 if (proc_cmdline_value_missing(key, value))
55 return 0;
56
059cb385 57 r = add_modules(value);
141a79f4
ZJS
58 if (r < 0)
59 return r;
03658d4f
LP
60 }
61
c007bb1b 62 return 0;
03658d4f
LP
63}
64
fabe5c0e
LP
65static 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
4cf7ea55 72 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f);
fabe5c0e
LP
73 if (r < 0) {
74 if (ignore_enoent && r == -ENOENT)
75 return 0;
76
a889e206 77 return log_error_errno(r, "Failed to open %s: %m", path);
fabe5c0e
LP
78 }
79
9f6445e3 80 log_debug("apply: %s", path);
fabe5c0e 81 for (;;) {
a889e206
LP
82 _cleanup_free_ char *line = NULL;
83 char *l;
fabe5c0e
LP
84 int k;
85
db8794c3
YW
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)
a889e206 90 break;
fabe5c0e
LP
91
92 l = strstrip(line);
a889e206 93 if (isempty(l))
fabe5c0e 94 continue;
a889e206 95 if (strchr(COMMENTS, *l))
fabe5c0e
LP
96 continue;
97
c3ad9786 98 k = module_load_and_warn(ctx, l, true);
db8794c3 99 if (k < 0 && r >= 0)
fabe5c0e
LP
100 r = k;
101 }
102
103 return r;
104}
105
37ec0fdd
LP
106static 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
fabe5c0e
LP
114 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
115 "Loads statically configured kernel modules.\n\n"
eb9da376 116 " -h --help Show this help\n"
37ec0fdd
LP
117 " --version Show package version\n"
118 "\nSee the %s for details.\n"
119 , program_invocation_short_name
120 , link
121 );
122
123 return 0;
fabe5c0e
LP
124}
125
126static int parse_argv(int argc, char *argv[]) {
127
eb9da376
LP
128 enum {
129 ARG_VERSION = 0x100,
130 };
131
fabe5c0e
LP
132 static const struct option options[] = {
133 { "help", no_argument, NULL, 'h' },
eb9da376
LP
134 { "version", no_argument, NULL, ARG_VERSION },
135 {}
fabe5c0e
LP
136 };
137
138 int c;
139
140 assert(argc >= 0);
141 assert(argv);
142
601185b4 143 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
144
145 switch (c) {
146
147 case 'h':
37ec0fdd 148 return help();
eb9da376
LP
149
150 case ARG_VERSION:
3f6fd1ba 151 return version();
fabe5c0e
LP
152
153 case '?':
154 return -EINVAL;
155
156 default:
eb9da376 157 assert_not_reached("Unhandled option");
fabe5c0e 158 }
fabe5c0e
LP
159
160 return 1;
161}
162
c3d6fb26 163static int run(int argc, char *argv[]) {
232ac0d6 164 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
c3d6fb26 165 int r, k;
b2423f1f 166
fabe5c0e
LP
167 r = parse_argv(argc, argv);
168 if (r <= 0)
c3d6fb26 169 return r;
b2423f1f 170
6bf3c61c 171 log_setup_service();
b2423f1f 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.");
c3d6fb26 182 return -ENOMEM;
b2423f1f
LP
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;
c3d6fb26 214 return r;
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
c3d6fb26 224 return r;
b2423f1f 225}
c3d6fb26
YW
226
227DEFINE_MAIN_FUNCTION(run);