| 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 |  | 
 | 20 | #include <android-base/logging.h> | 
 | 21 | #include <android-base/unique_fd.h> | 
 | 22 |  | 
 | 23 | #include <modprobe/modprobe.h> | 
 | 24 |  | 
| Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 25 | bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) { | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 26 |     android::base::unique_fd fd( | 
 | 27 |             TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); | 
 | 28 |     if (fd == -1) { | 
 | 29 |         LOG(ERROR) << "Could not open module '" << path_name << "'"; | 
 | 30 |         return false; | 
 | 31 |     } | 
 | 32 |  | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 33 |     auto canonical_name = MakeCanonical(path_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 34 |     std::string options = ""; | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 35 |     auto options_iter = module_options_.find(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 36 |     if (options_iter != module_options_.end()) { | 
 | 37 |         options = options_iter->second; | 
 | 38 |     } | 
| Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 39 |     if (!parameters.empty()) { | 
 | 40 |         options = options + " " + parameters; | 
 | 41 |     } | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 42 |  | 
 | 43 |     LOG(INFO) << "Loading module " << path_name << " with args \"" << options << "\""; | 
 | 44 |     int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0); | 
 | 45 |     if (ret != 0) { | 
 | 46 |         if (errno == EEXIST) { | 
 | 47 |             // Module already loaded | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 48 |             module_loaded_.emplace(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 49 |             return true; | 
 | 50 |         } | 
 | 51 |         LOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'"; | 
 | 52 |         return false; | 
 | 53 |     } | 
 | 54 |  | 
 | 55 |     LOG(INFO) << "Loaded kernel module " << path_name; | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 56 |     module_loaded_.emplace(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 57 |     return true; | 
 | 58 | } | 
 | 59 |  | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 60 | bool Modprobe::Rmmod(const std::string& module_name) { | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 61 |     auto canonical_name = MakeCanonical(module_name); | 
 | 62 |     int ret = syscall(__NR_delete_module, canonical_name.c_str(), O_NONBLOCK); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 63 |     if (ret != 0) { | 
 | 64 |         PLOG(ERROR) << "Failed to remove module '" << module_name << "'"; | 
 | 65 |         return false; | 
 | 66 |     } | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 67 |     module_loaded_.erase(canonical_name); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 68 |     return true; | 
 | 69 | } | 
 | 70 |  | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 71 | bool Modprobe::ModuleExists(const std::string& module_name) { | 
 | 72 |     struct stat fileStat; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 73 |     if (blacklist_enabled && module_blacklist_.count(module_name)) { | 
| Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 74 |         LOG(INFO) << "module " << module_name << " is blacklisted"; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 75 |         return false; | 
 | 76 |     } | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 77 |     auto deps = GetDependencies(module_name); | 
 | 78 |     if (deps.empty()) { | 
 | 79 |         // missing deps can happen in the case of an alias | 
 | 80 |         return false; | 
 | 81 |     } | 
 | 82 |     if (stat(deps.front().c_str(), &fileStat)) { | 
| Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 83 |         LOG(INFO) << "module " << module_name << " does not exist"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 84 |         return false; | 
 | 85 |     } | 
 | 86 |     if (!S_ISREG(fileStat.st_mode)) { | 
| Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 87 |         LOG(INFO) << "module " << module_name << " is not a regular file"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 88 |         return false; | 
 | 89 |     } | 
 | 90 |     return true; | 
 | 91 | } |