blob: 99472c14d5dad44ab2db7a7db4e1702c686ef418 [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 Muckle18b981e2019-04-15 17:43:02 -070066 return true;
67}
68
Steve Mucklebb58b012019-07-30 11:58:11 -070069bool Modprobe::Rmmod(const std::string& module_name) {
Mark Salyzyn8c105192019-10-29 08:38:55 -070070 auto canonical_name = MakeCanonical(module_name);
71 int ret = syscall(__NR_delete_module, canonical_name.c_str(), O_NONBLOCK);
Steve Mucklebb58b012019-07-30 11:58:11 -070072 if (ret != 0) {
73 PLOG(ERROR) << "Failed to remove module '" << module_name << "'";
74 return false;
75 }
Mark Salyzyn8c105192019-10-29 08:38:55 -070076 module_loaded_.erase(canonical_name);
Steve Mucklebb58b012019-07-30 11:58:11 -070077 return true;
78}
79
Steve Muckle18b981e2019-04-15 17:43:02 -070080bool Modprobe::ModuleExists(const std::string& module_name) {
81 struct stat fileStat;
Steve Mucklee31f8402019-07-31 14:34:52 -070082 if (blacklist_enabled && module_blacklist_.count(module_name)) {
Steve Muckleded44c02019-08-01 16:03:02 -070083 LOG(INFO) << "module " << module_name << " is blacklisted";
Steve Mucklee31f8402019-07-31 14:34:52 -070084 return false;
85 }
Steve Muckle18b981e2019-04-15 17:43:02 -070086 auto deps = GetDependencies(module_name);
87 if (deps.empty()) {
88 // missing deps can happen in the case of an alias
89 return false;
90 }
91 if (stat(deps.front().c_str(), &fileStat)) {
Steve Muckleded44c02019-08-01 16:03:02 -070092 LOG(INFO) << "module " << module_name << " does not exist";
Steve Muckle18b981e2019-04-15 17:43:02 -070093 return false;
94 }
95 if (!S_ISREG(fileStat.st_mode)) {
Steve Muckleded44c02019-08-01 16:03:02 -070096 LOG(INFO) << "module " << module_name << " is not a regular file";
Steve Muckle18b981e2019-04-15 17:43:02 -070097 return false;
98 }
99 return true;
100}