Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 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 | |
| 32 | bool Modprobe::Insmod(const std::string& path_name) { |
| 33 | 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 | } |
| 50 | modules_loaded.emplace_back(path_name + options); |
| 51 | return true; |
| 52 | } |
| 53 | |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame^] | 54 | bool Modprobe::Rmmod(const std::string& module_name) { |
| 55 | for (auto it = modules_loaded.begin(); it != modules_loaded.end(); it++) { |
| 56 | if (*it == module_name) { |
| 57 | modules_loaded.erase(it); |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 64 | bool Modprobe::ModuleExists(const std::string& module_name) { |
| 65 | auto deps = GetDependencies(module_name); |
| 66 | if (deps.empty()) { |
| 67 | // missing deps can happen in the case of an alias |
| 68 | return false; |
| 69 | } |
| 70 | return std::find(test_modules.begin(), test_modules.end(), deps.front()) != test_modules.end(); |
| 71 | } |