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 
29     bool LoadListedModules(bool strict = true);
30     bool LoadWithAliases(const std::string& module_name, bool strict,
31                          const std::string& parameters = "");
32     bool Remove(const std::string& module_name);
33     std::vector<std::string> ListModules(const std::string& pattern);
34     bool GetAllDependencies(const std::string& module, std::vector<std::string>* pre_dependencies,
35                             std::vector<std::string>* dependencies,
36                             std::vector<std::string>* post_dependencies);
ResetModuleCount()37     void ResetModuleCount() { module_count_ = 0; }
GetModuleCount()38     int GetModuleCount() { return module_count_; }
39     void EnableBlocklist(bool enable);
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 
52     bool ParseDepCallback(const std::string& base_path, const std::vector<std::string>& args);
53     bool ParseAliasCallback(const std::vector<std::string>& args);
54     bool ParseSoftdepCallback(const std::vector<std::string>& args);
55     bool ParseLoadCallback(const std::vector<std::string>& args);
56     bool ParseOptionsCallback(const std::vector<std::string>& args);
57     bool ParseBlocklistCallback(const std::vector<std::string>& args);
58     void ParseKernelCmdlineOptions();
59     void ParseCfg(const std::string& cfg, std::function<bool(const std::vector<std::string>&)> f);
60 
61     std::vector<std::pair<std::string, std::string>> module_aliases_;
62     std::unordered_map<std::string, std::vector<std::string>> module_deps_;
63     std::vector<std::pair<std::string, std::string>> module_pre_softdep_;
64     std::vector<std::pair<std::string, std::string>> module_post_softdep_;
65     std::vector<std::string> module_load_;
66     std::unordered_map<std::string, std::string> module_options_;
67     std::set<std::string> module_blocklist_;
68     std::unordered_set<std::string> module_loaded_;
69     int module_count_ = 0;
70     bool blocklist_enabled = false;
71 };
72