blob: 94a1dc4e97cde1f99fb64c24275e06d7971f5543 [file] [log] [blame]
Steve Muckle18b981e2019-04-15 17:43:02 -07001/*
2 * Copyright (C) 2018 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
Steve Muckle373a3ca2019-12-06 17:08:09 -080020#include <android-base/file.h>
Steve Muckle18b981e2019-04-15 17:43:02 -070021#include <android-base/logging.h>
22#include <android-base/unique_fd.h>
23
24#include <modprobe/modprobe.h>
25
Steve Muckle373a3ca2019-12-06 17:08:09 -080026std::string Modprobe::GetKernelCmdline(void) {
27 std::string cmdline;
28 if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) {
29 return "";
30 }
31 return cmdline;
32}
33
Steve Muckle13700a62019-07-31 09:59:48 -070034bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) {
Steve Muckle18b981e2019-04-15 17:43:02 -070035 android::base::unique_fd fd(
36 TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
37 if (fd == -1) {
Elliott Hughes1ab2c022020-06-16 11:48:28 -070038 PLOG(ERROR) << "Could not open module '" << path_name << "'";
Steve Muckle18b981e2019-04-15 17:43:02 -070039 return false;
40 }
41
Mark Salyzyn8c105192019-10-29 08:38:55 -070042 auto canonical_name = MakeCanonical(path_name);
Steve Muckle18b981e2019-04-15 17:43:02 -070043 std::string options = "";
Mark Salyzyn8c105192019-10-29 08:38:55 -070044 auto options_iter = module_options_.find(canonical_name);
Steve Muckle18b981e2019-04-15 17:43:02 -070045 if (options_iter != module_options_.end()) {
46 options = options_iter->second;
47 }
Steve Muckle13700a62019-07-31 09:59:48 -070048 if (!parameters.empty()) {
49 options = options + " " + parameters;
50 }
Steve Muckle18b981e2019-04-15 17:43:02 -070051
Elliott Hughes1ab2c022020-06-16 11:48:28 -070052 LOG(INFO) << "Loading module " << path_name << " with args '" << options << "'";
Steve Muckle18b981e2019-04-15 17:43:02 -070053 int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0);
54 if (ret != 0) {
55 if (errno == EEXIST) {
56 // Module already loaded
Chungkaic60300a2021-02-03 20:30:07 -080057 std::lock_guard guard(module_loaded_lock_);
58 module_loaded_paths_.emplace(path_name);
Mark Salyzyn8c105192019-10-29 08:38:55 -070059 module_loaded_.emplace(canonical_name);
Steve Muckle18b981e2019-04-15 17:43:02 -070060 return true;
61 }
Elliott Hughes1ab2c022020-06-16 11:48:28 -070062 PLOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'";
Steve Muckle18b981e2019-04-15 17:43:02 -070063 return false;
64 }
65
66 LOG(INFO) << "Loaded kernel module " << path_name;
Chungkaic60300a2021-02-03 20:30:07 -080067 std::lock_guard guard(module_loaded_lock_);
68 module_loaded_paths_.emplace(path_name);
Mark Salyzyn8c105192019-10-29 08:38:55 -070069 module_loaded_.emplace(canonical_name);
Steve Muckleb0c48812020-05-29 16:30:33 -070070 module_count_++;
Steve Muckle18b981e2019-04-15 17:43:02 -070071 return true;
72}
73
Steve Mucklebb58b012019-07-30 11:58:11 -070074bool Modprobe::Rmmod(const std::string& module_name) {
Mark Salyzyn8c105192019-10-29 08:38:55 -070075 auto canonical_name = MakeCanonical(module_name);
76 int ret = syscall(__NR_delete_module, canonical_name.c_str(), O_NONBLOCK);
Steve Mucklebb58b012019-07-30 11:58:11 -070077 if (ret != 0) {
78 PLOG(ERROR) << "Failed to remove module '" << module_name << "'";
79 return false;
80 }
Chungkaic60300a2021-02-03 20:30:07 -080081 std::lock_guard guard(module_loaded_lock_);
Mark Salyzyn8c105192019-10-29 08:38:55 -070082 module_loaded_.erase(canonical_name);
Steve Mucklebb58b012019-07-30 11:58:11 -070083 return true;
84}
85
Steve Muckle18b981e2019-04-15 17:43:02 -070086bool Modprobe::ModuleExists(const std::string& module_name) {
87 struct stat fileStat;
Mark Salyzyn703fb742020-06-15 11:51:59 -070088 if (blocklist_enabled && module_blocklist_.count(module_name)) {
89 LOG(INFO) << "module " << module_name << " is blocklisted";
Steve Mucklee31f8402019-07-31 14:34:52 -070090 return false;
91 }
Steve Muckle18b981e2019-04-15 17:43:02 -070092 auto deps = GetDependencies(module_name);
93 if (deps.empty()) {
94 // missing deps can happen in the case of an alias
95 return false;
96 }
97 if (stat(deps.front().c_str(), &fileStat)) {
Steve Muckleded44c02019-08-01 16:03:02 -070098 LOG(INFO) << "module " << module_name << " does not exist";
Steve Muckle18b981e2019-04-15 17:43:02 -070099 return false;
100 }
101 if (!S_ISREG(fileStat.st_mode)) {
Steve Muckleded44c02019-08-01 16:03:02 -0700102 LOG(INFO) << "module " << module_name << " is not a regular file";
Steve Muckle18b981e2019-04-15 17:43:02 -0700103 return false;
104 }
105 return true;
106}