]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/modules-load/modules-load.c
docs/RANDOM_SEEDS: update NetBSD link
[thirdparty/systemd.git] / src / modules-load / modules-load.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <limits.h>
6 #include <sys/stat.h>
7
8 #include "build.h"
9 #include "conf-files.h"
10 #include "constants.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "log.h"
14 #include "main-func.h"
15 #include "module-util.h"
16 #include "pretty-print.h"
17 #include "proc-cmdline.h"
18 #include "string-util.h"
19 #include "strv.h"
20
21 static char **arg_proc_cmdline_modules = NULL;
22 static const char conf_file_dirs[] = CONF_PATHS_NULSTR("modules-load.d");
23
24 STATIC_DESTRUCTOR_REGISTER(arg_proc_cmdline_modules, strv_freep);
25
26 static void systemd_kmod_log(void *data, int priority, const char *file, int line,
27 const char *fn, const char *format, va_list args) {
28
29 DISABLE_WARNING_FORMAT_NONLITERAL;
30 log_internalv(priority, 0, file, line, fn, format, args);
31 REENABLE_WARNING;
32 }
33
34 static int add_modules(const char *p) {
35 _cleanup_strv_free_ char **k = NULL;
36
37 k = strv_split(p, ",");
38 if (!k)
39 return log_oom();
40
41 if (strv_extend_strv(&arg_proc_cmdline_modules, k, true) < 0)
42 return log_oom();
43
44 return 0;
45 }
46
47 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
48 int r;
49
50 if (proc_cmdline_key_streq(key, "modules_load")) {
51
52 if (proc_cmdline_value_missing(key, value))
53 return 0;
54
55 r = add_modules(value);
56 if (r < 0)
57 return r;
58 }
59
60 return 0;
61 }
62
63 static int apply_file(struct kmod_ctx *ctx, const char *path, bool ignore_enoent) {
64 _cleanup_fclose_ FILE *f = NULL;
65 _cleanup_free_ char *pp = NULL;
66 int r;
67
68 assert(ctx);
69 assert(path);
70
71 r = search_and_fopen_nulstr(path, "re", NULL, conf_file_dirs, &f, &pp);
72 if (r < 0) {
73 if (ignore_enoent && r == -ENOENT)
74 return 0;
75
76 return log_error_errno(r, "Failed to open %s: %m", path);
77 }
78
79 log_debug("apply: %s", pp);
80 for (;;) {
81 _cleanup_free_ char *line = NULL;
82 int k;
83
84 k = read_stripped_line(f, LONG_LINE_MAX, &line);
85 if (k < 0)
86 return log_error_errno(k, "Failed to read file '%s': %m", pp);
87 if (k == 0)
88 break;
89
90 if (isempty(line))
91 continue;
92 if (strchr(COMMENTS, *line))
93 continue;
94
95 k = module_load_and_warn(ctx, line, true);
96 if (k == -ENOENT)
97 continue;
98 RET_GATHER(r, k);
99 }
100
101 return r;
102 }
103
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
112 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
113 "Loads statically configured kernel modules.\n\n"
114 " -h --help Show this help\n"
115 " --version Show package version\n"
116 "\nSee the %s for details.\n",
117 program_invocation_short_name,
118 link);
119
120 return 0;
121 }
122
123 static int parse_argv(int argc, char *argv[]) {
124 enum {
125 ARG_VERSION = 0x100,
126 };
127
128 static const struct option options[] = {
129 { "help", no_argument, NULL, 'h' },
130 { "version", no_argument, NULL, ARG_VERSION },
131 {}
132 };
133
134 int c;
135
136 assert(argc >= 0);
137 assert(argv);
138
139 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
140 switch (c) {
141
142 case 'h':
143 return help();
144
145 case ARG_VERSION:
146 return version();
147
148 case '?':
149 return -EINVAL;
150
151 default:
152 assert_not_reached();
153 }
154
155 return 1;
156 }
157
158 static int run(int argc, char *argv[]) {
159 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
160 int r, k;
161
162 r = parse_argv(argc, argv);
163 if (r <= 0)
164 return r;
165
166 log_setup();
167
168 umask(0022);
169
170 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
171 if (r < 0)
172 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
173
174 ctx = kmod_new(NULL, NULL);
175 if (!ctx)
176 return log_oom();
177
178 kmod_load_resources(ctx);
179 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
180
181 r = 0;
182
183 if (argc > optind) {
184 for (int i = optind; i < argc; i++)
185 RET_GATHER(r, apply_file(ctx, argv[i], false));
186
187 } else {
188 _cleanup_strv_free_ char **files = NULL;
189
190 STRV_FOREACH(i, arg_proc_cmdline_modules) {
191 k = module_load_and_warn(ctx, *i, true);
192 if (k == -ENOENT)
193 continue;
194 RET_GATHER(r, k);
195 }
196
197 k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
198 if (k < 0)
199 return log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
200
201 STRV_FOREACH(fn, files)
202 RET_GATHER(r, apply_file(ctx, *fn, true));
203 }
204
205 return r;
206 }
207
208 DEFINE_MAIN_FUNCTION(run);