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