]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b2423f1f | 2 | |
3f6fd1ba | 3 | #include <getopt.h> |
b2423f1f | 4 | #include <sys/stat.h> |
b2423f1f | 5 | |
decad482 | 6 | #include "alloc-util.h" |
d6b4d1c7 | 7 | #include "build.h" |
3f6fd1ba | 8 | #include "conf-files.h" |
28db6fbf | 9 | #include "constants.h" |
76d62b63 | 10 | #include "errno-util.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 | ||
96287a49 | 26 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
74df0fca | 27 | int r; |
03658d4f | 28 | |
1d84ad94 LP |
29 | if (proc_cmdline_key_streq(key, "modules_load")) { |
30 | ||
31 | if (proc_cmdline_value_missing(key, value)) | |
32 | return 0; | |
33 | ||
a2c8652a | 34 | r = strv_split_and_extend(&arg_proc_cmdline_modules, value, ",", /* filter_duplicates = */ true); |
141a79f4 | 35 | if (r < 0) |
a2c8652a | 36 | return log_error_errno(r, "Failed to parse modules_load= kernel command line option: %m"); |
03658d4f LP |
37 | } |
38 | ||
c007bb1b | 39 | return 0; |
03658d4f LP |
40 | } |
41 | ||
fabe5c0e LP |
42 | static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) { |
43 | _cleanup_fclose_ FILE *f = NULL; | |
2708160c | 44 | _cleanup_free_ char *pp = NULL; |
fabe5c0e LP |
45 | int r; |
46 | ||
47 | assert(ctx); | |
48 | assert(path); | |
49 | ||
2708160c | 50 | r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp); |
fabe5c0e LP |
51 | if (r < 0) { |
52 | if (ignore_enoent && r == -ENOENT) | |
53 | return 0; | |
54 | ||
a889e206 | 55 | return log_error_errno(r, "Failed to open %s: %m", path); |
fabe5c0e LP |
56 | } |
57 | ||
2708160c | 58 | log_debug("apply: %s", pp); |
fabe5c0e | 59 | for (;;) { |
a889e206 | 60 | _cleanup_free_ char *line = NULL; |
fabe5c0e LP |
61 | int k; |
62 | ||
0ff6ff2b | 63 | k = read_stripped_line(f, LONG_LINE_MAX, &line); |
db8794c3 | 64 | if (k < 0) |
2708160c | 65 | return log_error_errno(k, "Failed to read file '%s': %m", pp); |
db8794c3 | 66 | if (k == 0) |
a889e206 | 67 | break; |
fabe5c0e | 68 | |
0ff6ff2b | 69 | if (isempty(line)) |
fabe5c0e | 70 | continue; |
0ff6ff2b | 71 | if (strchr(COMMENTS, *line)) |
fabe5c0e LP |
72 | continue; |
73 | ||
0ff6ff2b | 74 | k = module_load_and_warn(ctx, line, true); |
fec837e9 ZJS |
75 | if (k == -ENOENT) |
76 | continue; | |
eb6cae1a | 77 | RET_GATHER(r, k); |
fabe5c0e LP |
78 | } |
79 | ||
80 | return r; | |
81 | } | |
82 | ||
37ec0fdd LP |
83 | static int help(void) { |
84 | _cleanup_free_ char *link = NULL; | |
85 | int r; | |
86 | ||
87 | r = terminal_urlify_man("systemd-modules-load.service", "8", &link); | |
88 | if (r < 0) | |
89 | return log_oom(); | |
90 | ||
fabe5c0e LP |
91 | printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n" |
92 | "Loads statically configured kernel modules.\n\n" | |
eb9da376 | 93 | " -h --help Show this help\n" |
37ec0fdd | 94 | " --version Show package version\n" |
bc556335 DDM |
95 | "\nSee the %s for details.\n", |
96 | program_invocation_short_name, | |
97 | link); | |
37ec0fdd LP |
98 | |
99 | return 0; | |
fabe5c0e LP |
100 | } |
101 | ||
102 | static int parse_argv(int argc, char *argv[]) { | |
eb9da376 LP |
103 | enum { |
104 | ARG_VERSION = 0x100, | |
105 | }; | |
106 | ||
fabe5c0e LP |
107 | static const struct option options[] = { |
108 | { "help", no_argument, NULL, 'h' }, | |
eb9da376 LP |
109 | { "version", no_argument, NULL, ARG_VERSION }, |
110 | {} | |
fabe5c0e LP |
111 | }; |
112 | ||
113 | int c; | |
114 | ||
115 | assert(argc >= 0); | |
116 | assert(argv); | |
117 | ||
601185b4 | 118 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) |
fabe5c0e LP |
119 | switch (c) { |
120 | ||
121 | case 'h': | |
37ec0fdd | 122 | return help(); |
eb9da376 LP |
123 | |
124 | case ARG_VERSION: | |
3f6fd1ba | 125 | return version(); |
fabe5c0e LP |
126 | |
127 | case '?': | |
128 | return -EINVAL; | |
129 | ||
130 | default: | |
04499a70 | 131 | assert_not_reached(); |
fabe5c0e | 132 | } |
fabe5c0e LP |
133 | |
134 | return 1; | |
135 | } | |
136 | ||
c3d6fb26 | 137 | static int run(int argc, char *argv[]) { |
1d98716e | 138 | _cleanup_(sym_kmod_unrefp) struct kmod_ctx *ctx = NULL; |
c3d6fb26 | 139 | int r, k; |
b2423f1f | 140 | |
fabe5c0e LP |
141 | r = parse_argv(argc, argv); |
142 | if (r <= 0) | |
c3d6fb26 | 143 | return r; |
b2423f1f | 144 | |
d2acb93d | 145 | log_setup(); |
b2423f1f | 146 | |
4c12626c LP |
147 | umask(0022); |
148 | ||
1d84ad94 | 149 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
b5884878 | 150 | if (r < 0) |
da927ba9 | 151 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
03658d4f | 152 | |
1d98716e LP |
153 | r = module_setup_context(&ctx); |
154 | if (r < 0) | |
155 | return log_error_errno(r, "Failed to initialize libkmod context: %m"); | |
b2423f1f | 156 | |
fabe5c0e | 157 | r = 0; |
03658d4f | 158 | |
fabe5c0e | 159 | if (argc > optind) { |
eb6cae1a ZJS |
160 | for (int i = optind; i < argc; i++) |
161 | RET_GATHER(r, apply_file(ctx, argv[i], false)); | |
b2423f1f | 162 | |
fabe5c0e | 163 | } else { |
4df32778 | 164 | _cleanup_strv_free_ char **files = NULL; |
b2423f1f | 165 | |
fabe5c0e | 166 | STRV_FOREACH(i, arg_proc_cmdline_modules) { |
c3ad9786 | 167 | k = module_load_and_warn(ctx, *i, true); |
fec837e9 ZJS |
168 | if (k == -ENOENT) |
169 | continue; | |
eb6cae1a | 170 | RET_GATHER(r, k); |
b2423f1f LP |
171 | } |
172 | ||
b5084605 | 173 | k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs); |
eb6cae1a ZJS |
174 | if (k < 0) |
175 | return log_error_errno(k, "Failed to enumerate modules-load.d files: %m"); | |
b2423f1f | 176 | |
eb6cae1a ZJS |
177 | STRV_FOREACH(fn, files) |
178 | RET_GATHER(r, apply_file(ctx, *fn, true)); | |
b2423f1f LP |
179 | } |
180 | ||
c3d6fb26 | 181 | return r; |
b2423f1f | 182 | } |
c3d6fb26 YW |
183 | |
184 | DEFINE_MAIN_FUNCTION(run); |