blob: f786862cd1481e5845d7dd7ce7a8b5010798a63c [file] [log] [blame]
Joe Hattori8def8382025-07-03 00:41:35 -07001/*
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 Hattorida9f36e2025-07-07 02:33:57 +000019#include <sys/syscall.h>
20
Joe Hattori8def8382025-07-03 00:41:35 -070021#include <android-base/logging.h>
22#include <android-base/strings.h>
Joe Hattorida9f36e2025-07-07 02:33:57 +000023#include <android-base/unique_fd.h>
Joe Hattori8def8382025-07-03 00:41:35 -070024
25namespace android {
26namespace modprobe {
27
Joe Hattoria8827b12025-07-07 02:17:23 +000028std::string CanonicalizeModulePath(const std::string& module_path) {
Joe Hattori8def8382025-07-03 00:41:35 -070029 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 Hattorida9f36e2025-07-07 02:33:57 +000049base::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) {
70 PLOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'";
71 return android::base::ErrnoError();
72 }
73 LOG(INFO) << "Loaded kernel module " << path_name;
74 return {};
75}
76
Joe Hattori8def8382025-07-03 00:41:35 -070077} // namespace modprobe
78} // namespace android