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> |
| 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 | |
| 34 | std::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 | |
| 55 | bool 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 ':'"; |
| 69 | } |
| 70 | |
| 71 | // Remaining items are dependencies of our module |
| 72 | for (auto arg = args.begin() + 1; arg != args.end(); ++arg) { |
| 73 | if ((*arg)[0] != '/') { |
| 74 | prefix = base_path + "/"; |
| 75 | } else { |
| 76 | prefix = ""; |
| 77 | } |
| 78 | deps.push_back(prefix + *arg); |
| 79 | } |
| 80 | |
| 81 | std::string canonical_name = MakeCanonical(args[0].substr(0, pos)); |
| 82 | if (canonical_name.empty()) { |
| 83 | return false; |
| 84 | } |
| 85 | this->module_deps_[canonical_name] = deps; |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | bool Modprobe::ParseAliasCallback(const std::vector<std::string>& args) { |
| 91 | auto it = args.begin(); |
| 92 | const std::string& type = *it++; |
| 93 | |
| 94 | if (type != "alias") { |
| 95 | LOG(ERROR) << "non-alias line encountered in modules.alias, found " << type; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | if (args.size() != 3) { |
| 100 | LOG(ERROR) << "alias lines in modules.alias must have 3 entries, not " << args.size(); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | const std::string& alias = *it++; |
| 105 | const std::string& module_name = *it++; |
| 106 | this->module_aliases_.emplace_back(alias, module_name); |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | bool Modprobe::ParseSoftdepCallback(const std::vector<std::string>& args) { |
| 112 | auto it = args.begin(); |
| 113 | const std::string& type = *it++; |
| 114 | std::string state = ""; |
| 115 | |
| 116 | if (type != "softdep") { |
| 117 | LOG(ERROR) << "non-softdep line encountered in modules.softdep, found " << type; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (args.size() < 4) { |
| 122 | LOG(ERROR) << "softdep lines in modules.softdep must have at least 4 entries"; |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | const std::string& module = *it++; |
| 127 | while (it != args.end()) { |
| 128 | const std::string& token = *it++; |
| 129 | if (token == "pre:" || token == "post:") { |
| 130 | state = token; |
| 131 | continue; |
| 132 | } |
| 133 | if (state == "") { |
| 134 | LOG(ERROR) << "malformed modules.softdep at token " << token; |
| 135 | return false; |
| 136 | } |
| 137 | if (state == "pre:") { |
| 138 | this->module_pre_softdep_.emplace_back(module, token); |
| 139 | } else { |
| 140 | this->module_post_softdep_.emplace_back(module, token); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | bool Modprobe::ParseLoadCallback(const std::vector<std::string>& args) { |
| 148 | auto it = args.begin(); |
| 149 | const std::string& module = *it++; |
| 150 | |
| 151 | const std::string& canonical_name = MakeCanonical(module); |
| 152 | if (canonical_name.empty()) { |
| 153 | return false; |
| 154 | } |
| 155 | this->module_load_.emplace_back(canonical_name); |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool Modprobe::ParseOptionsCallback(const std::vector<std::string>& args) { |
| 161 | auto it = args.begin(); |
| 162 | const std::string& type = *it++; |
| 163 | |
| 164 | if (type != "options") { |
| 165 | LOG(ERROR) << "non-options line encountered in modules.options"; |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | if (args.size() < 2) { |
| 170 | LOG(ERROR) << "lines in modules.options must have at least 2 entries, not " << args.size(); |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | const std::string& module = *it++; |
| 175 | std::string options = ""; |
| 176 | |
| 177 | const std::string& canonical_name = MakeCanonical(module); |
| 178 | if (canonical_name.empty()) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | while (it != args.end()) { |
| 183 | options += *it++; |
| 184 | if (it != args.end()) { |
| 185 | options += " "; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | auto [unused, inserted] = this->module_options_.emplace(canonical_name, options); |
| 190 | if (!inserted) { |
| 191 | LOG(ERROR) << "multiple options lines present for module " << module; |
| 192 | return false; |
| 193 | } |
| 194 | return true; |
| 195 | } |
| 196 | |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 197 | bool Modprobe::ParseBlocklistCallback(const std::vector<std::string>& args) { |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 198 | auto it = args.begin(); |
| 199 | const std::string& type = *it++; |
| 200 | |
Mark Salyzyn | 9debda1 | 2020-06-16 05:14:06 -0700 | [diff] [blame] | 201 | if (type != "blocklist") { |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 202 | LOG(ERROR) << "non-blocklist line encountered in modules.blocklist"; |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
| 206 | if (args.size() != 2) { |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 207 | 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] | 208 | return false; |
| 209 | } |
| 210 | |
| 211 | const std::string& module = *it++; |
| 212 | |
| 213 | const std::string& canonical_name = MakeCanonical(module); |
| 214 | if (canonical_name.empty()) { |
| 215 | return false; |
| 216 | } |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 217 | this->module_blocklist_.emplace(canonical_name); |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 222 | void Modprobe::ParseCfg(const std::string& cfg, |
| 223 | std::function<bool(const std::vector<std::string>&)> f) { |
| 224 | std::string cfg_contents; |
| 225 | if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | std::vector<std::string> lines = android::base::Split(cfg_contents, "\n"); |
| 230 | for (const std::string line : lines) { |
| 231 | if (line.empty() || line[0] == '#') { |
| 232 | continue; |
| 233 | } |
| 234 | const std::vector<std::string> args = android::base::Split(line, " "); |
| 235 | if (args.empty()) continue; |
| 236 | f(args); |
| 237 | } |
| 238 | return; |
| 239 | } |
| 240 | |
Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 241 | void Modprobe::AddOption(const std::string& module_name, const std::string& option_name, |
| 242 | const std::string& value) { |
| 243 | auto canonical_name = MakeCanonical(module_name); |
| 244 | auto options_iter = module_options_.find(canonical_name); |
| 245 | auto option_str = option_name + "=" + value; |
| 246 | if (options_iter != module_options_.end()) { |
| 247 | options_iter->second = options_iter->second + " " + option_str; |
| 248 | } else { |
| 249 | module_options_.emplace(canonical_name, option_str); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void Modprobe::ParseKernelCmdlineOptions(void) { |
| 254 | std::string cmdline = GetKernelCmdline(); |
| 255 | std::string module_name = ""; |
| 256 | std::string option_name = ""; |
| 257 | std::string value = ""; |
| 258 | bool in_module = true; |
| 259 | bool in_option = false; |
| 260 | bool in_value = false; |
| 261 | bool in_quotes = false; |
| 262 | int start = 0; |
| 263 | |
| 264 | for (int i = 0; i < cmdline.size(); i++) { |
| 265 | if (cmdline[i] == '"') { |
| 266 | in_quotes = !in_quotes; |
| 267 | } |
| 268 | |
| 269 | if (in_quotes) continue; |
| 270 | |
| 271 | if (cmdline[i] == ' ') { |
| 272 | if (in_value) { |
| 273 | value = cmdline.substr(start, i - start); |
| 274 | if (!module_name.empty() && !option_name.empty()) { |
| 275 | AddOption(module_name, option_name, value); |
| 276 | } |
| 277 | } |
| 278 | module_name = ""; |
| 279 | option_name = ""; |
| 280 | value = ""; |
| 281 | in_value = false; |
| 282 | start = i + 1; |
| 283 | in_module = true; |
| 284 | continue; |
| 285 | } |
| 286 | |
| 287 | if (cmdline[i] == '.') { |
| 288 | if (in_module) { |
| 289 | module_name = cmdline.substr(start, i - start); |
| 290 | start = i + 1; |
| 291 | in_module = false; |
| 292 | } |
| 293 | in_option = true; |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | if (cmdline[i] == '=') { |
| 298 | if (in_option) { |
| 299 | option_name = cmdline.substr(start, i - start); |
| 300 | start = i + 1; |
| 301 | in_option = false; |
| 302 | } |
| 303 | in_value = true; |
| 304 | continue; |
| 305 | } |
| 306 | } |
| 307 | if (in_value && !in_quotes) { |
| 308 | value = cmdline.substr(start, cmdline.size() - start); |
| 309 | if (!module_name.empty() && !option_name.empty()) { |
| 310 | AddOption(module_name, option_name, value); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Steve Muckle | 4c59323 | 2020-04-03 17:47:10 -0700 | [diff] [blame] | 315 | Modprobe::Modprobe(const std::vector<std::string>& base_paths, const std::string load_file) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 316 | using namespace std::placeholders; |
| 317 | |
| 318 | for (const auto& base_path : base_paths) { |
| 319 | auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1); |
| 320 | ParseCfg(base_path + "/modules.alias", alias_callback); |
| 321 | |
| 322 | auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1); |
| 323 | ParseCfg(base_path + "/modules.dep", dep_callback); |
| 324 | |
| 325 | auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1); |
| 326 | ParseCfg(base_path + "/modules.softdep", softdep_callback); |
| 327 | |
| 328 | auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1); |
Steve Muckle | 4c59323 | 2020-04-03 17:47:10 -0700 | [diff] [blame] | 329 | ParseCfg(base_path + "/" + load_file, load_callback); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 330 | |
| 331 | auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1); |
| 332 | ParseCfg(base_path + "/modules.options", options_callback); |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 333 | |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 334 | auto blocklist_callback = std::bind(&Modprobe::ParseBlocklistCallback, this, _1); |
| 335 | ParseCfg(base_path + "/modules.blocklist", blocklist_callback); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 336 | } |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 337 | |
Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 338 | ParseKernelCmdlineOptions(); |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 341 | void Modprobe::EnableBlocklist(bool enable) { |
| 342 | blocklist_enabled = enable; |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 345 | std::vector<std::string> Modprobe::GetDependencies(const std::string& module) { |
| 346 | auto it = module_deps_.find(module); |
| 347 | if (it == module_deps_.end()) { |
| 348 | return {}; |
| 349 | } |
| 350 | return it->second; |
| 351 | } |
| 352 | |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 353 | bool Modprobe::InsmodWithDeps(const std::string& module_name, const std::string& parameters) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 354 | if (module_name.empty()) { |
| 355 | LOG(ERROR) << "Need valid module name, given: " << module_name; |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | auto dependencies = GetDependencies(module_name); |
| 360 | if (dependencies.empty()) { |
| 361 | LOG(ERROR) << "Module " << module_name << " not in dependency file"; |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | // load module dependencies in reverse order |
| 366 | for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) { |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 367 | LOG(VERBOSE) << "Loading hard dep for '" << module_name << "': " << *dep; |
Steve Muckle | 73b2928 | 2019-07-30 16:03:44 -0700 | [diff] [blame] | 368 | if (!LoadWithAliases(*dep, true)) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // try to load soft pre-dependencies |
| 374 | for (const auto& [module, softdep] : module_pre_softdep_) { |
| 375 | if (module_name == module) { |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 376 | LOG(VERBOSE) << "Loading soft pre-dep for '" << module << "': " << softdep; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 377 | LoadWithAliases(softdep, false); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // load target module itself with args |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 382 | if (!Insmod(dependencies[0], parameters)) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | |
| 386 | // try to load soft post-dependencies |
| 387 | for (const auto& [module, softdep] : module_post_softdep_) { |
| 388 | if (module_name == module) { |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 389 | LOG(VERBOSE) << "Loading soft post-dep for '" << module << "': " << softdep; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 390 | LoadWithAliases(softdep, false); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 397 | bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict, |
| 398 | const std::string& parameters) { |
Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 399 | auto canonical_name = MakeCanonical(module_name); |
| 400 | if (module_loaded_.count(canonical_name)) { |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | std::set<std::string> modules_to_load = {canonical_name}; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 405 | bool module_loaded = false; |
| 406 | |
| 407 | // use aliases to expand list of modules to load (multiple modules |
| 408 | // may alias themselves to the requested name) |
| 409 | for (const auto& [alias, aliased_module] : module_aliases_) { |
| 410 | if (fnmatch(alias.c_str(), module_name.c_str(), 0) != 0) continue; |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 411 | LOG(VERBOSE) << "Found alias for '" << module_name << "': '" << aliased_module; |
Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 412 | if (module_loaded_.count(MakeCanonical(aliased_module))) continue; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 413 | modules_to_load.emplace(aliased_module); |
| 414 | } |
| 415 | |
| 416 | // attempt to load all modules aliased to this name |
| 417 | for (const auto& module : modules_to_load) { |
| 418 | if (!ModuleExists(module)) continue; |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 419 | if (InsmodWithDeps(module, parameters)) module_loaded = true; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (strict && !module_loaded) { |
Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 423 | LOG(ERROR) << "LoadWithAliases was unable to load " << module_name; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 424 | return false; |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 429 | bool Modprobe::LoadListedModules(bool strict) { |
| 430 | auto ret = true; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 431 | for (const auto& module : module_load_) { |
| 432 | if (!LoadWithAliases(module, true)) { |
Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 433 | ret = false; |
| 434 | if (strict) break; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
Mark Salyzyn | d478271 | 2019-10-29 09:32:09 -0700 | [diff] [blame] | 437 | return ret; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 438 | } |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 439 | |
| 440 | bool Modprobe::Remove(const std::string& module_name) { |
| 441 | auto dependencies = GetDependencies(MakeCanonical(module_name)); |
| 442 | if (dependencies.empty()) { |
| 443 | LOG(ERROR) << "Empty dependencies for module " << module_name; |
| 444 | return false; |
| 445 | } |
| 446 | if (!Rmmod(dependencies[0])) { |
| 447 | return false; |
| 448 | } |
| 449 | for (auto dep = dependencies.begin() + 1; dep != dependencies.end(); ++dep) { |
| 450 | Rmmod(*dep); |
| 451 | } |
| 452 | return true; |
| 453 | } |
Steve Muckle | 012cfa1 | 2019-07-31 15:55:00 -0700 | [diff] [blame] | 454 | |
| 455 | std::vector<std::string> Modprobe::ListModules(const std::string& pattern) { |
| 456 | std::vector<std::string> rv; |
| 457 | for (const auto& [module, deps] : module_deps_) { |
| 458 | // Attempt to match both the canonical module name and the module filename. |
| 459 | if (!fnmatch(pattern.c_str(), module.c_str(), 0)) { |
| 460 | rv.emplace_back(module); |
| 461 | } else if (!fnmatch(pattern.c_str(), basename(deps[0].c_str()), 0)) { |
| 462 | rv.emplace_back(deps[0]); |
| 463 | } |
| 464 | } |
| 465 | return rv; |
| 466 | } |
Steve Muckle | 781aa78 | 2019-08-01 14:55:07 -0700 | [diff] [blame] | 467 | |
| 468 | bool Modprobe::GetAllDependencies(const std::string& module, |
| 469 | std::vector<std::string>* pre_dependencies, |
| 470 | std::vector<std::string>* dependencies, |
| 471 | std::vector<std::string>* post_dependencies) { |
| 472 | std::string canonical_name = MakeCanonical(module); |
| 473 | if (pre_dependencies) { |
| 474 | pre_dependencies->clear(); |
| 475 | for (const auto& [it_module, it_softdep] : module_pre_softdep_) { |
| 476 | if (canonical_name == it_module) { |
| 477 | pre_dependencies->emplace_back(it_softdep); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | if (dependencies) { |
| 482 | dependencies->clear(); |
| 483 | auto hard_deps = GetDependencies(canonical_name); |
| 484 | if (hard_deps.empty()) { |
| 485 | return false; |
| 486 | } |
| 487 | for (auto dep = hard_deps.rbegin(); dep != hard_deps.rend(); dep++) { |
| 488 | dependencies->emplace_back(*dep); |
| 489 | } |
| 490 | } |
| 491 | if (post_dependencies) { |
| 492 | for (const auto& [it_module, it_softdep] : module_post_softdep_) { |
| 493 | if (canonical_name == it_module) { |
| 494 | post_dependencies->emplace_back(it_softdep); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | return true; |
| 499 | } |