blob: 6c87f7a4d6b6f9a8e72347df1c18954246d0b418 [file] [log] [blame]
Andreas Gampe73dae112015-11-19 14:12:14 -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
17#include <algorithm>
18#include <inttypes.h>
19#include <random>
Andreas Gampe1842af32016-03-16 14:28:50 -070020#include <regex>
Andreas Gampe73dae112015-11-19 14:12:14 -080021#include <selinux/android.h>
22#include <selinux/avc.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/capability.h>
26#include <sys/prctl.h>
27#include <sys/stat.h>
28#include <sys/wait.h>
29
30#include <android-base/logging.h>
31#include <android-base/macros.h>
32#include <android-base/stringprintf.h>
Andreas Gampe6db8db92016-06-03 10:22:19 -070033#include <android-base/strings.h>
Andreas Gampe73dae112015-11-19 14:12:14 -080034#include <cutils/fs.h>
35#include <cutils/log.h>
36#include <cutils/properties.h>
37#include <private/android_filesystem_config.h>
38
39#include <commands.h>
Andreas Gampe1842af32016-03-16 14:28:50 -070040#include <file_parsing.h>
Andreas Gampe73dae112015-11-19 14:12:14 -080041#include <globals.h>
42#include <installd_deps.h> // Need to fill in requirements of commands.
Andreas Gampe73dae112015-11-19 14:12:14 -080043#include <system_properties.h>
44#include <utils.h>
45
46#ifndef LOG_TAG
47#define LOG_TAG "otapreopt"
48#endif
49
50#define BUFFER_MAX 1024 /* input buffer for commands */
51#define TOKEN_MAX 16 /* max number of arguments in buffer */
52#define REPLY_MAX 256 /* largest reply allowed */
53
Andreas Gampe56f79f92016-06-08 15:11:37 -070054using android::base::EndsWith;
Andreas Gampe6db8db92016-06-03 10:22:19 -070055using android::base::Join;
56using android::base::Split;
Andreas Gampe56f79f92016-06-08 15:11:37 -070057using android::base::StartsWith;
Andreas Gampe73dae112015-11-19 14:12:14 -080058using android::base::StringPrintf;
59
60namespace android {
61namespace installd {
62
Andreas Gampe73dae112015-11-19 14:12:14 -080063template<typename T>
64static constexpr T RoundDown(T x, typename std::decay<T>::type n) {
65 return DCHECK_CONSTEXPR(IsPowerOfTwo(n), , T(0))(x & -n);
66}
67
68template<typename T>
69static constexpr T RoundUp(T x, typename std::remove_reference<T>::type n) {
70 return RoundDown(x + n - 1, n);
71}
72
73class OTAPreoptService {
74 public:
Andreas Gampe73dae112015-11-19 14:12:14 -080075 // Main driver. Performs the following steps.
76 //
77 // 1) Parse options (read system properties etc from B partition).
78 //
79 // 2) Read in package data.
80 //
81 // 3) Prepare environment variables.
82 //
83 // 4) Prepare(compile) boot image, if necessary.
84 //
85 // 5) Run update.
86 int Main(int argc, char** argv) {
Andreas Gamped089ca12016-06-27 14:25:30 -070087 if (!ReadArguments(argc, argv)) {
88 LOG(ERROR) << "Failed reading command line.";
89 return 1;
90 }
91
Andreas Gampe73dae112015-11-19 14:12:14 -080092 if (!ReadSystemProperties()) {
93 LOG(ERROR)<< "Failed reading system properties.";
Andreas Gamped089ca12016-06-27 14:25:30 -070094 return 2;
Andreas Gampe73dae112015-11-19 14:12:14 -080095 }
96
97 if (!ReadEnvironment()) {
98 LOG(ERROR) << "Failed reading environment properties.";
Andreas Gamped089ca12016-06-27 14:25:30 -070099 return 3;
Andreas Gampe73dae112015-11-19 14:12:14 -0800100 }
101
Andreas Gamped089ca12016-06-27 14:25:30 -0700102 if (!CheckAndInitializeInstalldGlobals()) {
103 LOG(ERROR) << "Failed initializing globals.";
104 return 4;
Andreas Gampe73dae112015-11-19 14:12:14 -0800105 }
106
107 PrepareEnvironment();
108
Andreas Gamped089ca12016-06-27 14:25:30 -0700109 if (!PrepareBootImage(/* force */ false)) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800110 LOG(ERROR) << "Failed preparing boot image.";
Andreas Gamped089ca12016-06-27 14:25:30 -0700111 return 5;
Andreas Gampe73dae112015-11-19 14:12:14 -0800112 }
113
114 int dexopt_retcode = RunPreopt();
115
116 return dexopt_retcode;
117 }
118
Andreas Gamped089ca12016-06-27 14:25:30 -0700119 int GetProperty(const char* key, char* value, const char* default_value) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800120 const std::string* prop_value = system_properties_.GetProperty(key);
121 if (prop_value == nullptr) {
122 if (default_value == nullptr) {
123 return 0;
124 }
125 // Copy in the default value.
126 strncpy(value, default_value, kPropertyValueMax - 1);
127 value[kPropertyValueMax - 1] = 0;
128 return strlen(default_value);// TODO: Need to truncate?
129 }
130 size_t size = std::min(kPropertyValueMax - 1, prop_value->length());
131 strncpy(value, prop_value->data(), size);
132 value[size] = 0;
133 return static_cast<int>(size);
134 }
135
Andreas Gamped089ca12016-06-27 14:25:30 -0700136 std::string GetOTADataDirectory() const {
137 return StringPrintf("%s/%s", GetOtaDirectoryPrefix().c_str(), target_slot_.c_str());
138 }
139
140 const std::string& GetTargetSlot() const {
141 return target_slot_;
142 }
143
Andreas Gampe73dae112015-11-19 14:12:14 -0800144private:
Andreas Gamped089ca12016-06-27 14:25:30 -0700145
Andreas Gampe73dae112015-11-19 14:12:14 -0800146 bool ReadSystemProperties() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700147 static constexpr const char* kPropertyFiles[] = {
148 "/default.prop", "/system/build.prop"
149 };
Andreas Gampe73dae112015-11-19 14:12:14 -0800150
Andreas Gampe1842af32016-03-16 14:28:50 -0700151 for (size_t i = 0; i < arraysize(kPropertyFiles); ++i) {
152 if (!system_properties_.Load(kPropertyFiles[i])) {
153 return false;
154 }
155 }
156
157 return true;
Andreas Gampe73dae112015-11-19 14:12:14 -0800158 }
159
160 bool ReadEnvironment() {
Andreas Gampe1842af32016-03-16 14:28:50 -0700161 // Parse the environment variables from init.environ.rc, which have the form
162 // export NAME VALUE
163 // For simplicity, don't respect string quotation. The values we are interested in can be
164 // encoded without them.
165 std::regex export_regex("\\s*export\\s+(\\S+)\\s+(\\S+)");
166 bool parse_result = ParseFile("/init.environ.rc", [&](const std::string& line) {
167 std::smatch export_match;
168 if (!std::regex_match(line, export_match, export_regex)) {
169 return true;
170 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800171
Andreas Gampe1842af32016-03-16 14:28:50 -0700172 if (export_match.size() != 3) {
173 return true;
174 }
175
176 std::string name = export_match[1].str();
177 std::string value = export_match[2].str();
178
179 system_properties_.SetProperty(name, value);
180
181 return true;
182 });
183 if (!parse_result) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800184 return false;
185 }
Andreas Gampe1842af32016-03-16 14:28:50 -0700186
Andreas Gamped089ca12016-06-27 14:25:30 -0700187 if (system_properties_.GetProperty(kAndroidDataPathPropertyName) == nullptr) {
188 return false;
189 }
190 android_data_ = *system_properties_.GetProperty(kAndroidDataPathPropertyName);
191
192 if (system_properties_.GetProperty(kAndroidRootPathPropertyName) == nullptr) {
193 return false;
194 }
195 android_root_ = *system_properties_.GetProperty(kAndroidRootPathPropertyName);
196
197 if (system_properties_.GetProperty(kBootClassPathPropertyName) == nullptr) {
198 return false;
199 }
200 boot_classpath_ = *system_properties_.GetProperty(kBootClassPathPropertyName);
201
202 if (system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) == nullptr) {
203 return false;
204 }
205 asec_mountpoint_ = *system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME);
206
207 return true;
208 }
209
210 const std::string& GetAndroidData() const {
211 return android_data_;
212 }
213
214 const std::string& GetAndroidRoot() const {
215 return android_root_;
216 }
217
218 const std::string GetOtaDirectoryPrefix() const {
219 return GetAndroidData() + "/ota";
220 }
221
222 bool CheckAndInitializeInstalldGlobals() {
223 // init_globals_from_data_and_root requires "ASEC_MOUNTPOINT" in the environment. We
224 // do not use any datapath that includes this, but we'll still have to set it.
225 CHECK(system_properties_.GetProperty(ASEC_MOUNTPOINT_ENV_NAME) != nullptr);
226 int result = setenv(ASEC_MOUNTPOINT_ENV_NAME, asec_mountpoint_.c_str(), 0);
227 if (result != 0) {
228 LOG(ERROR) << "Could not set ASEC_MOUNTPOINT environment variable";
229 return false;
230 }
231
232 if (!init_globals_from_data_and_root(GetAndroidData().c_str(), GetAndroidRoot().c_str())) {
233 LOG(ERROR) << "Could not initialize globals; exiting.";
234 return false;
235 }
236
237 // This is different from the normal installd. We only do the base
238 // directory, the rest will be created on demand when each app is compiled.
239 if (access(GetOtaDirectoryPrefix().c_str(), R_OK) < 0) {
240 LOG(ERROR) << "Could not access " << GetOtaDirectoryPrefix();
241 return false;
Andreas Gampe1842af32016-03-16 14:28:50 -0700242 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800243
244 return true;
245 }
246
Andreas Gamped089ca12016-06-27 14:25:30 -0700247 bool ReadArguments(int argc ATTRIBUTE_UNUSED, char** argv) {
248 // Expected command line:
249 // target-slot dexopt {DEXOPT_PARAMETERS}
250 // The DEXOPT_PARAMETERS are passed on to dexopt(), so we expect DEXOPT_PARAM_COUNT
251 // of them. We store them in package_parameters_ (size checks are done when
252 // parsing the special parameters and when copying into package_parameters_.
253
Andreas Gampe548bdb92016-06-02 17:56:45 -0700254 static_assert(DEXOPT_PARAM_COUNT == ARRAY_SIZE(package_parameters_),
255 "Unexpected dexopt param count");
Andreas Gamped089ca12016-06-27 14:25:30 -0700256
257 const char* target_slot_arg = argv[1];
258 if (target_slot_arg == nullptr) {
259 LOG(ERROR) << "Missing parameters";
260 return false;
261 }
262 // Sanitize value. Only allow (a-zA-Z0-9_)+.
263 target_slot_ = target_slot_arg;
264 {
265 std::regex slot_suffix_regex("[a-zA-Z0-9_]+");
266 std::smatch slot_suffix_match;
267 if (!std::regex_match(target_slot_, slot_suffix_match, slot_suffix_regex)) {
268 LOG(ERROR) << "Target slot suffix not legal: " << target_slot_;
269 return false;
270 }
271 }
272
273 // Check for "dexopt" next.
274 if (argv[2] == nullptr) {
275 LOG(ERROR) << "Missing parameters";
276 return false;
277 }
278 if (std::string("dexopt").compare(argv[2]) != 0) {
279 LOG(ERROR) << "Second parameter not dexopt: " << argv[2];
280 return false;
281 }
282
283 // Copy the rest into package_parameters_, but be careful about over- and underflow.
284 size_t index = 0;
Andreas Gampe548bdb92016-06-02 17:56:45 -0700285 while (index < DEXOPT_PARAM_COUNT &&
Andreas Gamped089ca12016-06-27 14:25:30 -0700286 argv[index + 3] != nullptr) {
287 package_parameters_[index] = argv[index + 3];
Andreas Gampe73dae112015-11-19 14:12:14 -0800288 index++;
289 }
Andreas Gamped089ca12016-06-27 14:25:30 -0700290 if (index != ARRAY_SIZE(package_parameters_) || argv[index + 3] != nullptr) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800291 LOG(ERROR) << "Wrong number of parameters";
292 return false;
293 }
294
295 return true;
296 }
297
298 void PrepareEnvironment() {
Andreas Gamped089ca12016-06-27 14:25:30 -0700299 environ_.push_back(StringPrintf("BOOTCLASSPATH=%s", boot_classpath_.c_str()));
300 environ_.push_back(StringPrintf("ANDROID_DATA=%s", GetOTADataDirectory().c_str()));
301 environ_.push_back(StringPrintf("ANDROID_ROOT=%s", android_root_.c_str()));
Andreas Gampe73dae112015-11-19 14:12:14 -0800302
303 for (const std::string& e : environ_) {
304 putenv(const_cast<char*>(e.c_str()));
305 }
306 }
307
308 // Ensure that we have the right boot image. The first time any app is
309 // compiled, we'll try to generate it.
Andreas Gamped089ca12016-06-27 14:25:30 -0700310 bool PrepareBootImage(bool force) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800311 if (package_parameters_[kISAIndex] == nullptr) {
312 LOG(ERROR) << "Instruction set missing.";
313 return false;
314 }
315 const char* isa = package_parameters_[kISAIndex];
316
317 // Check whether the file exists where expected.
Andreas Gamped089ca12016-06-27 14:25:30 -0700318 std::string dalvik_cache = GetOTADataDirectory() + "/" + DALVIK_CACHE;
Andreas Gampe73dae112015-11-19 14:12:14 -0800319 std::string isa_path = dalvik_cache + "/" + isa;
320 std::string art_path = isa_path + "/system@framework@boot.art";
321 std::string oat_path = isa_path + "/system@framework@boot.oat";
Andreas Gamped089ca12016-06-27 14:25:30 -0700322 bool cleared = false;
323 if (access(art_path.c_str(), F_OK) == 0 && access(oat_path.c_str(), F_OK) == 0) {
324 // Files exist, assume everything is alright if not forced. Otherwise clean up.
325 if (!force) {
326 return true;
327 }
328 ClearDirectory(isa_path);
329 cleared = true;
Andreas Gampe73dae112015-11-19 14:12:14 -0800330 }
331
Andreas Gamped089ca12016-06-27 14:25:30 -0700332 // Reset umask in otapreopt, so that we control the the access for the files we create.
333 umask(0);
334
Andreas Gampe73dae112015-11-19 14:12:14 -0800335 // Create the directories, if necessary.
336 if (access(dalvik_cache.c_str(), F_OK) != 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700337 if (!CreatePath(dalvik_cache)) {
338 PLOG(ERROR) << "Could not create dalvik-cache dir " << dalvik_cache;
Andreas Gampe73dae112015-11-19 14:12:14 -0800339 return false;
340 }
341 }
342 if (access(isa_path.c_str(), F_OK) != 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700343 if (!CreatePath(isa_path)) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800344 PLOG(ERROR) << "Could not create dalvik-cache isa dir";
345 return false;
346 }
347 }
348
Andreas Gampe5709b572016-02-12 17:42:59 -0800349 // Prepare to create.
Andreas Gamped089ca12016-06-27 14:25:30 -0700350 if (!cleared) {
351 ClearDirectory(isa_path);
352 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800353
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700354 std::string preopted_boot_art_path = StringPrintf("/system/framework/%s/boot.art", isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800355 if (access(preopted_boot_art_path.c_str(), F_OK) == 0) {
356 return PatchoatBootImage(art_path, isa);
357 } else {
358 // No preopted boot image. Try to compile.
Andreas Gamped089ca12016-06-27 14:25:30 -0700359 return Dex2oatBootImage(boot_classpath_, art_path, oat_path, isa);
Andreas Gampe5709b572016-02-12 17:42:59 -0800360 }
361 }
362
Andreas Gamped089ca12016-06-27 14:25:30 -0700363 static bool CreatePath(const std::string& path) {
364 // Create the given path. Use string processing instead of dirname, as dirname's need for
365 // a writable char buffer is painful.
366
367 // First, try to use the full path.
368 if (mkdir(path.c_str(), 0711) == 0) {
369 return true;
370 }
371 if (errno != ENOENT) {
372 PLOG(ERROR) << "Could not create path " << path;
373 return false;
374 }
375
376 // Now find the parent and try that first.
377 size_t last_slash = path.find_last_of('/');
378 if (last_slash == std::string::npos || last_slash == 0) {
379 PLOG(ERROR) << "Could not create " << path;
380 return false;
381 }
382
383 if (!CreatePath(path.substr(0, last_slash))) {
384 return false;
385 }
386
387 if (mkdir(path.c_str(), 0711) == 0) {
388 return true;
389 }
390 PLOG(ERROR) << "Could not create " << path;
391 return false;
392 }
393
394 static void ClearDirectory(const std::string& dir) {
395 DIR* c_dir = opendir(dir.c_str());
396 if (c_dir == nullptr) {
397 PLOG(WARNING) << "Unable to open " << dir << " to delete it's contents";
398 return;
399 }
400
401 for (struct dirent* de = readdir(c_dir); de != nullptr; de = readdir(c_dir)) {
402 const char* name = de->d_name;
403 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
404 continue;
405 }
406 // We only want to delete regular files and symbolic links.
407 std::string file = StringPrintf("%s/%s", dir.c_str(), name);
408 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
409 LOG(WARNING) << "Unexpected file "
410 << file
411 << " of type "
412 << std::hex
413 << de->d_type
414 << " encountered.";
415 } else {
416 // Try to unlink the file.
417 if (unlink(file.c_str()) != 0) {
418 PLOG(ERROR) << "Unable to unlink " << file;
419 }
420 }
421 }
422 CHECK_EQ(0, closedir(c_dir)) << "Unable to close directory.";
423 }
424
425 bool PatchoatBootImage(const std::string& art_path, const char* isa) const {
Andreas Gampe5709b572016-02-12 17:42:59 -0800426 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
427
428 std::vector<std::string> cmd;
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700429 cmd.push_back("/system/bin/patchoat");
Andreas Gampe5709b572016-02-12 17:42:59 -0800430
431 cmd.push_back("--input-image-location=/system/framework/boot.art");
432 cmd.push_back(StringPrintf("--output-image-file=%s", art_path.c_str()));
433
434 cmd.push_back(StringPrintf("--instruction-set=%s", isa));
435
436 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
437 ART_BASE_ADDRESS_MAX_DELTA);
Andreas Gampefebf0bf2016-02-29 18:04:17 -0800438 cmd.push_back(StringPrintf("--base-offset-delta=%d", base_offset));
Andreas Gampe5709b572016-02-12 17:42:59 -0800439
440 std::string error_msg;
441 bool result = Exec(cmd, &error_msg);
442 if (!result) {
443 LOG(ERROR) << "Could not generate boot image: " << error_msg;
444 }
445 return result;
446 }
447
448 bool Dex2oatBootImage(const std::string& boot_cp,
449 const std::string& art_path,
450 const std::string& oat_path,
Andreas Gamped089ca12016-06-27 14:25:30 -0700451 const char* isa) const {
Andreas Gampe73dae112015-11-19 14:12:14 -0800452 // This needs to be kept in sync with ART, see art/runtime/gc/space/image_space.cc.
453 std::vector<std::string> cmd;
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700454 cmd.push_back("/system/bin/dex2oat");
Andreas Gampe73dae112015-11-19 14:12:14 -0800455 cmd.push_back(StringPrintf("--image=%s", art_path.c_str()));
Andreas Gampe6db8db92016-06-03 10:22:19 -0700456 for (const std::string& boot_part : Split(boot_cp, ":")) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800457 cmd.push_back(StringPrintf("--dex-file=%s", boot_part.c_str()));
458 }
459 cmd.push_back(StringPrintf("--oat-file=%s", oat_path.c_str()));
460
461 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
462 ART_BASE_ADDRESS_MAX_DELTA);
463 cmd.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
464
465 cmd.push_back(StringPrintf("--instruction-set=%s", isa));
466
467 // These things are pushed by AndroidRuntime, see frameworks/base/core/jni/AndroidRuntime.cpp.
468 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xms",
469 "-Xms",
470 true,
471 cmd);
472 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-Xmx",
473 "-Xmx",
474 true,
475 cmd);
476 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-filter",
477 "--compiler-filter=",
478 false,
479 cmd);
Andreas Gampe9fb85b02016-03-16 10:09:29 -0700480 cmd.push_back("--image-classes=/system/etc/preloaded-classes");
Andreas Gampe73dae112015-11-19 14:12:14 -0800481 // TODO: Compiled-classes.
482 const std::string* extra_opts =
483 system_properties_.GetProperty("dalvik.vm.image-dex2oat-flags");
484 if (extra_opts != nullptr) {
Andreas Gampe6db8db92016-06-03 10:22:19 -0700485 std::vector<std::string> extra_vals = Split(*extra_opts, " ");
Andreas Gampe73dae112015-11-19 14:12:14 -0800486 cmd.insert(cmd.end(), extra_vals.begin(), extra_vals.end());
487 }
488 // TODO: Should we lower this? It's usually set close to max, because
489 // normally there's not much else going on at boot.
490 AddCompilerOptionFromSystemProperty("dalvik.vm.image-dex2oat-threads",
491 "-j",
492 false,
493 cmd);
494 AddCompilerOptionFromSystemProperty(
495 StringPrintf("dalvik.vm.isa.%s.variant", isa).c_str(),
496 "--instruction-set-variant=",
497 false,
498 cmd);
499 AddCompilerOptionFromSystemProperty(
500 StringPrintf("dalvik.vm.isa.%s.features", isa).c_str(),
501 "--instruction-set-features=",
502 false,
503 cmd);
504
505 std::string error_msg;
506 bool result = Exec(cmd, &error_msg);
507 if (!result) {
508 LOG(ERROR) << "Could not generate boot image: " << error_msg;
509 }
510 return result;
511 }
512
513 static const char* ParseNull(const char* arg) {
514 return (strcmp(arg, "!") == 0) ? nullptr : arg;
515 }
516
Andreas Gamped089ca12016-06-27 14:25:30 -0700517 bool ShouldSkipPreopt() const {
Andreas Gampe56f79f92016-06-08 15:11:37 -0700518 // There's one thing we have to be careful about: we may/will be asked to compile an app
519 // living in the system image. This may be a valid request - if the app wasn't compiled,
520 // e.g., if the system image wasn't large enough to include preopted files. However, the
521 // data we have is from the old system, so the driver (the OTA service) can't actually
522 // know. Thus, we will get requests for apps that have preopted components. To avoid
523 // duplication (we'd generate files that are not used and are *not* cleaned up), do two
524 // simple checks:
525 //
526 // 1) Does the apk_path start with the value of ANDROID_ROOT? (~in the system image)
527 // (For simplicity, assume the value of ANDROID_ROOT does not contain a symlink.)
528 //
529 // 2) If you replace the name in the apk_path with "oat," does the path exist?
530 // (=have a subdirectory for preopted files)
531 //
532 // If the answer to both is yes, skip the dexopt.
533 //
534 // Note: while one may think it's OK to call dexopt and it will fail (because APKs should
535 // be stripped), that's not true for APKs signed outside the build system (so the
536 // jar content must be exactly the same).
537
538 // (This is ugly as it's the only thing where we need to understand the contents
539 // of package_parameters_, but it beats postponing the decision or using the call-
540 // backs to do weird things.)
541 constexpr size_t kApkPathIndex = 0;
542 CHECK_GT(DEXOPT_PARAM_COUNT, kApkPathIndex);
543 CHECK(package_parameters_[kApkPathIndex] != nullptr);
Andreas Gamped089ca12016-06-27 14:25:30 -0700544 if (StartsWith(package_parameters_[kApkPathIndex], android_root_.c_str())) {
Andreas Gampe56f79f92016-06-08 15:11:37 -0700545 const char* last_slash = strrchr(package_parameters_[kApkPathIndex], '/');
546 if (last_slash != nullptr) {
547 std::string path(package_parameters_[kApkPathIndex],
548 last_slash - package_parameters_[kApkPathIndex] + 1);
549 CHECK(EndsWith(path, "/"));
550 path = path + "oat";
551 if (access(path.c_str(), F_OK) == 0) {
Andreas Gamped089ca12016-06-27 14:25:30 -0700552 return true;
Andreas Gampe56f79f92016-06-08 15:11:37 -0700553 }
554 }
555 }
556
Andreas Gamped089ca12016-06-27 14:25:30 -0700557 // Another issue is unavailability of files in the new system. If the partition
558 // layout changes, otapreopt_chroot may not know about this. Then files from that
559 // partition will not be available and fail to build. This is problematic, as
560 // this tool will wipe the OTA artifact cache and try again (for robustness after
561 // a failed OTA with remaining cache artifacts).
562 if (access(package_parameters_[kApkPathIndex], F_OK) != 0) {
563 LOG(WARNING) << "Skipping preopt of non-existing package "
564 << package_parameters_[kApkPathIndex];
565 return true;
566 }
567
568 return false;
569 }
570
571 int RunPreopt() {
572 if (ShouldSkipPreopt()) {
573 return 0;
574 }
575
576 int dexopt_result = dexopt(package_parameters_);
577 if (dexopt_result == 0) {
578 return 0;
579 }
580
581 // If the dexopt failed, we may have a stale boot image from a previous OTA run.
582 // Try to delete and retry.
583
584 if (!PrepareBootImage(/* force */ true)) {
585 LOG(ERROR) << "Forced boot image creating failed. Original error return was "
586 << dexopt_result;
587 return dexopt_result;
588 }
589
590 LOG(WARNING) << "Original dexopt failed, re-trying after boot image was regenerated.";
Andreas Gampe548bdb92016-06-02 17:56:45 -0700591 return dexopt(package_parameters_);
Andreas Gampe73dae112015-11-19 14:12:14 -0800592 }
593
594 ////////////////////////////////////
595 // Helpers, mostly taken from ART //
596 ////////////////////////////////////
597
598 // Wrapper on fork/execv to run a command in a subprocess.
Andreas Gamped089ca12016-06-27 14:25:30 -0700599 static bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) {
Andreas Gampe6db8db92016-06-03 10:22:19 -0700600 const std::string command_line = Join(arg_vector, ' ');
Andreas Gampe73dae112015-11-19 14:12:14 -0800601
602 CHECK_GE(arg_vector.size(), 1U) << command_line;
603
604 // Convert the args to char pointers.
605 const char* program = arg_vector[0].c_str();
606 std::vector<char*> args;
607 for (size_t i = 0; i < arg_vector.size(); ++i) {
608 const std::string& arg = arg_vector[i];
609 char* arg_str = const_cast<char*>(arg.c_str());
610 CHECK(arg_str != nullptr) << i;
611 args.push_back(arg_str);
612 }
613 args.push_back(nullptr);
614
615 // Fork and exec.
616 pid_t pid = fork();
617 if (pid == 0) {
618 // No allocation allowed between fork and exec.
619
620 // Change process groups, so we don't get reaped by ProcessManager.
621 setpgid(0, 0);
622
623 execv(program, &args[0]);
624
625 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
626 // _exit to avoid atexit handlers in child.
627 _exit(1);
628 } else {
629 if (pid == -1) {
630 *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
631 command_line.c_str(), strerror(errno));
632 return false;
633 }
634
635 // wait for subprocess to finish
636 int status;
637 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
638 if (got_pid != pid) {
639 *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
640 "wanted %d, got %d: %s",
641 command_line.c_str(), pid, got_pid, strerror(errno));
642 return false;
643 }
644 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
645 *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
646 command_line.c_str());
647 return false;
648 }
649 }
650 return true;
651 }
652
653 // Choose a random relocation offset. Taken from art/runtime/gc/image_space.cc.
654 static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
655 constexpr size_t kPageSize = PAGE_SIZE;
656 CHECK_EQ(min_delta % kPageSize, 0u);
657 CHECK_EQ(max_delta % kPageSize, 0u);
658 CHECK_LT(min_delta, max_delta);
659
660 std::default_random_engine generator;
661 generator.seed(GetSeed());
662 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
663 int32_t r = distribution(generator);
664 if (r % 2 == 0) {
665 r = RoundUp(r, kPageSize);
666 } else {
667 r = RoundDown(r, kPageSize);
668 }
669 CHECK_LE(min_delta, r);
670 CHECK_GE(max_delta, r);
671 CHECK_EQ(r % kPageSize, 0u);
672 return r;
673 }
674
675 static uint64_t GetSeed() {
676#ifdef __BIONIC__
677 // Bionic exposes arc4random, use it.
678 uint64_t random_data;
679 arc4random_buf(&random_data, sizeof(random_data));
680 return random_data;
681#else
682#error "This is only supposed to run with bionic. Otherwise, implement..."
683#endif
684 }
685
686 void AddCompilerOptionFromSystemProperty(const char* system_property,
687 const char* prefix,
688 bool runtime,
Andreas Gamped089ca12016-06-27 14:25:30 -0700689 std::vector<std::string>& out) const {
690 const std::string* value = system_properties_.GetProperty(system_property);
Andreas Gampe73dae112015-11-19 14:12:14 -0800691 if (value != nullptr) {
692 if (runtime) {
693 out.push_back("--runtime-arg");
694 }
695 if (prefix != nullptr) {
696 out.push_back(StringPrintf("%s%s", prefix, value->c_str()));
697 } else {
698 out.push_back(*value);
699 }
700 }
701 }
702
Andreas Gamped089ca12016-06-27 14:25:30 -0700703 static constexpr const char* kBootClassPathPropertyName = "BOOTCLASSPATH";
704 static constexpr const char* kAndroidRootPathPropertyName = "ANDROID_ROOT";
705 static constexpr const char* kAndroidDataPathPropertyName = "ANDROID_DATA";
706 // The index of the instruction-set string inside the package parameters. Needed for
707 // some special-casing that requires knowledge of the instruction-set.
708 static constexpr size_t kISAIndex = 3;
709
Andreas Gampe73dae112015-11-19 14:12:14 -0800710 // Stores the system properties read out of the B partition. We need to use these properties
711 // to compile, instead of the A properties we could get from init/get_property.
712 SystemProperties system_properties_;
713
Andreas Gamped089ca12016-06-27 14:25:30 -0700714 // Some select properties that are always needed.
715 std::string target_slot_;
716 std::string android_root_;
717 std::string android_data_;
718 std::string boot_classpath_;
719 std::string asec_mountpoint_;
720
Andreas Gampe548bdb92016-06-02 17:56:45 -0700721 const char* package_parameters_[DEXOPT_PARAM_COUNT];
Andreas Gampe73dae112015-11-19 14:12:14 -0800722
723 // Store environment values we need to set.
724 std::vector<std::string> environ_;
725};
726
727OTAPreoptService gOps;
728
729////////////////////////
730// Plug-in functions. //
731////////////////////////
732
733int get_property(const char *key, char *value, const char *default_value) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800734 return gOps.GetProperty(key, value, default_value);
735}
736
737// Compute the output path of
738bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir,
739 const char *apk_path,
740 const char *instruction_set) {
Dan Austin9c8f93a2016-06-03 16:15:54 -0700741 const char *file_name_start;
742 const char *file_name_end;
Andreas Gampe73dae112015-11-19 14:12:14 -0800743
744 file_name_start = strrchr(apk_path, '/');
745 if (file_name_start == nullptr) {
746 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
747 return false;
748 }
749 file_name_end = strrchr(file_name_start, '.');
750 if (file_name_end == nullptr) {
751 ALOGE("apk_path '%s' has no extension\n", apk_path);
752 return false;
753 }
754
755 // Calculate file_name
756 file_name_start++; // Move past '/', is valid as file_name_end is valid.
757 size_t file_name_len = file_name_end - file_name_start;
758 std::string file_name(file_name_start, file_name_len);
759
760 // <apk_parent_dir>/oat/<isa>/<file_name>.odex.b
Andreas Gamped089ca12016-06-27 14:25:30 -0700761 snprintf(path,
762 PKG_PATH_MAX,
763 "%s/%s/%s.odex.%s",
764 oat_dir,
765 instruction_set,
766 file_name.c_str(),
767 gOps.GetTargetSlot().c_str());
Andreas Gampe73dae112015-11-19 14:12:14 -0800768 return true;
769}
770
771/*
772 * Computes the odex file for the given apk_path and instruction_set.
773 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
774 *
775 * Returns false if it failed to determine the odex file path.
776 */
777bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
778 const char *instruction_set) {
Andreas Gampe73dae112015-11-19 14:12:14 -0800779 const char *path_end = strrchr(apk_path, '/');
780 if (path_end == nullptr) {
781 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
782 return false;
783 }
784 std::string path_component(apk_path, path_end - apk_path);
785
786 const char *name_begin = path_end + 1;
787 const char *extension_start = strrchr(name_begin, '.');
788 if (extension_start == nullptr) {
789 ALOGE("apk_path '%s' has no extension.\n", apk_path);
790 return false;
791 }
792 std::string name_component(name_begin, extension_start - name_begin);
793
Andreas Gamped089ca12016-06-27 14:25:30 -0700794 std::string new_path = StringPrintf("%s/oat/%s/%s.odex.%s",
Andreas Gampe73dae112015-11-19 14:12:14 -0800795 path_component.c_str(),
796 instruction_set,
Andreas Gamped089ca12016-06-27 14:25:30 -0700797 name_component.c_str(),
798 gOps.GetTargetSlot().c_str());
799 if (new_path.length() >= PKG_PATH_MAX) {
800 LOG(ERROR) << "apk_path of " << apk_path << " is too long: " << new_path;
801 return false;
802 }
Andreas Gampe73dae112015-11-19 14:12:14 -0800803 strcpy(path, new_path.c_str());
804 return true;
805}
806
807bool create_cache_path(char path[PKG_PATH_MAX],
808 const char *src,
809 const char *instruction_set) {
810 size_t srclen = strlen(src);
811
812 /* demand that we are an absolute path */
813 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
814 return false;
815 }
816
817 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
818 return false;
819 }
820
821 std::string from_src = std::string(src + 1);
822 std::replace(from_src.begin(), from_src.end(), '/', '@');
823
824 std::string assembled_path = StringPrintf("%s/%s/%s/%s%s",
Andreas Gamped089ca12016-06-27 14:25:30 -0700825 gOps.GetOTADataDirectory().c_str(),
Andreas Gampe73dae112015-11-19 14:12:14 -0800826 DALVIK_CACHE,
827 instruction_set,
828 from_src.c_str(),
829 DALVIK_CACHE_POSTFIX2);
830
831 if (assembled_path.length() + 1 > PKG_PATH_MAX) {
832 return false;
833 }
834 strcpy(path, assembled_path.c_str());
835
836 return true;
837}
838
Andreas Gampe73dae112015-11-19 14:12:14 -0800839static int log_callback(int type, const char *fmt, ...) {
840 va_list ap;
841 int priority;
842
843 switch (type) {
844 case SELINUX_WARNING:
845 priority = ANDROID_LOG_WARN;
846 break;
847 case SELINUX_INFO:
848 priority = ANDROID_LOG_INFO;
849 break;
850 default:
851 priority = ANDROID_LOG_ERROR;
852 break;
853 }
854 va_start(ap, fmt);
855 LOG_PRI_VA(priority, "SELinux", fmt, ap);
856 va_end(ap);
857 return 0;
858}
859
860static int otapreopt_main(const int argc, char *argv[]) {
861 int selinux_enabled = (is_selinux_enabled() > 0);
862
863 setenv("ANDROID_LOG_TAGS", "*:v", 1);
864 android::base::InitLogging(argv);
865
Andreas Gampe73dae112015-11-19 14:12:14 -0800866 if (argc < 2) {
867 ALOGE("Expecting parameters");
868 exit(1);
869 }
870
871 union selinux_callback cb;
872 cb.func_log = log_callback;
873 selinux_set_callback(SELINUX_CB_LOG, cb);
874
Andreas Gampe73dae112015-11-19 14:12:14 -0800875 if (selinux_enabled && selinux_status_open(true) < 0) {
876 ALOGE("Could not open selinux status; exiting.\n");
877 exit(1);
878 }
879
880 int ret = android::installd::gOps.Main(argc, argv);
881
882 return ret;
883}
884
885} // namespace installd
886} // namespace android
887
888int main(const int argc, char *argv[]) {
889 return android::installd::otapreopt_main(argc, argv);
890}