blob: 658970874cce1106fcce2c94020a4a88a7d9dded [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) {
38 LOG(ERROR) << "Could not open module '" << path_name << "'";
39 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
52 LOG(INFO) << "Loading module " << path_name << " with args \"" << options << "\"";
53 int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0);
54 if (ret != 0) {
55 if (errno == EEXIST) {
56 // Module already loaded
Mark Salyzyn8c105192019-10-29 08:38:55 -070057 module_loaded_.emplace(canonical_name);
Steve Muckle18b981e2019-04-15 17:43:02 -070058 return true;
59 }
60 LOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'";
61 return false;
62 }
63
64 LOG(INFO) << "Loaded kernel module " << path_name;
Mark Salyzyn8c105192019-10-29 08:38:55 -070065 module_loaded_.emplace(canonical_name);
Steve Muckleb0c48812020-05-29 16:30:33 -070066 module_count_++;
Steve Muckle18b981e2019-04-15 17:43:02 -070067 return true;
68}
69
Steve Mucklebb58b012019-07-30 11:58:11 -070070bool Modprobe::Rmmod(const std::string& module_name) {
Mark Salyzyn8c105192019-10-29 08:38:55 -070071 auto canonical_name = MakeCanonical(module_name);
72 int ret = syscall(__NR_delete_module, canonical_name.c_str(), O_NONBLOCK);
Steve Mucklebb58b012019-07-30 11:58:11 -070073 if (ret != 0) {
74 PLOG(ERROR) << "Failed to remove module '" << module_name << "'";
75 return false;
76 }
Mark Salyzyn8c105192019-10-29 08:38:55 -070077 module_loaded_.erase(canonical_name);
Steve Mucklebb58b012019-07-30 11:58:11 -070078 return true;
79}
80
Steve Muckle18b981e2019-04-15 17:43:02 -070081bool Modprobe::ModuleExists(const std::string& module_name) {
82 struct stat fileStat;
Steve Mucklee31f8402019-07-31 14:34:52 -070083 if (blacklist_enabled && module_blacklist_.count(module_name)) {
Steve Muckleded44c02019-08-01 16:03:02 -070084 LOG(INFO) << "module " << module_name << " is blacklisted";
Steve Mucklee31f8402019-07-31 14:34:52 -070085 return false;
86 }
Steve Muckle18b981e2019-04-15 17:43:02 -070087 auto deps = GetDependencies(module_name);
88 if (deps.empty()) {
89 // missing deps can happen in the case of an alias
90 return false;
91 }
92 if (stat(deps.front().c_str(), &fileStat)) {
Steve Muckleded44c02019-08-01 16:03:02 -070093 LOG(INFO) << "module " << module_name << " does not exist";
Steve Muckle18b981e2019-04-15 17:43:02 -070094 return false;
95 }
96 if (!S_ISREG(fileStat.st_mode)) {
Steve Muckleded44c02019-08-01 16:03:02 -070097 LOG(INFO) << "module " << module_name << " is not a regular file";
Steve Muckle18b981e2019-04-15 17:43:02 -070098 return false;
99 }
100 return true;
101}