otapreopt_chroot.cpp revision 6c2c056193010cf93b4264810d462c120ce801c8
1/* 2 ** Copyright 2016, 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 <fcntl.h> 18#include <linux/unistd.h> 19#include <sys/mount.h> 20#include <sys/wait.h> 21 22#include <sstream> 23 24#include <android-base/logging.h> 25#include <android-base/macros.h> 26#include <android-base/stringprintf.h> 27 28#include "installd_constants.h" 29#include "otapreopt_utils.h" 30#include "dexopt.h" 31 32#ifndef LOG_TAG 33#define LOG_TAG "otapreopt" 34#endif 35 36using android::base::StringPrintf; 37 38namespace android { 39namespace installd { 40 41static void CloseDescriptor(int fd) { 42 if (fd >= 0) { 43 int result = close(fd); 44 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor 45 // that we do *not* want. 46 } 47} 48 49static void CloseDescriptor(const char* descriptor_string) { 50 int fd = -1; 51 std::istringstream stream(descriptor_string); 52 stream >> fd; 53 if (!stream.fail()) { 54 CloseDescriptor(fd); 55 } 56} 57 58// Entry for otapreopt_chroot. Expected parameters are: 59// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params] 60// The file descriptor denoted by status-fd will be closed. The rest of the parameters will 61// be passed on to otapreopt in the chroot. 62static int otapreopt_chroot(const int argc, char **arg) { 63 // Close all file descriptors. They are coming from the caller, we do not want to pass them 64 // on across our fork/exec into a different domain. 65 // 1) Default descriptors. 66 CloseDescriptor(STDIN_FILENO); 67 CloseDescriptor(STDOUT_FILENO); 68 CloseDescriptor(STDERR_FILENO); 69 // 2) The status channel. 70 CloseDescriptor(arg[1]); 71 72 // We need to run the otapreopt tool from the postinstall partition. As such, set up a 73 // mount namespace and change root. 74 75 // Create our own mount namespace. 76 if (unshare(CLONE_NEWNS) != 0) { 77 PLOG(ERROR) << "Failed to unshare() for otapreopt."; 78 exit(200); 79 } 80 81 // Make postinstall private, so that our changes don't propagate. 82 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { 83 PLOG(ERROR) << "Failed to mount private."; 84 exit(201); 85 } 86 87 // Bind mount necessary directories. 88 constexpr const char* kBindMounts[] = { 89 "/data", "/dev", "/proc", "/sys" 90 }; 91 for (size_t i = 0; i < arraysize(kBindMounts); ++i) { 92 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); 93 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { 94 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; 95 exit(202); 96 } 97 } 98 99 // Try to mount the vendor partition. update_engine doesn't do this for us, but we 100 // want it for vendor APKs. 101 // Notes: 102 // 1) We pretty much guess a name here and hope to find the partition by name. 103 // It is just as complicated and brittle to scan /proc/mounts. But this requires 104 // validating the target-slot so as not to try to mount some totally random path. 105 // 2) We're in a mount namespace here, so when we die, this will be cleaned up. 106 // 3) Ignore errors. Printing anything at this stage will open a file descriptor 107 // for logging. 108 if (!ValidateTargetSlotSuffix(arg[2])) { 109 LOG(ERROR) << "Target slot suffix not legal: " << arg[2]; 110 exit(207); 111 } 112 std::string vendor_partition = StringPrintf("/dev/block/bootdevice/by-name/vendor%s", 113 arg[2]); 114 int vendor_result = mount(vendor_partition.c_str(), 115 "/postinstall/vendor", 116 "ext4", 117 MS_RDONLY, 118 /* data */ nullptr); 119 UNUSED(vendor_result); 120 121 // Chdir into /postinstall. 122 if (chdir("/postinstall") != 0) { 123 PLOG(ERROR) << "Unable to chdir into /postinstall."; 124 exit(203); 125 } 126 127 // Make /postinstall the root in our mount namespace. 128 if (chroot(".") != 0) { 129 PLOG(ERROR) << "Failed to chroot"; 130 exit(204); 131 } 132 133 if (chdir("/") != 0) { 134 PLOG(ERROR) << "Unable to chdir into /."; 135 exit(205); 136 } 137 138 // Now go on and run otapreopt. 139 140 // Incoming: cmd + status-fd + target-slot + "dexopt" + dexopt-params + null 141 // Outgoing: cmd + target-slot + "dexopt" + dexopt-params + null 142 constexpr size_t kInArguments = 1 // Binary name. 143 + 1 // status file descriptor. 144 + 1 // target-slot. 145 + 1 // "dexopt." 146 + DEXOPT_PARAM_COUNT // dexopt parameters. 147 + 1; // null termination. 148 constexpr size_t kOutArguments = 1 // Binary name. 149 + 1 // target-slot. 150 + 1 // "dexopt." 151 + DEXOPT_PARAM_COUNT // dexopt parameters. 152 + 1; // null termination. 153 const char* argv[kOutArguments]; 154 if (static_cast<size_t>(argc) != kInArguments - 1 /* null termination */) { 155 LOG(ERROR) << "Unexpected argument size " 156 << argc 157 << " vs " 158 << (kInArguments - 1); 159 for (size_t i = 0; i < static_cast<size_t>(argc); ++i) { 160 if (arg[i] == nullptr) { 161 LOG(ERROR) << "(null)"; 162 } else { 163 LOG(ERROR) << "\"" << arg[i] << "\""; 164 } 165 } 166 exit(206); 167 } 168 argv[0] = "/system/bin/otapreopt"; 169 170 // The first parameter is the status file descriptor, skip. 171 172 for (size_t i = 1; i <= kOutArguments - 2 /* cmd + null */; ++i) { 173 argv[i] = arg[i + 1]; 174 } 175 argv[kOutArguments - 1] = nullptr; 176 177 execv(argv[0], (char * const *)argv); 178 PLOG(ERROR) << "execv(OTAPREOPT) failed."; 179 exit(99); 180} 181 182} // namespace installd 183} // namespace android 184 185int main(const int argc, char *argv[]) { 186 return android::installd::otapreopt_chroot(argc, argv); 187} 188