blob: c402c3cb527734368307dfbf8cfce8ac6806d39f [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
Andreas Gamped089ca12016-06-27 14:25:30 -070017#include <fcntl.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080018#include <linux/unistd.h>
19#include <sys/mount.h>
20#include <sys/wait.h>
21
Andreas Gamped089ca12016-06-27 14:25:30 -070022#include <sstream>
23
Andreas Gampe01ad5982016-03-09 16:27:29 -080024#include <android-base/logging.h>
25#include <android-base/macros.h>
26#include <android-base/stringprintf.h>
27
Jeff Sharkey0274c972016-12-06 09:32:04 -070028#include "installd_constants.h"
29#include "otapreopt_utils.h"
Andreas Gampe548bdb92016-06-02 17:56:45 -070030
Andreas Gampe01ad5982016-03-09 16:27:29 -080031#ifndef LOG_TAG
32#define LOG_TAG "otapreopt"
33#endif
34
35using android::base::StringPrintf;
36
37namespace android {
38namespace installd {
39
Andreas Gamped089ca12016-06-27 14:25:30 -070040static 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.
Andreas Gampe01ad5982016-03-09 16:27:29 -080061static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -060062 // 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 Gamped089ca12016-06-27 14:25:30 -070068 // 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 Gampe01ad5982016-03-09 16:27:29 -080077 // 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 Gampefd12eda2016-07-12 09:47:17 -0700104 // 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 Gampe01ad5982016-03-09 16:27:29 -0800126 // 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 Gampec093d3a2017-04-14 11:15:17 -0700145 // 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 Gamped089ca12016-06-27 14:25:30 -0700149 argv[0] = "/system/bin/otapreopt";
150
151 // The first parameter is the status file descriptor, skip.
Andreas Gampec093d3a2017-04-14 11:15:17 -0700152 for (size_t i = 2; i <= static_cast<size_t>(argc); ++i) {
153 argv[i - 1] = arg[i];
Andreas Gamped089ca12016-06-27 14:25:30 -0700154 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800155
Andreas Gampec093d3a2017-04-14 11:15:17 -0700156 execv(argv[0], static_cast<char * const *>(const_cast<char**>(argv)));
Andreas Gampe01ad5982016-03-09 16:27:29 -0800157 PLOG(ERROR) << "execv(OTAPREOPT) failed.";
158 exit(99);
159}
160
161} // namespace installd
162} // namespace android
163
164int main(const int argc, char *argv[]) {
165 return android::installd::otapreopt_chroot(argc, argv);
166}