]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/modules-load/modules-load.c
tree-wide: drop libkmod.h when module-util.h is included
[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
LP
4#include <getopt.h>
5#include <limits.h>
b2423f1f 6#include <sys/stat.h>
b2423f1f 7
3f6fd1ba 8#include "conf-files.h"
a0f29c76 9#include "def.h"
3ffd4af2 10#include "fd-util.h"
0d39fa9c 11#include "fileio.h"
b2423f1f 12#include "log.h"
c3d6fb26 13#include "main-func.h"
232ac0d6 14#include "module-util.h"
294bf0c3 15#include "pretty-print.h"
4e731273 16#include "proc-cmdline.h"
07630cea 17#include "string-util.h"
b2423f1f 18#include "strv.h"
3f6fd1ba 19#include "util.h"
03658d4f
LP
20
21static char **arg_proc_cmdline_modules = NULL;
75eb6154 22static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
fabe5c0e 23
c3d6fb26
YW
24STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep);
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
a889e206 75 return log_error_errno(r, "Failed to open %s: %m", path);
fabe5c0e
LP
76 }
77
9f6445e3 78 log_debug("apply: %s", path);
fabe5c0e 79 for (;;) {
a889e206
LP
80 _cleanup_free_ char *line = NULL;
81 char *l;
fabe5c0e
LP
82 int k;
83
db8794c3
YW
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)
a889e206 88 break;
fabe5c0e
LP
89
90 l = strstrip(line);
a889e206 91 if (isempty(l))
fabe5c0e 92 continue;
a889e206 93 if (strchr(COMMENTS, *l))
fabe5c0e
LP
94 continue;
95
c3ad9786 96 k = module_load_and_warn(ctx, l, true);
fec837e9
ZJS
97 if (k == -ENOENT)
98 continue;
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[]) {
eb9da376
LP
127 enum {
128 ARG_VERSION = 0x100,
129 };
130
fabe5c0e
LP
131 static const struct option options[] = {
132 { "help", no_argument, NULL, 'h' },
eb9da376
LP
133 { "version", no_argument, NULL, ARG_VERSION },
134 {}
fabe5c0e
LP
135 };
136
137 int c;
138
139 assert(argc >= 0);
140 assert(argv);
141
601185b4 142 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
fabe5c0e
LP
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
c3d6fb26 161static int run(int argc, char *argv[]) {
232ac0d6 162 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
c3d6fb26 163 int r, k;
b2423f1f 164
fabe5c0e
LP
165 r = parse_argv(argc, argv);
166 if (r <= 0)
c3d6fb26 167 return r;
b2423f1f 168
6bf3c61c 169 log_setup_service();
b2423f1f 170
4c12626c
LP
171 umask(0022);
172
1d84ad94 173 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
b5884878 174 if (r < 0)
da927ba9 175 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
03658d4f 176
4e2075ce
LP
177 ctx = kmod_new(NULL, NULL);
178 if (!ctx) {
83684a35 179 log_error("Failed to allocate memory for kmod.");
c3d6fb26 180 return -ENOMEM;
b2423f1f
LP
181 }
182
83684a35 183 kmod_load_resources(ctx);
83684a35 184 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
b2423f1f 185
fabe5c0e 186 r = 0;
03658d4f 187
fabe5c0e
LP
188 if (argc > optind) {
189 int i;
b2423f1f 190
fabe5c0e
LP
191 for (i = optind; i < argc; i++) {
192 k = apply_file(ctx, argv[i], false);
193 if (k < 0 && r == 0)
194 r = k;
b2423f1f
LP
195 }
196
fabe5c0e 197 } else {
4df32778 198 _cleanup_strv_free_ char **files = NULL;
fabe5c0e 199 char **fn, **i;
b2423f1f 200
fabe5c0e 201 STRV_FOREACH(i, arg_proc_cmdline_modules) {
c3ad9786 202 k = module_load_and_warn(ctx, *i, true);
fec837e9
ZJS
203 if (k == -ENOENT)
204 continue;
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);