blob: 7d817b1f2843b5017637fdd61011d5acc734ab3f [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 Muckle13700a62019-07-31 09:59:48 -070032bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) {
Steve Muckle18b981e2019-04-15 17:43:02 -070033 auto deps = GetDependencies(MakeCanonical(path_name));
34 if (deps.empty()) {
35 return false;
36 }
37 if (std::find(test_modules.begin(), test_modules.end(), deps.front()) == test_modules.end()) {
38 return false;
39 }
40 for (auto it = modules_loaded.begin(); it != modules_loaded.end(); ++it) {
41 if (android::base::StartsWith(*it, path_name)) {
42 return true;
43 }
44 }
45 std::string options;
46 auto options_iter = module_options_.find(MakeCanonical(path_name));
47 if (options_iter != module_options_.end()) {
48 options = " " + options_iter->second;
49 }
Steve Muckle13700a62019-07-31 09:59:48 -070050 if (!parameters.empty()) {
51 options = options + " " + parameters;
52 }
53
Steve Muckle18b981e2019-04-15 17:43:02 -070054 modules_loaded.emplace_back(path_name + options);
55 return true;
56}
57
Steve Mucklebb58b012019-07-30 11:58:11 -070058bool Modprobe::Rmmod(const std::string& module_name) {
59 for (auto it = modules_loaded.begin(); it != modules_loaded.end(); it++) {
60 if (*it == module_name) {
61 modules_loaded.erase(it);
62 return true;
63 }
64 }
65 return false;
66}
67
Steve Muckle18b981e2019-04-15 17:43:02 -070068bool Modprobe::ModuleExists(const std::string& module_name) {
69 auto deps = GetDependencies(module_name);
Steve Mucklee31f8402019-07-31 14:34:52 -070070 if (blacklist_enabled && module_blacklist_.count(module_name)) {
71 return false;
72 }
Steve Muckle18b981e2019-04-15 17:43:02 -070073 if (deps.empty()) {
74 // missing deps can happen in the case of an alias
75 return false;
76 }
77 return std::find(test_modules.begin(), test_modules.end(), deps.front()) != test_modules.end();
78}