1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker ** Copyright 2016, The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker **
4*38e8c45fSAndroid Build Coastguard Worker ** Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker ** you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker ** You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker **
8*38e8c45fSAndroid Build Coastguard Worker ** http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker **
10*38e8c45fSAndroid Build Coastguard Worker ** Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker ** distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker ** See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker ** limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include <fcntl.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <linux/unistd.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <sys/mount.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <sys/stat.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <sys/wait.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <unistd.h>
23*38e8c45fSAndroid Build Coastguard Worker
24*38e8c45fSAndroid Build Coastguard Worker #include <algorithm>
25*38e8c45fSAndroid Build Coastguard Worker #include <array>
26*38e8c45fSAndroid Build Coastguard Worker #include <fstream>
27*38e8c45fSAndroid Build Coastguard Worker #include <iostream>
28*38e8c45fSAndroid Build Coastguard Worker #include <sstream>
29*38e8c45fSAndroid Build Coastguard Worker
30*38e8c45fSAndroid Build Coastguard Worker #include <android-base/file.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <android-base/macros.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <android-base/scopeguard.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
37*38e8c45fSAndroid Build Coastguard Worker #include <libdm/dm.h>
38*38e8c45fSAndroid Build Coastguard Worker #include <selinux/android.h>
39*38e8c45fSAndroid Build Coastguard Worker
40*38e8c45fSAndroid Build Coastguard Worker #include "installd_constants.h"
41*38e8c45fSAndroid Build Coastguard Worker #include "otapreopt_utils.h"
42*38e8c45fSAndroid Build Coastguard Worker
43*38e8c45fSAndroid Build Coastguard Worker #ifndef LOG_TAG
44*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "otapreopt_chroot"
45*38e8c45fSAndroid Build Coastguard Worker #endif
46*38e8c45fSAndroid Build Coastguard Worker
47*38e8c45fSAndroid Build Coastguard Worker using android::base::StringPrintf;
48*38e8c45fSAndroid Build Coastguard Worker
49*38e8c45fSAndroid Build Coastguard Worker namespace android {
50*38e8c45fSAndroid Build Coastguard Worker namespace installd {
51*38e8c45fSAndroid Build Coastguard Worker
52*38e8c45fSAndroid Build Coastguard Worker // We don't know the filesystem types of the partitions in the update package,
53*38e8c45fSAndroid Build Coastguard Worker // so just try the possibilities one by one.
54*38e8c45fSAndroid Build Coastguard Worker static constexpr std::array kTryMountFsTypes = {"ext4", "erofs"};
55*38e8c45fSAndroid Build Coastguard Worker
CloseDescriptor(const char * descriptor_string)56*38e8c45fSAndroid Build Coastguard Worker static void CloseDescriptor(const char* descriptor_string) {
57*38e8c45fSAndroid Build Coastguard Worker int fd = -1;
58*38e8c45fSAndroid Build Coastguard Worker std::istringstream stream(descriptor_string);
59*38e8c45fSAndroid Build Coastguard Worker stream >> fd;
60*38e8c45fSAndroid Build Coastguard Worker if (!stream.fail()) {
61*38e8c45fSAndroid Build Coastguard Worker if (fd >= 0) {
62*38e8c45fSAndroid Build Coastguard Worker if (close(fd) < 0) {
63*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to close " << fd;
64*38e8c45fSAndroid Build Coastguard Worker }
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker }
67*38e8c45fSAndroid Build Coastguard Worker }
68*38e8c45fSAndroid Build Coastguard Worker
SetCloseOnExec(int fd)69*38e8c45fSAndroid Build Coastguard Worker static void SetCloseOnExec(int fd) {
70*38e8c45fSAndroid Build Coastguard Worker if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
71*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to set FD_CLOEXEC on " << fd;
72*38e8c45fSAndroid Build Coastguard Worker }
73*38e8c45fSAndroid Build Coastguard Worker }
74*38e8c45fSAndroid Build Coastguard Worker
ActivateApexPackages()75*38e8c45fSAndroid Build Coastguard Worker static void ActivateApexPackages() {
76*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> apexd_cmd{"/system/bin/apexd", "--otachroot-bootstrap"};
77*38e8c45fSAndroid Build Coastguard Worker std::string apexd_error_msg;
78*38e8c45fSAndroid Build Coastguard Worker
79*38e8c45fSAndroid Build Coastguard Worker bool exec_result = Exec(apexd_cmd, &apexd_error_msg);
80*38e8c45fSAndroid Build Coastguard Worker if (!exec_result) {
81*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Running otapreopt failed: " << apexd_error_msg;
82*38e8c45fSAndroid Build Coastguard Worker exit(220);
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker }
85*38e8c45fSAndroid Build Coastguard Worker
DeactivateApexPackages()86*38e8c45fSAndroid Build Coastguard Worker static void DeactivateApexPackages() {
87*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> apexd_cmd{"/system/bin/apexd", "--unmount-all"};
88*38e8c45fSAndroid Build Coastguard Worker std::string apexd_error_msg;
89*38e8c45fSAndroid Build Coastguard Worker bool exec_result = Exec(apexd_cmd, &apexd_error_msg);
90*38e8c45fSAndroid Build Coastguard Worker if (!exec_result) {
91*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Running /system/bin/apexd --unmount-all failed: " << apexd_error_msg;
92*38e8c45fSAndroid Build Coastguard Worker }
93*38e8c45fSAndroid Build Coastguard Worker }
94*38e8c45fSAndroid Build Coastguard Worker
TryMountWithFstypes(const char * block_device,const char * target)95*38e8c45fSAndroid Build Coastguard Worker static bool TryMountWithFstypes(const char* block_device, const char* target) {
96*38e8c45fSAndroid Build Coastguard Worker for (int i = 0; i < kTryMountFsTypes.size(); ++i) {
97*38e8c45fSAndroid Build Coastguard Worker const char* fstype = kTryMountFsTypes[i];
98*38e8c45fSAndroid Build Coastguard Worker int mount_result = mount(block_device, target, fstype, MS_RDONLY, /* data */ nullptr);
99*38e8c45fSAndroid Build Coastguard Worker if (mount_result == 0) {
100*38e8c45fSAndroid Build Coastguard Worker return true;
101*38e8c45fSAndroid Build Coastguard Worker }
102*38e8c45fSAndroid Build Coastguard Worker if (errno == EINVAL && i < kTryMountFsTypes.size() - 1) {
103*38e8c45fSAndroid Build Coastguard Worker // Only try the next fstype if mounting failed due to the current one
104*38e8c45fSAndroid Build Coastguard Worker // being invalid.
105*38e8c45fSAndroid Build Coastguard Worker LOG(WARNING) << "Failed to mount " << block_device << " on " << target << " with "
106*38e8c45fSAndroid Build Coastguard Worker << fstype << " - trying " << kTryMountFsTypes[i + 1];
107*38e8c45fSAndroid Build Coastguard Worker } else {
108*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to mount " << block_device << " on " << target << " with "
109*38e8c45fSAndroid Build Coastguard Worker << fstype;
110*38e8c45fSAndroid Build Coastguard Worker return false;
111*38e8c45fSAndroid Build Coastguard Worker }
112*38e8c45fSAndroid Build Coastguard Worker }
113*38e8c45fSAndroid Build Coastguard Worker __builtin_unreachable();
114*38e8c45fSAndroid Build Coastguard Worker }
115*38e8c45fSAndroid Build Coastguard Worker
TryExtraMount(const char * name,const char * slot,const char * target)116*38e8c45fSAndroid Build Coastguard Worker static void TryExtraMount(const char* name, const char* slot, const char* target) {
117*38e8c45fSAndroid Build Coastguard Worker std::string partition_name = StringPrintf("%s%s", name, slot);
118*38e8c45fSAndroid Build Coastguard Worker
119*38e8c45fSAndroid Build Coastguard Worker // See whether update_engine mounted a logical partition.
120*38e8c45fSAndroid Build Coastguard Worker {
121*38e8c45fSAndroid Build Coastguard Worker auto& dm = dm::DeviceMapper::Instance();
122*38e8c45fSAndroid Build Coastguard Worker if (dm.GetState(partition_name) != dm::DmDeviceState::INVALID) {
123*38e8c45fSAndroid Build Coastguard Worker std::string path;
124*38e8c45fSAndroid Build Coastguard Worker if (dm.GetDmDevicePathByName(partition_name, &path)) {
125*38e8c45fSAndroid Build Coastguard Worker if (TryMountWithFstypes(path.c_str(), target)) {
126*38e8c45fSAndroid Build Coastguard Worker return;
127*38e8c45fSAndroid Build Coastguard Worker }
128*38e8c45fSAndroid Build Coastguard Worker }
129*38e8c45fSAndroid Build Coastguard Worker }
130*38e8c45fSAndroid Build Coastguard Worker }
131*38e8c45fSAndroid Build Coastguard Worker
132*38e8c45fSAndroid Build Coastguard Worker // Fall back and attempt a direct mount.
133*38e8c45fSAndroid Build Coastguard Worker std::string block_device = StringPrintf("/dev/block/by-name/%s", partition_name.c_str());
134*38e8c45fSAndroid Build Coastguard Worker (void)TryMountWithFstypes(block_device.c_str(), target);
135*38e8c45fSAndroid Build Coastguard Worker }
136*38e8c45fSAndroid Build Coastguard Worker
137*38e8c45fSAndroid Build Coastguard Worker // Entry for otapreopt_chroot. Expected parameters are:
138*38e8c45fSAndroid Build Coastguard Worker //
139*38e8c45fSAndroid Build Coastguard Worker // [cmd] [status-fd] [target-slot-suffix]
140*38e8c45fSAndroid Build Coastguard Worker //
141*38e8c45fSAndroid Build Coastguard Worker // The file descriptor denoted by status-fd will be closed. Dexopt commands on
142*38e8c45fSAndroid Build Coastguard Worker // the form
143*38e8c45fSAndroid Build Coastguard Worker //
144*38e8c45fSAndroid Build Coastguard Worker // "dexopt" [dexopt-params]
145*38e8c45fSAndroid Build Coastguard Worker //
146*38e8c45fSAndroid Build Coastguard Worker // are then read from stdin until EOF and passed on to /system/bin/otapreopt one
147*38e8c45fSAndroid Build Coastguard Worker // by one. After each call a line with the current command count is written to
148*38e8c45fSAndroid Build Coastguard Worker // stdout and flushed.
otapreopt_chroot(const int argc,char ** arg)149*38e8c45fSAndroid Build Coastguard Worker static int otapreopt_chroot(const int argc, char **arg) {
150*38e8c45fSAndroid Build Coastguard Worker // Validate arguments
151*38e8c45fSAndroid Build Coastguard Worker if (argc == 2 && std::string_view(arg[1]) == "--version") {
152*38e8c45fSAndroid Build Coastguard Worker // Accept a single --version flag, to allow the script to tell this binary
153*38e8c45fSAndroid Build Coastguard Worker // from the earlier one.
154*38e8c45fSAndroid Build Coastguard Worker std::cout << "2" << std::endl;
155*38e8c45fSAndroid Build Coastguard Worker return 0;
156*38e8c45fSAndroid Build Coastguard Worker }
157*38e8c45fSAndroid Build Coastguard Worker if (argc != 3) {
158*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "Wrong number of arguments: " << argc;
159*38e8c45fSAndroid Build Coastguard Worker exit(208);
160*38e8c45fSAndroid Build Coastguard Worker }
161*38e8c45fSAndroid Build Coastguard Worker const char* status_fd = arg[1];
162*38e8c45fSAndroid Build Coastguard Worker const char* slot_suffix = arg[2];
163*38e8c45fSAndroid Build Coastguard Worker
164*38e8c45fSAndroid Build Coastguard Worker // Set O_CLOEXEC on standard fds. They are coming from the caller, we do not
165*38e8c45fSAndroid Build Coastguard Worker // want to pass them on across our fork/exec into a different domain.
166*38e8c45fSAndroid Build Coastguard Worker SetCloseOnExec(STDIN_FILENO);
167*38e8c45fSAndroid Build Coastguard Worker SetCloseOnExec(STDOUT_FILENO);
168*38e8c45fSAndroid Build Coastguard Worker SetCloseOnExec(STDERR_FILENO);
169*38e8c45fSAndroid Build Coastguard Worker // Close the status channel.
170*38e8c45fSAndroid Build Coastguard Worker CloseDescriptor(status_fd);
171*38e8c45fSAndroid Build Coastguard Worker
172*38e8c45fSAndroid Build Coastguard Worker // We need to run the otapreopt tool from the postinstall partition. As such, set up a
173*38e8c45fSAndroid Build Coastguard Worker // mount namespace and change root.
174*38e8c45fSAndroid Build Coastguard Worker
175*38e8c45fSAndroid Build Coastguard Worker // Create our own mount namespace.
176*38e8c45fSAndroid Build Coastguard Worker if (unshare(CLONE_NEWNS) != 0) {
177*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to unshare() for otapreopt.";
178*38e8c45fSAndroid Build Coastguard Worker exit(200);
179*38e8c45fSAndroid Build Coastguard Worker }
180*38e8c45fSAndroid Build Coastguard Worker
181*38e8c45fSAndroid Build Coastguard Worker // Make postinstall private, so that our changes don't propagate.
182*38e8c45fSAndroid Build Coastguard Worker if (mount("", "/postinstall", nullptr, MS_PRIVATE, nullptr) != 0) {
183*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to mount private.";
184*38e8c45fSAndroid Build Coastguard Worker exit(201);
185*38e8c45fSAndroid Build Coastguard Worker }
186*38e8c45fSAndroid Build Coastguard Worker
187*38e8c45fSAndroid Build Coastguard Worker // Bind mount necessary directories.
188*38e8c45fSAndroid Build Coastguard Worker constexpr const char* kBindMounts[] = {
189*38e8c45fSAndroid Build Coastguard Worker "/data", "/dev", "/proc", "/sys",
190*38e8c45fSAndroid Build Coastguard Worker "/sys/fs/selinux" /* Required for apexd which includes libselinux */
191*38e8c45fSAndroid Build Coastguard Worker };
192*38e8c45fSAndroid Build Coastguard Worker for (size_t i = 0; i < arraysize(kBindMounts); ++i) {
193*38e8c45fSAndroid Build Coastguard Worker std::string trg = StringPrintf("/postinstall%s", kBindMounts[i]);
194*38e8c45fSAndroid Build Coastguard Worker if (mount(kBindMounts[i], trg.c_str(), nullptr, MS_BIND, nullptr) != 0) {
195*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to bind-mount " << kBindMounts[i];
196*38e8c45fSAndroid Build Coastguard Worker exit(202);
197*38e8c45fSAndroid Build Coastguard Worker }
198*38e8c45fSAndroid Build Coastguard Worker }
199*38e8c45fSAndroid Build Coastguard Worker
200*38e8c45fSAndroid Build Coastguard Worker // Try to mount the vendor partition. update_engine doesn't do this for us, but we
201*38e8c45fSAndroid Build Coastguard Worker // want it for vendor APKs.
202*38e8c45fSAndroid Build Coastguard Worker // Notes:
203*38e8c45fSAndroid Build Coastguard Worker // 1) We pretty much guess a name here and hope to find the partition by name.
204*38e8c45fSAndroid Build Coastguard Worker // It is just as complicated and brittle to scan /proc/mounts. But this requires
205*38e8c45fSAndroid Build Coastguard Worker // validating the target-slot so as not to try to mount some totally random path.
206*38e8c45fSAndroid Build Coastguard Worker // 2) We're in a mount namespace here, so when we die, this will be cleaned up.
207*38e8c45fSAndroid Build Coastguard Worker // 3) Ignore errors. Printing anything at this stage will open a file descriptor
208*38e8c45fSAndroid Build Coastguard Worker // for logging.
209*38e8c45fSAndroid Build Coastguard Worker if (!ValidateTargetSlotSuffix(slot_suffix)) {
210*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "Target slot suffix not legal: " << slot_suffix;
211*38e8c45fSAndroid Build Coastguard Worker exit(207);
212*38e8c45fSAndroid Build Coastguard Worker }
213*38e8c45fSAndroid Build Coastguard Worker TryExtraMount("vendor", slot_suffix, "/postinstall/vendor");
214*38e8c45fSAndroid Build Coastguard Worker
215*38e8c45fSAndroid Build Coastguard Worker // Try to mount the product partition. update_engine doesn't do this for us, but we
216*38e8c45fSAndroid Build Coastguard Worker // want it for product APKs. Same notes as vendor above.
217*38e8c45fSAndroid Build Coastguard Worker TryExtraMount("product", slot_suffix, "/postinstall/product");
218*38e8c45fSAndroid Build Coastguard Worker
219*38e8c45fSAndroid Build Coastguard Worker // Try to mount the system_ext partition. update_engine doesn't do this for
220*38e8c45fSAndroid Build Coastguard Worker // us, but we want it for system_ext APKs. Same notes as vendor and product
221*38e8c45fSAndroid Build Coastguard Worker // above.
222*38e8c45fSAndroid Build Coastguard Worker TryExtraMount("system_ext", slot_suffix, "/postinstall/system_ext");
223*38e8c45fSAndroid Build Coastguard Worker
224*38e8c45fSAndroid Build Coastguard Worker constexpr const char* kPostInstallLinkerconfig = "/postinstall/linkerconfig";
225*38e8c45fSAndroid Build Coastguard Worker // Try to mount /postinstall/linkerconfig. we will set it up after performing the chroot
226*38e8c45fSAndroid Build Coastguard Worker if (mount("tmpfs", kPostInstallLinkerconfig, "tmpfs", 0, nullptr) != 0) {
227*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to mount a tmpfs for " << kPostInstallLinkerconfig;
228*38e8c45fSAndroid Build Coastguard Worker exit(215);
229*38e8c45fSAndroid Build Coastguard Worker }
230*38e8c45fSAndroid Build Coastguard Worker
231*38e8c45fSAndroid Build Coastguard Worker // Setup APEX mount point and its security context.
232*38e8c45fSAndroid Build Coastguard Worker static constexpr const char* kPostinstallApexDir = "/postinstall/apex";
233*38e8c45fSAndroid Build Coastguard Worker // The following logic is similar to the one in system/core/rootdir/init.rc:
234*38e8c45fSAndroid Build Coastguard Worker //
235*38e8c45fSAndroid Build Coastguard Worker // mount tmpfs tmpfs /apex nodev noexec nosuid
236*38e8c45fSAndroid Build Coastguard Worker // chmod 0755 /apex
237*38e8c45fSAndroid Build Coastguard Worker // chown root root /apex
238*38e8c45fSAndroid Build Coastguard Worker // restorecon /apex
239*38e8c45fSAndroid Build Coastguard Worker //
240*38e8c45fSAndroid Build Coastguard Worker // except we perform the `restorecon` step just after mounting the tmpfs
241*38e8c45fSAndroid Build Coastguard Worker // filesystem in /postinstall/apex, so that this directory is correctly
242*38e8c45fSAndroid Build Coastguard Worker // labeled (with type `postinstall_apex_mnt_dir`) and may be manipulated in
243*38e8c45fSAndroid Build Coastguard Worker // following operations (`chmod`, `chown`, etc.) following policies
244*38e8c45fSAndroid Build Coastguard Worker // restricted to `postinstall_apex_mnt_dir`:
245*38e8c45fSAndroid Build Coastguard Worker //
246*38e8c45fSAndroid Build Coastguard Worker // mount tmpfs tmpfs /postinstall/apex nodev noexec nosuid
247*38e8c45fSAndroid Build Coastguard Worker // restorecon /postinstall/apex
248*38e8c45fSAndroid Build Coastguard Worker // chmod 0755 /postinstall/apex
249*38e8c45fSAndroid Build Coastguard Worker // chown root root /postinstall/apex
250*38e8c45fSAndroid Build Coastguard Worker //
251*38e8c45fSAndroid Build Coastguard Worker if (mount("tmpfs", kPostinstallApexDir, "tmpfs", MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr)
252*38e8c45fSAndroid Build Coastguard Worker != 0) {
253*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to mount tmpfs in " << kPostinstallApexDir;
254*38e8c45fSAndroid Build Coastguard Worker exit(209);
255*38e8c45fSAndroid Build Coastguard Worker }
256*38e8c45fSAndroid Build Coastguard Worker if (selinux_android_restorecon(kPostinstallApexDir, 0) < 0) {
257*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to restorecon " << kPostinstallApexDir;
258*38e8c45fSAndroid Build Coastguard Worker exit(214);
259*38e8c45fSAndroid Build Coastguard Worker }
260*38e8c45fSAndroid Build Coastguard Worker if (chmod(kPostinstallApexDir, 0755) != 0) {
261*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to chmod " << kPostinstallApexDir << " to 0755";
262*38e8c45fSAndroid Build Coastguard Worker exit(210);
263*38e8c45fSAndroid Build Coastguard Worker }
264*38e8c45fSAndroid Build Coastguard Worker if (chown(kPostinstallApexDir, 0, 0) != 0) {
265*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to chown " << kPostinstallApexDir << " to root:root";
266*38e8c45fSAndroid Build Coastguard Worker exit(211);
267*38e8c45fSAndroid Build Coastguard Worker }
268*38e8c45fSAndroid Build Coastguard Worker
269*38e8c45fSAndroid Build Coastguard Worker // Chdir into /postinstall.
270*38e8c45fSAndroid Build Coastguard Worker if (chdir("/postinstall") != 0) {
271*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Unable to chdir into /postinstall.";
272*38e8c45fSAndroid Build Coastguard Worker exit(203);
273*38e8c45fSAndroid Build Coastguard Worker }
274*38e8c45fSAndroid Build Coastguard Worker
275*38e8c45fSAndroid Build Coastguard Worker // Make /postinstall the root in our mount namespace.
276*38e8c45fSAndroid Build Coastguard Worker if (chroot(".") != 0) {
277*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to chroot";
278*38e8c45fSAndroid Build Coastguard Worker exit(204);
279*38e8c45fSAndroid Build Coastguard Worker }
280*38e8c45fSAndroid Build Coastguard Worker
281*38e8c45fSAndroid Build Coastguard Worker if (chdir("/") != 0) {
282*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Unable to chdir into /.";
283*38e8c45fSAndroid Build Coastguard Worker exit(205);
284*38e8c45fSAndroid Build Coastguard Worker }
285*38e8c45fSAndroid Build Coastguard Worker
286*38e8c45fSAndroid Build Coastguard Worker // Call apexd --unmount-all to free up loop and dm block devices, so that we can re-use
287*38e8c45fSAndroid Build Coastguard Worker // them during the next invocation. Since otapreopt_chroot calls exit in case something goes
288*38e8c45fSAndroid Build Coastguard Worker // wrong we need to register our own atexit handler.
289*38e8c45fSAndroid Build Coastguard Worker // We want to register this handler before actually activating apex packages. This is mostly
290*38e8c45fSAndroid Build Coastguard Worker // due to the fact that if fail to unmount apexes, then on the next run of otapreopt_chroot
291*38e8c45fSAndroid Build Coastguard Worker // we will ask for new loop devices instead of re-using existing ones, and we really don't want
292*38e8c45fSAndroid Build Coastguard Worker // to do that. :)
293*38e8c45fSAndroid Build Coastguard Worker if (atexit(DeactivateApexPackages) != 0) {
294*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "Failed to register atexit hander";
295*38e8c45fSAndroid Build Coastguard Worker exit(206);
296*38e8c45fSAndroid Build Coastguard Worker }
297*38e8c45fSAndroid Build Coastguard Worker
298*38e8c45fSAndroid Build Coastguard Worker // Try to mount APEX packages in "/apex" in the chroot dir. We need at least
299*38e8c45fSAndroid Build Coastguard Worker // the ART APEX, as it is required by otapreopt to run dex2oat.
300*38e8c45fSAndroid Build Coastguard Worker ActivateApexPackages();
301*38e8c45fSAndroid Build Coastguard Worker
302*38e8c45fSAndroid Build Coastguard Worker auto cleanup = android::base::make_scope_guard([](){
303*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> apexd_cmd{"/system/bin/apexd", "--unmount-all"};
304*38e8c45fSAndroid Build Coastguard Worker std::string apexd_error_msg;
305*38e8c45fSAndroid Build Coastguard Worker bool exec_result = Exec(apexd_cmd, &apexd_error_msg);
306*38e8c45fSAndroid Build Coastguard Worker if (!exec_result) {
307*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Running /system/bin/apexd --unmount-all failed: " << apexd_error_msg;
308*38e8c45fSAndroid Build Coastguard Worker }
309*38e8c45fSAndroid Build Coastguard Worker });
310*38e8c45fSAndroid Build Coastguard Worker // Check that an ART APEX has been activated; clean up and exit
311*38e8c45fSAndroid Build Coastguard Worker // early otherwise.
312*38e8c45fSAndroid Build Coastguard Worker static constexpr const std::string_view kRequiredApexs[] = {
313*38e8c45fSAndroid Build Coastguard Worker "com.android.art",
314*38e8c45fSAndroid Build Coastguard Worker "com.android.runtime",
315*38e8c45fSAndroid Build Coastguard Worker "com.android.sdkext", // For derive_classpath
316*38e8c45fSAndroid Build Coastguard Worker };
317*38e8c45fSAndroid Build Coastguard Worker std::array<bool, arraysize(kRequiredApexs)> found_apexs{ false, false };
318*38e8c45fSAndroid Build Coastguard Worker DIR* apex_dir = opendir("/apex");
319*38e8c45fSAndroid Build Coastguard Worker if (apex_dir == nullptr) {
320*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "unable to open /apex";
321*38e8c45fSAndroid Build Coastguard Worker exit(220);
322*38e8c45fSAndroid Build Coastguard Worker }
323*38e8c45fSAndroid Build Coastguard Worker for (dirent* entry = readdir(apex_dir); entry != nullptr; entry = readdir(apex_dir)) {
324*38e8c45fSAndroid Build Coastguard Worker for (int i = 0; i < found_apexs.size(); i++) {
325*38e8c45fSAndroid Build Coastguard Worker if (kRequiredApexs[i] == std::string_view(entry->d_name)) {
326*38e8c45fSAndroid Build Coastguard Worker found_apexs[i] = true;
327*38e8c45fSAndroid Build Coastguard Worker break;
328*38e8c45fSAndroid Build Coastguard Worker }
329*38e8c45fSAndroid Build Coastguard Worker }
330*38e8c45fSAndroid Build Coastguard Worker }
331*38e8c45fSAndroid Build Coastguard Worker closedir(apex_dir);
332*38e8c45fSAndroid Build Coastguard Worker auto it = std::find(found_apexs.cbegin(), found_apexs.cend(), false);
333*38e8c45fSAndroid Build Coastguard Worker if (it != found_apexs.cend()) {
334*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "No activated " << kRequiredApexs[std::distance(found_apexs.cbegin(), it)]
335*38e8c45fSAndroid Build Coastguard Worker << " package!";
336*38e8c45fSAndroid Build Coastguard Worker exit(221);
337*38e8c45fSAndroid Build Coastguard Worker }
338*38e8c45fSAndroid Build Coastguard Worker
339*38e8c45fSAndroid Build Coastguard Worker // Setup /linkerconfig. Doing it after the chroot means it doesn't need its own category
340*38e8c45fSAndroid Build Coastguard Worker if (selinux_android_restorecon("/linkerconfig", 0) < 0) {
341*38e8c45fSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to restorecon /linkerconfig";
342*38e8c45fSAndroid Build Coastguard Worker exit(219);
343*38e8c45fSAndroid Build Coastguard Worker }
344*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> linkerconfig_cmd{"/apex/com.android.runtime/bin/linkerconfig",
345*38e8c45fSAndroid Build Coastguard Worker "--target", "/linkerconfig"};
346*38e8c45fSAndroid Build Coastguard Worker std::string linkerconfig_error_msg;
347*38e8c45fSAndroid Build Coastguard Worker bool linkerconfig_exec_result = Exec(linkerconfig_cmd, &linkerconfig_error_msg);
348*38e8c45fSAndroid Build Coastguard Worker if (!linkerconfig_exec_result) {
349*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "Running linkerconfig failed: " << linkerconfig_error_msg;
350*38e8c45fSAndroid Build Coastguard Worker exit(218);
351*38e8c45fSAndroid Build Coastguard Worker }
352*38e8c45fSAndroid Build Coastguard Worker
353*38e8c45fSAndroid Build Coastguard Worker // Now go on and read dexopt lines from stdin and pass them on to otapreopt.
354*38e8c45fSAndroid Build Coastguard Worker
355*38e8c45fSAndroid Build Coastguard Worker int count = 1;
356*38e8c45fSAndroid Build Coastguard Worker for (std::array<char, 10000> linebuf;
357*38e8c45fSAndroid Build Coastguard Worker std::cin.clear(), std::cin.getline(&linebuf[0], linebuf.size()); ++count) {
358*38e8c45fSAndroid Build Coastguard Worker // Subtract one from gcount() since getline() counts the newline.
359*38e8c45fSAndroid Build Coastguard Worker std::string line(&linebuf[0], std::cin.gcount() - 1);
360*38e8c45fSAndroid Build Coastguard Worker
361*38e8c45fSAndroid Build Coastguard Worker if (std::cin.fail()) {
362*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "Command exceeds max length " << linebuf.size() << " - skipped: " << line;
363*38e8c45fSAndroid Build Coastguard Worker continue;
364*38e8c45fSAndroid Build Coastguard Worker }
365*38e8c45fSAndroid Build Coastguard Worker
366*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> tokenized_line = android::base::Tokenize(line, " ");
367*38e8c45fSAndroid Build Coastguard Worker std::vector<std::string> cmd{"/system/bin/otapreopt", slot_suffix};
368*38e8c45fSAndroid Build Coastguard Worker std::move(tokenized_line.begin(), tokenized_line.end(), std::back_inserter(cmd));
369*38e8c45fSAndroid Build Coastguard Worker
370*38e8c45fSAndroid Build Coastguard Worker LOG(INFO) << "Command " << count << ": " << android::base::Join(cmd, " ");
371*38e8c45fSAndroid Build Coastguard Worker
372*38e8c45fSAndroid Build Coastguard Worker // Fork and execute otapreopt in its own process.
373*38e8c45fSAndroid Build Coastguard Worker std::string error_msg;
374*38e8c45fSAndroid Build Coastguard Worker bool exec_result = Exec(cmd, &error_msg);
375*38e8c45fSAndroid Build Coastguard Worker if (!exec_result) {
376*38e8c45fSAndroid Build Coastguard Worker LOG(ERROR) << "Running otapreopt failed: " << error_msg;
377*38e8c45fSAndroid Build Coastguard Worker }
378*38e8c45fSAndroid Build Coastguard Worker
379*38e8c45fSAndroid Build Coastguard Worker // Print the count to stdout and flush to indicate progress.
380*38e8c45fSAndroid Build Coastguard Worker std::cout << count << std::endl;
381*38e8c45fSAndroid Build Coastguard Worker }
382*38e8c45fSAndroid Build Coastguard Worker
383*38e8c45fSAndroid Build Coastguard Worker LOG(INFO) << "No more dexopt commands";
384*38e8c45fSAndroid Build Coastguard Worker return 0;
385*38e8c45fSAndroid Build Coastguard Worker }
386*38e8c45fSAndroid Build Coastguard Worker
387*38e8c45fSAndroid Build Coastguard Worker } // namespace installd
388*38e8c45fSAndroid Build Coastguard Worker } // namespace android
389*38e8c45fSAndroid Build Coastguard Worker
main(const int argc,char * argv[])390*38e8c45fSAndroid Build Coastguard Worker int main(const int argc, char *argv[]) {
391*38e8c45fSAndroid Build Coastguard Worker return android::installd::otapreopt_chroot(argc, argv);
392*38e8c45fSAndroid Build Coastguard Worker }
393