Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 24 | #include <map> |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 25 | #include <set> |
| 26 | #include <string> |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 27 | #include <thread> |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | #include <android-base/chrono_utils.h> |
| 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/strings.h> |
| 34 | #include <android-base/unique_fd.h> |
| 35 | |
| 36 | std::string Modprobe::MakeCanonical(const std::string& module_path) { |
| 37 | auto start = module_path.find_last_of('/'); |
| 38 | if (start == std::string::npos) { |
| 39 | start = 0; |
| 40 | } else { |
| 41 | start += 1; |
| 42 | } |
| 43 | auto end = module_path.size(); |
| 44 | if (android::base::EndsWith(module_path, ".ko")) { |
| 45 | end -= 3; |
| 46 | } |
| 47 | if ((end - start) <= 1) { |
| 48 | LOG(ERROR) << "malformed module name: " << module_path; |
| 49 | return ""; |
| 50 | } |
| 51 | std::string module_name = module_path.substr(start, end - start); |
| 52 | // module names can have '-', but their file names will have '_' |
| 53 | std::replace(module_name.begin(), module_name.end(), '-', '_'); |
| 54 | return module_name; |
| 55 | } |
| 56 | |
| 57 | bool Modprobe::ParseDepCallback(const std::string& base_path, |
| 58 | const std::vector<std::string>& args) { |
| 59 | std::vector<std::string> deps; |
| 60 | std::string prefix = ""; |
| 61 | |
| 62 | // Set first item as our modules path |
| 63 | std::string::size_type pos = args[0].find(':'); |
| 64 | if (args[0][0] != '/') { |
| 65 | prefix = base_path + "/"; |
| 66 | } |
| 67 | if (pos != std::string::npos) { |
| 68 | deps.emplace_back(prefix + args[0].substr(0, pos)); |
| 69 | } else { |
| 70 | LOG(ERROR) << "dependency lines must start with name followed by ':'"; |
Andrew Scull | fb18f6e | 2020-10-18 17:37:27 +0100 | [diff] [blame] | 71 | return false; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Remaining items are dependencies of our module |
| 75 | for (auto arg = args.begin() + 1; arg != args.end(); ++arg) { |
| 76 | if ((*arg)[0] != '/') { |
| 77 | prefix = base_path + "/"; |
| 78 | } else { |
| 79 | prefix = ""; |
| 80 | } |
| 81 | deps.push_back(prefix + *arg); |
| 82 | } |
| 83 | |
| 84 | std::string canonical_name = MakeCanonical(args[0].substr(0, pos)); |
| 85 | if (canonical_name.empty()) { |
| 86 | return false; |
| 87 | } |
| 88 | this->module_deps_[canonical_name] = deps; |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool Modprobe::ParseAliasCallback(const std::vector<std::string>& args) { |
| 94 | auto it = args.begin(); |
| 95 | const std::string& type = *it++; |
| 96 | |
| 97 | if (type != "alias") { |
| 98 | LOG(ERROR) << "non-alias line encountered in modules.alias, found " << type; |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | if (args.size() != 3) { |
| 103 | LOG(ERROR) << "alias lines in modules.alias must have 3 entries, not " << args.size(); |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | const std::string& alias = *it++; |
| 108 | const std::string& module_name = *it++; |
| 109 | this->module_aliases_.emplace_back(alias, module_name); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool Modprobe::ParseSoftdepCallback(const std::vector<std::string>& args) { |
| 115 | auto it = args.begin(); |
| 116 | const std::string& type = *it++; |
| 117 | std::string state = ""; |
| 118 | |
| 119 | if (type != "softdep") { |
| 120 | LOG(ERROR) << "non-softdep line encountered in modules.softdep, found " << type; |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | if (args.size() < 4) { |
| 125 | LOG(ERROR) << "softdep lines in modules.softdep must have at least 4 entries"; |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | const std::string& module = *it++; |
| 130 | while (it != args.end()) { |
| 131 | const std::string& token = *it++; |
| 132 | if (token == "pre:" || token == "post:") { |
| 133 | state = token; |
| 134 | continue; |
| 135 | } |
| 136 | if (state == "") { |
| 137 | LOG(ERROR) << "malformed modules.softdep at token " << token; |
| 138 | return false; |
| 139 | } |
| 140 | if (state == "pre:") { |
| 141 | this->module_pre_softdep_.emplace_back(module, token); |
| 142 | } else { |
| 143 | this->module_post_softdep_.emplace_back(module, token); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | bool Modprobe::ParseLoadCallback(const std::vector<std::string>& args) { |
| 151 | auto it = args.begin(); |
| 152 | const std::string& module = *it++; |
| 153 | |
| 154 | const std::string& canonical_name = MakeCanonical(module); |
| 155 | if (canonical_name.empty()) { |
| 156 | return false; |
| 157 | } |
| 158 | this->module_load_.emplace_back(canonical_name); |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool Modprobe::ParseOptionsCallback(const std::vector<std::string>& args) { |
| 164 | auto it = args.begin(); |
| 165 | const std::string& type = *it++; |
| 166 | |
| 167 | if (type != "options") { |
| 168 | LOG(ERROR) << "non-options line encountered in modules.options"; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | if (args.size() < 2) { |
| 173 | LOG(ERROR) << "lines in modules.options must have at least 2 entries, not " << args.size(); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | const std::string& module = *it++; |
| 178 | std::string options = ""; |
| 179 | |
| 180 | const std::string& canonical_name = MakeCanonical(module); |
| 181 | if (canonical_name.empty()) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | while (it != args.end()) { |
| 186 | options += *it++; |
| 187 | if (it != args.end()) { |
| 188 | options += " "; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | auto [unused, inserted] = this->module_options_.emplace(canonical_name, options); |
| 193 | if (!inserted) { |
| 194 | LOG(ERROR) << "multiple options lines present for module " << module; |
| 195 | return false; |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 200 | bool Modprobe::ParseBlocklistCallback(const std::vector<std::string>& args) { |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 201 | auto it = args.begin(); |
| 202 | const std::string& type = *it++; |
| 203 | |
Mark Salyzyn | 9debda1 | 2020-06-16 05:14:06 -0700 | [diff] [blame] | 204 | if (type != "blocklist") { |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 205 | LOG(ERROR) << "non-blocklist line encountered in modules.blocklist"; |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if (args.size() != 2) { |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 210 | LOG(ERROR) << "lines in modules.blocklist must have exactly 2 entries, not " << args.size(); |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | |
| 214 | const std::string& module = *it++; |
| 215 | |
| 216 | const std::string& canonical_name = MakeCanonical(module); |
| 217 | if (canonical_name.empty()) { |
| 218 | return false; |
| 219 | } |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 220 | this->module_blocklist_.emplace(canonical_name); |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 225 | void Modprobe::ParseCfg(const std::string& cfg, |
| 226 | std::function<bool(const std::vector<std::string>&)> f) { |
| 227 | std::string cfg_contents; |
| 228 | if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | std::vector<std::string> lines = android::base::Split(cfg_contents, "\n"); |
| 233 | for (const std::string line : lines) { |
| 234 | if (line.empty() || line[0] == '#') { |
| 235 | continue; |
| 236 | } |
| 237 | const std::vector<std::string> args = android::base::Split(line, " "); |
| 238 | if (args.empty()) continue; |
| 239 | f(args); |
| 240 | } |
| 241 | return; |
| 242 | } |
| 243 | |
Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 244 | void Modprobe::AddOption(const std::string& module_name, const std::string& option_name, |
| 245 | const std::string& value) { |
| 246 | auto canonical_name = MakeCanonical(module_name); |
| 247 | auto options_iter = module_options_.find(canonical_name); |
| 248 | auto option_str = option_name + "=" + value; |
| 249 | if (options_iter != module_options_.end()) { |
| 250 | options_iter->second = options_iter->second + " " + option_str; |
| 251 | } else { |
| 252 | module_options_.emplace(canonical_name, option_str); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void Modprobe::ParseKernelCmdlineOptions(void) { |
| 257 | std::string cmdline = GetKernelCmdline(); |
| 258 | std::string module_name = ""; |
| 259 | std::string option_name = ""; |
| 260 | std::string value = ""; |
| 261 | bool in_module = true; |
| 262 | bool in_option = false; |
| 263 | bool in_value = false; |
| 264 | bool in_quotes = false; |
| 265 | int start = 0; |
| 266 | |
| 267 | for (int i = 0; i < cmdline.size(); i++) { |
| 268 | if (cmdline[i] == '"') { |
| 269 | in_quotes = !in_quotes; |
| 270 | } |
| 271 | |
| 272 | if (in_quotes) continue; |
| 273 | |
| 274 | if (cmdline[i] == ' ') { |
| 275 | if (in_value) { |
| 276 | value = cmdline.substr(start, i - start); |
| 277 | if (!module_name.empty() && !option_name.empty()) { |
| 278 | AddOption(module_name, option_name, value); |
| 279 | } |
| 280 | } |
| 281 | module_name = ""; |
| 282 | option_name = ""; |
| 283 | value = ""; |
| 284 | in_value = false; |
| 285 | start = i + 1; |
| 286 | in_module = true; |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (cmdline[i] == '.') { |
| 291 | if (in_module) { |
| 292 | module_name = cmdline.substr(start, i - start); |
| 293 | start = i + 1; |
| 294 | in_module = false; |
| 295 | } |
| 296 | in_option = true; |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | if (cmdline[i] == '=') { |
| 301 | if (in_option) { |
| 302 | option_name = cmdline.substr(start, i - start); |
| 303 | start = i + 1; |
| 304 | in_option = false; |
| 305 | } |
| 306 | in_value = true; |
| 307 | continue; |
| 308 | } |
| 309 | } |
| 310 | if (in_value && !in_quotes) { |
| 311 | value = cmdline.substr(start, cmdline.size() - start); |
| 312 | if (!module_name.empty() && !option_name.empty()) { |
| 313 | AddOption(module_name, option_name, value); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 318 | Modprobe::Modprobe(const std::vector<std::string>& base_paths, const std::string load_file, |
| 319 | bool use_blocklist) |
| 320 | : blocklist_enabled(use_blocklist) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 321 | using namespace std::placeholders; |
| 322 | |
| 323 | for (const auto& base_path : base_paths) { |
| 324 | auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1); |
| 325 | ParseCfg(base_path + "/modules.alias", alias_callback); |
| 326 | |
| 327 | auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1); |
| 328 | ParseCfg(base_path + "/modules.dep", dep_callback); |
| 329 | |
| 330 | auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1); |
| 331 | ParseCfg(base_path + "/modules.softdep", softdep_callback); |
| 332 | |
| 333 | auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1); |
Steve Muckle | 4c59323 | 2020-04-03 17:47:10 -0700 | [diff] [blame] | 334 | ParseCfg(base_path + "/" + load_file, load_callback); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 335 | |
| 336 | auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1); |
| 337 | ParseCfg(base_path + "/modules.options", options_callback); |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 338 | |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 339 | auto blocklist_callback = std::bind(&Modprobe::ParseBlocklistCallback, this, _1); |
| 340 | ParseCfg(base_path + "/modules.blocklist", blocklist_callback); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 341 | } |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 342 | |
Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 343 | ParseKernelCmdlineOptions(); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | std::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 Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 354 | bool Modprobe::InsmodWithDeps(const std::string& module_name, const std::string& parameters) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 355 | 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 Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 368 | LOG(VERBOSE) << "Loading hard dep for '" << module_name << "': " << *dep; |
Steve Muckle | 73b2928 | 2019-07-30 16:03:44 -0700 | [diff] [blame] | 369 | if (!LoadWithAliases(*dep, true)) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 370 | 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 Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 377 | LOG(VERBOSE) << "Loading soft pre-dep for '" << module << "': " << softdep; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 378 | LoadWithAliases(softdep, false); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // load target module itself with args |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 383 | if (!Insmod(dependencies[0], parameters)) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 384 | 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 Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 390 | LOG(VERBOSE) << "Loading soft post-dep for '" << module << "': " << softdep; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 391 | LoadWithAliases(softdep, false); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 398 | bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict, |
| 399 | const std::string& parameters) { |
Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 400 | 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 Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 406 | 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 Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 412 | LOG(VERBOSE) << "Found alias for '" << module_name << "': '" << aliased_module; |
Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 413 | if (module_loaded_.count(MakeCanonical(aliased_module))) continue; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 414 | 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 Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 420 | if (InsmodWithDeps(module, parameters)) module_loaded = true; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | if (strict && !module_loaded) { |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 424 | LOG(ERROR) << "LoadWithAliases was unable to load " << module_name; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | return true; |
| 428 | } |
| 429 | |
Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 430 | bool Modprobe::IsBlocklisted(const std::string& module_name) { |
| 431 | if (!blocklist_enabled) return false; |
| 432 | |
| 433 | auto canonical_name = MakeCanonical(module_name); |
| 434 | auto dependencies = GetDependencies(canonical_name); |
| 435 | for (auto dep = dependencies.begin(); dep != dependencies.end(); ++dep) { |
| 436 | if (module_blocklist_.count(MakeCanonical(*dep))) return true; |
| 437 | } |
| 438 | |
| 439 | return module_blocklist_.count(canonical_name) > 0; |
| 440 | } |
| 441 | |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 442 | // Another option to load kernel modules. load in independent modules in parallel |
| 443 | // and then update dependency list of other remaining modules, repeat these steps |
| 444 | // until all modules are loaded. |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 445 | bool Modprobe::LoadModulesParallel(int num_threads) { |
| 446 | bool ret = true; |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 447 | int count = -1; |
| 448 | std::map<std::string, std::set<std::string>> mod_with_deps; |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 449 | |
| 450 | // Get dependencies |
| 451 | for (const auto& module : module_load_) { |
| 452 | auto dependencies = GetDependencies(MakeCanonical(module)); |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 453 | |
| 454 | for (auto dep = dependencies.rbegin(); dep != dependencies.rend(); dep++) { |
| 455 | mod_with_deps[module].emplace(*dep); |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 459 | // Get soft dependencies |
| 460 | for (const auto& [it_mod, it_softdep] : module_pre_softdep_) { |
| 461 | if (mod_with_deps.find(MakeCanonical(it_softdep)) != mod_with_deps.end()) { |
| 462 | mod_with_deps[MakeCanonical(it_mod)].emplace( |
| 463 | GetDependencies(MakeCanonical(it_softdep))[0]); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Get soft post dependencies |
| 468 | for (const auto& [it_mod, it_softdep] : module_post_softdep_) { |
| 469 | if (mod_with_deps.find(MakeCanonical(it_softdep)) != mod_with_deps.end()) { |
| 470 | mod_with_deps[MakeCanonical(it_softdep)].emplace( |
| 471 | GetDependencies(MakeCanonical(it_mod))[0]); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | while (!mod_with_deps.empty() && count != module_loaded_.size()) { |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 476 | std::vector<std::thread> threads; |
| 477 | std::vector<std::string> mods_path_to_load; |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 478 | std::mutex vector_lock; |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 479 | count = module_loaded_.size(); |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 480 | |
chungkai | d84c42e | 2022-06-27 10:22:08 +0000 | [diff] [blame] | 481 | // Find independent modules |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 482 | for (const auto& [it_mod, it_dep] : mod_with_deps) { |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 483 | if (it_dep.size() == 1) { |
| 484 | if (module_options_[it_mod].find("load_sequential=1") != std::string::npos) { |
| 485 | if (!LoadWithAliases(it_mod, true) && !IsBlocklisted(it_mod)) { |
| 486 | return false; |
| 487 | } |
| 488 | } else { |
| 489 | mods_path_to_load.emplace_back(it_mod); |
chungkai | d84c42e | 2022-06-27 10:22:08 +0000 | [diff] [blame] | 490 | } |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | // Load independent modules in parallel |
| 495 | auto thread_function = [&] { |
| 496 | std::unique_lock lk(vector_lock); |
| 497 | while (!mods_path_to_load.empty()) { |
chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 498 | auto ret_load = true; |
| 499 | auto mod_to_load = std::move(mods_path_to_load.back()); |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 500 | mods_path_to_load.pop_back(); |
| 501 | |
| 502 | lk.unlock(); |
chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 503 | ret_load &= LoadWithAliases(mod_to_load, true); |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 504 | lk.lock(); |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 505 | if (!ret_load && !IsBlocklisted(mod_to_load)) { |
chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 506 | ret &= ret_load; |
| 507 | } |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 508 | } |
| 509 | }; |
| 510 | |
| 511 | std::generate_n(std::back_inserter(threads), num_threads, |
| 512 | [&] { return std::thread(thread_function); }); |
| 513 | |
| 514 | // Wait for the threads. |
| 515 | for (auto& thread : threads) { |
| 516 | thread.join(); |
| 517 | } |
| 518 | |
chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 519 | if (!ret) return ret; |
| 520 | |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 521 | std::lock_guard guard(module_loaded_lock_); |
| 522 | // Remove loaded module form mod_with_deps and soft dependencies of other modules |
| 523 | for (const auto& module_loaded : module_loaded_) { |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 524 | mod_with_deps.erase(module_loaded); |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // Remove loaded module form dependencies of other modules which are not loaded yet |
| 528 | for (const auto& module_loaded_path : module_loaded_paths_) { |
| 529 | for (auto& [mod, deps] : mod_with_deps) { |
William McVicker | a0e6703 | 2023-02-06 17:34:57 +0000 | [diff] [blame] | 530 | deps.erase(module_loaded_path); |
Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | |
Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 538 | bool Modprobe::LoadListedModules(bool strict) { |
| 539 | auto ret = true; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 540 | for (const auto& module : module_load_) { |
| 541 | if (!LoadWithAliases(module, true)) { |
Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 542 | if (IsBlocklisted(module)) continue; |
Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 543 | ret = false; |
| 544 | if (strict) break; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 547 | return ret; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 548 | } |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 549 | |
| 550 | bool Modprobe::Remove(const std::string& module_name) { |
| 551 | auto dependencies = GetDependencies(MakeCanonical(module_name)); |
Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 552 | for (auto dep = dependencies.begin(); dep != dependencies.end(); ++dep) { |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 553 | Rmmod(*dep); |
| 554 | } |
Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 555 | Rmmod(module_name); |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 556 | return true; |
| 557 | } |
Steve Muckle | 012cfa1 | 2019-07-31 15:55:00 -0700 | [diff] [blame] | 558 | |
| 559 | std::vector<std::string> Modprobe::ListModules(const std::string& pattern) { |
| 560 | std::vector<std::string> rv; |
| 561 | for (const auto& [module, deps] : module_deps_) { |
| 562 | // Attempt to match both the canonical module name and the module filename. |
| 563 | if (!fnmatch(pattern.c_str(), module.c_str(), 0)) { |
| 564 | rv.emplace_back(module); |
Colin Cross | d459ccd | 2023-04-13 21:59:58 -0700 | [diff] [blame] | 565 | } else if (!fnmatch(pattern.c_str(), android::base::Basename(deps[0]).c_str(), 0)) { |
Steve Muckle | 012cfa1 | 2019-07-31 15:55:00 -0700 | [diff] [blame] | 566 | rv.emplace_back(deps[0]); |
| 567 | } |
| 568 | } |
| 569 | return rv; |
| 570 | } |
Steve Muckle | 781aa78 | 2019-08-01 14:55:07 -0700 | [diff] [blame] | 571 | |
| 572 | bool Modprobe::GetAllDependencies(const std::string& module, |
| 573 | std::vector<std::string>* pre_dependencies, |
| 574 | std::vector<std::string>* dependencies, |
| 575 | std::vector<std::string>* post_dependencies) { |
| 576 | std::string canonical_name = MakeCanonical(module); |
| 577 | if (pre_dependencies) { |
| 578 | pre_dependencies->clear(); |
| 579 | for (const auto& [it_module, it_softdep] : module_pre_softdep_) { |
| 580 | if (canonical_name == it_module) { |
| 581 | pre_dependencies->emplace_back(it_softdep); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | if (dependencies) { |
| 586 | dependencies->clear(); |
| 587 | auto hard_deps = GetDependencies(canonical_name); |
| 588 | if (hard_deps.empty()) { |
| 589 | return false; |
| 590 | } |
| 591 | for (auto dep = hard_deps.rbegin(); dep != hard_deps.rend(); dep++) { |
| 592 | dependencies->emplace_back(*dep); |
| 593 | } |
| 594 | } |
| 595 | if (post_dependencies) { |
| 596 | for (const auto& [it_module, it_softdep] : module_post_softdep_) { |
| 597 | if (canonical_name == it_module) { |
| 598 | post_dependencies->emplace_back(it_softdep); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | return true; |
| 603 | } |