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 | 0354bd0 | 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 | 0354bd0 | 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 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame^] | 28 | #include <commands.h> |
Andreas Gampe | aef445d | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 29 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 30 | #ifndef LOG_TAG |
| 31 | #define LOG_TAG "otapreopt" |
| 32 | #endif |
| 33 | |
| 34 | using android::base::StringPrintf; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace installd { |
| 38 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame^] | 39 | static void CloseDescriptor(int fd) { |
| 40 | if (fd >= 0) { |
| 41 | int result = close(fd); |
| 42 | UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor |
| 43 | // that we do *not* want. |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static void CloseDescriptor(const char* descriptor_string) { |
| 48 | int fd = -1; |
| 49 | std::istringstream stream(descriptor_string); |
| 50 | stream >> fd; |
| 51 | if (!stream.fail()) { |
| 52 | CloseDescriptor(fd); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Entry for otapreopt_chroot. Expected parameters are: |
| 57 | // [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params] |
| 58 | // The file descriptor denoted by status-fd will be closed. The rest of the parameters will |
| 59 | // be passed on to otapreopt in the chroot. |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 60 | static int otapreopt_chroot(const int argc, char **arg) { |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame^] | 61 | // Close all file descriptors. They are coming from the caller, we do not want to pass them |
| 62 | // on across our fork/exec into a different domain. |
| 63 | // 1) Default descriptors. |
| 64 | CloseDescriptor(STDIN_FILENO); |
| 65 | CloseDescriptor(STDOUT_FILENO); |
| 66 | CloseDescriptor(STDERR_FILENO); |
| 67 | // 2) The status channel. |
| 68 | CloseDescriptor(arg[1]); |
| 69 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 70 | // We need to run the otapreopt tool from the postinstall partition. As such, set up a |
| 71 | // mount namespace and change root. |
| 72 | |
| 73 | // Create our own mount namespace. |
| 74 | if (unshare(CLONE_NEWNS) != 0) { |
| 75 | PLOG(ERROR) << "Failed to unshare() for otapreopt."; |
| 76 | exit(200); |
| 77 | } |
| 78 | |
| 79 | // Make postinstall private, so that our changes don't propagate. |
| 80 | if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { |
| 81 | PLOG(ERROR) << "Failed to mount private."; |
| 82 | exit(201); |
| 83 | } |
| 84 | |
| 85 | // Bind mount necessary directories. |
| 86 | constexpr const char* kBindMounts[] = { |
| 87 | "/data", "/dev", "/proc", "/sys" |
| 88 | }; |
| 89 | for (size_t i = 0; i < arraysize(kBindMounts); ++i) { |
| 90 | std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); |
| 91 | if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { |
| 92 | PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; |
| 93 | exit(202); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Chdir into /postinstall. |
| 98 | if (chdir("/postinstall") != 0) { |
| 99 | PLOG(ERROR) << "Unable to chdir into /postinstall."; |
| 100 | exit(203); |
| 101 | } |
| 102 | |
| 103 | // Make /postinstall the root in our mount namespace. |
| 104 | if (chroot(".") != 0) { |
| 105 | PLOG(ERROR) << "Failed to chroot"; |
| 106 | exit(204); |
| 107 | } |
| 108 | |
| 109 | if (chdir("/") != 0) { |
| 110 | PLOG(ERROR) << "Unable to chdir into /."; |
| 111 | exit(205); |
| 112 | } |
| 113 | |
| 114 | // Now go on and run otapreopt. |
| 115 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame^] | 116 | // Incoming: cmd + status-fd + target-slot + "dexopt" + dexopt-params + null |
| 117 | // Outgoing: cmd + target-slot + "dexopt" + dexopt-params + null |
| 118 | constexpr size_t kInArguments = 1 // Binary name. |
| 119 | + 1 // status file descriptor. |
| 120 | + 1 // target-slot. |
| 121 | + 1 // "dexopt." |
| 122 | + DEXOPT_PARAM_COUNT // dexopt parameters. |
| 123 | + 1; // null termination. |
| 124 | constexpr size_t kOutArguments = 1 // Binary name. |
| 125 | + 1 // target-slot. |
| 126 | + 1 // "dexopt." |
| 127 | + DEXOPT_PARAM_COUNT // dexopt parameters. |
| 128 | + 1; // null termination. |
| 129 | const char* argv[kOutArguments]; |
| 130 | if (static_cast<size_t>(argc) != kInArguments - 1 /* null termination */) { |
| 131 | LOG(ERROR) << "Unexpected argument size " |
| 132 | << argc |
| 133 | << " vs " |
| 134 | << (kInArguments - 1); |
| 135 | for (size_t i = 0; i < static_cast<size_t>(argc); ++i) { |
| 136 | if (arg[i] == nullptr) { |
| 137 | LOG(ERROR) << "(null)"; |
| 138 | } else { |
| 139 | LOG(ERROR) << "\"" << arg[i] << "\""; |
| 140 | } |
| 141 | } |
| 142 | exit(206); |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 143 | } |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame^] | 144 | argv[0] = "/system/bin/otapreopt"; |
| 145 | |
| 146 | // The first parameter is the status file descriptor, skip. |
| 147 | |
| 148 | for (size_t i = 1; i <= kOutArguments - 2 /* cmd + null */; ++i) { |
| 149 | argv[i] = arg[i + 1]; |
| 150 | } |
| 151 | argv[kOutArguments - 1] = nullptr; |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 152 | |
| 153 | execv(argv[0], (char * const *)argv); |
| 154 | PLOG(ERROR) << "execv(OTAPREOPT) failed."; |
| 155 | exit(99); |
| 156 | } |
| 157 | |
| 158 | } // namespace installd |
| 159 | } // namespace android |
| 160 | |
| 161 | int main(const int argc, char *argv[]) { |
| 162 | return android::installd::otapreopt_chroot(argc, argv); |
| 163 | } |