• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <commands.h>
29 #include <otapreopt_utils.h>
30 
31 #ifndef LOG_TAG
32 #define LOG_TAG "otapreopt"
33 #endif
34 
35 using android::base::StringPrintf;
36 
37 namespace android {
38 namespace installd {
39 
CloseDescriptor(int fd)40 static 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 
CloseDescriptor(const char * descriptor_string)48 static 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.
otapreopt_chroot(const int argc,char ** arg)61 static 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 + "dexopt" + dexopt-params + null
140     // Outgoing:  cmd             + target-slot + "dexopt" + dexopt-params + null
141     constexpr size_t kInArguments =   1                       // Binary name.
142                                     + 1                       // status file descriptor.
143                                     + 1                       // target-slot.
144                                     + 1                       // "dexopt."
145                                     + DEXOPT_PARAM_COUNT      // dexopt parameters.
146                                     + 1;                      // null termination.
147     constexpr size_t kOutArguments =   1                       // Binary name.
148                                      + 1                       // target-slot.
149                                      + 1                       // "dexopt."
150                                      + DEXOPT_PARAM_COUNT      // dexopt parameters.
151                                      + 1;                      // null termination.
152     const char* argv[kOutArguments];
153     if (static_cast<size_t>(argc) !=  kInArguments - 1 /* null termination */) {
154         LOG(ERROR) << "Unexpected argument size "
155                    << argc
156                    << " vs "
157                    << (kInArguments - 1);
158         for (size_t i = 0; i < static_cast<size_t>(argc); ++i) {
159             if (arg[i] == nullptr) {
160                 LOG(ERROR) << "(null)";
161             } else {
162                 LOG(ERROR) << "\"" << arg[i] << "\"";
163             }
164         }
165         exit(206);
166     }
167     argv[0] = "/system/bin/otapreopt";
168 
169     // The first parameter is the status file descriptor, skip.
170 
171     for (size_t i = 1; i <= kOutArguments - 2 /* cmd + null */; ++i) {
172         argv[i] = arg[i + 1];
173     }
174     argv[kOutArguments - 1] = nullptr;
175 
176     execv(argv[0], (char * const *)argv);
177     PLOG(ERROR) << "execv(OTAPREOPT) failed.";
178     exit(99);
179 }
180 
181 }  // namespace installd
182 }  // namespace android
183 
main(const int argc,char * argv[])184 int main(const int argc, char *argv[]) {
185     return android::installd::otapreopt_chroot(argc, argv);
186 }
187