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