]>
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 | |
d6b4d1c7 | 8 | #include "build.h" |
3f6fd1ba | 9 | #include "conf-files.h" |
28db6fbf | 10 | #include "constants.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
0d39fa9c | 12 | #include "fileio.h" |
b2423f1f | 13 | #include "log.h" |
c3d6fb26 | 14 | #include "main-func.h" |
232ac0d6 | 15 | #include "module-util.h" |
294bf0c3 | 16 | #include "pretty-print.h" |
4e731273 | 17 | #include "proc-cmdline.h" |
07630cea | 18 | #include "string-util.h" |
b2423f1f | 19 | #include "strv.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 | 81 | _cleanup_free_ char *line = NULL; |
fabe5c0e LP |
82 | int k; |
83 | ||
0ff6ff2b | 84 | k = read_stripped_line(f, LONG_LINE_MAX, &line); |
db8794c3 | 85 | if (k < 0) |
2708160c | 86 | return log_error_errno(k, "Failed to read file '%s': %m", pp); |
db8794c3 | 87 | if (k == 0) |
a889e206 | 88 | break; |
fabe5c0e | 89 | |
0ff6ff2b | 90 | if (isempty(line)) |
fabe5c0e | 91 | continue; |
0ff6ff2b | 92 | if (strchr(COMMENTS, *line)) |
fabe5c0e LP |
93 | continue; |
94 | ||
0ff6ff2b | 95 | k = module_load_and_warn(ctx, line, true); |
fec837e9 ZJS |
96 | if (k == -ENOENT) |
97 | continue; | |
eb6cae1a | 98 | RET_GATHER(r, k); |
fabe5c0e LP |
99 | } |
100 | ||
101 | return r; | |
102 | } | |
103 | ||
37ec0fdd LP |
104 | static 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 | 115 | " --version Show package version\n" |
bc556335 DDM |
116 | "\nSee the %s for details.\n", |
117 | program_invocation_short_name, | |
118 | link); | |
37ec0fdd LP |
119 | |
120 | return 0; | |
fabe5c0e LP |
121 | } |
122 | ||
123 | static int parse_argv(int argc, char *argv[]) { | |
eb9da376 LP |
124 | enum { |
125 | ARG_VERSION = 0x100, | |
126 | }; | |
127 | ||
fabe5c0e LP |
128 | static const struct option options[] = { |
129 | { "help", no_argument, NULL, 'h' }, | |
eb9da376 LP |
130 | { "version", no_argument, NULL, ARG_VERSION }, |
131 | {} | |
fabe5c0e LP |
132 | }; |
133 | ||
134 | int c; | |
135 | ||
136 | assert(argc >= 0); | |
137 | assert(argv); | |
138 | ||
601185b4 | 139 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
fabe5c0e LP |
140 | switch (c) { |
141 | ||
142 | case 'h': | |
37ec0fdd | 143 | return help(); |
eb9da376 LP |
144 | |
145 | case ARG_VERSION: | |
3f6fd1ba | 146 | return version(); |
fabe5c0e LP |
147 | |
148 | case '?': | |
149 | return -EINVAL; | |
150 | ||
151 | default: | |
04499a70 | 152 | assert_not_reached(); |
fabe5c0e | 153 | } |
fabe5c0e LP |
154 | |
155 | return 1; | |
156 | } | |
157 | ||
c3d6fb26 | 158 | static int run(int argc, char *argv[]) { |
232ac0d6 | 159 | _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL; |
c3d6fb26 | 160 | int r, k; |
b2423f1f | 161 | |
fabe5c0e LP |
162 | r = parse_argv(argc, argv); |
163 | if (r <= 0) | |
c3d6fb26 | 164 | return r; |
b2423f1f | 165 | |
d2acb93d | 166 | log_setup(); |
b2423f1f | 167 | |
4c12626c LP |
168 | umask(0022); |
169 | ||
1d84ad94 | 170 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
b5884878 | 171 | if (r < 0) |
da927ba9 | 172 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
03658d4f | 173 | |
4e2075ce | 174 | ctx = kmod_new(NULL, NULL); |
0b3c2708 FS |
175 | if (!ctx) |
176 | return log_oom(); | |
b2423f1f | 177 | |
83684a35 | 178 | kmod_load_resources(ctx); |
83684a35 | 179 | kmod_set_log_fn(ctx, systemd_kmod_log, NULL); |
b2423f1f | 180 | |
fabe5c0e | 181 | r = 0; |
03658d4f | 182 | |
fabe5c0e | 183 | if (argc > optind) { |
eb6cae1a ZJS |
184 | for (int i = optind; i < argc; i++) |
185 | RET_GATHER(r, apply_file(ctx, argv[i], false)); | |
b2423f1f | 186 | |
fabe5c0e | 187 | } else { |
4df32778 | 188 | _cleanup_strv_free_ char **files = NULL; |
b2423f1f | 189 | |
fabe5c0e | 190 | STRV_FOREACH(i, arg_proc_cmdline_modules) { |
c3ad9786 | 191 | k = module_load_and_warn(ctx, *i, true); |
fec837e9 ZJS |
192 | if (k == -ENOENT) |
193 | continue; | |
eb6cae1a | 194 | RET_GATHER(r, k); |
b2423f1f LP |
195 | } |
196 | ||
b5084605 | 197 | k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs); |
eb6cae1a ZJS |
198 | if (k < 0) |
199 | return log_error_errno(k, "Failed to enumerate modules-load.d files: %m"); | |
b2423f1f | 200 | |
eb6cae1a ZJS |
201 | STRV_FOREACH(fn, files) |
202 | RET_GATHER(r, apply_file(ctx, *fn, true)); | |
b2423f1f LP |
203 | } |
204 | ||
c3d6fb26 | 205 | return r; |
b2423f1f | 206 | } |
c3d6fb26 YW |
207 | |
208 | DEFINE_MAIN_FUNCTION(run); |