| 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 |  | 
|  | 25 | bool Modprobe::Insmod(const std::string& path_name) { | 
|  | 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 |  | 
|  | 33 | std::string options = ""; | 
|  | 34 | auto options_iter = module_options_.find(MakeCanonical(path_name)); | 
|  | 35 | if (options_iter != module_options_.end()) { | 
|  | 36 | options = options_iter->second; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | LOG(INFO) << "Loading module " << path_name << " with args \"" << options << "\""; | 
|  | 40 | int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0); | 
|  | 41 | if (ret != 0) { | 
|  | 42 | if (errno == EEXIST) { | 
|  | 43 | // Module already loaded | 
|  | 44 | return true; | 
|  | 45 | } | 
|  | 46 | LOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'"; | 
|  | 47 | return false; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | LOG(INFO) << "Loaded kernel module " << path_name; | 
|  | 51 | return true; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | bool Modprobe::ModuleExists(const std::string& module_name) { | 
|  | 55 | struct stat fileStat; | 
|  | 56 | auto deps = GetDependencies(module_name); | 
|  | 57 | if (deps.empty()) { | 
|  | 58 | // missing deps can happen in the case of an alias | 
|  | 59 | return false; | 
|  | 60 | } | 
|  | 61 | if (stat(deps.front().c_str(), &fileStat)) { | 
|  | 62 | return false; | 
|  | 63 | } | 
|  | 64 | if (!S_ISREG(fileStat.st_mode)) { | 
|  | 65 | return false; | 
|  | 66 | } | 
|  | 67 | return true; | 
|  | 68 | } |