blob: 8cbd08c5a832012cf441e01b6fbe5932fa3368d5 [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
20#include <android-base/logging.h>
21#include <android-base/unique_fd.h>
22
23#include <modprobe/modprobe.h>
24
25bool 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
Steve Mucklebb58b012019-07-30 11:58:11 -070054bool Modprobe::Rmmod(const std::string& module_name) {
55 int ret = syscall(__NR_delete_module, MakeCanonical(module_name).c_str(), O_NONBLOCK);
56 if (ret != 0) {
57 PLOG(ERROR) << "Failed to remove module '" << module_name << "'";
58 return false;
59 }
60 return true;
61}
62
Steve Muckle18b981e2019-04-15 17:43:02 -070063bool Modprobe::ModuleExists(const std::string& module_name) {
64 struct stat fileStat;
65 auto deps = GetDependencies(module_name);
66 if (deps.empty()) {
67 // missing deps can happen in the case of an alias
68 return false;
69 }
70 if (stat(deps.front().c_str(), &fileStat)) {
71 return false;
72 }
73 if (!S_ISREG(fileStat.st_mode)) {
74 return false;
75 }
76 return true;
77}