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