blob: 4df458ffeef4e62a5f5d0d5fa3e6c27430d82713 [file] [log] [blame]
Steve Muckle18b981e2019-04-15 17:43:02 -07001/*
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
34std::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
55bool 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
90bool 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
111bool 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
147bool 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
160bool 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
197void Modprobe::ParseCfg(const std::string& cfg,
198 std::function<bool(const std::vector<std::string>&)> f) {
199 std::string cfg_contents;
200 if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) {
201 return;
202 }
203
204 std::vector<std::string> lines = android::base::Split(cfg_contents, "\n");
205 for (const std::string line : lines) {
206 if (line.empty() || line[0] == '#') {
207 continue;
208 }
209 const std::vector<std::string> args = android::base::Split(line, " ");
210 if (args.empty()) continue;
211 f(args);
212 }
213 return;
214}
215
216Modprobe::Modprobe(const std::vector<std::string>& base_paths) {
217 using namespace std::placeholders;
218
219 for (const auto& base_path : base_paths) {
220 auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1);
221 ParseCfg(base_path + "/modules.alias", alias_callback);
222
223 auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1);
224 ParseCfg(base_path + "/modules.dep", dep_callback);
225
226 auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1);
227 ParseCfg(base_path + "/modules.softdep", softdep_callback);
228
229 auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1);
230 ParseCfg(base_path + "/modules.load", load_callback);
231
232 auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1);
233 ParseCfg(base_path + "/modules.options", options_callback);
234 }
235}
236
237std::vector<std::string> Modprobe::GetDependencies(const std::string& module) {
238 auto it = module_deps_.find(module);
239 if (it == module_deps_.end()) {
240 return {};
241 }
242 return it->second;
243}
244
245bool Modprobe::InsmodWithDeps(const std::string& module_name) {
246 if (module_name.empty()) {
247 LOG(ERROR) << "Need valid module name, given: " << module_name;
248 return false;
249 }
250
251 auto dependencies = GetDependencies(module_name);
252 if (dependencies.empty()) {
253 LOG(ERROR) << "Module " << module_name << " not in dependency file";
254 return false;
255 }
256
257 // load module dependencies in reverse order
258 for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
Steve Muckle73b29282019-07-30 16:03:44 -0700259 if (!LoadWithAliases(*dep, true)) {
Steve Muckle18b981e2019-04-15 17:43:02 -0700260 return false;
261 }
262 }
263
264 // try to load soft pre-dependencies
265 for (const auto& [module, softdep] : module_pre_softdep_) {
266 if (module_name == module) {
267 LoadWithAliases(softdep, false);
268 }
269 }
270
271 // load target module itself with args
272 if (!Insmod(dependencies[0])) {
273 return false;
274 }
275
276 // try to load soft post-dependencies
277 for (const auto& [module, softdep] : module_post_softdep_) {
278 if (module_name == module) {
279 LoadWithAliases(softdep, false);
280 }
281 }
282
283 return true;
284}
285
286bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict) {
Steve Muckle73b29282019-07-30 16:03:44 -0700287 std::set<std::string> modules_to_load = {MakeCanonical(module_name)};
Steve Muckle18b981e2019-04-15 17:43:02 -0700288 bool module_loaded = false;
289
290 // use aliases to expand list of modules to load (multiple modules
291 // may alias themselves to the requested name)
292 for (const auto& [alias, aliased_module] : module_aliases_) {
293 if (fnmatch(alias.c_str(), module_name.c_str(), 0) != 0) continue;
294 modules_to_load.emplace(aliased_module);
295 }
296
297 // attempt to load all modules aliased to this name
298 for (const auto& module : modules_to_load) {
299 if (!ModuleExists(module)) continue;
300 if (InsmodWithDeps(module)) module_loaded = true;
301 }
302
303 if (strict && !module_loaded) {
304 LOG(ERROR) << "LoadWithAliases did not find a module for " << module_name;
305 return false;
306 }
307 return true;
308}
309
310bool Modprobe::LoadListedModules() {
311 for (const auto& module : module_load_) {
312 if (!LoadWithAliases(module, true)) {
313 return false;
314 }
315 }
316 return true;
317}
Steve Mucklebb58b012019-07-30 11:58:11 -0700318
319bool Modprobe::Remove(const std::string& module_name) {
320 auto dependencies = GetDependencies(MakeCanonical(module_name));
321 if (dependencies.empty()) {
322 LOG(ERROR) << "Empty dependencies for module " << module_name;
323 return false;
324 }
325 if (!Rmmod(dependencies[0])) {
326 return false;
327 }
328 for (auto dep = dependencies.begin() + 1; dep != dependencies.end(); ++dep) {
329 Rmmod(*dep);
330 }
331 return true;
332}