| Joe Hattori | 8def838 | 2025-07-03 00:41:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2025 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 <modprobe/utils.h> |
| 18 | |
| Joe Hattori | da9f36e | 2025-07-07 02:33:57 +0000 | [diff] [blame] | 19 | #include <sys/syscall.h> |
| 20 | |
| Joe Hattori | 8def838 | 2025-07-03 00:41:35 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/strings.h> |
| Joe Hattori | da9f36e | 2025-07-07 02:33:57 +0000 | [diff] [blame] | 23 | #include <android-base/unique_fd.h> |
| Joe Hattori | 8def838 | 2025-07-03 00:41:35 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace modprobe { |
| 27 | |
| Joe Hattori | a8827b1 | 2025-07-07 02:17:23 +0000 | [diff] [blame] | 28 | std::string CanonicalizeModulePath(const std::string& module_path) { |
| Joe Hattori | 8def838 | 2025-07-03 00:41:35 -0700 | [diff] [blame] | 29 | auto start = module_path.find_last_of('/'); |
| 30 | if (start == std::string::npos) { |
| 31 | start = 0; |
| 32 | } else { |
| 33 | start += 1; |
| 34 | } |
| 35 | auto end = module_path.size(); |
| 36 | if (android::base::EndsWith(module_path, ".ko")) { |
| 37 | end -= 3; |
| 38 | } |
| 39 | if ((end - start) <= 1) { |
| 40 | LOG(ERROR) << "malformed module name: " << module_path; |
| 41 | return ""; |
| 42 | } |
| 43 | std::string module_name = module_path.substr(start, end - start); |
| 44 | // module names can have '-', but their file names will have '_' |
| 45 | std::replace(module_name.begin(), module_name.end(), '-', '_'); |
| 46 | return module_name; |
| 47 | } |
| 48 | |
| Joe Hattori | da9f36e | 2025-07-07 02:33:57 +0000 | [diff] [blame] | 49 | base::Result<void> InitModule(const std::string& path_name, |
| 50 | const std::unordered_map<std::string, std::string>& module_options, |
| 51 | const std::string& parameters) { |
| 52 | std::string options = ""; |
| 53 | auto options_iter = module_options.find(CanonicalizeModulePath(path_name)); |
| 54 | if (options_iter != module_options.end()) { |
| 55 | options = options_iter->second; |
| 56 | } |
| 57 | if (!parameters.empty()) { |
| 58 | options += " " + parameters; |
| 59 | } |
| 60 | |
| 61 | LOG(INFO) << "Loading module " << path_name << " with args '" << options << "'"; |
| 62 | android::base::unique_fd fd( |
| 63 | TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); |
| 64 | if (fd == -1) { |
| 65 | PLOG(ERROR) << "Could not open module '" << path_name << "'"; |
| 66 | return android::base::ErrnoError(); |
| 67 | } |
| 68 | int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0); |
| 69 | if (ret != 0) { |
| Joe Hattori | cbebd72 | 2025-07-30 04:47:17 +0000 | [diff] [blame^] | 70 | if (errno != EEXIST) { |
| 71 | PLOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'"; |
| 72 | } |
| Joe Hattori | da9f36e | 2025-07-07 02:33:57 +0000 | [diff] [blame] | 73 | return android::base::ErrnoError(); |
| 74 | } |
| 75 | LOG(INFO) << "Loaded kernel module " << path_name; |
| 76 | return {}; |
| 77 | } |
| 78 | |
| Joe Hattori | 8def838 | 2025-07-03 00:41:35 -0700 | [diff] [blame] | 79 | } // namespace modprobe |
| 80 | } // namespace android |