blob: b3ae93785141bf6329556c2640a0fc727ffdd18e [file] [log] [blame]
Steve Muckle18b981e2019-04-15 17:43:02 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <modprobe/modprobe.h>
18
19#include <fnmatch.h>
20#include <sys/stat.h>
21#include <sys/syscall.h>
22
23#include <algorithm>
24#include <set>
25#include <string>
26#include <vector>
27
28#include <android-base/chrono_utils.h>
29#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/strings.h>
32#include <android-base/unique_fd.h>
33
34std::string Modprobe::MakeCanonical(const std::string& module_path) {
35 auto start = module_path.find_last_of('/');
36 if (start == std::string::npos) {
37 start = 0;
38 } else {
39 start += 1;
40 }
41 auto end = module_path.size();
42 if (android::base::EndsWith(module_path, ".ko")) {
43 end -= 3;
44 }
45 if ((end - start) <= 1) {
46 LOG(ERROR) << "malformed module name: " << module_path;
47 return "";
48 }
49 std::string module_name = module_path.substr(start, end - start);
50 // module names can have '-', but their file names will have '_'
51 std::replace(module_name.begin(), module_name.end(), '-', '_');
52 return module_name;
53}
54
55bool Modprobe::ParseDepCallback(const std::string& base_path,
56 const std::vector<std::string>& args) {
57 std::vector<std::string> deps;
58 std::string prefix = "";
59
60 // Set first item as our modules path
61 std::string::size_type pos = args[0].find(':');
62 if (args[0][0] != '/') {
63 prefix = base_path + "/";
64 }
65 if (pos != std::string::npos) {
66 deps.emplace_back(prefix + args[0].substr(0, pos));
67 } else {
68 LOG(ERROR) << "dependency lines must start with name followed by ':'";
Andrew Scullfb18f6e2020-10-18 17:37:27 +010069 return false;
Steve Muckle18b981e2019-04-15 17:43:02 -070070 }
71
72 // Remaining items are dependencies of our module
73 for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
74 if ((*arg)[0] != '/') {
75 prefix = base_path + "/";
76 } else {
77 prefix = "";
78 }
79 deps.push_back(prefix + *arg);
80 }
81
82 std::string canonical_name = MakeCanonical(args[0].substr(0, pos));
83 if (canonical_name.empty()) {
84 return false;
85 }
86 this->module_deps_[canonical_name] = deps;
87
88 return true;
89}
90
91bool Modprobe::ParseAliasCallback(const std::vector<std::string>& args) {
92 auto it = args.begin();
93 const std::string& type = *it++;
94
95 if (type != "alias") {
96 LOG(ERROR) << "non-alias line encountered in modules.alias, found " << type;
97 return false;
98 }
99
100 if (args.size() != 3) {
101 LOG(ERROR) << "alias lines in modules.alias must have 3 entries, not " << args.size();
102 return false;
103 }
104
105 const std::string& alias = *it++;
106 const std::string& module_name = *it++;
107 this->module_aliases_.emplace_back(alias, module_name);
108
109 return true;
110}
111
112bool Modprobe::ParseSoftdepCallback(const std::vector<std::string>& args) {
113 auto it = args.begin();
114 const std::string& type = *it++;
115 std::string state = "";
116
117 if (type != "softdep") {
118 LOG(ERROR) << "non-softdep line encountered in modules.softdep, found " << type;
119 return false;
120 }
121
122 if (args.size() < 4) {
123 LOG(ERROR) << "softdep lines in modules.softdep must have at least 4 entries";
124 return false;
125 }
126
127 const std::string& module = *it++;
128 while (it != args.end()) {
129 const std::string& token = *it++;
130 if (token == "pre:" || token == "post:") {
131 state = token;
132 continue;
133 }
134 if (state == "") {
135 LOG(ERROR) << "malformed modules.softdep at token " << token;
136 return false;
137 }
138 if (state == "pre:") {
139 this->module_pre_softdep_.emplace_back(module, token);
140 } else {
141 this->module_post_softdep_.emplace_back(module, token);
142 }
143 }
144
145 return true;
146}
147
148bool Modprobe::ParseLoadCallback(const std::vector<std::string>& args) {
149 auto it = args.begin();
150 const std::string& module = *it++;
151
152 const std::string& canonical_name = MakeCanonical(module);
153 if (canonical_name.empty()) {
154 return false;
155 }
156 this->module_load_.emplace_back(canonical_name);
157
158 return true;
159}
160
161bool Modprobe::ParseOptionsCallback(const std::vector<std::string>& args) {
162 auto it = args.begin();
163 const std::string& type = *it++;
164
165 if (type != "options") {
166 LOG(ERROR) << "non-options line encountered in modules.options";
167 return false;
168 }
169
170 if (args.size() < 2) {
171 LOG(ERROR) << "lines in modules.options must have at least 2 entries, not " << args.size();
172 return false;
173 }
174
175 const std::string& module = *it++;
176 std::string options = "";
177
178 const std::string& canonical_name = MakeCanonical(module);
179 if (canonical_name.empty()) {
180 return false;
181 }
182
183 while (it != args.end()) {
184 options += *it++;
185 if (it != args.end()) {
186 options += " ";
187 }
188 }
189
190 auto [unused, inserted] = this->module_options_.emplace(canonical_name, options);
191 if (!inserted) {
192 LOG(ERROR) << "multiple options lines present for module " << module;
193 return false;
194 }
195 return true;
196}
197
Mark Salyzyn703fb742020-06-15 11:51:59 -0700198bool Modprobe::ParseBlocklistCallback(const std::vector<std::string>& args) {
Steve Mucklee31f8402019-07-31 14:34:52 -0700199 auto it = args.begin();
200 const std::string& type = *it++;
201
Mark Salyzyn9debda12020-06-16 05:14:06 -0700202 if (type != "blocklist") {
Mark Salyzyn703fb742020-06-15 11:51:59 -0700203 LOG(ERROR) << "non-blocklist line encountered in modules.blocklist";
Steve Mucklee31f8402019-07-31 14:34:52 -0700204 return false;
205 }
206
207 if (args.size() != 2) {
Mark Salyzyn703fb742020-06-15 11:51:59 -0700208 LOG(ERROR) << "lines in modules.blocklist must have exactly 2 entries, not " << args.size();
Steve Mucklee31f8402019-07-31 14:34:52 -0700209 return false;
210 }
211
212 const std::string& module = *it++;
213
214 const std::string& canonical_name = MakeCanonical(module);
215 if (canonical_name.empty()) {
216 return false;
217 }
Mark Salyzyn703fb742020-06-15 11:51:59 -0700218 this->module_blocklist_.emplace(canonical_name);
Steve Mucklee31f8402019-07-31 14:34:52 -0700219
220 return true;
221}
222
Steve Muckle18b981e2019-04-15 17:43:02 -0700223void Modprobe::ParseCfg(const std::string& cfg,
224 std::function<bool(const std::vector<std::string>&)> f) {
225 std::string cfg_contents;
226 if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) {
227 return;
228 }
229
230 std::vector<std::string> lines = android::base::Split(cfg_contents, "\n");
231 for (const std::string line : lines) {
232 if (line.empty() || line[0] == '#') {
233 continue;
234 }
235 const std::vector<std::string> args = android::base::Split(line, " ");
236 if (args.empty()) continue;
237 f(args);
238 }
239 return;
240}
241
Steve Muckle373a3ca2019-12-06 17:08:09 -0800242void Modprobe::AddOption(const std::string& module_name, const std::string& option_name,
243 const std::string& value) {
244 auto canonical_name = MakeCanonical(module_name);
245 auto options_iter = module_options_.find(canonical_name);
246 auto option_str = option_name + "=" + value;
247 if (options_iter != module_options_.end()) {
248 options_iter->second = options_iter->second + " " + option_str;
249 } else {
250 module_options_.emplace(canonical_name, option_str);
251 }
252}
253
254void Modprobe::ParseKernelCmdlineOptions(void) {
255 std::string cmdline = GetKernelCmdline();
256 std::string module_name = "";
257 std::string option_name = "";
258 std::string value = "";
259 bool in_module = true;
260 bool in_option = false;
261 bool in_value = false;
262 bool in_quotes = false;
263 int start = 0;
264
265 for (int i = 0; i < cmdline.size(); i++) {
266 if (cmdline[i] == '"') {
267 in_quotes = !in_quotes;
268 }
269
270 if (in_quotes) continue;
271
272 if (cmdline[i] == ' ') {
273 if (in_value) {
274 value = cmdline.substr(start, i - start);
275 if (!module_name.empty() && !option_name.empty()) {
276 AddOption(module_name, option_name, value);
277 }
278 }
279 module_name = "";
280 option_name = "";
281 value = "";
282 in_value = false;
283 start = i + 1;
284 in_module = true;
285 continue;
286 }
287
288 if (cmdline[i] == '.') {
289 if (in_module) {
290 module_name = cmdline.substr(start, i - start);
291 start = i + 1;
292 in_module = false;
293 }
294 in_option = true;
295 continue;
296 }
297
298 if (cmdline[i] == '=') {
299 if (in_option) {
300 option_name = cmdline.substr(start, i - start);
301 start = i + 1;
302 in_option = false;
303 }
304 in_value = true;
305 continue;
306 }
307 }
308 if (in_value && !in_quotes) {
309 value = cmdline.substr(start, cmdline.size() - start);
310 if (!module_name.empty() && !option_name.empty()) {
311 AddOption(module_name, option_name, value);
312 }
313 }
314}
315
Steve Muckle4c593232020-04-03 17:47:10 -0700316Modprobe::Modprobe(const std::vector<std::string>& base_paths, const std::string load_file) {
Steve Muckle18b981e2019-04-15 17:43:02 -0700317 using namespace std::placeholders;
318
319 for (const auto& base_path : base_paths) {
320 auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1);
321 ParseCfg(base_path + "/modules.alias", alias_callback);
322
323 auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1);
324 ParseCfg(base_path + "/modules.dep", dep_callback);
325
326 auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1);
327 ParseCfg(base_path + "/modules.softdep", softdep_callback);
328
329 auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1);
Steve Muckle4c593232020-04-03 17:47:10 -0700330 ParseCfg(base_path + "/" + load_file, load_callback);
Steve Muckle18b981e2019-04-15 17:43:02 -0700331
332 auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1);
333 ParseCfg(base_path + "/modules.options", options_callback);
Steve Mucklee31f8402019-07-31 14:34:52 -0700334
Mark Salyzyn703fb742020-06-15 11:51:59 -0700335 auto blocklist_callback = std::bind(&Modprobe::ParseBlocklistCallback, this, _1);
336 ParseCfg(base_path + "/modules.blocklist", blocklist_callback);
Steve Muckle18b981e2019-04-15 17:43:02 -0700337 }
Steve Muckleded44c02019-08-01 16:03:02 -0700338
Steve Muckle373a3ca2019-12-06 17:08:09 -0800339 ParseKernelCmdlineOptions();
Steve Muckle18b981e2019-04-15 17:43:02 -0700340}
341
Mark Salyzyn703fb742020-06-15 11:51:59 -0700342void Modprobe::EnableBlocklist(bool enable) {
343 blocklist_enabled = enable;
Steve Mucklee31f8402019-07-31 14:34:52 -0700344}
345
Steve Muckle18b981e2019-04-15 17:43:02 -0700346std::vector<std::string> Modprobe::GetDependencies(const std::string& module) {
347 auto it = module_deps_.find(module);
348 if (it == module_deps_.end()) {
349 return {};
350 }
351 return it->second;
352}
353
Steve Muckle13700a62019-07-31 09:59:48 -0700354bool Modprobe::InsmodWithDeps(const std::string& module_name, const std::string& parameters) {
Steve Muckle18b981e2019-04-15 17:43:02 -0700355 if (module_name.empty()) {
356 LOG(ERROR) << "Need valid module name, given: " << module_name;
357 return false;
358 }
359
360 auto dependencies = GetDependencies(module_name);
361 if (dependencies.empty()) {
362 LOG(ERROR) << "Module " << module_name << " not in dependency file";
363 return false;
364 }
365
366 // load module dependencies in reverse order
367 for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
Steve Muckleded44c02019-08-01 16:03:02 -0700368 LOG(VERBOSE) << "Loading hard dep for '" << module_name << "': " << *dep;
Steve Muckle73b29282019-07-30 16:03:44 -0700369 if (!LoadWithAliases(*dep, true)) {
Steve Muckle18b981e2019-04-15 17:43:02 -0700370 return false;
371 }
372 }
373
374 // try to load soft pre-dependencies
375 for (const auto& [module, softdep] : module_pre_softdep_) {
376 if (module_name == module) {
Steve Muckleded44c02019-08-01 16:03:02 -0700377 LOG(VERBOSE) << "Loading soft pre-dep for '" << module << "': " << softdep;
Steve Muckle18b981e2019-04-15 17:43:02 -0700378 LoadWithAliases(softdep, false);
379 }
380 }
381
382 // load target module itself with args
Steve Muckle13700a62019-07-31 09:59:48 -0700383 if (!Insmod(dependencies[0], parameters)) {
Steve Muckle18b981e2019-04-15 17:43:02 -0700384 return false;
385 }
386
387 // try to load soft post-dependencies
388 for (const auto& [module, softdep] : module_post_softdep_) {
389 if (module_name == module) {
Steve Muckleded44c02019-08-01 16:03:02 -0700390 LOG(VERBOSE) << "Loading soft post-dep for '" << module << "': " << softdep;
Steve Muckle18b981e2019-04-15 17:43:02 -0700391 LoadWithAliases(softdep, false);
392 }
393 }
394
395 return true;
396}
397
Steve Muckle13700a62019-07-31 09:59:48 -0700398bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict,
399 const std::string& parameters) {
Mark Salyzyn8c105192019-10-29 08:38:55 -0700400 auto canonical_name = MakeCanonical(module_name);
401 if (module_loaded_.count(canonical_name)) {
402 return true;
403 }
404
405 std::set<std::string> modules_to_load = {canonical_name};
Steve Muckle18b981e2019-04-15 17:43:02 -0700406 bool module_loaded = false;
407
408 // use aliases to expand list of modules to load (multiple modules
409 // may alias themselves to the requested name)
410 for (const auto& [alias, aliased_module] : module_aliases_) {
411 if (fnmatch(alias.c_str(), module_name.c_str(), 0) != 0) continue;
Steve Muckleded44c02019-08-01 16:03:02 -0700412 LOG(VERBOSE) << "Found alias for '" << module_name << "': '" << aliased_module;
Mark Salyzyn8c105192019-10-29 08:38:55 -0700413 if (module_loaded_.count(MakeCanonical(aliased_module))) continue;
Steve Muckle18b981e2019-04-15 17:43:02 -0700414 modules_to_load.emplace(aliased_module);
415 }
416
417 // attempt to load all modules aliased to this name
418 for (const auto& module : modules_to_load) {
419 if (!ModuleExists(module)) continue;
Steve Muckle13700a62019-07-31 09:59:48 -0700420 if (InsmodWithDeps(module, parameters)) module_loaded = true;
Steve Muckle18b981e2019-04-15 17:43:02 -0700421 }
422
423 if (strict && !module_loaded) {
Steve Muckleded44c02019-08-01 16:03:02 -0700424 LOG(ERROR) << "LoadWithAliases was unable to load " << module_name;
Steve Muckle18b981e2019-04-15 17:43:02 -0700425 return false;
426 }
427 return true;
428}
429
Mark Salyzynd4782712019-10-29 09:32:09 -0700430bool Modprobe::LoadListedModules(bool strict) {
431 auto ret = true;
Steve Muckle18b981e2019-04-15 17:43:02 -0700432 for (const auto& module : module_load_) {
433 if (!LoadWithAliases(module, true)) {
Mark Salyzynd4782712019-10-29 09:32:09 -0700434 ret = false;
435 if (strict) break;
Steve Muckle18b981e2019-04-15 17:43:02 -0700436 }
437 }
Mark Salyzynd4782712019-10-29 09:32:09 -0700438 return ret;
Steve Muckle18b981e2019-04-15 17:43:02 -0700439}
Steve Mucklebb58b012019-07-30 11:58:11 -0700440
441bool Modprobe::Remove(const std::string& module_name) {
442 auto dependencies = GetDependencies(MakeCanonical(module_name));
443 if (dependencies.empty()) {
444 LOG(ERROR) << "Empty dependencies for module " << module_name;
445 return false;
446 }
447 if (!Rmmod(dependencies[0])) {
448 return false;
449 }
450 for (auto dep = dependencies.begin() + 1; dep != dependencies.end(); ++dep) {
451 Rmmod(*dep);
452 }
453 return true;
454}
Steve Muckle012cfa12019-07-31 15:55:00 -0700455
456std::vector<std::string> Modprobe::ListModules(const std::string& pattern) {
457 std::vector<std::string> rv;
458 for (const auto& [module, deps] : module_deps_) {
459 // Attempt to match both the canonical module name and the module filename.
460 if (!fnmatch(pattern.c_str(), module.c_str(), 0)) {
461 rv.emplace_back(module);
462 } else if (!fnmatch(pattern.c_str(), basename(deps[0].c_str()), 0)) {
463 rv.emplace_back(deps[0]);
464 }
465 }
466 return rv;
467}
Steve Muckle781aa782019-08-01 14:55:07 -0700468
469bool Modprobe::GetAllDependencies(const std::string& module,
470 std::vector<std::string>* pre_dependencies,
471 std::vector<std::string>* dependencies,
472 std::vector<std::string>* post_dependencies) {
473 std::string canonical_name = MakeCanonical(module);
474 if (pre_dependencies) {
475 pre_dependencies->clear();
476 for (const auto& [it_module, it_softdep] : module_pre_softdep_) {
477 if (canonical_name == it_module) {
478 pre_dependencies->emplace_back(it_softdep);
479 }
480 }
481 }
482 if (dependencies) {
483 dependencies->clear();
484 auto hard_deps = GetDependencies(canonical_name);
485 if (hard_deps.empty()) {
486 return false;
487 }
488 for (auto dep = hard_deps.rbegin(); dep != hard_deps.rend(); dep++) {
489 dependencies->emplace_back(*dep);
490 }
491 }
492 if (post_dependencies) {
493 for (const auto& [it_module, it_softdep] : module_post_softdep_) {
494 if (canonical_name == it_module) {
495 post_dependencies->emplace_back(it_softdep);
496 }
497 }
498 }
499 return true;
500}