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 | |
| 17 | #include <linux/unistd.h> |
| 18 | #include <sys/mount.h> |
| 19 | #include <sys/wait.h> |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/macros.h> |
| 23 | #include <android-base/stringprintf.h> |
| 24 | |
Andreas Gampe | aef445d | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 25 | #include <installd_constants.h> |
| 26 | |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 27 | #ifndef LOG_TAG |
| 28 | #define LOG_TAG "otapreopt" |
| 29 | #endif |
| 30 | |
| 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace installd { |
| 35 | |
| 36 | static int otapreopt_chroot(const int argc, char **arg) { |
| 37 | // We need to run the otapreopt tool from the postinstall partition. As such, set up a |
| 38 | // mount namespace and change root. |
| 39 | |
| 40 | // Create our own mount namespace. |
| 41 | if (unshare(CLONE_NEWNS) != 0) { |
| 42 | PLOG(ERROR) << "Failed to unshare() for otapreopt."; |
| 43 | exit(200); |
| 44 | } |
| 45 | |
| 46 | // Make postinstall private, so that our changes don't propagate. |
| 47 | if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) { |
| 48 | PLOG(ERROR) << "Failed to mount private."; |
| 49 | exit(201); |
| 50 | } |
| 51 | |
| 52 | // Bind mount necessary directories. |
| 53 | constexpr const char* kBindMounts[] = { |
| 54 | "/data", "/dev", "/proc", "/sys" |
| 55 | }; |
| 56 | for (size_t i = 0; i < arraysize(kBindMounts); ++i) { |
| 57 | std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]); |
| 58 | if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) { |
| 59 | PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i]; |
| 60 | exit(202); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Chdir into /postinstall. |
| 65 | if (chdir("/postinstall") != 0) { |
| 66 | PLOG(ERROR) << "Unable to chdir into /postinstall."; |
| 67 | exit(203); |
| 68 | } |
| 69 | |
| 70 | // Make /postinstall the root in our mount namespace. |
| 71 | if (chroot(".") != 0) { |
| 72 | PLOG(ERROR) << "Failed to chroot"; |
| 73 | exit(204); |
| 74 | } |
| 75 | |
| 76 | if (chdir("/") != 0) { |
| 77 | PLOG(ERROR) << "Unable to chdir into /."; |
| 78 | exit(205); |
| 79 | } |
| 80 | |
| 81 | // Now go on and run otapreopt. |
| 82 | |
Andreas Gampe | aef445d | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 83 | const char* argv[1 + DEXOPT_PARAM_COUNT + 1]; |
| 84 | CHECK_EQ(static_cast<size_t>(argc), DEXOPT_PARAM_COUNT + 1); |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 85 | argv[0] = "/system/bin/otapreopt"; |
Andreas Gampe | aef445d | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 86 | for (size_t i = 1; i <= DEXOPT_PARAM_COUNT; ++i) { |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 87 | argv[i] = arg[i]; |
| 88 | } |
Andreas Gampe | aef445d | 2016-06-02 17:56:45 -0700 | [diff] [blame] | 89 | argv[DEXOPT_PARAM_COUNT + 1] = nullptr; |
Andreas Gampe | 01ad598 | 2016-03-09 16:27:29 -0800 | [diff] [blame] | 90 | |
| 91 | execv(argv[0], (char * const *)argv); |
| 92 | PLOG(ERROR) << "execv(OTAPREOPT) failed."; |
| 93 | exit(99); |
| 94 | } |
| 95 | |
| 96 | } // namespace installd |
| 97 | } // namespace android |
| 98 | |
| 99 | int main(const int argc, char *argv[]) { |
| 100 | return android::installd::otapreopt_chroot(argc, argv); |
| 101 | } |