blob: 386d2f33af35c685bfe922d25f49fbafc7a8f467 [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>
24
25/* Directory records that are used in execution of commands. */
26dir_rec_t android_data_dir;
27dir_rec_t android_asec_dir;
28dir_rec_t android_app_dir;
29dir_rec_t android_app_private_dir;
30dir_rec_t android_app_lib_dir;
31dir_rec_t android_media_dir;
32dir_rec_array_t android_system_dirs;
33
Robert Craig4d3fd4e2013-03-25 06:33:03 -040034int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070035{
36 char pkgdir[PKG_PATH_MAX];
37 char libsymlink[PKG_PATH_MAX];
38 char applibdir[PKG_PATH_MAX];
39 struct stat libStat;
40
41 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
42 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
43 return -1;
44 }
45
46 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
47 ALOGE("cannot create package path\n");
48 return -1;
49 }
50
51 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
52 ALOGE("cannot create package lib symlink origin path\n");
53 return -1;
54 }
55
56 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
57 ALOGE("cannot create package lib symlink dest path\n");
58 return -1;
59 }
60
Nick Kralevicha2d838a2013-01-09 16:00:35 -080061 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070062 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
63 return -1;
64 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080065 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070066 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
67 unlink(pkgdir);
68 return -1;
69 }
70
71 if (lstat(libsymlink, &libStat) < 0) {
72 if (errno != ENOENT) {
73 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
74 return -1;
75 }
76 } else {
77 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +010078 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070079 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
80 return -1;
81 }
82 } else if (S_ISLNK(libStat.st_mode)) {
83 if (unlink(libsymlink) < 0) {
84 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
85 return -1;
86 }
87 }
88 }
89
Stephen Smalley26288202014-02-07 09:16:46 -050090 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070091 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
92 unlink(libsymlink);
93 unlink(pkgdir);
94 return -errno;
95 }
96
Stephen Smalley3a983892014-05-13 12:53:07 -040097 if (symlink(applibdir, libsymlink) < 0) {
98 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
99 strerror(errno));
100 unlink(pkgdir);
101 return -1;
102 }
103
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104 if (chown(pkgdir, uid, gid) < 0) {
105 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
106 unlink(libsymlink);
107 unlink(pkgdir);
108 return -1;
109 }
110
111 return 0;
112}
113
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700114int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115{
116 char pkgdir[PKG_PATH_MAX];
117
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700118 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119 return -1;
120
Dave Allisond9370732014-01-30 14:19:23 -0800121 remove_profile_file(pkgname);
122
Mike Lockwood94afecf2012-10-24 10:45:23 -0700123 /* delete contents AND directory, no exceptions */
124 return delete_dir_contents(pkgdir, 1, NULL);
125}
126
127int renamepkg(const char *oldpkgname, const char *newpkgname)
128{
129 char oldpkgdir[PKG_PATH_MAX];
130 char newpkgdir[PKG_PATH_MAX];
131
132 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
133 return -1;
134 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
135 return -1;
136
137 if (rename(oldpkgdir, newpkgdir) < 0) {
138 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
139 return -errno;
140 }
141 return 0;
142}
143
144int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
145{
146 char pkgdir[PKG_PATH_MAX];
147 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700148
149 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
150 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
151 return -1;
152 }
153
154 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
155 ALOGE("cannot create package path\n");
156 return -1;
157 }
158
159 if (stat(pkgdir, &s) < 0) return -1;
160
161 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700162 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 -0700163 return -1;
164 }
165
166 if (chmod(pkgdir, 0751) < 0) {
167 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
168 unlink(pkgdir);
169 return -errno;
170 }
171 if (chown(pkgdir, uid, gid) < 0) {
172 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
173 unlink(pkgdir);
174 return -errno;
175 }
176
177 return 0;
178}
179
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700180int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181{
182 char pkgdir[PKG_PATH_MAX];
183
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700184 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700185 return -1;
186
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700187 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188}
189
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700190int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700191{
192 char pkgdir[PKG_PATH_MAX];
193 char applibdir[PKG_PATH_MAX];
194 char libsymlink[PKG_PATH_MAX];
195 struct stat libStat;
196
197 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700198 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700199 return -1;
200 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700201 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700202 ALOGE("cannot create package lib symlink origin path\n");
203 return -1;
204 }
205 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
206 ALOGE("cannot create package lib symlink dest path\n");
207 return -1;
208 }
209
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800210 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700211 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
212 return -errno;
213 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800214 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
216 unlink(pkgdir);
217 return -errno;
218 }
219
220 if (lstat(libsymlink, &libStat) < 0) {
221 if (errno != ENOENT) {
222 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
223 unlink(pkgdir);
224 return -1;
225 }
226 } else {
227 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100228 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700229 ALOGE("couldn't delete lib directory during install for non-primary: %s",
230 libsymlink);
231 unlink(pkgdir);
232 return -1;
233 }
234 } else if (S_ISLNK(libStat.st_mode)) {
235 if (unlink(libsymlink) < 0) {
236 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
237 libsymlink);
238 unlink(pkgdir);
239 return -1;
240 }
241 }
242 }
243
Stephen Smalley26288202014-02-07 09:16:46 -0500244 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700245 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
246 unlink(libsymlink);
247 unlink(pkgdir);
248 return -errno;
249 }
250
Stephen Smalley3a983892014-05-13 12:53:07 -0400251 if (symlink(applibdir, libsymlink) < 0) {
252 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
253 applibdir, strerror(errno));
254 unlink(pkgdir);
255 return -1;
256 }
257
Mike Lockwood94afecf2012-10-24 10:45:23 -0700258 if (chown(pkgdir, uid, uid) < 0) {
259 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
260 unlink(libsymlink);
261 unlink(pkgdir);
262 return -errno;
263 }
264
265 return 0;
266}
267
Robin Lee7c8bec02014-06-10 18:46:26 +0100268int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700269{
Robin Lee095c7632014-04-25 15:05:19 +0100270 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700271 return -1;
272 }
273
274 return 0;
275}
276
Robin Lee095c7632014-04-25 15:05:19 +0100277int delete_user(userid_t userid)
278{
279 int status = 0;
280
281 char data_path[PKG_PATH_MAX];
282 if ((create_user_path(data_path, userid) != 0)
283 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
284 status = -1;
285 }
286
287 char media_path[PATH_MAX];
288 if ((create_user_media_path(media_path, userid) != 0)
289 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
290 status = -1;
291 }
292
293 char config_path[PATH_MAX];
294 if ((create_user_config_path(config_path, userid) != 0)
295 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
296 status = -1;
297 }
298
299 return status;
300}
301
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700302int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700303{
304 char cachedir[PKG_PATH_MAX];
305
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700306 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307 return -1;
308
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700309 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100310 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311}
312
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700313int delete_code_cache(const char *pkgname, userid_t userid)
314{
315 char codecachedir[PKG_PATH_MAX];
Jeff Sharkey770180a2014-09-08 17:14:26 -0700316 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700317
318 if (create_pkg_path(codecachedir, pkgname, CODE_CACHE_DIR_POSTFIX, userid))
319 return -1;
320
Jeff Sharkey770180a2014-09-08 17:14:26 -0700321 /* it's okay if code cache is missing */
322 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
323 return 0;
324 }
325
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700326 /* delete contents, not the directory, no exceptions */
327 return delete_dir_contents(codecachedir, 0, NULL);
328}
329
Mike Lockwood94afecf2012-10-24 10:45:23 -0700330/* Try to ensure free_size bytes of storage are available.
331 * Returns 0 on success.
332 * This is rather simple-minded because doing a full LRU would
333 * be potentially memory-intensive, and without atime it would
334 * also require that apps constantly modify file metadata even
335 * when just reading from the cache, which is pretty awful.
336 */
337int free_cache(int64_t free_size)
338{
339 cache_t* cache;
340 int64_t avail;
341 DIR *d;
342 struct dirent *de;
343 char tmpdir[PATH_MAX];
344 char *dirpos;
345
346 avail = data_disk_free();
347 if (avail < 0) return -1;
348
349 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
350 if (avail >= free_size) return 0;
351
352 cache = start_cache_collection();
353
354 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700355 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700356 //ALOGI("adding cache files from %s\n", tmpdir);
357 add_cache_files(cache, tmpdir, "cache");
358 }
359
360 // Search for other users and add any cache files from them.
361 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
362 SECONDARY_USER_PREFIX);
363 dirpos = tmpdir + strlen(tmpdir);
364 d = opendir(tmpdir);
365 if (d != NULL) {
366 while ((de = readdir(d))) {
367 if (de->d_type == DT_DIR) {
368 const char *name = de->d_name;
369 /* always skip "." and ".." */
370 if (name[0] == '.') {
371 if (name[1] == 0) continue;
372 if ((name[1] == '.') && (name[2] == 0)) continue;
373 }
374 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
375 strcpy(dirpos, name);
376 //ALOGI("adding cache files from %s\n", tmpdir);
377 add_cache_files(cache, tmpdir, "cache");
378 } else {
379 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
380 }
381 }
382 }
383 closedir(d);
384 }
385
386 // Collect cache files on external storage for all users (if it is mounted as part
387 // of the internal storage).
388 strcpy(tmpdir, android_media_dir.path);
389 dirpos = tmpdir + strlen(tmpdir);
390 d = opendir(tmpdir);
391 if (d != NULL) {
392 while ((de = readdir(d))) {
393 if (de->d_type == DT_DIR) {
394 const char *name = de->d_name;
395 /* skip any dir that doesn't start with a number, so not a user */
396 if (name[0] < '0' || name[0] > '9') {
397 continue;
398 }
399 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
400 strcpy(dirpos, name);
401 if (lookup_media_dir(tmpdir, "Android") == 0
402 && lookup_media_dir(tmpdir, "data") == 0) {
403 //ALOGI("adding cache files from %s\n", tmpdir);
404 add_cache_files(cache, tmpdir, "cache");
405 }
406 } else {
407 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
408 }
409 }
410 }
411 closedir(d);
412 }
413
414 clear_cache_files(cache, free_size);
415 finish_cache_collection(cache);
416
417 return data_disk_free() >= free_size ? 0 : -1;
418}
419
Narayan Kamath1b400322014-04-11 13:17:00 +0100420int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700421{
422 char src_dex[PKG_PATH_MAX];
423 char dst_dex[PKG_PATH_MAX];
424
Jeff Sharkey770180a2014-09-08 17:14:26 -0700425 if (validate_apk_path(src)) {
426 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
427 return -1;
428 }
429 if (validate_apk_path(dst)) {
430 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
431 return -1;
432 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700433
Narayan Kamath1b400322014-04-11 13:17:00 +0100434 if (create_cache_path(src_dex, src, instruction_set)) return -1;
435 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700436
437 ALOGV("move %s -> %s\n", src_dex, dst_dex);
438 if (rename(src_dex, dst_dex) < 0) {
439 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
440 return -1;
441 } else {
442 return 0;
443 }
444}
445
Narayan Kamath1b400322014-04-11 13:17:00 +0100446int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700447{
448 char dex_path[PKG_PATH_MAX];
449
Jeff Sharkey770180a2014-09-08 17:14:26 -0700450 if (validate_apk_path(path) && validate_system_app_path(path)) {
451 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
452 return -1;
453 }
454
Narayan Kamath1b400322014-04-11 13:17:00 +0100455 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700456
457 ALOGV("unlink %s\n", dex_path);
458 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700459 if (errno != ENOENT) {
460 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
461 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700462 return -1;
463 } else {
464 return 0;
465 }
466}
467
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700468int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700469 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100470 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
471 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700472{
473 DIR *d;
474 int dfd;
475 struct dirent *de;
476 struct stat s;
477 char path[PKG_PATH_MAX];
478
479 int64_t codesize = 0;
480 int64_t datasize = 0;
481 int64_t cachesize = 0;
482 int64_t asecsize = 0;
483
484 /* count the source apk as code -- but only if it's not
485 * on the /system partition and its not on the sdcard.
486 */
487 if (validate_system_app_path(apkpath) &&
488 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
489 if (stat(apkpath, &s) == 0) {
490 codesize += stat_size(&s);
491 }
492 }
493 /* count the forward locked apk as code if it is given
494 */
495 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
496 if (stat(fwdlock_apkpath, &s) == 0) {
497 codesize += stat_size(&s);
498 }
499 }
500 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100501 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700502 if (stat(path, &s) == 0) {
503 codesize += stat_size(&s);
504 }
505 }
506
507 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700508 if (libdirpath != NULL && libdirpath[0] != '!') {
509 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700510 if (d != NULL) {
511 dfd = dirfd(d);
512 codesize += calculate_dir_size(dfd);
513 closedir(d);
514 }
515 }
516
517 /* compute asec size if it is given
518 */
519 if (asecpath != NULL && asecpath[0] != '!') {
520 if (stat(asecpath, &s) == 0) {
521 asecsize += stat_size(&s);
522 }
523 }
524
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700525 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700526 goto done;
527 }
528
529 d = opendir(path);
530 if (d == NULL) {
531 goto done;
532 }
533 dfd = dirfd(d);
534
535 /* most stuff in the pkgdir is data, except for the "cache"
536 * directory and below, which is cache, and the "lib" directory
537 * and below, which is code...
538 */
539 while ((de = readdir(d))) {
540 const char *name = de->d_name;
541
542 if (de->d_type == DT_DIR) {
543 int subfd;
544 int64_t statsize = 0;
545 int64_t dirsize = 0;
546 /* always skip "." and ".." */
547 if (name[0] == '.') {
548 if (name[1] == 0) continue;
549 if ((name[1] == '.') && (name[2] == 0)) continue;
550 }
551 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
552 statsize = stat_size(&s);
553 }
554 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
555 if (subfd >= 0) {
556 dirsize = calculate_dir_size(subfd);
557 }
558 if(!strcmp(name,"lib")) {
559 codesize += dirsize + statsize;
560 } else if(!strcmp(name,"cache")) {
561 cachesize += dirsize + statsize;
562 } else {
563 datasize += dirsize + statsize;
564 }
565 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
566 // This is the symbolic link to the application's library
567 // code. We'll count this as code instead of data, since
568 // it is not something that the app creates.
569 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
570 codesize += stat_size(&s);
571 }
572 } else {
573 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
574 datasize += stat_size(&s);
575 }
576 }
577 }
578 closedir(d);
579done:
580 *_codesize = codesize;
581 *_datasize = datasize;
582 *_cachesize = cachesize;
583 *_asecsize = asecsize;
584 return 0;
585}
586
Narayan Kamath1b400322014-04-11 13:17:00 +0100587int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700588{
589 char *tmp;
590 int srclen;
591 int dstlen;
592
593 srclen = strlen(src);
594
595 /* demand that we are an absolute path */
596 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
597 return -1;
598 }
599
600 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
601 return -1;
602 }
603
Dave Allisond9370732014-01-30 14:19:23 -0800604 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100605 strlen(instruction_set) +
606 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800607
Mike Lockwood94afecf2012-10-24 10:45:23 -0700608 if (dstlen > PKG_PATH_MAX) {
609 return -1;
610 }
611
Narayan Kamath1b400322014-04-11 13:17:00 +0100612 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700613 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100614 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700615 src + 1, /* skip the leading / */
616 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800617
Narayan Kamath1b400322014-04-11 13:17:00 +0100618 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700619 if (*tmp == '/') {
620 *tmp = '@';
621 }
622 }
623
624 return 0;
625}
626
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700627static int split_count(const char *str)
628{
629 char *ctx;
630 int count = 0;
631 char buf[PROPERTY_VALUE_MAX];
632
633 strncpy(buf, str, sizeof(buf));
634 char *pBuf = buf;
635
636 while(strtok_r(pBuf, " ", &ctx) != NULL) {
637 count++;
638 pBuf = NULL;
639 }
640
641 return count;
642}
643
neo.chae14e084d2015-01-07 18:46:13 +0900644static int split(char *buf, const char **argv)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700645{
646 char *ctx;
647 int count = 0;
648 char *tok;
649 char *pBuf = buf;
650
651 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
652 argv[count++] = tok;
653 pBuf = NULL;
654 }
655
656 return count;
657}
658
Alex Light7365a102014-07-21 12:23:48 -0700659static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700660 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700661{
662 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100663 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700664
665 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
666 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
667 ALOGE("Instruction set %s longer than max length of %d",
668 instruction_set, MAX_INSTRUCTION_SET_LEN);
669 return;
670 }
671
672 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
673 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
674 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
675 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
676 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
677 // The caller has already gotten all the locks we need.
678 const char* no_lock_arg = "--no-lock-output";
679 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
680 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
681 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700682 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700683 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
684
685 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
686 char* argv[7];
687 argv[0] = (char*) PATCHOAT_BIN;
688 argv[1] = (char*) patched_image_location_arg;
689 argv[2] = (char*) no_lock_arg;
690 argv[3] = instruction_set_arg;
691 argv[4] = output_oat_fd_arg;
692 argv[5] = input_oat_fd_arg;
693 argv[6] = NULL;
694
695 execv(PATCHOAT_BIN, (char* const *)argv);
696 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
697}
698
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700699static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Andreas Gampee1c01352014-12-10 16:41:11 -0800700 const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set,
Calin Juravleb1efac12014-08-21 19:05:20 +0100701 bool vm_safe_mode)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700702{
Calin Juravle8fc73152014-08-19 18:48:50 +0100703 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
704
705 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
706 ALOGE("Instruction set %s longer than max length of %d",
707 instruction_set, MAX_INSTRUCTION_SET_LEN);
708 return;
709 }
710
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700711 char prop_buf[PROPERTY_VALUE_MAX];
712 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
713
714 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
715 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
716
717 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
718 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
719
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700720 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
721 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
722 dex2oat_compiler_filter_flag, NULL) > 0;
723
Calin Juravle8fc73152014-08-19 18:48:50 +0100724 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
725 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
726 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
727 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
728 dex2oat_isa_features, NULL) > 0;
729
Ian Rogers16a95b22014-11-08 16:58:13 -0800730 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
731 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
732 char dex2oat_isa_variant[PROPERTY_VALUE_MAX];
733 bool have_dex2oat_isa_variant = property_get(dex2oat_isa_variant_key,
734 dex2oat_isa_variant, NULL) > 0;
735
neo.chae14e084d2015-01-07 18:46:13 +0900736 const char *dex2oat_norelocation = "-Xnorelocate";
737 bool have_dex2oat_relocation_skip_flag = false;
738
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800739 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700740 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
741 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800742 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
743
Brian Carlstrom538998f2014-07-30 14:37:11 -0700744 // If we booting without the real /data, don't spend time compiling.
745 char vold_decrypt[PROPERTY_VALUE_MAX];
746 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
747 bool skip_compilation = (have_vold_decrypt &&
748 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
749 (strcmp(vold_decrypt, "1") == 0)));
750
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700751 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700752
Brian Carlstrom53e07762014-06-27 14:15:19 -0700753 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700754
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700755 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100756
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700757 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
758 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
759 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700760 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100761 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Ian Rogers16a95b22014-11-08 16:58:13 -0800762 char instruction_set_variant_arg[strlen("--instruction-set-variant=") + PROPERTY_VALUE_MAX];
Calin Juravle8fc73152014-08-19 18:48:50 +0100763 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100764 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
765 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700766 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
767 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700768 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800769 bool have_dex2oat_swap_fd = false;
770 char dex2oat_swap_fd[strlen("--swap-fd=") + MAX_INT_LEN];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700771
772 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
773 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
774 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
775 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100776 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Ian Rogers16a95b22014-11-08 16:58:13 -0800777 sprintf(instruction_set_variant_arg, "--instruction-set-variant=%s", dex2oat_isa_variant);
Calin Juravle8fc73152014-08-19 18:48:50 +0100778 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Andreas Gampee1c01352014-12-10 16:41:11 -0800779 if (swap_fd >= 0) {
780 have_dex2oat_swap_fd = true;
781 sprintf(dex2oat_swap_fd, "--swap-fd=%d", swap_fd);
782 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100783
Calin Juravle4fdff462014-06-06 16:58:43 +0100784 bool have_profile_file = false;
785 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100786 if (profiler && (strcmp(pkgname, "*") != 0)) {
787 char profile_file[PKG_PATH_MAX];
788 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800789 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100790 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100791 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100792 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100793 have_profile_file = true;
794 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
795 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
796 "--top-k-profile-threshold=%s", prop_buf);
797 have_top_k_profile_threshold = true;
798 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100799 }
Dave Allisond9370732014-01-30 14:19:23 -0800800 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700801
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700802 if (have_dex2oat_Xms_flag) {
803 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
804 }
805 if (have_dex2oat_Xmx_flag) {
806 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
807 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700808 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700809 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700810 have_dex2oat_compiler_filter_flag = true;
neo.chae14e084d2015-01-07 18:46:13 +0900811 have_dex2oat_relocation_skip_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100812 } else if (vm_safe_mode) {
813 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100814 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700815 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700816 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
817 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700818
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700819 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100820
neo.chae14e084d2015-01-07 18:46:13 +0900821 const char* argv[7 // program name, mandatory arguments and the final NULL
822 + (have_dex2oat_isa_variant ? 1 : 0)
823 + (have_dex2oat_isa_features ? 1 : 0)
824 + (have_profile_file ? 1 : 0)
825 + (have_top_k_profile_threshold ? 1 : 0)
826 + (have_dex2oat_Xms_flag ? 2 : 0)
827 + (have_dex2oat_Xmx_flag ? 2 : 0)
828 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
829 + (have_dex2oat_swap_fd ? 1 : 0)
830 + (have_dex2oat_relocation_skip_flag ? 2 : 0)
831 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100832 int i = 0;
neo.chae14e084d2015-01-07 18:46:13 +0900833 argv[i++] = DEX2OAT_BIN;
Calin Juravle4fdff462014-06-06 16:58:43 +0100834 argv[i++] = zip_fd_arg;
835 argv[i++] = zip_location_arg;
836 argv[i++] = oat_fd_arg;
837 argv[i++] = oat_location_arg;
838 argv[i++] = instruction_set_arg;
Ian Rogers16a95b22014-11-08 16:58:13 -0800839 if (have_dex2oat_isa_variant) {
840 argv[i++] = instruction_set_variant_arg;
841 }
Calin Juravle8fc73152014-08-19 18:48:50 +0100842 if (have_dex2oat_isa_features) {
843 argv[i++] = instruction_set_features_arg;
844 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100845 if (have_profile_file) {
846 argv[i++] = profile_file_arg;
847 }
848 if (have_top_k_profile_threshold) {
849 argv[i++] = top_k_profile_threshold_arg;
850 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700851 if (have_dex2oat_Xms_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900852 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700853 argv[i++] = dex2oat_Xms_arg;
854 }
855 if (have_dex2oat_Xmx_flag) {
neo.chae14e084d2015-01-07 18:46:13 +0900856 argv[i++] = RUNTIME_ARG;
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700857 argv[i++] = dex2oat_Xmx_arg;
858 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700859 if (have_dex2oat_compiler_filter_flag) {
860 argv[i++] = dex2oat_compiler_filter_arg;
861 }
Andreas Gampee1c01352014-12-10 16:41:11 -0800862 if (have_dex2oat_swap_fd) {
863 argv[i++] = dex2oat_swap_fd;
864 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700865 if (dex2oat_flags_count) {
866 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100867 }
neo.chae14e084d2015-01-07 18:46:13 +0900868 if (have_dex2oat_relocation_skip_flag) {
869 argv[i++] = RUNTIME_ARG;
870 argv[i++] = dex2oat_norelocation;
871 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700872 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100873 argv[i] = NULL;
874
neo.chae14e084d2015-01-07 18:46:13 +0900875 execv(DEX2OAT_BIN, (char * const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700876 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700877}
878
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100879static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700880{
881 int status;
882 pid_t got_pid;
883
Mike Lockwood94afecf2012-10-24 10:45:23 -0700884 while (1) {
885 got_pid = waitpid(pid, &status, 0);
886 if (got_pid == -1 && errno == EINTR) {
887 printf("waitpid interrupted, retrying\n");
888 } else {
889 break;
890 }
891 }
892 if (got_pid != pid) {
893 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
894 (int) pid, (int) got_pid, strerror(errno));
895 return 1;
896 }
897
898 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700899 return 0;
900 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700901 return status; /* always nonzero */
902 }
903}
904
Andreas Gampee1c01352014-12-10 16:41:11 -0800905/*
906 * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this
907 * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do
908 * this on a low-mem device.
909 */
910static bool kAlwaysProvideSwapFile = true;
911
912static bool ShouldUseSwapFileForDexopt() {
913 if (kAlwaysProvideSwapFile) {
914 return true;
915 }
916
917 char low_mem_buf[PROPERTY_VALUE_MAX];
918 property_get("ro.config.low_ram", low_mem_buf, "");
919 return (strcmp(low_mem_buf, "true") == 0);
920}
921
Calin Juravleb1efac12014-08-21 19:05:20 +0100922int dexopt(const char *apk_path, uid_t uid, bool is_public,
Alex Light7365a102014-07-21 12:23:48 -0700923 const char *pkgname, const char *instruction_set,
Calin Juravleb1efac12014-08-21 19:05:20 +0100924 bool vm_safe_mode, bool is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700925{
926 struct utimbuf ut;
Alex Light7365a102014-07-21 12:23:48 -0700927 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700928 char out_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800929 char swap_file_name[PKG_PATH_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700930 char *end;
Alex Light7365a102014-07-21 12:23:48 -0700931 const char *input_file;
932 char in_odex_path[PKG_PATH_MAX];
Andreas Gampee1c01352014-12-10 16:41:11 -0800933 int res, input_fd=-1, out_fd=-1, swap_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700934
Andreas Gampee1c01352014-12-10 16:41:11 -0800935 // Early best-effort check whether we can fit the the path into our buffers.
936 // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
937 // without a swap file, if necessary.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700938 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
939 return -1;
940 }
941
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700942 /* Before anything else: is there a .odex file? If so, we have
943 * precompiled the apk and there is nothing to do here.
Alex Light7365a102014-07-21 12:23:48 -0700944 *
945 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700946 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800947 strcpy(out_path, apk_path);
948 end = strrchr(out_path, '.');
Alex Light7365a102014-07-21 12:23:48 -0700949 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800950 strcpy(end, ".odex");
951 if (stat(out_path, &dex_stat) == 0) {
952 return 0;
953 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700954 }
955
Narayan Kamath1b400322014-04-11 13:17:00 +0100956 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700957 return -1;
958 }
959
Alex Light7365a102014-07-21 12:23:48 -0700960 if (is_patchoat) {
961 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
962 strcpy(in_odex_path, apk_path);
963 end = strrchr(in_odex_path, '/');
964 if (end == NULL) {
965 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
966 return -1;
967 }
968 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
969 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
970 strcat(in_odex_path, apk_end);
971 end = strrchr(in_odex_path, '.');
972 if (end == NULL) {
973 return -1;
974 }
975 strcpy(end + 1, "odex");
976 input_file = in_odex_path;
977 } else {
978 input_file = apk_path;
979 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700980
Alex Light7365a102014-07-21 12:23:48 -0700981 memset(&input_stat, 0, sizeof(input_stat));
982 stat(input_file, &input_stat);
983
984 input_fd = open(input_file, O_RDONLY, 0);
985 if (input_fd < 0) {
986 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700987 return -1;
988 }
989
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700990 unlink(out_path);
991 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
992 if (out_fd < 0) {
993 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700994 goto fail;
995 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700996 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700997 S_IRUSR|S_IWUSR|S_IRGRP |
998 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700999 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001000 goto fail;
1001 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001002 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
1003 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001004 goto fail;
1005 }
1006
Dave Allisond9370732014-01-30 14:19:23 -08001007 // Create profile file if there is a package name present.
1008 if (strcmp(pkgname, "*") != 0) {
1009 create_profile_file(pkgname, uid);
1010 }
1011
Andreas Gampee1c01352014-12-10 16:41:11 -08001012 // Create a swap file if necessary.
1013 if (!is_patchoat && ShouldUseSwapFileForDexopt()) {
1014 // Make sure there really is enough space.
1015 size_t out_len = strlen(out_path);
1016 if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
1017 strcpy(swap_file_name, out_path);
1018 strcpy(swap_file_name + strlen(out_path), ".swap");
1019 unlink(swap_file_name);
1020 swap_fd = open(swap_file_name, O_RDWR | O_CREAT | O_EXCL, 0600);
1021 if (swap_fd < 0) {
1022 // Could not create swap file. Optimistically go on and hope that we can compile
1023 // without it.
1024 ALOGE("installd could not create '%s' for swap during dexopt\n", swap_file_name);
1025 } else {
1026 // Immediately unlink. We don't really want to hit flash.
1027 unlink(swap_file_name);
1028 }
1029 } else {
1030 // Swap file path is too long. Try to run without.
1031 ALOGE("installd could not create swap file for path %s during dexopt\n", out_path);
1032 }
1033 }
Dave Allisond9370732014-01-30 14:19:23 -08001034
Alex Light7365a102014-07-21 12:23:48 -07001035 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001036
1037 pid_t pid;
1038 pid = fork();
1039 if (pid == 0) {
1040 /* child -- drop privileges before continuing */
1041 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001042 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001043 exit(64);
1044 }
1045 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001046 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001047 exit(65);
1048 }
1049 // drop capabilities
1050 struct __user_cap_header_struct capheader;
1051 struct __user_cap_data_struct capdata[2];
1052 memset(&capheader, 0, sizeof(capheader));
1053 memset(&capdata, 0, sizeof(capdata));
1054 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1055 if (capset(&capheader, &capdata[0]) < 0) {
1056 ALOGE("capset failed: %s\n", strerror(errno));
1057 exit(66);
1058 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001059 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1060 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1061 exit(70);
1062 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001063 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1064 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001065 exit(67);
1066 }
1067
Andreas Gampebd872e42014-12-15 11:41:11 -08001068 if (is_patchoat) {
1069 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001070 } else {
Andreas Gampee1c01352014-12-10 16:41:11 -08001071 run_dex2oat(input_fd, out_fd, input_file, out_path, swap_fd, pkgname, instruction_set,
Andreas Gampebd872e42014-12-15 11:41:11 -08001072 vm_safe_mode);
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001073 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001074 exit(68); /* only get here on exec failure */
1075 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001076 res = wait_child(pid);
1077 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001078 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001079 } else {
Alex Light7365a102014-07-21 12:23:48 -07001080 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001081 goto fail;
1082 }
1083 }
1084
Alex Light7365a102014-07-21 12:23:48 -07001085 ut.actime = input_stat.st_atime;
1086 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001087 utime(out_path, &ut);
1088
1089 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001090 close(input_fd);
Andreas Gampee1c01352014-12-10 16:41:11 -08001091 if (swap_fd != -1) {
1092 close(swap_fd);
1093 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001094 return 0;
1095
1096fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001097 if (out_fd >= 0) {
1098 close(out_fd);
1099 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001100 }
Alex Light7365a102014-07-21 12:23:48 -07001101 if (input_fd >= 0) {
1102 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001103 }
1104 return -1;
1105}
1106
Narayan Kamath091ea772014-11-10 15:03:46 +00001107int mark_boot_complete(const char* instruction_set)
1108{
1109 char boot_marker_path[PKG_PATH_MAX];
1110 sprintf(boot_marker_path,"%s%s/.booting", DALVIK_CACHE_PREFIX, instruction_set);
1111
1112 ALOGV("mark_boot_complete : %s", boot_marker_path);
1113 if (unlink(boot_marker_path) != 0) {
1114 ALOGE("Unable to unlink boot marker at %s, error=%s", boot_marker_path,
1115 strerror(errno));
1116 return -1;
1117 }
1118
1119 return 0;
1120}
1121
Mike Lockwood94afecf2012-10-24 10:45:23 -07001122void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1123 struct stat* statbuf)
1124{
1125 while (path[basepos] != 0) {
1126 if (path[basepos] == '/') {
1127 path[basepos] = 0;
1128 if (lstat(path, statbuf) < 0) {
1129 ALOGV("Making directory: %s\n", path);
1130 if (mkdir(path, mode) == 0) {
1131 chown(path, uid, gid);
1132 } else {
1133 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1134 }
1135 }
1136 path[basepos] = '/';
1137 basepos++;
1138 }
1139 basepos++;
1140 }
1141}
1142
1143int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1144 int dstuid, int dstgid, struct stat* statbuf)
1145{
1146 DIR *d;
1147 struct dirent *de;
1148 int res;
1149
1150 int srcend = strlen(srcpath);
1151 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001152
Mike Lockwood94afecf2012-10-24 10:45:23 -07001153 if (lstat(srcpath, statbuf) < 0) {
1154 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1155 return 1;
1156 }
Dave Allisond9370732014-01-30 14:19:23 -08001157
Mike Lockwood94afecf2012-10-24 10:45:23 -07001158 if ((statbuf->st_mode&S_IFDIR) == 0) {
1159 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1160 dstuid, dstgid, statbuf);
1161 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1162 if (rename(srcpath, dstpath) >= 0) {
1163 if (chown(dstpath, dstuid, dstgid) < 0) {
1164 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1165 unlink(dstpath);
1166 return 1;
1167 }
1168 } else {
1169 ALOGW("Unable to rename %s to %s: %s\n",
1170 srcpath, dstpath, strerror(errno));
1171 return 1;
1172 }
1173 return 0;
1174 }
1175
1176 d = opendir(srcpath);
1177 if (d == NULL) {
1178 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1179 return 1;
1180 }
1181
1182 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001183
Mike Lockwood94afecf2012-10-24 10:45:23 -07001184 while ((de = readdir(d))) {
1185 const char *name = de->d_name;
1186 /* always skip "." and ".." */
1187 if (name[0] == '.') {
1188 if (name[1] == 0) continue;
1189 if ((name[1] == '.') && (name[2] == 0)) continue;
1190 }
Dave Allisond9370732014-01-30 14:19:23 -08001191
Mike Lockwood94afecf2012-10-24 10:45:23 -07001192 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1193 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1194 continue;
1195 }
Dave Allisond9370732014-01-30 14:19:23 -08001196
Mike Lockwood94afecf2012-10-24 10:45:23 -07001197 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1198 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1199 continue;
1200 }
Dave Allisond9370732014-01-30 14:19:23 -08001201
Mike Lockwood94afecf2012-10-24 10:45:23 -07001202 srcpath[srcend] = dstpath[dstend] = '/';
1203 strcpy(srcpath+srcend+1, name);
1204 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001205
Mike Lockwood94afecf2012-10-24 10:45:23 -07001206 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1207 res = 1;
1208 }
Dave Allisond9370732014-01-30 14:19:23 -08001209
Mike Lockwood94afecf2012-10-24 10:45:23 -07001210 // Note: we will be leaving empty directories behind in srcpath,
1211 // but that is okay, the package manager will be erasing all of the
1212 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001213
Mike Lockwood94afecf2012-10-24 10:45:23 -07001214 srcpath[srcend] = dstpath[dstend] = 0;
1215 }
Dave Allisond9370732014-01-30 14:19:23 -08001216
Mike Lockwood94afecf2012-10-24 10:45:23 -07001217 closedir(d);
1218 return res;
1219}
1220
1221int movefiles()
1222{
1223 DIR *d;
1224 int dfd, subfd;
1225 struct dirent *de;
1226 struct stat s;
1227 char buf[PKG_PATH_MAX+1];
1228 int bufp, bufe, bufi, readlen;
1229
1230 char srcpkg[PKG_NAME_MAX];
1231 char dstpkg[PKG_NAME_MAX];
1232 char srcpath[PKG_PATH_MAX];
1233 char dstpath[PKG_PATH_MAX];
1234 int dstuid=-1, dstgid=-1;
1235 int hasspace;
1236
1237 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1238 if (d == NULL) {
1239 goto done;
1240 }
1241 dfd = dirfd(d);
1242
1243 /* Iterate through all files in the directory, executing the
1244 * file movements requested there-in.
1245 */
1246 while ((de = readdir(d))) {
1247 const char *name = de->d_name;
1248
1249 if (de->d_type == DT_DIR) {
1250 continue;
1251 } else {
1252 subfd = openat(dfd, name, O_RDONLY);
1253 if (subfd < 0) {
1254 ALOGW("Unable to open update commands at %s%s\n",
1255 UPDATE_COMMANDS_DIR_PREFIX, name);
1256 continue;
1257 }
Dave Allisond9370732014-01-30 14:19:23 -08001258
Mike Lockwood94afecf2012-10-24 10:45:23 -07001259 bufp = 0;
1260 bufe = 0;
1261 buf[PKG_PATH_MAX] = 0;
1262 srcpkg[0] = dstpkg[0] = 0;
1263 while (1) {
1264 bufi = bufp;
1265 while (bufi < bufe && buf[bufi] != '\n') {
1266 bufi++;
1267 }
1268 if (bufi < bufe) {
1269 buf[bufi] = 0;
1270 ALOGV("Processing line: %s\n", buf+bufp);
1271 hasspace = 0;
1272 while (bufp < bufi && isspace(buf[bufp])) {
1273 hasspace = 1;
1274 bufp++;
1275 }
1276 if (buf[bufp] == '#' || bufp == bufi) {
1277 // skip comments and empty lines.
1278 } else if (hasspace) {
1279 if (dstpkg[0] == 0) {
1280 ALOGW("Path before package line in %s%s: %s\n",
1281 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1282 } else if (srcpkg[0] == 0) {
1283 // Skip -- source package no longer exists.
1284 } else {
1285 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1286 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1287 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1288 movefileordir(srcpath, dstpath,
1289 strlen(dstpath)-strlen(buf+bufp),
1290 dstuid, dstgid, &s);
1291 }
1292 }
1293 } else {
1294 char* div = strchr(buf+bufp, ':');
1295 if (div == NULL) {
1296 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1297 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1298 } else {
1299 *div = 0;
1300 div++;
1301 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1302 strcpy(dstpkg, buf+bufp);
1303 } else {
1304 srcpkg[0] = dstpkg[0] = 0;
1305 ALOGW("Package name too long in %s%s: %s\n",
1306 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1307 }
1308 if (strlen(div) < PKG_NAME_MAX) {
1309 strcpy(srcpkg, div);
1310 } else {
1311 srcpkg[0] = dstpkg[0] = 0;
1312 ALOGW("Package name too long in %s%s: %s\n",
1313 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1314 }
1315 if (srcpkg[0] != 0) {
1316 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1317 if (lstat(srcpath, &s) < 0) {
1318 // Package no longer exists -- skip.
1319 srcpkg[0] = 0;
1320 }
1321 } else {
1322 srcpkg[0] = 0;
1323 ALOGW("Can't create path %s in %s%s\n",
1324 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1325 }
1326 if (srcpkg[0] != 0) {
1327 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1328 if (lstat(dstpath, &s) == 0) {
1329 dstuid = s.st_uid;
1330 dstgid = s.st_gid;
1331 } else {
1332 // Destination package doesn't
1333 // exist... due to original-package,
1334 // this is normal, so don't be
1335 // noisy about it.
1336 srcpkg[0] = 0;
1337 }
1338 } else {
1339 srcpkg[0] = 0;
1340 ALOGW("Can't create path %s in %s%s\n",
1341 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1342 }
1343 }
1344 ALOGV("Transfering from %s to %s: uid=%d\n",
1345 srcpkg, dstpkg, dstuid);
1346 }
1347 }
1348 }
1349 bufp = bufi+1;
1350 } else {
1351 if (bufp == 0) {
1352 if (bufp < bufe) {
1353 ALOGW("Line too long in %s%s, skipping: %s\n",
1354 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1355 }
1356 } else if (bufp < bufe) {
1357 memcpy(buf, buf+bufp, bufe-bufp);
1358 bufe -= bufp;
1359 bufp = 0;
1360 }
1361 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1362 if (readlen < 0) {
1363 ALOGW("Failure reading update commands in %s%s: %s\n",
1364 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1365 break;
1366 } else if (readlen == 0) {
1367 break;
1368 }
1369 bufe += readlen;
1370 buf[bufe] = 0;
1371 ALOGV("Read buf: %s\n", buf);
1372 }
1373 }
1374 close(subfd);
1375 }
1376 }
1377 closedir(d);
1378done:
1379 return 0;
1380}
1381
1382int linklib(const char* pkgname, const char* asecLibDir, int userId)
1383{
1384 char pkgdir[PKG_PATH_MAX];
1385 char libsymlink[PKG_PATH_MAX];
1386 struct stat s, libStat;
1387 int rc = 0;
1388
1389 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1390 ALOGE("cannot create package path\n");
1391 return -1;
1392 }
1393 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1394 ALOGE("cannot create package lib symlink origin path\n");
1395 return -1;
1396 }
1397
1398 if (stat(pkgdir, &s) < 0) return -1;
1399
1400 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1401 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1402 return -1;
1403 }
1404
1405 if (chmod(pkgdir, 0700) < 0) {
1406 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1407 rc = -1;
1408 goto out;
1409 }
1410
1411 if (lstat(libsymlink, &libStat) < 0) {
1412 if (errno != ENOENT) {
1413 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1414 rc = -1;
1415 goto out;
1416 }
1417 } else {
1418 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001419 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001420 rc = -1;
1421 goto out;
1422 }
1423 } else if (S_ISLNK(libStat.st_mode)) {
1424 if (unlink(libsymlink) < 0) {
1425 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1426 rc = -1;
1427 goto out;
1428 }
1429 }
1430 }
1431
1432 if (symlink(asecLibDir, libsymlink) < 0) {
1433 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1434 strerror(errno));
1435 rc = -errno;
1436 goto out;
1437 }
1438
1439out:
1440 if (chmod(pkgdir, s.st_mode) < 0) {
1441 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1442 rc = -errno;
1443 }
1444
1445 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1446 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1447 return -errno;
1448 }
1449
1450 return rc;
1451}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001452
1453static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1454{
1455 static const char *IDMAP_BIN = "/system/bin/idmap";
1456 static const size_t MAX_INT_LEN = 32;
1457 char idmap_str[MAX_INT_LEN];
1458
1459 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1460
1461 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1462 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1463}
1464
1465// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1466// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1467static int flatten_path(const char *prefix, const char *suffix,
1468 const char *overlay_path, char *idmap_path, size_t N)
1469{
1470 if (overlay_path == NULL || idmap_path == NULL) {
1471 return -1;
1472 }
1473 const size_t len_overlay_path = strlen(overlay_path);
1474 // will access overlay_path + 1 further below; requires absolute path
1475 if (len_overlay_path < 2 || *overlay_path != '/') {
1476 return -1;
1477 }
1478 const size_t len_idmap_root = strlen(prefix);
1479 const size_t len_suffix = strlen(suffix);
1480 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1481 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1482 // additions below would cause overflow
1483 return -1;
1484 }
1485 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1486 return -1;
1487 }
1488 memset(idmap_path, 0, N);
1489 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1490 char *ch = idmap_path + len_idmap_root;
1491 while (*ch != '\0') {
1492 if (*ch == '/') {
1493 *ch = '@';
1494 }
1495 ++ch;
1496 }
1497 return 0;
1498}
1499
1500int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1501{
1502 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1503
1504 int idmap_fd = -1;
1505 char idmap_path[PATH_MAX];
1506
1507 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1508 idmap_path, sizeof(idmap_path)) == -1) {
1509 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1510 goto fail;
1511 }
1512
1513 unlink(idmap_path);
1514 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1515 if (idmap_fd < 0) {
1516 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1517 goto fail;
1518 }
1519 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1520 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1521 goto fail;
1522 }
1523 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1524 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1525 goto fail;
1526 }
1527
1528 pid_t pid;
1529 pid = fork();
1530 if (pid == 0) {
1531 /* child -- drop privileges before continuing */
1532 if (setgid(uid) != 0) {
1533 ALOGE("setgid(%d) failed during idmap\n", uid);
1534 exit(1);
1535 }
1536 if (setuid(uid) != 0) {
1537 ALOGE("setuid(%d) failed during idmap\n", uid);
1538 exit(1);
1539 }
1540 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1541 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1542 exit(1);
1543 }
1544
1545 run_idmap(target_apk, overlay_apk, idmap_fd);
1546 exit(1); /* only if exec call to idmap failed */
1547 } else {
1548 int status = wait_child(pid);
1549 if (status != 0) {
1550 ALOGE("idmap failed, status=0x%04x\n", status);
1551 goto fail;
1552 }
1553 }
1554
1555 close(idmap_fd);
1556 return 0;
1557fail:
1558 if (idmap_fd >= 0) {
1559 close(idmap_fd);
1560 unlink(idmap_path);
1561 }
1562 return -1;
1563}
Robert Craige9887e42014-02-20 10:25:56 -05001564
Robert Craigda30dc72014-03-27 10:21:12 -04001565int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001566{
Robert Craigda30dc72014-03-27 10:21:12 -04001567 struct dirent *entry;
1568 DIR *d;
1569 struct stat s;
1570 char *userdir;
1571 char *primarydir;
1572 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001573 int ret = 0;
1574
Robert Craigda30dc72014-03-27 10:21:12 -04001575 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1576 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1577
1578 if (!pkgName || !seinfo) {
1579 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001580 return -1;
1581 }
1582
Robert Craigda30dc72014-03-27 10:21:12 -04001583 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1584 return -1;
1585 }
1586
1587 // Relabel for primary user.
1588 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1589 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001590 ret |= -1;
1591 }
1592
Robert Craigda30dc72014-03-27 10:21:12 -04001593 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1594 free(primarydir);
1595 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001596 }
1597
Robert Craigda30dc72014-03-27 10:21:12 -04001598 // Relabel package directory for all secondary users.
1599 d = opendir(userdir);
1600 if (d == NULL) {
1601 free(primarydir);
1602 free(userdir);
1603 return -1;
1604 }
1605
1606 while ((entry = readdir(d))) {
1607 if (entry->d_type != DT_DIR) {
1608 continue;
1609 }
1610
1611 const char *user = entry->d_name;
1612 // Ignore "." and ".."
1613 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1614 continue;
1615 }
1616
1617 // user directories start with a number
1618 if (user[0] < '0' || user[0] > '9') {
1619 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1620 continue;
1621 }
1622
1623 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1624 continue;
1625 }
1626
1627 if (stat(pkgdir, &s) < 0) {
1628 free(pkgdir);
1629 continue;
1630 }
1631
Stephen Smalley8ac2a642014-09-08 15:51:55 -04001632 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, s.st_uid, flags) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001633 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1634 ret |= -1;
1635 }
1636 free(pkgdir);
1637 }
1638
1639 closedir(d);
1640 free(primarydir);
1641 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001642 return ret;
1643}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001644