blob: ff3ed7e90650496d767a7e7615c2499a265710b1 [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>
Roland Levillainc19c6042018-12-18 12:15:12 +000020#include <sys/stat.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080021#include <sys/wait.h>
22
Andreas Gamped089ca12016-06-27 14:25:30 -070023#include <sstream>
24
Andreas Gampe01ad5982016-03-09 16:27:29 -080025#include <android-base/logging.h>
26#include <android-base/macros.h>
27#include <android-base/stringprintf.h>
Roland Levillainc19c6042018-12-18 12:15:12 +000028#include <selinux/android.h>
29
30#include <apexd.h>
Andreas Gampe01ad5982016-03-09 16:27:29 -080031
Jeff Sharkey0274c972016-12-06 09:32:04 -070032#include "installd_constants.h"
33#include "otapreopt_utils.h"
Andreas Gampe548bdb92016-06-02 17:56:45 -070034
Andreas Gampe01ad5982016-03-09 16:27:29 -080035#ifndef LOG_TAG
36#define LOG_TAG "otapreopt"
37#endif
38
39using android::base::StringPrintf;
40
41namespace android {
42namespace installd {
43
Andreas Gamped089ca12016-06-27 14:25:30 -070044static void CloseDescriptor(int fd) {
45 if (fd >= 0) {
46 int result = close(fd);
47 UNUSED(result); // Ignore result. Printing to logcat will open a new descriptor
48 // that we do *not* want.
49 }
50}
51
52static void CloseDescriptor(const char* descriptor_string) {
53 int fd = -1;
54 std::istringstream stream(descriptor_string);
55 stream >> fd;
56 if (!stream.fail()) {
57 CloseDescriptor(fd);
58 }
59}
60
61// Entry for otapreopt_chroot. Expected parameters are:
62// [cmd] [status-fd] [target-slot] "dexopt" [dexopt-params]
63// The file descriptor denoted by status-fd will be closed. The rest of the parameters will
64// be passed on to otapreopt in the chroot.
Andreas Gampe01ad5982016-03-09 16:27:29 -080065static int otapreopt_chroot(const int argc, char **arg) {
Zach Riggle318853a2018-01-18 18:34:04 -060066 // Validate arguments
67 // We need the command, status channel and target slot, at a minimum.
68 if(argc < 3) {
69 PLOG(ERROR) << "Not enough arguments.";
70 exit(208);
71 }
Andreas Gamped089ca12016-06-27 14:25:30 -070072 // Close all file descriptors. They are coming from the caller, we do not want to pass them
73 // on across our fork/exec into a different domain.
74 // 1) Default descriptors.
75 CloseDescriptor(STDIN_FILENO);
76 CloseDescriptor(STDOUT_FILENO);
77 CloseDescriptor(STDERR_FILENO);
78 // 2) The status channel.
79 CloseDescriptor(arg[1]);
80
Andreas Gampe01ad5982016-03-09 16:27:29 -080081 // We need to run the otapreopt tool from the postinstall partition. As such, set up a
82 // mount namespace and change root.
83
84 // Create our own mount namespace.
85 if (unshare(CLONE_NEWNS) != 0) {
86 PLOG(ERROR) << "Failed to unshare() for otapreopt.";
87 exit(200);
88 }
89
90 // Make postinstall private, so that our changes don't propagate.
91 if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
92 PLOG(ERROR) << "Failed to mount private.";
93 exit(201);
94 }
95
96 // Bind mount necessary directories.
97 constexpr const char* kBindMounts[] = {
98 "/data", "/dev", "/proc", "/sys"
99 };
100 for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
101 std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
102 if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
103 PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
104 exit(202);
105 }
106 }
107
Andreas Gampefd12eda2016-07-12 09:47:17 -0700108 // Try to mount the vendor partition. update_engine doesn't do this for us, but we
109 // want it for vendor APKs.
110 // Notes:
111 // 1) We pretty much guess a name here and hope to find the partition by name.
112 // It is just as complicated and brittle to scan /proc/mounts. But this requires
113 // validating the target-slot so as not to try to mount some totally random path.
114 // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
115 // 3) Ignore errors. Printing anything at this stage will open a file descriptor
116 // for logging.
117 if (!ValidateTargetSlotSuffix(arg[2])) {
118 LOG(ERROR) << "Target slot suffix not legal: " << arg[2];
119 exit(207);
120 }
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700121 {
Bowgo Tsai19d2d082018-05-31 10:16:46 +0800122 std::string vendor_partition = StringPrintf("/dev/block/by-name/vendor%s",
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700123 arg[2]);
124 int vendor_result = mount(vendor_partition.c_str(),
125 "/postinstall/vendor",
126 "ext4",
127 MS_RDONLY,
128 /* data */ nullptr);
129 UNUSED(vendor_result);
130 }
131
132 // Try to mount the product partition. update_engine doesn't do this for us, but we
133 // want it for product APKs. Same notes as vendor above.
134 {
Bowgo Tsai19d2d082018-05-31 10:16:46 +0800135 std::string product_partition = StringPrintf("/dev/block/by-name/product%s",
Andreas Gampeb87a1c72018-04-13 17:42:23 -0700136 arg[2]);
137 int product_result = mount(product_partition.c_str(),
138 "/postinstall/product",
139 "ext4",
140 MS_RDONLY,
141 /* data */ nullptr);
142 UNUSED(product_result);
143 }
Andreas Gampefd12eda2016-07-12 09:47:17 -0700144
Roland Levillainc19c6042018-12-18 12:15:12 +0000145 // Setup APEX mount point and its security context.
146 static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
147 // The following logic is similar to the one in system/core/rootdir/init.rc:
148 //
149 // mount tmpfs tmpfs /apex nodev noexec nosuid
150 // chmod 0755 /apex
151 // chown root root /apex
152 // restorecon /apex
153 //
154 if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
155 != 0) {
156 PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
157 exit(209);
158 }
159 if (chmod(kPostinstallApexDir, 0755) != 0) {
160 PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
161 exit(210);
162 }
163 if (chown(kPostinstallApexDir, 0, 0) != 0) {
164 PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
165 exit(211);
166 }
167 if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
168 PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
169 exit(212);
170 }
171
Andreas Gampe01ad5982016-03-09 16:27:29 -0800172 // Chdir into /postinstall.
173 if (chdir("/postinstall") != 0) {
174 PLOG(ERROR) << "Unable to chdir into /postinstall.";
175 exit(203);
176 }
177
178 // Make /postinstall the root in our mount namespace.
179 if (chroot(".") != 0) {
180 PLOG(ERROR) << "Failed to chroot";
181 exit(204);
182 }
183
184 if (chdir("/") != 0) {
185 PLOG(ERROR) << "Unable to chdir into /.";
186 exit(205);
187 }
188
Roland Levillainc19c6042018-12-18 12:15:12 +0000189 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
190 // the Android Runtime APEX, as it is required by otapreopt to run dex2oat.
191 // The logic here is (partially) copied and adapted from
192 // system/apex/apexd/apexd_main.cpp.
193 //
194 // Only scan the APEX directory under /system (within the chroot dir).
195 // Note that this leaves around the loop devices created and used by
196 // libapexd's code, but this is fine, as we expect to reboot soon after.
197 apex::scanPackagesDirAndActivate(apex::kApexPackageSystemDir);
198
Andreas Gampe01ad5982016-03-09 16:27:29 -0800199 // Now go on and run otapreopt.
200
Andreas Gampec093d3a2017-04-14 11:15:17 -0700201 // Incoming: cmd + status-fd + target-slot + cmd... + null | Incoming | = argc + 1
202 // Outgoing: cmd + target-slot + cmd... + null | Outgoing | = argc
203 const char** argv = new const char*[argc];
204
Andreas Gamped089ca12016-06-27 14:25:30 -0700205 argv[0] = "/system/bin/otapreopt";
206
207 // The first parameter is the status file descriptor, skip.
Andreas Gampec093d3a2017-04-14 11:15:17 -0700208 for (size_t i = 2; i <= static_cast<size_t>(argc); ++i) {
209 argv[i - 1] = arg[i];
Andreas Gamped089ca12016-06-27 14:25:30 -0700210 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800211
Andreas Gampec093d3a2017-04-14 11:15:17 -0700212 execv(argv[0], static_cast<char * const *>(const_cast<char**>(argv)));
Andreas Gampe01ad5982016-03-09 16:27:29 -0800213 PLOG(ERROR) << "execv(OTAPREOPT) failed.";
214 exit(99);
215}
216
217} // namespace installd
218} // namespace android
219
220int main(const int argc, char *argv[]) {
221 return android::installd::otapreopt_chroot(argc, argv);
222}