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 #pragma once 18 19 #include <mutex> 20 #include <set> 21 #include <string> 22 #include <unordered_map> 23 #include <unordered_set> 24 #include <vector> 25 26 #include <android-base/thread_annotations.h> 27 28 class Modprobe { 29 public: 30 Modprobe(const std::vector<std::string>&, const std::string load_file = "modules.load", 31 bool use_blocklist = true); 32 33 bool LoadModulesParallel(int num_threads); 34 bool LoadListedModules(bool strict = true); 35 bool LoadWithAliases(const std::string& module_name, bool strict, 36 const std::string& parameters = ""); 37 bool Remove(const std::string& module_name); 38 std::vector<std::string> ListModules(const std::string& pattern); 39 bool GetAllDependencies(const std::string& module, std::vector<std::string>* pre_dependencies, 40 std::vector<std::string>* dependencies, 41 std::vector<std::string>* post_dependencies); ResetModuleCount()42 void ResetModuleCount() { module_count_ = 0; } GetModuleCount()43 int GetModuleCount() { return module_count_; } 44 45 private: 46 std::string MakeCanonical(const std::string& module_path); 47 bool InsmodWithDeps(const std::string& module_name, const std::string& parameters); 48 bool Insmod(const std::string& path_name, const std::string& parameters); 49 bool Rmmod(const std::string& module_name); 50 std::vector<std::string> GetDependencies(const std::string& module); 51 bool ModuleExists(const std::string& module_name); 52 void AddOption(const std::string& module_name, const std::string& option_name, 53 const std::string& value); 54 std::string GetKernelCmdline(); 55 bool IsBlocklisted(const std::string& module_name); 56 57 bool ParseDepCallback(const std::string& base_path, const std::vector<std::string>& args); 58 bool ParseAliasCallback(const std::vector<std::string>& args); 59 bool ParseSoftdepCallback(const std::vector<std::string>& args); 60 bool ParseLoadCallback(const std::vector<std::string>& args); 61 bool ParseOptionsCallback(const std::vector<std::string>& args); 62 bool ParseBlocklistCallback(const std::vector<std::string>& args); 63 void ParseKernelCmdlineOptions(); 64 void ParseCfg(const std::string& cfg, std::function<bool(const std::vector<std::string>&)> f); 65 66 std::vector<std::pair<std::string, std::string>> module_aliases_; 67 std::unordered_map<std::string, std::vector<std::string>> module_deps_; 68 std::vector<std::pair<std::string, std::string>> module_pre_softdep_; 69 std::vector<std::pair<std::string, std::string>> module_post_softdep_; 70 std::vector<std::string> module_load_; 71 std::unordered_map<std::string, std::string> module_options_; 72 std::set<std::string> module_blocklist_; 73 std::mutex module_loaded_lock_; 74 std::unordered_set<std::string> module_loaded_; 75 std::unordered_set<std::string> module_loaded_paths_; 76 int module_count_ = 0; 77 bool blocklist_enabled = false; 78 }; 79