| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 20 | #include <android-base/file.h> | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> | 
|  | 22 | #include <android-base/unique_fd.h> | 
|  | 23 |  | 
|  | 24 | #include <modprobe/modprobe.h> | 
|  | 25 |  | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 26 | std::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 Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 34 | bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) { | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 35 | 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 Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 42 | auto canonical_name = MakeCanonical(path_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 43 | std::string options = ""; | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 44 | auto options_iter = module_options_.find(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 45 | if (options_iter != module_options_.end()) { | 
|  | 46 | options = options_iter->second; | 
|  | 47 | } | 
| Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 48 | if (!parameters.empty()) { | 
|  | 49 | options = options + " " + parameters; | 
|  | 50 | } | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 51 |  | 
|  | 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 Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 57 | module_loaded_.emplace(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 58 | 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 Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 65 | module_loaded_.emplace(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 66 | return true; | 
|  | 67 | } | 
|  | 68 |  | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 69 | bool Modprobe::Rmmod(const std::string& module_name) { | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 70 | auto canonical_name = MakeCanonical(module_name); | 
|  | 71 | int ret = syscall(__NR_delete_module, canonical_name.c_str(), O_NONBLOCK); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 72 | if (ret != 0) { | 
|  | 73 | PLOG(ERROR) << "Failed to remove module '" << module_name << "'"; | 
|  | 74 | return false; | 
|  | 75 | } | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 76 | module_loaded_.erase(canonical_name); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 77 | return true; | 
|  | 78 | } | 
|  | 79 |  | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 80 | bool Modprobe::ModuleExists(const std::string& module_name) { | 
|  | 81 | struct stat fileStat; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 82 | if (blacklist_enabled && module_blacklist_.count(module_name)) { | 
| Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 83 | LOG(INFO) << "module " << module_name << " is blacklisted"; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 84 | return false; | 
|  | 85 | } | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 86 | 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 Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 92 | LOG(INFO) << "module " << module_name << " does not exist"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 93 | return false; | 
|  | 94 | } | 
|  | 95 | if (!S_ISREG(fileStat.st_mode)) { | 
| Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 96 | LOG(INFO) << "module " << module_name << " is not a regular file"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 97 | return false; | 
|  | 98 | } | 
|  | 99 | return true; | 
|  | 100 | } |