• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 <sys/stat.h>
18 #include <sys/syscall.h>
19 
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/logging.h>
24 #include <android-base/strings.h>
25 #include <android-base/unique_fd.h>
26 #include <gtest/gtest.h>
27 
28 #include <modprobe/modprobe.h>
29 
30 #include "libmodprobe_test.h"
31 
GetKernelCmdline(void)32 std::string Modprobe::GetKernelCmdline(void) {
33     return kernel_cmdline;
34 }
35 
Insmod(const std::string & path_name,const std::string & parameters)36 bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) {
37     auto deps = GetDependencies(MakeCanonical(path_name));
38     if (deps.empty()) {
39         return false;
40     }
41     if (std::find(test_modules.begin(), test_modules.end(), deps.front()) == test_modules.end()) {
42         return false;
43     }
44     for (auto it = modules_loaded.begin(); it != modules_loaded.end(); ++it) {
45         if (android::base::StartsWith(*it, path_name)) {
46             return true;
47         }
48     }
49     std::string options;
50     auto options_iter = module_options_.find(MakeCanonical(path_name));
51     if (options_iter != module_options_.end()) {
52         options = " " + options_iter->second;
53     }
54     if (!parameters.empty()) {
55         options = options + " " + parameters;
56     }
57 
58     modules_loaded.emplace_back(path_name + options);
59     module_count_++;
60     return true;
61 }
62 
Rmmod(const std::string & module_name)63 bool Modprobe::Rmmod(const std::string& module_name) {
64     for (auto it = modules_loaded.begin(); it != modules_loaded.end(); it++) {
65         if (*it == module_name || android::base::StartsWith(*it, module_name + " ")) {
66             modules_loaded.erase(it);
67             return true;
68         }
69     }
70     return false;
71 }
72 
ModuleExists(const std::string & module_name)73 bool Modprobe::ModuleExists(const std::string& module_name) {
74     auto deps = GetDependencies(module_name);
75     if (blocklist_enabled && module_blocklist_.count(module_name)) {
76         return false;
77     }
78     if (deps.empty()) {
79         // missing deps can happen in the case of an alias
80         return false;
81     }
82     return std::find(test_modules.begin(), test_modules.end(), deps.front()) != test_modules.end();
83 }
84