blob: a3dfa2d88de03979b1c45feaafdd9ece74d188e5 [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 Levillain833e6362018-12-13 16:59:33 +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 Levillain833e6362018-12-13 16:59:33 +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 Levillain833e6362018-12-13 16:59:33 +0000145 // Setup APEX mount point and its security context.
146 // The logic here is similar to the one in system/core/rootdir/init.rc:
147 //
148 // mount tmpfs tmpfs /apex nodev noexec nosuid
149 // chmod 0755 /apex
150 // chown root root /apex
151 // restorecon /apex
152 //
153 if (mount("tmpfs", "/postinstall/apex", "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
154 != 0) {
155 PLOG(ERROR) << "Failed to mount tmpfs in /postinstall/apex";
156 exit(209);
157 }
158 if (chmod("/postinstall/apex", 0755) != 0) {
159 PLOG(ERROR) << "Failed to chmod /postinstall/apex to 0755";
160 exit(210);
161 }
162 if (chown("/postinstall/apex", 0, 0) != 0) {
163 PLOG(ERROR) << "Failed to chown /postinstall/apex to root:root";
164 exit(211);
165 }
166 if (selinux_android_restorecon("/postinstall/apex", 0) < 0) {
167 PLOG(ERROR) << "Failed to restorecon /postinstall/apex";
168 exit(212);
169 }
170
Andreas Gampe01ad5982016-03-09 16:27:29 -0800171 // Chdir into /postinstall.
172 if (chdir("/postinstall") != 0) {
173 PLOG(ERROR) << "Unable to chdir into /postinstall.";
174 exit(203);
175 }
176
177 // Make /postinstall the root in our mount namespace.
178 if (chroot(".") != 0) {
179 PLOG(ERROR) << "Failed to chroot";
180 exit(204);
181 }
182
183 if (chdir("/") != 0) {
184 PLOG(ERROR) << "Unable to chdir into /.";
185 exit(205);
186 }
187
Roland Levillain833e6362018-12-13 16:59:33 +0000188 // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
189 // the Android Runtime APEX, as it is required by otapreopt to run dex2oat.
190 {
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 }
199
Andreas Gampe01ad5982016-03-09 16:27:29 -0800200 // Now go on and run otapreopt.
201
Andreas Gampec093d3a2017-04-14 11:15:17 -0700202 // Incoming: cmd + status-fd + target-slot + cmd... + null | Incoming | = argc + 1
203 // Outgoing: cmd + target-slot + cmd... + null | Outgoing | = argc
204 const char** argv = new const char*[argc];
205
Andreas Gamped089ca12016-06-27 14:25:30 -0700206 argv[0] = "/system/bin/otapreopt";
207
208 // The first parameter is the status file descriptor, skip.
Andreas Gampec093d3a2017-04-14 11:15:17 -0700209 for (size_t i = 2; i <= static_cast<size_t>(argc); ++i) {
210 argv[i - 1] = arg[i];
Andreas Gamped089ca12016-06-27 14:25:30 -0700211 }
Andreas Gampe01ad5982016-03-09 16:27:29 -0800212
Andreas Gampec093d3a2017-04-14 11:15:17 -0700213 execv(argv[0], static_cast<char * const *>(const_cast<char**>(argv)));
Andreas Gampe01ad5982016-03-09 16:27:29 -0800214 PLOG(ERROR) << "execv(OTAPREOPT) failed.";
215 exit(99);
216}
217
218} // namespace installd
219} // namespace android
220
221int main(const int argc, char *argv[]) {
222 return android::installd::otapreopt_chroot(argc, argv);
223}