blob: be0ff2e140915ac92407b1da6e73a305a3acd26a [file] [log] [blame]
Andreas Gampe01ad5982016-03-09 16:27:29 -08001/*
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 Gampeaef445d2016-06-02 17:56:45 -070025#include <installd_constants.h>
26
Andreas Gampe01ad5982016-03-09 16:27:29 -080027#ifndef LOG_TAG
28#define LOG_TAG "otapreopt"
29#endif
30
31using android::base::StringPrintf;
32
33namespace android {
34namespace installd {
35
36static 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 Gampeaef445d2016-06-02 17:56:45 -070083 const char* argv[1 + DEXOPT_PARAM_COUNT + 1];
84 CHECK_EQ(static_cast<size_t>(argc), DEXOPT_PARAM_COUNT + 1);
Andreas Gampe01ad5982016-03-09 16:27:29 -080085 argv[0] = "/system/bin/otapreopt";
Andreas Gampeaef445d2016-06-02 17:56:45 -070086 for (size_t i = 1; i <= DEXOPT_PARAM_COUNT; ++i) {
Andreas Gampe01ad5982016-03-09 16:27:29 -080087 argv[i] = arg[i];
88 }
Andreas Gampeaef445d2016-06-02 17:56:45 -070089 argv[DEXOPT_PARAM_COUNT + 1] = nullptr;
Andreas Gampe01ad5982016-03-09 16:27:29 -080090
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
99int main(const int argc, char *argv[]) {
100 return android::installd::otapreopt_chroot(argc, argv);
101}