| 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"); | 
| Kelvin Zhang | db15b6f | 2023-05-03 15:28:39 -0700 | [diff] [blame] | 233 | for (const auto& line : lines) { | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 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) { | 
| Kelvin Zhang | db15b6f | 2023-05-03 15:28:39 -0700 | [diff] [blame] | 424 | LOG(ERROR) << "LoadWithAliases was unable to load " << module_name | 
|  | 425 | << ", tried: " << android::base::Join(modules_to_load, ", "); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 426 | return false; | 
|  | 427 | } | 
|  | 428 | return true; | 
|  | 429 | } | 
|  | 430 |  | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 431 | bool Modprobe::IsBlocklisted(const std::string& module_name) { | 
|  | 432 | if (!blocklist_enabled) return false; | 
|  | 433 |  | 
|  | 434 | auto canonical_name = MakeCanonical(module_name); | 
|  | 435 | auto dependencies = GetDependencies(canonical_name); | 
|  | 436 | for (auto dep = dependencies.begin(); dep != dependencies.end(); ++dep) { | 
|  | 437 | if (module_blocklist_.count(MakeCanonical(*dep))) return true; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | return module_blocklist_.count(canonical_name) > 0; | 
|  | 441 | } | 
|  | 442 |  | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 443 | // Another option to load kernel modules. load independent modules dependencies | 
|  | 444 | // in parallel and then update dependency list of other remaining modules, | 
|  | 445 | // repeat these steps until all modules are loaded. | 
|  | 446 | // Discard all blocklist. | 
|  | 447 | // Softdeps are taken care in InsmodWithDeps(). | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 448 | bool Modprobe::LoadModulesParallel(int num_threads) { | 
|  | 449 | bool ret = true; | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 450 | std::unordered_map<std::string, std::vector<std::string>> mod_with_deps; | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 451 |  | 
|  | 452 | // Get dependencies | 
|  | 453 | for (const auto& module : module_load_) { | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 454 | // Skip blocklist modules | 
|  | 455 | if (IsBlocklisted(module)) { | 
|  | 456 | LOG(VERBOSE) << "LMP: Blocklist: Module " << module << " skipping..."; | 
|  | 457 | continue; | 
|  | 458 | } | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 459 | auto dependencies = GetDependencies(MakeCanonical(module)); | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 460 | if (dependencies.empty()) { | 
|  | 461 | LOG(ERROR) << "LMP: Hard-dep: Module " << module | 
|  | 462 | << " not in .dep file"; | 
|  | 463 | return false; | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 464 | } | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 465 | mod_with_deps[MakeCanonical(module)] = dependencies; | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 468 | while (!mod_with_deps.empty()) { | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 469 | std::vector<std::thread> threads; | 
|  | 470 | std::vector<std::string> mods_path_to_load; | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 471 | std::mutex vector_lock; | 
|  | 472 |  | 
| chungkai | d84c42e | 2022-06-27 10:22:08 +0000 | [diff] [blame] | 473 | // Find independent modules | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 474 | for (const auto& [it_mod, it_dep] : mod_with_deps) { | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 475 | auto itd_last = it_dep.rbegin(); | 
|  | 476 | if (itd_last == it_dep.rend()) | 
|  | 477 | continue; | 
|  | 478 |  | 
|  | 479 | auto cnd_last = MakeCanonical(*itd_last); | 
|  | 480 | // Hard-dependencies cannot be blocklisted | 
|  | 481 | if (IsBlocklisted(cnd_last)) { | 
|  | 482 | LOG(ERROR) << "LMP: Blocklist: Module-dep " << cnd_last | 
|  | 483 | << " : failed to load module " << it_mod; | 
|  | 484 | return false; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | if (module_options_[cnd_last].find("load_sequential=1") != std::string::npos) { | 
|  | 488 | if (!LoadWithAliases(cnd_last, true)) { | 
|  | 489 | return false; | 
|  | 490 | } | 
|  | 491 | } else { | 
|  | 492 | if (std::find(mods_path_to_load.begin(), mods_path_to_load.end(), | 
|  | 493 | cnd_last) == mods_path_to_load.end()) { | 
|  | 494 | mods_path_to_load.emplace_back(cnd_last); | 
| chungkai | d84c42e | 2022-06-27 10:22:08 +0000 | [diff] [blame] | 495 | } | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 496 | } | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | // Load independent modules in parallel | 
|  | 500 | auto thread_function = [&] { | 
|  | 501 | std::unique_lock lk(vector_lock); | 
|  | 502 | while (!mods_path_to_load.empty()) { | 
| chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 503 | auto ret_load = true; | 
|  | 504 | auto mod_to_load = std::move(mods_path_to_load.back()); | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 505 | mods_path_to_load.pop_back(); | 
|  | 506 |  | 
|  | 507 | lk.unlock(); | 
| chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 508 | ret_load &= LoadWithAliases(mod_to_load, true); | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 509 | lk.lock(); | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 510 | if (!ret_load) { | 
| chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 511 | ret &= ret_load; | 
|  | 512 | } | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 513 | } | 
|  | 514 | }; | 
|  | 515 |  | 
|  | 516 | std::generate_n(std::back_inserter(threads), num_threads, | 
|  | 517 | [&] { return std::thread(thread_function); }); | 
|  | 518 |  | 
|  | 519 | // Wait for the threads. | 
|  | 520 | for (auto& thread : threads) { | 
|  | 521 | thread.join(); | 
|  | 522 | } | 
|  | 523 |  | 
| chungkai | 8b45152 | 2022-07-28 05:09:32 +0000 | [diff] [blame] | 524 | if (!ret) return ret; | 
|  | 525 |  | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 526 | std::lock_guard guard(module_loaded_lock_); | 
|  | 527 | // Remove loaded module form mod_with_deps and soft dependencies of other modules | 
| Wasim Nazir | ecd154e | 2023-04-06 15:07:22 +0530 | [diff] [blame] | 528 | for (const auto& module_loaded : module_loaded_) | 
|  | 529 | mod_with_deps.erase(module_loaded); | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 530 |  | 
|  | 531 | // Remove loaded module form dependencies of other modules which are not loaded yet | 
|  | 532 | for (const auto& module_loaded_path : module_loaded_paths_) { | 
|  | 533 | for (auto& [mod, deps] : mod_with_deps) { | 
| Chung-Kai (Michael) Mei | 763e869 | 2023-03-01 04:24:10 +0000 | [diff] [blame] | 534 | auto it = std::find(deps.begin(), deps.end(), module_loaded_path); | 
|  | 535 | if (it != deps.end()) { | 
|  | 536 | deps.erase(it); | 
|  | 537 | } | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 538 | } | 
|  | 539 | } | 
|  | 540 | } | 
|  | 541 |  | 
|  | 542 | return ret; | 
|  | 543 | } | 
|  | 544 |  | 
| Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 545 | bool Modprobe::LoadListedModules(bool strict) { | 
|  | 546 | auto ret = true; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 547 | for (const auto& module : module_load_) { | 
|  | 548 | if (!LoadWithAliases(module, true)) { | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 549 | if (IsBlocklisted(module)) continue; | 
| Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 550 | ret = false; | 
|  | 551 | if (strict) break; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 552 | } | 
|  | 553 | } | 
| Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 554 | return ret; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 555 | } | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 556 |  | 
|  | 557 | bool Modprobe::Remove(const std::string& module_name) { | 
|  | 558 | auto dependencies = GetDependencies(MakeCanonical(module_name)); | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 559 | for (auto dep = dependencies.begin(); dep != dependencies.end(); ++dep) { | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 560 | Rmmod(*dep); | 
|  | 561 | } | 
| Will McVicker | 87b2ef0 | 2021-03-12 11:11:37 -0800 | [diff] [blame] | 562 | Rmmod(module_name); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 563 | return true; | 
|  | 564 | } | 
| Steve Muckle | 012cfa1 | 2019-07-31 15:55:00 -0700 | [diff] [blame] | 565 |  | 
|  | 566 | std::vector<std::string> Modprobe::ListModules(const std::string& pattern) { | 
|  | 567 | std::vector<std::string> rv; | 
|  | 568 | for (const auto& [module, deps] : module_deps_) { | 
|  | 569 | // Attempt to match both the canonical module name and the module filename. | 
|  | 570 | if (!fnmatch(pattern.c_str(), module.c_str(), 0)) { | 
|  | 571 | rv.emplace_back(module); | 
| Colin Cross | d459ccd | 2023-04-13 21:59:58 -0700 | [diff] [blame] | 572 | } 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] | 573 | rv.emplace_back(deps[0]); | 
|  | 574 | } | 
|  | 575 | } | 
|  | 576 | return rv; | 
|  | 577 | } | 
| Steve Muckle | 781aa78 | 2019-08-01 14:55:07 -0700 | [diff] [blame] | 578 |  | 
|  | 579 | bool Modprobe::GetAllDependencies(const std::string& module, | 
|  | 580 | std::vector<std::string>* pre_dependencies, | 
|  | 581 | std::vector<std::string>* dependencies, | 
|  | 582 | std::vector<std::string>* post_dependencies) { | 
|  | 583 | std::string canonical_name = MakeCanonical(module); | 
|  | 584 | if (pre_dependencies) { | 
|  | 585 | pre_dependencies->clear(); | 
|  | 586 | for (const auto& [it_module, it_softdep] : module_pre_softdep_) { | 
|  | 587 | if (canonical_name == it_module) { | 
|  | 588 | pre_dependencies->emplace_back(it_softdep); | 
|  | 589 | } | 
|  | 590 | } | 
|  | 591 | } | 
|  | 592 | if (dependencies) { | 
|  | 593 | dependencies->clear(); | 
|  | 594 | auto hard_deps = GetDependencies(canonical_name); | 
|  | 595 | if (hard_deps.empty()) { | 
|  | 596 | return false; | 
|  | 597 | } | 
|  | 598 | for (auto dep = hard_deps.rbegin(); dep != hard_deps.rend(); dep++) { | 
|  | 599 | dependencies->emplace_back(*dep); | 
|  | 600 | } | 
|  | 601 | } | 
|  | 602 | if (post_dependencies) { | 
|  | 603 | for (const auto& [it_module, it_softdep] : module_post_softdep_) { | 
|  | 604 | if (canonical_name == it_module) { | 
|  | 605 | post_dependencies->emplace_back(it_softdep); | 
|  | 606 | } | 
|  | 607 | } | 
|  | 608 | } | 
|  | 609 | return true; | 
|  | 610 | } |