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 | |
Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 32 | std::string Modprobe::GetKernelCmdline(void) { |
| 33 | return kernel_cmdline; |
| 34 | } |
| 35 | |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 36 | bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) { |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 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 | } |
Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 54 | if (!parameters.empty()) { |
| 55 | options = options + " " + parameters; |
| 56 | } |
| 57 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 58 | modules_loaded.emplace_back(path_name + options); |
Steve Muckle | b0c4881 | 2020-05-29 16:30:33 -0700 | [diff] [blame] | 59 | module_count_++; |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 63 | bool Modprobe::Rmmod(const std::string& module_name) { |
| 64 | for (auto it = modules_loaded.begin(); it != modules_loaded.end(); it++) { |
Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 65 | if (*it == module_name || android::base::StartsWith(*it, module_name + " ")) { |
Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 66 | modules_loaded.erase(it); |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 73 | bool Modprobe::ModuleExists(const std::string& module_name) { |
| 74 | auto deps = GetDependencies(module_name); |
Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame^] | 75 | if (blocklist_enabled && module_blocklist_.count(module_name)) { |
Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 76 | return false; |
| 77 | } |
Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 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 | } |