blob: 2d1965d9231cd2ebae924015aae871cfda09ffcd [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080018#include <sys/capability.h>
Elliott Hughes2ead70c2015-02-16 10:44:22 -080019#include <sys/file.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070020#include "installd.h"
Brian Carlstrom0378aaf2014-08-08 00:52:22 -070021#include <cutils/sched_policy.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070022#include <diskusage/dirsize.h>
23#include <selinux/android.h>
Igor Murashkin9e87a802014-11-05 15:21:12 -080024#include <system/thread_defs.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
26/* Directory records that are used in execution of commands. */
27dir_rec_t android_data_dir;
28dir_rec_t android_asec_dir;
29dir_rec_t android_app_dir;
30dir_rec_t android_app_private_dir;
31dir_rec_t android_app_lib_dir;
32dir_rec_t android_media_dir;
Jeff Sharkeye23a1322015-04-06 16:19:39 -070033dir_rec_t android_mnt_expand_dir;
Mike Lockwood94afecf2012-10-24 10:45:23 -070034dir_rec_array_t android_system_dirs;
35
Jeff Sharkeyc03de092015-04-07 18:14:05 -070036int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070037{
Mike Lockwood94afecf2012-10-24 10:45:23 -070038 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
39 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
40 return -1;
41 }
42
Jeff Sharkeyc03de092015-04-07 18:14:05 -070043 std::string _pkgdir(create_package_data_path(uuid, pkgname, 0));
44 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070045
Nick Kralevicha2d838a2013-01-09 16:00:35 -080046 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070047 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
48 return -1;
49 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080050 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070051 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
52 unlink(pkgdir);
53 return -1;
54 }
55
Stephen Smalley26288202014-02-07 09:16:46 -050056 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070057 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070058 unlink(pkgdir);
59 return -errno;
60 }
61
62 if (chown(pkgdir, uid, gid) < 0) {
63 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -070064 unlink(pkgdir);
65 return -1;
66 }
67
68 return 0;
69}
70
Jeff Sharkeyc03de092015-04-07 18:14:05 -070071int uninstall(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -070072{
Jeff Sharkeyc03de092015-04-07 18:14:05 -070073 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
74 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -070075
Dave Allisond9370732014-01-30 14:19:23 -080076 remove_profile_file(pkgname);
77
Mike Lockwood94afecf2012-10-24 10:45:23 -070078 /* delete contents AND directory, no exceptions */
79 return delete_dir_contents(pkgdir, 1, NULL);
80}
81
82int renamepkg(const char *oldpkgname, const char *newpkgname)
83{
84 char oldpkgdir[PKG_PATH_MAX];
85 char newpkgdir[PKG_PATH_MAX];
86
87 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
88 return -1;
89 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
90 return -1;
91
92 if (rename(oldpkgdir, newpkgdir) < 0) {
93 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
94 return -errno;
95 }
96 return 0;
97}
98
Jeff Sharkeyc03de092015-04-07 18:14:05 -070099int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700100{
Mike Lockwood94afecf2012-10-24 10:45:23 -0700101 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102
103 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
104 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
105 return -1;
106 }
107
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700108 std::string _pkgdir(create_package_data_path(uuid, pkgname, 0));
109 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700110
111 if (stat(pkgdir, &s) < 0) return -1;
112
113 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700114 ALOGE("fixing uid of non-root pkg: %s %" PRIu32 " %" PRIu32 "\n", pkgdir, s.st_uid, s.st_gid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115 return -1;
116 }
117
118 if (chmod(pkgdir, 0751) < 0) {
119 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
120 unlink(pkgdir);
121 return -errno;
122 }
123 if (chown(pkgdir, uid, gid) < 0) {
124 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
125 unlink(pkgdir);
126 return -errno;
127 }
128
129 return 0;
130}
131
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700132int delete_user_data(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700133{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700134 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
135 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700136
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700137 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138}
139
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700140int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700141{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700142 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
143 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700144
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800145 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700146 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
147 return -errno;
148 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800149 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700150 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
151 unlink(pkgdir);
152 return -errno;
153 }
154
Stephen Smalley26288202014-02-07 09:16:46 -0500155 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700156 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700157 unlink(pkgdir);
158 return -errno;
159 }
160
161 if (chown(pkgdir, uid, uid) < 0) {
162 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700163 unlink(pkgdir);
164 return -errno;
165 }
166
167 return 0;
168}
169
Robin Lee7c8bec02014-06-10 18:46:26 +0100170int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700171{
Robin Lee095c7632014-04-25 15:05:19 +0100172 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700173 return -1;
174 }
175
176 return 0;
177}
178
Robin Lee095c7632014-04-25 15:05:19 +0100179int delete_user(userid_t userid)
180{
181 int status = 0;
182
183 char data_path[PKG_PATH_MAX];
184 if ((create_user_path(data_path, userid) != 0)
185 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
186 status = -1;
187 }
188
189 char media_path[PATH_MAX];
190 if ((create_user_media_path(media_path, userid) != 0)
191 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
192 status = -1;
193 }
194
195 char config_path[PATH_MAX];
196 if ((create_user_config_path(config_path, userid) != 0)
197 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
198 status = -1;
199 }
200
201 return status;
202}
203
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700204int delete_cache(const char *uuid, const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700205{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700206 std::string _cachedir(
207 create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX);
208 const char* cachedir = _cachedir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700209
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700210 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100211 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700212}
213
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700214int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid)
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700215{
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700216 std::string _codecachedir(
217 create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX);
218 const char* codecachedir = _codecachedir.c_str();
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700219
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700220 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700221
Jeff Sharkey770180a2014-09-08 17:14:26 -0700222 /* it's okay if code cache is missing */
223 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
224 return 0;
225 }
226
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700227 /* delete contents, not the directory, no exceptions */
228 return delete_dir_contents(codecachedir, 0, NULL);
229}
230
Mike Lockwood94afecf2012-10-24 10:45:23 -0700231/* Try to ensure free_size bytes of storage are available.
232 * Returns 0 on success.
233 * This is rather simple-minded because doing a full LRU would
234 * be potentially memory-intensive, and without atime it would
235 * also require that apps constantly modify file metadata even
236 * when just reading from the cache, which is pretty awful.
237 */
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700238// TODO: extend to know about other volumes
Mike Lockwood94afecf2012-10-24 10:45:23 -0700239int free_cache(int64_t free_size)
240{
241 cache_t* cache;
242 int64_t avail;
243 DIR *d;
244 struct dirent *de;
245 char tmpdir[PATH_MAX];
246 char *dirpos;
247
248 avail = data_disk_free();
249 if (avail < 0) return -1;
250
251 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
252 if (avail >= free_size) return 0;
253
254 cache = start_cache_collection();
255
256 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700257 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 //ALOGI("adding cache files from %s\n", tmpdir);
259 add_cache_files(cache, tmpdir, "cache");
260 }
261
262 // Search for other users and add any cache files from them.
263 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
264 SECONDARY_USER_PREFIX);
265 dirpos = tmpdir + strlen(tmpdir);
266 d = opendir(tmpdir);
267 if (d != NULL) {
268 while ((de = readdir(d))) {
269 if (de->d_type == DT_DIR) {
270 const char *name = de->d_name;
271 /* always skip "." and ".." */
272 if (name[0] == '.') {
273 if (name[1] == 0) continue;
274 if ((name[1] == '.') && (name[2] == 0)) continue;
275 }
276 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
277 strcpy(dirpos, name);
278 //ALOGI("adding cache files from %s\n", tmpdir);
279 add_cache_files(cache, tmpdir, "cache");
280 } else {
281 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
282 }
283 }
284 }
285 closedir(d);
286 }
287
288 // Collect cache files on external storage for all users (if it is mounted as part
289 // of the internal storage).
290 strcpy(tmpdir, android_media_dir.path);
291 dirpos = tmpdir + strlen(tmpdir);
292 d = opendir(tmpdir);
293 if (d != NULL) {
294 while ((de = readdir(d))) {
295 if (de->d_type == DT_DIR) {
296 const char *name = de->d_name;
297 /* skip any dir that doesn't start with a number, so not a user */
298 if (name[0] < '0' || name[0] > '9') {
299 continue;
300 }
301 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
302 strcpy(dirpos, name);
303 if (lookup_media_dir(tmpdir, "Android") == 0
304 && lookup_media_dir(tmpdir, "data") == 0) {
305 //ALOGI("adding cache files from %s\n", tmpdir);
306 add_cache_files(cache, tmpdir, "cache");
307 }
308 } else {
309 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
310 }
311 }
312 }
313 closedir(d);
314 }
315
316 clear_cache_files(cache, free_size);
317 finish_cache_collection(cache);
318
319 return data_disk_free() >= free_size ? 0 : -1;
320}
321
Narayan Kamath1b400322014-04-11 13:17:00 +0100322int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700323{
324 char src_dex[PKG_PATH_MAX];
325 char dst_dex[PKG_PATH_MAX];
326
Jeff Sharkey770180a2014-09-08 17:14:26 -0700327 if (validate_apk_path(src)) {
328 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
329 return -1;
330 }
331 if (validate_apk_path(dst)) {
332 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
333 return -1;
334 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700335
Narayan Kamath1b400322014-04-11 13:17:00 +0100336 if (create_cache_path(src_dex, src, instruction_set)) return -1;
337 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700338
339 ALOGV("move %s -> %s\n", src_dex, dst_dex);
340 if (rename(src_dex, dst_dex) < 0) {
341 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
342 return -1;
343 } else {
344 return 0;
345 }
346}
347
Narayan Kamath1b400322014-04-11 13:17:00 +0100348int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700349{
350 char dex_path[PKG_PATH_MAX];
351
Jeff Sharkey770180a2014-09-08 17:14:26 -0700352 if (validate_apk_path(path) && validate_system_app_path(path)) {
353 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
354 return -1;
355 }
356
Narayan Kamath1b400322014-04-11 13:17:00 +0100357 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700358
359 ALOGV("unlink %s\n", dex_path);
360 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700361 if (errno != ENOENT) {
362 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
363 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700364 return -1;
365 } else {
366 return 0;
367 }
368}
369
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700370int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700371 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100372 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
373 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700374{
375 DIR *d;
376 int dfd;
377 struct dirent *de;
378 struct stat s;
379 char path[PKG_PATH_MAX];
380
381 int64_t codesize = 0;
382 int64_t datasize = 0;
383 int64_t cachesize = 0;
384 int64_t asecsize = 0;
385
386 /* count the source apk as code -- but only if it's not
387 * on the /system partition and its not on the sdcard.
388 */
389 if (validate_system_app_path(apkpath) &&
390 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
391 if (stat(apkpath, &s) == 0) {
392 codesize += stat_size(&s);
393 }
394 }
395 /* count the forward locked apk as code if it is given
396 */
397 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
398 if (stat(fwdlock_apkpath, &s) == 0) {
399 codesize += stat_size(&s);
400 }
401 }
402 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100403 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700404 if (stat(path, &s) == 0) {
405 codesize += stat_size(&s);
406 }
407 }
408
409 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700410 if (libdirpath != NULL && libdirpath[0] != '!') {
411 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700412 if (d != NULL) {
413 dfd = dirfd(d);
414 codesize += calculate_dir_size(dfd);
415 closedir(d);
416 }
417 }
418
419 /* compute asec size if it is given
420 */
421 if (asecpath != NULL && asecpath[0] != '!') {
422 if (stat(asecpath, &s) == 0) {
423 asecsize += stat_size(&s);
424 }
425 }
426
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700427 std::string _pkgdir(create_package_data_path(uuid, pkgname, userid));
428 const char* pkgdir = _pkgdir.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700429
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700430 d = opendir(pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700431 if (d == NULL) {
432 goto done;
433 }
434 dfd = dirfd(d);
435
436 /* most stuff in the pkgdir is data, except for the "cache"
437 * directory and below, which is cache, and the "lib" directory
438 * and below, which is code...
439 */
440 while ((de = readdir(d))) {
441 const char *name = de->d_name;
442
443 if (de->d_type == DT_DIR) {
444 int subfd;
445 int64_t statsize = 0;
446 int64_t dirsize = 0;
447 /* always skip "." and ".." */
448 if (name[0] == '.') {
449 if (name[1] == 0) continue;
450 if ((name[1] == '.') && (name[2] == 0)) continue;
451 }
452 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
453 statsize = stat_size(&s);
454 }
455 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
456 if (subfd >= 0) {
457 dirsize = calculate_dir_size(subfd);
458 }
459 if(!strcmp(name,"lib")) {
460 codesize += dirsize + statsize;
461 } else if(!strcmp(name,"cache")) {
462 cachesize += dirsize + statsize;
463 } else {
464 datasize += dirsize + statsize;
465 }
466 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
467 // This is the symbolic link to the application's library
468 // code. We'll count this as code instead of data, since
469 // it is not something that the app creates.
470 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
471 codesize += stat_size(&s);
472 }
473 } else {
474 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
475 datasize += stat_size(&s);
476 }
477 }
478 }
479 closedir(d);
480done:
481 *_codesize = codesize;
482 *_datasize = datasize;
483 *_cachesize = cachesize;
484 *_asecsize = asecsize;
485 return 0;
486}
487
Narayan Kamath1b400322014-04-11 13:17:00 +0100488int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700489{
490 char *tmp;
491 int srclen;
492 int dstlen;
493
494 srclen = strlen(src);
495
496 /* demand that we are an absolute path */
497 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
498 return -1;
499 }
500
501 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
502 return -1;
503 }
504
Dave Allisond9370732014-01-30 14:19:23 -0800505 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100506 strlen(instruction_set) +
507 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800508
Mike Lockwood94afecf2012-10-24 10:45:23 -0700509 if (dstlen > PKG_PATH_MAX) {
510 return -1;
511 }
512
Narayan Kamath1b400322014-04-11 13:17:00 +0100513 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700514 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100515 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700516 src + 1, /* skip the leading / */
517 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800518
Narayan Kamath1b400322014-04-11 13:17:00 +0100519 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700520 if (*tmp == '/') {
521 *tmp = '@';
522 }
523 }
524
525 return 0;
526}
527
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700528static int split_count(const char *str)
529{
530 char *ctx;
531 int count = 0;
532 char buf[PROPERTY_VALUE_MAX];
533
534 strncpy(buf, str, sizeof(buf));
535 char *pBuf = buf;
536
537 while(strtok_r(pBuf, " ", &ctx) != NULL) {
538 count++;
539 pBuf = NULL;
540 }
541
542 return count;
543}
544
neo.chae14e084d2015-01-07 18:46:13 +0900545static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700546{
547 char *ctx;
548 int count = 0;
549 char *tok;
550 char *pBuf = buf;
551
552 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
553 argv[count++] = tok;
554 pBuf = NULL;
555 }
556
557 return count;
558}
559
Alex Light7365a102014-07-21 12:23:48 -0700560static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700561 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700562{
563 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100564 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700565
566 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
567 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
568 ALOGE("Instruction set %s longer than max length of %d",
569 instruction_set, MAX_INSTRUCTION_SET_LEN);
570 return;
571 }
572
573 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
574 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
575 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
576 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
577 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
578 // The caller has already gotten all the locks we need.
579 const char* no_lock_arg = "--no-lock-output";
580 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
581 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
582 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700583 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700584 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
585
586 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
587 char* argv[7];
588 argv[0] = (char*) PATCHOAT_BIN;
589 argv[1] = (char*) patched_image_location_arg;
590 argv[2] = (char*) no_lock_arg;
591 argv[3] = instruction_set_arg;
592 argv[4] = output_oat_fd_arg;
593 argv[5] = input_oat_fd_arg;
594 argv[6] = NULL;
595
596 execv(PATCHOAT_BIN, (char* const *)argv);
597 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
598}
599
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700600static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Andreas Gampee1c01352014-12-10 16:41:11 -0800601 const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set,
Andreas Gampe598c25e2015-03-03 09:15:06 -0800602 bool vm_safe_mode, bool debuggable)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700603{
Calin Juravle8fc73152014-08-19 18:48:50 +0100604 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
605
606 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
607 ALOGE("Instruction set %s longer than max length of %d",
608 instruction_set, MAX_INSTRUCTION_SET_LEN);
609 return;
610 }
611
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700612 char prop_buf[PROPERTY_VALUE_MAX];
613 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
614
615 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
616 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
617
618 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
619 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
620
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700621 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
622 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
623 dex2oat_compiler_filter_flag, NULL) > 0;
624
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700625 char dex2oat_threads_buf[PROPERTY_VALUE_MAX];
626 bool have_dex2oat_threads_flag = property_get("dalvik.vm.dex2oat-threads", dex2oat_threads_buf,
627 NULL) > 0;
628 char dex2oat_threads_arg[PROPERTY_VALUE_MAX + 2];
629 if (have_dex2oat_threads_flag) {
630 sprintf(dex2oat_threads_arg, "-j%s", dex2oat_threads_buf);
631 }
632
Calin Juravle8fc73152014-08-19 18:48:50 +0100633 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
634 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
635 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
636 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
637 dex2oat_isa_features, NULL) > 0;
638
Ian Rogers16a95b22014-11-08 16:58:13 -0800639 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
640 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
641 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
642 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
643 dex2oat_isa_variant, NULL) > 0;
644
neo.chae14e084d2015-01-07 18:46:13 +0900645 const char *dex2oat_norelocation = "-Xnorelocate";
646 bool have_dex2oat_relocation_skip_flag = false;
647
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800648 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700649 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
650 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800651 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
652
Brian Carlstrom538998f2014-07-30 14:37:11 -0700653 // If we booting without the real /data, don't spend time compiling.
654 char vold_decrypt[PROPERTY_VALUE_MAX];
655 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
656 bool skip_compilation = (have_vold_decrypt &&
657 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
658 (strcmp(vold_decrypt, "1") == 0)));
659
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700660 char use_jit_property[PROPERTY_VALUE_MAX];
661 bool have_jit_property = property_get("debug.usejit", use_jit_property, NULL) > 0;
662 bool use_jit = have_jit_property && strcmp(use_jit_property, "true") == 0;
663
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700664 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700665
Brian Carlstrom53e07762014-06-27 14:15:19 -0700666 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700667
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700668 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100669
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700670 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
671 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
672 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700673 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100674 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800675 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100676 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100677 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
678 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700679 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
680 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700681 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800682 bool have_dex2oat_swap_fd = false;
683 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700684
685 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
686 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
687 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
688 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100689 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800690 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100691 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800692 if (swap_fd >= 0) {
693 have_dex2oat_swap_fd = true;
694 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
695 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100696
Calin Juravle4fdff462014-06-06 16:58:43 +0100697 bool have_profile_file = false;
698 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100699 if (profiler && (strcmp(pkgname, "*") != 0)) {
700 char profile_file[PKG_PATH_MAX];
701 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800702 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100703 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100704 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100705 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100706 have_profile_file = true;
707 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
708 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
709 "--top-k-profile-threshold=%s", prop_buf);
710 have_top_k_profile_threshold = true;
711 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100712 }
Dave Allisond9370732014-01-30 14:19:23 -0800713 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700714
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700715 if (have_dex2oat_Xms_flag) {
716 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
717 }
718 if (have_dex2oat_Xmx_flag) {
719 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
720 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700721 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700722 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700723 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900724 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100725 } else if (vm_safe_mode) {
726 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100727 have_dex2oat_compiler_filter_flag = true;
Mathieu Chartierd4a7b452015-03-20 15:39:47 -0700728 } else if (use_jit) {
729 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-at-runtime");
730 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700731 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700732 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
733 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700734
Andreas Gampe598c25e2015-03-03 09:15:06 -0800735 // Check whether all apps should be compiled debuggable.
736 if (!debuggable) {
737 debuggable =
738 (property_get("dalvik.vm.always_debuggable", prop_buf, "0") > 0) &&
739 (prop_buf[0] == '1');
740 }
741
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700742 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100743
neo.chae14e084d2015-01-07 18:46:13 +0900744 const char* argv[7 // program name, mandatory arguments and the final NULL
745 + (have_dex2oat_isa_variant ? 1 : 0)
746 + (have_dex2oat_isa_features ? 1 : 0)
747 + (have_profile_file ? 1 : 0)
748 + (have_top_k_profile_threshold ? 1 : 0)
749 + (have_dex2oat_Xms_flag ? 2 : 0)
750 + (have_dex2oat_Xmx_flag ? 2 : 0)
751 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700752 + (have_dex2oat_threads_flag ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900753 + (have_dex2oat_swap_fd ? 1 : 0)
754 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
Andreas Gampe598c25e2015-03-03 09:15:06 -0800755 + (debuggable ? 1 : 0)
neo.chae14e084d2015-01-07 18:46:13 +0900756 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100757 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900758 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100759 argv[i++] = zip_fd_arg;
760 argv[i++] = zip_location_arg;
761 argv[i++] = oat_fd_arg;
762 argv[i++] = oat_location_arg;
763 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800764 if (have_dex2oat_isa_variant) {
765 argv[i++] = instruction_set_variant_arg;
766 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100767 if (have_dex2oat_isa_features) {
768 argv[i++] = instruction_set_features_arg;
769 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100770 if (have_profile_file) {
771 argv[i++] = profile_file_arg;
772 }
773 if (have_top_k_profile_threshold) {
774 argv[i++] = top_k_profile_threshold_arg;
775 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700776 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900777 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700778 argv[i++] = dex2oat_Xms_arg;
779 }
780 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900781 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700782 argv[i++] = dex2oat_Xmx_arg;
783 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700784 if (have_dex2oat_compiler_filter_flag) {
785 argv[i++] = dex2oat_compiler_filter_arg;
786 }
Andreas Gampe8d7af8b2015-03-30 18:45:03 -0700787 if (have_dex2oat_threads_flag) {
788 argv[i++] = dex2oat_threads_arg;
789 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800790 if (have_dex2oat_swap_fd) {
791 argv[i++] = dex2oat_swap_fd;
792 }
Andreas Gampe598c25e2015-03-03 09:15:06 -0800793 if (debuggable) {
794 argv[i++] = "--debuggable";
795 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700796 if (dex2oat_flags_count) {
797 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100798 }
neo.chae14e084d2015-01-07 18:46:13 +0900799 if (have_dex2oat_relocation_skip_flag) {
800 argv[i++] = RUNTIME_ARG;
801 argv[i++] = dex2oat_norelocation;
802 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700803 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100804 argv[i] = NULL;
805
neo.chae14e084d2015-01-07 18:46:13 +0900806 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700807 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700808}
809
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100810static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700811{
812 int status;
813 pid_t got_pid;
814
Mike Lockwood94afecf2012-10-24 10:45:23 -0700815 while (1) {
816 got_pid = waitpid(pid, &status, 0);
817 if (got_pid == -1 && errno == EINTR) {
818 printf("waitpid interrupted, retrying\n");
819 } else {
820 break;
821 }
822 }
823 if (got_pid != pid) {
824 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
825 (int) pid, (int) got_pid, strerror(errno));
826 return 1;
827 }
828
829 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700830 return 0;
831 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700832 return status; /* always nonzero */
833 }
834}
835
Andreas Gampee1c01352014-12-10 16:41:11 -0800836/*
837 * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this
838 * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do
839 * this on a low-mem device.
840 */
841static bool kAlwaysProvideSwapFile = true;
842
843static bool ShouldUseSwapFileForDexopt() {
844 if (kAlwaysProvideSwapFile) {
845 return true;
846 }
847
848 char low_mem_buf[PROPERTY_VALUE_MAX];
849 property_get("ro.config.low_ram", low_mem_buf, "");
850 return (strcmp(low_mem_buf, "true") == 0);
851}
852
Richard Uhler009b8772015-03-18 12:39:09 -0700853/*
854 * Computes the odex file for the given apk_path and instruction_set.
855 * /system/framework/whatever.jar -> /system/framework/oat/<isa>/whatever.odex
856 *
857 * Returns false if it failed to determine the odex file path.
858 */
859static bool calculate_odex_file_path(char path[PKG_PATH_MAX],
860 const char *apk_path,
861 const char *instruction_set)
862{
863 if (strlen(apk_path) + strlen("oat/") + strlen(instruction_set)
864 + strlen("/") + strlen("odex") + 1 > PKG_PATH_MAX) {
865 ALOGE("apk_path '%s' may be too long to form odex file path.\n", apk_path);
866 return false;
867 }
868
869 strcpy(path, apk_path);
870 char *end = strrchr(path, '/');
871 if (end == NULL) {
872 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
873 return false;
874 }
875 const char *apk_end = apk_path + (end - path); // strrchr(apk_path, '/');
876
877 strcpy(end + 1, "oat/"); // path = /system/framework/oat/\0
878 strcat(path, instruction_set); // path = /system/framework/oat/<isa>\0
879 strcat(path, apk_end); // path = /system/framework/oat/<isa>/whatever.jar\0
880 end = strrchr(path, '.');
881 if (end == NULL) {
882 ALOGE("apk_path '%s' has no extension.\n", apk_path);
883 return false;
884 }
885 strcpy(end + 1, "odex");
886 return true;
887}
888
Calin Juravleb1efac12014-08-21 19:05:20 +0100889int dexopt(const char *apk_path, uid_t uid, bool is_public,
Richard Uhlerc92fb622015-03-26 15:47:38 -0700890 const char *pkgname, const char *instruction_set, int dexopt_needed,
891 bool vm_safe_mode, bool debuggable, const char* oat_dir)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700892{
893 struct utimbuf ut;
Fyodor Kupolov26ff93c2015-04-02 16:59:10 -0700894 struct stat input_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700895 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800896 char swap_file_name[PKG_PATH_MAX];
Alex Light7365a102014-07-21 12:23:48 -0700897 const char *input_file;
898 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800899 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700900
Andreas Gampee1c01352014-12-10 16:41:11 -0800901 // Early best-effort check whether we can fit the the path into our buffers.
902 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
903 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700904 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800905 ALOGE("apk_path too long '%s'\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700906 return -1;
907 }
908
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800909 if (oat_dir != NULL && oat_dir[0] != '!') {
910 if (validate_apk_path(oat_dir)) {
911 ALOGE("invalid oat_dir '%s'\n", oat_dir);
912 return -1;
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800913 }
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -0800914 if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
915 return -1;
916 }
917 } else {
918 if (create_cache_path(out_path, apk_path, instruction_set)) {
919 return -1;
920 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700921 }
922
Richard Uhlerc92fb622015-03-26 15:47:38 -0700923 switch (dexopt_needed) {
924 case DEXOPT_DEX2OAT_NEEDED:
925 input_file = apk_path;
926 break;
927
928 case DEXOPT_PATCHOAT_NEEDED:
929 if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
930 return -1;
931 }
932 input_file = in_odex_path;
933 break;
934
935 case DEXOPT_SELF_PATCHOAT_NEEDED:
936 input_file = out_path;
937 break;
938
939 default:
940 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
941 exit(72);
Alex Light7365a102014-07-21 12:23:48 -0700942 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700943
Alex Light7365a102014-07-21 12:23:48 -0700944 memset(&input_stat, 0, sizeof(input_stat));
945 stat(input_file, &input_stat);
946
947 input_fd = open(input_file, O_RDONLY, 0);
948 if (input_fd < 0) {
949 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700950 return -1;
951 }
952
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700953 unlink(out_path);
954 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
955 if (out_fd < 0) {
956 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700957 goto fail;
958 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700959 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700960 S_IRUSR|S_IWUSR|S_IRGRP |
961 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700962 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700963 goto fail;
964 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700965 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
966 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700967 goto fail;
968 }
969
Dave Allisond9370732014-01-30 14:19:23 -0800970 // Create profile file if there is a package name present.
971 if (strcmp(pkgname, "*") != 0) {
972 create_profile_file(pkgname, uid);
973 }
974
Andreas Gampee1c01352014-12-10 16:41:11 -0800975 // Create a swap file if necessary.
Richard Uhlerc92fb622015-03-26 15:47:38 -0700976 if (ShouldUseSwapFileForDexopt()) {
Andreas Gampee1c01352014-12-10 16:41:11 -0800977 // Make sure there really is enough space.
978 size_t out_len = strlen(out_path);
979 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
980 strcpy(swap_file_name, out_path);
981 strcpy(swap_file_name + strlen(out_path), ".swap");
982 unlink(swap_file_name);
983 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
984 if (swap_fd < 0) {
985 // Could not create swap file. Optimistically go on and hope that we can compile
986 // without it.
987 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
988 } else {
989 // Immediately unlink. We don't really want to hit flash.
990 unlink(swap_file_name);
991 }
992 } else {
993 // Swap file path is too long. Try to run without.
994 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
995 }
996 }
Dave Allisond9370732014-01-30 14:19:23 -0800997
Alex Light7365a102014-07-21 12:23:48 -0700998 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700999
1000 pid_t pid;
1001 pid = fork();
1002 if (pid == 0) {
1003 /* child -- drop privileges before continuing */
1004 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001005 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001006 exit(64);
1007 }
1008 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001009 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001010 exit(65);
1011 }
1012 // drop capabilities
1013 struct __user_cap_header_struct capheader;
1014 struct __user_cap_data_struct capdata[2];
1015 memset(&capheader, 0, sizeof(capheader));
1016 memset(&capdata, 0, sizeof(capdata));
1017 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1018 if (capset(&capheader, &capdata[0]) < 0) {
1019 ALOGE("capset failed: %s\n", strerror(errno));
1020 exit(66);
1021 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001022 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1023 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1024 exit(70);
1025 }
Igor Murashkin9e87a802014-11-05 15:21:12 -08001026 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
1027 ALOGE("setpriority failed: %s\n", strerror(errno));
1028 exit(71);
1029 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001030 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1031 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001032 exit(67);
1033 }
1034
Richard Uhlerc92fb622015-03-26 15:47:38 -07001035 if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
1036 || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
Andreas Gampebd872e42014-12-15 11:41:11 -08001037 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001038 } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001039 const char *input_file_name = strrchr(input_file, '/');
1040 if (input_file_name == NULL) {
1041 input_file_name = input_file;
1042 } else {
1043 input_file_name++;
1044 }
1045 run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
1046 instruction_set, vm_safe_mode, debuggable);
Richard Uhlerc92fb622015-03-26 15:47:38 -07001047 } else {
1048 ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
1049 exit(73);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001050 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001051 exit(68); /* only get here on exec failure */
1052 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001053 res = wait_child(pid);
1054 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001055 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001056 } else {
Alex Light7365a102014-07-21 12:23:48 -07001057 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001058 goto fail;
1059 }
1060 }
1061
Alex Light7365a102014-07-21 12:23:48 -07001062 ut.actime = input_stat.st_atime;
1063 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001064 utime(out_path, &ut);
1065
1066 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001067 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001068 if (swap_fd != -1) {
1069 close(swap_fd);
1070 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001071 return 0;
1072
1073fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001074 if (out_fd >= 0) {
1075 close(out_fd);
1076 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001077 }
Alex Light7365a102014-07-21 12:23:48 -07001078 if (input_fd >= 0) {
1079 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001080 }
1081 return -1;
1082}
1083
Narayan Kamath091ea772014-11-10 15:03:46 +00001084int mark_boot_complete(const char* instruction_set)
1085{
1086 char boot_marker_path[PKG_PATH_MAX];
1087 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1088
1089 ALOGV("mark_boot_complete : %s", boot_marker_path);
1090 if (unlink(boot_marker_path) != 0) {
1091 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1092 strerror(errno));
1093 return -1;
1094 }
1095
1096 return 0;
1097}
1098
Mike Lockwood94afecf2012-10-24 10:45:23 -07001099void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1100 struct stat* statbuf)
1101{
1102 while (path[basepos] != 0) {
1103 if (path[basepos] == '/') {
1104 path[basepos] = 0;
1105 if (lstat(path, statbuf) < 0) {
1106 ALOGV("Making directory: %s\n", path);
1107 if (mkdir(path, mode) == 0) {
1108 chown(path, uid, gid);
1109 } else {
1110 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1111 }
1112 }
1113 path[basepos] = '/';
1114 basepos++;
1115 }
1116 basepos++;
1117 }
1118}
1119
1120int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1121 int dstuid, int dstgid, struct stat* statbuf)
1122{
1123 DIR *d;
1124 struct dirent *de;
1125 int res;
1126
1127 int srcend = strlen(srcpath);
1128 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001129
Mike Lockwood94afecf2012-10-24 10:45:23 -07001130 if (lstat(srcpath, statbuf) < 0) {
1131 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1132 return 1;
1133 }
Dave Allisond9370732014-01-30 14:19:23 -08001134
Mike Lockwood94afecf2012-10-24 10:45:23 -07001135 if ((statbuf->st_mode&S_IFDIR) == 0) {
1136 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1137 dstuid, dstgid, statbuf);
1138 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1139 if (rename(srcpath, dstpath) >= 0) {
1140 if (chown(dstpath, dstuid, dstgid) < 0) {
1141 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1142 unlink(dstpath);
1143 return 1;
1144 }
1145 } else {
1146 ALOGW("Unable to rename %s to %s: %s\n",
1147 srcpath, dstpath, strerror(errno));
1148 return 1;
1149 }
1150 return 0;
1151 }
1152
1153 d = opendir(srcpath);
1154 if (d == NULL) {
1155 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1156 return 1;
1157 }
1158
1159 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001160
Mike Lockwood94afecf2012-10-24 10:45:23 -07001161 while ((de = readdir(d))) {
1162 const char *name = de->d_name;
1163 /* always skip "." and ".." */
1164 if (name[0] == '.') {
1165 if (name[1] == 0) continue;
1166 if ((name[1] == '.') && (name[2] == 0)) continue;
1167 }
Dave Allisond9370732014-01-30 14:19:23 -08001168
Mike Lockwood94afecf2012-10-24 10:45:23 -07001169 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1170 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1171 continue;
1172 }
Dave Allisond9370732014-01-30 14:19:23 -08001173
Mike Lockwood94afecf2012-10-24 10:45:23 -07001174 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1175 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1176 continue;
1177 }
Dave Allisond9370732014-01-30 14:19:23 -08001178
Mike Lockwood94afecf2012-10-24 10:45:23 -07001179 srcpath[srcend] = dstpath[dstend] = '/';
1180 strcpy(srcpath+srcend+1, name);
1181 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001182
Mike Lockwood94afecf2012-10-24 10:45:23 -07001183 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1184 res = 1;
1185 }
Dave Allisond9370732014-01-30 14:19:23 -08001186
Mike Lockwood94afecf2012-10-24 10:45:23 -07001187 // Note: we will be leaving empty directories behind in srcpath,
1188 // but that is okay, the package manager will be erasing all of the
1189 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001190
Mike Lockwood94afecf2012-10-24 10:45:23 -07001191 srcpath[srcend] = dstpath[dstend] = 0;
1192 }
Dave Allisond9370732014-01-30 14:19:23 -08001193
Mike Lockwood94afecf2012-10-24 10:45:23 -07001194 closedir(d);
1195 return res;
1196}
1197
1198int movefiles()
1199{
1200 DIR *d;
1201 int dfd, subfd;
1202 struct dirent *de;
1203 struct stat s;
1204 char buf[PKG_PATH_MAX+1];
1205 int bufp, bufe, bufi, readlen;
1206
1207 char srcpkg[PKG_NAME_MAX];
1208 char dstpkg[PKG_NAME_MAX];
1209 char srcpath[PKG_PATH_MAX];
1210 char dstpath[PKG_PATH_MAX];
1211 int dstuid=-1, dstgid=-1;
1212 int hasspace;
1213
1214 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1215 if (d == NULL) {
1216 goto done;
1217 }
1218 dfd = dirfd(d);
1219
1220 /* Iterate through all files in the directory, executing the
1221 * file movements requested there-in.
1222 */
1223 while ((de = readdir(d))) {
1224 const char *name = de->d_name;
1225
1226 if (de->d_type == DT_DIR) {
1227 continue;
1228 } else {
1229 subfd = openat(dfd, name, O_RDONLY);
1230 if (subfd < 0) {
1231 ALOGW("Unable to open update commands at %s%s\n",
1232 UPDATE_COMMANDS_DIR_PREFIX, name);
1233 continue;
1234 }
Dave Allisond9370732014-01-30 14:19:23 -08001235
Mike Lockwood94afecf2012-10-24 10:45:23 -07001236 bufp = 0;
1237 bufe = 0;
1238 buf[PKG_PATH_MAX] = 0;
1239 srcpkg[0] = dstpkg[0] = 0;
1240 while (1) {
1241 bufi = bufp;
1242 while (bufi < bufe && buf[bufi] != '\n') {
1243 bufi++;
1244 }
1245 if (bufi < bufe) {
1246 buf[bufi] = 0;
1247 ALOGV("Processing line: %s\n", buf+bufp);
1248 hasspace = 0;
1249 while (bufp < bufi && isspace(buf[bufp])) {
1250 hasspace = 1;
1251 bufp++;
1252 }
1253 if (buf[bufp] == '#' || bufp == bufi) {
1254 // skip comments and empty lines.
1255 } else if (hasspace) {
1256 if (dstpkg[0] == 0) {
1257 ALOGW("Path before package line in %s%s: %s\n",
1258 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1259 } else if (srcpkg[0] == 0) {
1260 // Skip -- source package no longer exists.
1261 } else {
1262 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1263 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1264 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1265 movefileordir(srcpath, dstpath,
1266 strlen(dstpath)-strlen(buf+bufp),
1267 dstuid, dstgid, &s);
1268 }
1269 }
1270 } else {
1271 char* div = strchr(buf+bufp, ':');
1272 if (div == NULL) {
1273 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1274 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1275 } else {
1276 *div = 0;
1277 div++;
1278 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1279 strcpy(dstpkg, buf+bufp);
1280 } else {
1281 srcpkg[0] = dstpkg[0] = 0;
1282 ALOGW("Package name too long in %s%s: %s\n",
1283 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1284 }
1285 if (strlen(div) < PKG_NAME_MAX) {
1286 strcpy(srcpkg, div);
1287 } else {
1288 srcpkg[0] = dstpkg[0] = 0;
1289 ALOGW("Package name too long in %s%s: %s\n",
1290 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1291 }
1292 if (srcpkg[0] != 0) {
1293 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1294 if (lstat(srcpath, &s) < 0) {
1295 // Package no longer exists -- skip.
1296 srcpkg[0] = 0;
1297 }
1298 } else {
1299 srcpkg[0] = 0;
1300 ALOGW("Can't create path %s in %s%s\n",
1301 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1302 }
1303 if (srcpkg[0] != 0) {
1304 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1305 if (lstat(dstpath, &s) == 0) {
1306 dstuid = s.st_uid;
1307 dstgid = s.st_gid;
1308 } else {
1309 // Destination package doesn't
1310 // exist... due to original-package,
1311 // this is normal, so don't be
1312 // noisy about it.
1313 srcpkg[0] = 0;
1314 }
1315 } else {
1316 srcpkg[0] = 0;
1317 ALOGW("Can't create path %s in %s%s\n",
1318 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1319 }
1320 }
1321 ALOGV("Transfering from %s to %s: uid=%d\n",
1322 srcpkg, dstpkg, dstuid);
1323 }
1324 }
1325 }
1326 bufp = bufi+1;
1327 } else {
1328 if (bufp == 0) {
1329 if (bufp < bufe) {
1330 ALOGW("Line too long in %s%s, skipping: %s\n",
1331 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1332 }
1333 } else if (bufp < bufe) {
1334 memcpy(buf, buf+bufp, bufe-bufp);
1335 bufe -= bufp;
1336 bufp = 0;
1337 }
1338 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1339 if (readlen < 0) {
1340 ALOGW("Failure reading update commands in %s%s: %s\n",
1341 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1342 break;
1343 } else if (readlen == 0) {
1344 break;
1345 }
1346 bufe += readlen;
1347 buf[bufe] = 0;
1348 ALOGV("Read buf: %s\n", buf);
1349 }
1350 }
1351 close(subfd);
1352 }
1353 }
1354 closedir(d);
1355done:
1356 return 0;
1357}
1358
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001359int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int userId)
Mike Lockwood94afecf2012-10-24 10:45:23 -07001360{
Mike Lockwood94afecf2012-10-24 10:45:23 -07001361 struct stat s, libStat;
1362 int rc = 0;
1363
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001364 std::string _pkgdir(create_package_data_path(uuid, pkgname, userId));
1365 std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX);
1366
1367 const char* pkgdir = _pkgdir.c_str();
1368 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001369
1370 if (stat(pkgdir, &s) < 0) return -1;
1371
1372 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1373 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1374 return -1;
1375 }
1376
1377 if (chmod(pkgdir, 0700) < 0) {
1378 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1379 rc = -1;
1380 goto out;
1381 }
1382
1383 if (lstat(libsymlink, &libStat) < 0) {
1384 if (errno != ENOENT) {
1385 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1386 rc = -1;
1387 goto out;
1388 }
1389 } else {
1390 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001391 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001392 rc = -1;
1393 goto out;
1394 }
1395 } else if (S_ISLNK(libStat.st_mode)) {
1396 if (unlink(libsymlink) < 0) {
1397 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1398 rc = -1;
1399 goto out;
1400 }
1401 }
1402 }
1403
1404 if (symlink(asecLibDir, libsymlink) < 0) {
1405 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1406 strerror(errno));
1407 rc = -errno;
1408 goto out;
1409 }
1410
1411out:
1412 if (chmod(pkgdir, s.st_mode) < 0) {
1413 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1414 rc = -errno;
1415 }
1416
1417 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1418 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1419 return -errno;
1420 }
1421
1422 return rc;
1423}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001424
1425static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1426{
1427 static const char *IDMAP_BIN = "/system/bin/idmap";
1428 static const size_t MAX_INT_LEN = 32;
1429 char idmap_str[MAX_INT_LEN];
1430
1431 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1432
1433 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1434 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1435}
1436
1437// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1438// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1439static int flatten_path(const char *prefix, const char *suffix,
1440 const char *overlay_path, char *idmap_path, size_t N)
1441{
1442 if (overlay_path == NULL || idmap_path == NULL) {
1443 return -1;
1444 }
1445 const size_t len_overlay_path = strlen(overlay_path);
1446 // will access overlay_path + 1 further below; requires absolute path
1447 if (len_overlay_path < 2 || *overlay_path != '/') {
1448 return -1;
1449 }
1450 const size_t len_idmap_root = strlen(prefix);
1451 const size_t len_suffix = strlen(suffix);
1452 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1453 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1454 // additions below would cause overflow
1455 return -1;
1456 }
1457 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1458 return -1;
1459 }
1460 memset(idmap_path, 0, N);
1461 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1462 char *ch = idmap_path + len_idmap_root;
1463 while (*ch != '\0') {
1464 if (*ch == '/') {
1465 *ch = '@';
1466 }
1467 ++ch;
1468 }
1469 return 0;
1470}
1471
1472int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1473{
1474 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1475
1476 int idmap_fd = -1;
1477 char idmap_path[PATH_MAX];
1478
1479 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1480 idmap_path, sizeof(idmap_path)) == -1) {
1481 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1482 goto fail;
1483 }
1484
1485 unlink(idmap_path);
1486 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1487 if (idmap_fd < 0) {
1488 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1489 goto fail;
1490 }
1491 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1492 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1493 goto fail;
1494 }
1495 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1496 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1497 goto fail;
1498 }
1499
1500 pid_t pid;
1501 pid = fork();
1502 if (pid == 0) {
1503 /* child -- drop privileges before continuing */
1504 if (setgid(uid) != 0) {
1505 ALOGE("setgid(%d) failed during idmap\n", uid);
1506 exit(1);
1507 }
1508 if (setuid(uid) != 0) {
1509 ALOGE("setuid(%d) failed during idmap\n", uid);
1510 exit(1);
1511 }
1512 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1513 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1514 exit(1);
1515 }
1516
1517 run_idmap(target_apk, overlay_apk, idmap_fd);
1518 exit(1); /* only if exec call to idmap failed */
1519 } else {
1520 int status = wait_child(pid);
1521 if (status != 0) {
1522 ALOGE("idmap failed, status=0x%04x\n", status);
1523 goto fail;
1524 }
1525 }
1526
1527 close(idmap_fd);
1528 return 0;
1529fail:
1530 if (idmap_fd >= 0) {
1531 close(idmap_fd);
1532 unlink(idmap_path);
1533 }
1534 return -1;
1535}
Robert Craige9887e42014-02-20 10:25:56 -05001536
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001537// TODO: extend to know about other volumes
Andreas Gampe0ad7a112015-04-09 09:52:45 -07001538int restorecon_data(const char* uuid __attribute__((unused)), const char* pkgName,
1539 const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001540{
Robert Craigda30dc72014-03-27 10:21:12 -04001541 struct dirent *entry;
1542 DIR *d;
1543 struct stat s;
1544 char *userdir;
1545 char *primarydir;
1546 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001547 int ret = 0;
1548
Robert Craigda30dc72014-03-27 10:21:12 -04001549 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1550 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1551
1552 if (!pkgName || !seinfo) {
1553 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001554 return -1;
1555 }
1556
Robert Craigda30dc72014-03-27 10:21:12 -04001557 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1558 return -1;
1559 }
1560
1561 // Relabel for primary user.
1562 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1563 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001564 ret |= -1;
1565 }
1566
Robert Craigda30dc72014-03-27 10:21:12 -04001567 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1568 free(primarydir);
1569 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001570 }
1571
Robert Craigda30dc72014-03-27 10:21:12 -04001572 // Relabel package directory for all secondary users.
1573 d = opendir(userdir);
1574 if (d == NULL) {
1575 free(primarydir);
1576 free(userdir);
1577 return -1;
1578 }
1579
1580 while ((entry = readdir(d))) {
1581 if (entry->d_type != DT_DIR) {
1582 continue;
1583 }
1584
1585 const char *user = entry->d_name;
1586 // Ignore "." and ".."
1587 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1588 continue;
1589 }
1590
1591 // user directories start with a number
1592 if (user[0] < '0' || user[0] > '9') {
1593 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1594 continue;
1595 }
1596
1597 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1598 continue;
1599 }
1600
1601 if (stat(pkgdir, &s) < 0) {
1602 free(pkgdir);
1603 continue;
1604 }
1605
Stephen Smalley8ac2a642014-09-08 15:51:55 -04001606 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, s.st_uid, flags) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001607 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1608 ret |= -1;
1609 }
1610 free(pkgdir);
1611 }
1612
1613 closedir(d);
1614 free(primarydir);
1615 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001616 return ret;
1617}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001618
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001619int create_oat_dir(const char* oat_dir, const char* instruction_set)
1620{
1621 char oat_instr_dir[PKG_PATH_MAX];
1622
1623 if (validate_apk_path(oat_dir)) {
1624 ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir);
1625 return -1;
1626 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001627 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001628 return -1;
1629 }
1630 if (selinux_android_restorecon(oat_dir, 0)) {
1631 ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno));
1632 return -1;
1633 }
1634 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001635 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001636 return -1;
1637 }
1638 return 0;
1639}
1640
1641int rm_package_dir(const char* apk_path)
1642{
1643 if (validate_apk_path(apk_path)) {
1644 ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path);
1645 return -1;
1646 }
1647 return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */);
1648}
1649
1650int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
1651 const char *instruction_set) {
1652 char *file_name_start;
1653 char *file_name_end;
1654
1655 file_name_start = strrchr(apk_path, '/');
1656 if (file_name_start == NULL) {
1657 ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
1658 return -1;
1659 }
1660 file_name_end = strrchr(apk_path, '.');
1661 if (file_name_end < file_name_start) {
1662 ALOGE("apk_path '%s' has no extension\n", apk_path);
1663 return -1;
1664 }
1665
1666 // Calculate file_name
1667 int file_name_len = file_name_end - file_name_start - 1;
1668 char file_name[file_name_len + 1];
1669 memcpy(file_name, file_name_start + 1, file_name_len);
1670 file_name[file_name_len] = '\0';
1671
1672 // <apk_parent_dir>/oat/<isa>/<file_name>.odex
1673 snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
1674 return 0;
1675}