1 /* 2 * Copyright (C) 2020 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 <dirent.h> 18 #include <sys/mount.h> 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 #include <sys/utsname.h> 22 #include <sys/wait.h> 23 #include <unistd.h> 24 25 #include <cstdlib> 26 #include <filesystem> 27 #include <fstream> 28 #include <iostream> 29 #include <memory> 30 #include <string> 31 #include <vector> 32 33 #include <modprobe/modprobe.h> 34 35 #include "android-base/logging.h" 36 #include "android-base/strings.h" 37 38 using namespace android::base; 39 40 static constexpr const char MODULE_BASE_DIR[] = "/lib/modules"; 41 42 bool LoadKernelModules() { 43 struct utsname uts; 44 if (uname(&uts)) { 45 LOG(ERROR) << "Failed to get kernel version."; 46 return false; 47 } 48 int major, minor; 49 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { 50 LOG(ERROR) << "Failed to parse kernel version " << uts.release; 51 return false; 52 } 53 54 std::unique_ptr<DIR, decltype(&closedir)> base_dir(opendir(MODULE_BASE_DIR), closedir); 55 if (!base_dir) { 56 LOG(ERROR) << "Unable to open /lib/modules, skipping module loading."; 57 return false; 58 } 59 dirent* entry; 60 std::vector<std::string> module_dirs; 61 while ((entry = readdir(base_dir.get()))) { 62 if (entry->d_type != DT_DIR) { 63 continue; 64 } 65 int dir_major, dir_minor; 66 if (sscanf(entry->d_name, "%d.%d", &dir_major, &dir_minor) != 2 || dir_major != major || 67 dir_minor != minor) { 68 continue; 69 } 70 module_dirs.emplace_back(entry->d_name); 71 } 72 73 // Sort the directories so they are iterated over during module loading 74 // in a consistent order. Alphabetical sorting is fine here because the 75 // kernel version at the beginning of the directory name must match the 76 // current kernel version, so the sort only applies to a label that 77 // follows the kernel version, for example /lib/modules/5.4 vs. 78 // /lib/modules/5.4-gki. 79 std::sort(module_dirs.begin(), module_dirs.end()); 80 81 for (const auto& module_dir : module_dirs) { 82 std::string dir_path(MODULE_BASE_DIR); 83 dir_path.append("/"); 84 dir_path.append(module_dir); 85 Modprobe m({dir_path}); 86 bool retval = m.LoadListedModules(); 87 int modules_loaded = m.GetModuleCount(); 88 if (modules_loaded > 0) { 89 return retval; 90 } 91 } 92 93 Modprobe m({MODULE_BASE_DIR}); 94 bool retval = m.LoadListedModules(); 95 int modules_loaded = m.GetModuleCount(); 96 if (modules_loaded > 0) { 97 return retval; 98 } 99 100 return true; 101 } 102 103 int main(int argc, const char* argv[]) { 104 SetLogger(StderrLogger); 105 106 LOG(INFO) << "Guest VM init process"; 107 LOG(INFO) << "Command line: " << Join(std::vector(argv, argv + argc), " "); 108 109 if (clearenv() != EXIT_SUCCESS) { 110 PLOG(ERROR) << "clearenv"; 111 return EXIT_FAILURE; 112 } 113 114 LOG(INFO) << "Loading kernel modules..."; 115 if (!LoadKernelModules()) { 116 LOG(ERROR) << "LoadKernelModules failed"; 117 return EXIT_FAILURE; 118 } 119 120 LOG(INFO) << "Executing test binary " << argv[1] << "..."; 121 execv(argv[1], (char**)(argv + 1)); 122 123 PLOG(ERROR) << "execv"; 124 return EXIT_FAILURE; 125 } 126