]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
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 | |
21 | static char **arg_proc_cmdline_modules = NULL; | |
75eb6154 | 22 | static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d"); |
fabe5c0e | 23 | |
c3d6fb26 YW |
24 | STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep); |
25 | ||
83684a35 | 26 | static 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 | 34 | static 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 | 47 | static 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 |
63 | static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) { |
64 | _cleanup_fclose_ FILE *f = NULL; | |
2708160c | 65 | _cleanup_free_ char *pp = NULL; |
fabe5c0e LP |
66 | int r; |
67 | ||
68 | assert(ctx); | |
69 | assert(path); | |
70 | ||
2708160c | 71 | r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp); |
fabe5c0e LP |
72 | if (r < 0) { |
73 | if (ignore_enoent && r == -ENOENT) | |
74 | return 0; | |
75 | ||
a889e206 | 76 | return log_error_errno(r, "Failed to open %s: %m", path); |
fabe5c0e LP |
77 | } |
78 | ||
2708160c | 79 | log_debug("apply: %s", pp); |
fabe5c0e | 80 | for (;;) { |
a889e206 LP |
81 | _cleanup_free_ char *line = NULL; |
82 | char *l; | |
fabe5c0e LP |
83 | int k; |
84 | ||
db8794c3 YW |
85 | k = read_line(f, LONG_LINE_MAX, &line); |
86 | if (k < 0) | |
2708160c | 87 | return log_error_errno(k, "Failed to read file '%s': %m", pp); |
db8794c3 | 88 | if (k == 0) |
a889e206 | 89 | break; |
fabe5c0e LP |
90 | |
91 | l = strstrip(line); | |
a889e206 | 92 | if (isempty(l)) |
fabe5c0e | 93 | continue; |
a889e206 | 94 | if (strchr(COMMENTS, *l)) |
fabe5c0e LP |
95 | continue; |
96 | ||
c3ad9786 | 97 | k = module_load_and_warn(ctx, l, true); |
fec837e9 ZJS |
98 | if (k == -ENOENT) |
99 | continue; | |
db8794c3 | 100 | if (k < 0 && r >= 0) |
fabe5c0e LP |
101 | r = k; |
102 | } | |
103 | ||
104 | return r; | |
105 | } | |
106 | ||
37ec0fdd LP |
107 | static int help(void) { |
108 | _cleanup_free_ char *link = NULL; | |
109 | int r; | |
110 | ||
111 | r = terminal_urlify_man("systemd-modules-load.service", "8", &link); | |
112 | if (r < 0) | |
113 | return log_oom(); | |
114 | ||
fabe5c0e LP |
115 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
116 | "Loads statically configured kernel modules.\n\n" | |
eb9da376 | 117 | " -h --help Show this help\n" |
37ec0fdd | 118 | " --version Show package version\n" |
bc556335 DDM |
119 | "\nSee the %s for details.\n", |
120 | program_invocation_short_name, | |
121 | link); | |
37ec0fdd LP |
122 | |
123 | return 0; | |
fabe5c0e LP |
124 | } |
125 | ||
126 | static 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: | |
04499a70 | 155 | assert_not_reached(); |
fabe5c0e | 156 | } |
fabe5c0e LP |
157 | |
158 | return 1; | |
159 | } | |
160 | ||
c3d6fb26 | 161 | static 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 | |
d2acb93d | 169 | log_setup(); |
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 | 188 | if (argc > optind) { |
be21b60b | 189 | for (int i = optind; i < argc; i++) { |
fabe5c0e LP |
190 | k = apply_file(ctx, argv[i], false); |
191 | if (k < 0 && r == 0) | |
192 | r = k; | |
b2423f1f LP |
193 | } |
194 | ||
fabe5c0e | 195 | } else { |
4df32778 | 196 | _cleanup_strv_free_ char **files = NULL; |
b2423f1f | 197 | |
fabe5c0e | 198 | STRV_FOREACH(i, arg_proc_cmdline_modules) { |
c3ad9786 | 199 | k = module_load_and_warn(ctx, *i, true); |
fec837e9 ZJS |
200 | if (k == -ENOENT) |
201 | continue; | |
b857193b LP |
202 | if (k < 0 && r == 0) |
203 | r = k; | |
b2423f1f LP |
204 | } |
205 | ||
b5084605 | 206 | k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs); |
4b462d1a | 207 | if (k < 0) { |
da927ba9 | 208 | log_error_errno(k, "Failed to enumerate modules-load.d files: %m"); |
4b462d1a LP |
209 | if (r == 0) |
210 | r = k; | |
c3d6fb26 | 211 | return r; |
b2423f1f LP |
212 | } |
213 | ||
fabe5c0e LP |
214 | STRV_FOREACH(fn, files) { |
215 | k = apply_file(ctx, *fn, true); | |
216 | if (k < 0 && r == 0) | |
217 | r = k; | |
218 | } | |
b2423f1f LP |
219 | } |
220 | ||
c3d6fb26 | 221 | return r; |
b2423f1f | 222 | } |
c3d6fb26 YW |
223 | |
224 | DEFINE_MAIN_FUNCTION(run); |