otapreopt_chroot.cpp revision c093d3a68143f73b0ad89678874086e2b84a5675
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
31#ifndef LOG_TAG
32#define LOG_TAG "otapreopt"
33#endif
34
35using android::base::StringPrintf;
36
37namespace android {
38namespace installd {
39
40static 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
48static 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.
61static int otapreopt_chroot(const int argc, char **arg) {
62    // Close all file descriptors. They are coming from the caller, we do not want to pass them
63    // on across our fork/exec into a different domain.
64    // 1) Default descriptors.
65    CloseDescriptor(STDIN_FILENO);
66    CloseDescriptor(STDOUT_FILENO);
67    CloseDescriptor(STDERR_FILENO);
68    // 2) The status channel.
69    CloseDescriptor(arg[1]);
70
71    // We need to run the otapreopt tool from the postinstall partition. As such, set up a
72    // mount namespace and change root.
73
74    // Create our own mount namespace.
75    if (unshare(CLONE_NEWNS) != 0) {
76        PLOG(ERROR) << "Failed to unshare() for otapreopt.";
77        exit(200);
78    }
79
80    // Make postinstall private, so that our changes don't propagate.
81    if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
82        PLOG(ERROR) << "Failed to mount private.";
83        exit(201);
84    }
85
86    // Bind mount necessary directories.
87    constexpr const char* kBindMounts[] = {
88            "/data", "/dev", "/proc", "/sys"
89    };
90    for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
91        std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
92        if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
93            PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
94            exit(202);
95        }
96    }
97
98    // Try to mount the vendor partition. update_engine doesn't do this for us, but we
99    // want it for vendor APKs.
100    // Notes:
101    //  1) We pretty much guess a name here and hope to find the partition by name.
102    //     It is just as complicated and brittle to scan /proc/mounts. But this requires
103    //     validating the target-slot so as not to try to mount some totally random path.
104    //  2) We're in a mount namespace here, so when we die, this will be cleaned up.
105    //  3) Ignore errors. Printing anything at this stage will open a file descriptor
106    //     for logging.
107    if (!ValidateTargetSlotSuffix(arg[2])) {
108        LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
109        exit(207);
110    }
111    std::string vendor_partition = StringPrintf("/dev/block/bootdevice/by-name/vendor%s",
112                                                arg[2]);
113    int vendor_result = mount(vendor_partition.c_str(),
114                              "/postinstall/vendor",
115                              "ext4",
116                              MS_RDONLY,
117                              /* data */ nullptr);
118    UNUSED(vendor_result);
119
120    // Chdir into /postinstall.
121    if (chdir("/postinstall") != 0) {
122        PLOG(ERROR) << "Unable to chdir into /postinstall.";
123        exit(203);
124    }
125
126    // Make /postinstall the root in our mount namespace.
127    if (chroot(".")  != 0) {
128        PLOG(ERROR) << "Failed to chroot";
129        exit(204);
130    }
131
132    if (chdir("/") != 0) {
133        PLOG(ERROR) << "Unable to chdir into /.";
134        exit(205);
135    }
136
137    // Now go on and run otapreopt.
138
139    // Incoming:  cmd + status-fd + target-slot + cmd... + null      | Incoming | = argc + 1
140    // Outgoing:  cmd             + target-slot + cmd... + null      | Outgoing | = argc
141    const char** argv = new const char*[argc];
142
143    argv[0] = "/system/bin/otapreopt";
144
145    // The first parameter is the status file descriptor, skip.
146    for (size_t i = 2; i <= static_cast<size_t>(argc); ++i) {
147        argv[i - 1] = arg[i];
148    }
149
150    execv(argv[0], static_cast<char * const *>(const_cast<char**>(argv)));
151    PLOG(ERROR) << "execv(OTAPREOPT) failed.";
152    exit(99);
153}
154
155}  // namespace installd
156}  // namespace android
157
158int main(const int argc, char *argv[]) {
159    return android::installd::otapreopt_chroot(argc, argv);
160}
161