blob: e79bfaf8434082c307b8c8200d3c7c352d2def50 [file] [log] [blame]
Steve Muckle18b981e2019-04-15 17:43:02 -07001/*
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
Steve Muckle373a3ca2019-12-06 17:08:09 -080032std::string Modprobe::GetKernelCmdline(void) {
33 return kernel_cmdline;
34}
35
Steve Muckle13700a62019-07-31 09:59:48 -070036bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) {
Steve Muckle18b981e2019-04-15 17:43:02 -070037 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 }
Steve Muckle13700a62019-07-31 09:59:48 -070054 if (!parameters.empty()) {
55 options = options + " " + parameters;
56 }
57
Steve Muckle18b981e2019-04-15 17:43:02 -070058 modules_loaded.emplace_back(path_name + options);
Steve Muckleb0c48812020-05-29 16:30:33 -070059 module_count_++;
Steve Muckle18b981e2019-04-15 17:43:02 -070060 return true;
61}
62
Steve Mucklebb58b012019-07-30 11:58:11 -070063bool Modprobe::Rmmod(const std::string& module_name) {
64 for (auto it = modules_loaded.begin(); it != modules_loaded.end(); it++) {
Steve Muckle373a3ca2019-12-06 17:08:09 -080065 if (*it == module_name || android::base::StartsWith(*it, module_name + " ")) {
Steve Mucklebb58b012019-07-30 11:58:11 -070066 modules_loaded.erase(it);
67 return true;
68 }
69 }
70 return false;
71}
72
Steve Muckle18b981e2019-04-15 17:43:02 -070073bool Modprobe::ModuleExists(const std::string& module_name) {
74 auto deps = GetDependencies(module_name);
Mark Salyzyn703fb742020-06-15 11:51:59 -070075 if (blocklist_enabled && module_blocklist_.count(module_name)) {
Steve Mucklee31f8402019-07-31 14:34:52 -070076 return false;
77 }
Steve Muckle18b981e2019-04-15 17:43:02 -070078 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}