blob: 83ab2a8218ac743bdaa5a1d13c4ec431eaed79c8 [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>
Mike Lockwood94afecf2012-10-24 10:45:23 -070019#include "installd.h"
Brian Carlstrom3b14e5b2014-08-08 00:52:22 -070020#include <cutils/sched_policy.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070021#include <diskusage/dirsize.h>
22#include <selinux/android.h>
23
24/* Directory records that are used in execution of commands. */
25dir_rec_t android_data_dir;
26dir_rec_t android_asec_dir;
27dir_rec_t android_app_dir;
28dir_rec_t android_app_private_dir;
29dir_rec_t android_app_lib_dir;
30dir_rec_t android_media_dir;
31dir_rec_array_t android_system_dirs;
32
Robert Craig4d3fd4e2013-03-25 06:33:03 -040033int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070034{
35 char pkgdir[PKG_PATH_MAX];
36 char libsymlink[PKG_PATH_MAX];
37 char applibdir[PKG_PATH_MAX];
38 struct stat libStat;
39
40 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
41 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
42 return -1;
43 }
44
45 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
46 ALOGE("cannot create package path\n");
47 return -1;
48 }
49
50 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
51 ALOGE("cannot create package lib symlink origin path\n");
52 return -1;
53 }
54
55 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
56 ALOGE("cannot create package lib symlink dest path\n");
57 return -1;
58 }
59
Nick Kralevicha2d838a2013-01-09 16:00:35 -080060 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070061 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
62 return -1;
63 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080064 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070065 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
66 unlink(pkgdir);
67 return -1;
68 }
69
70 if (lstat(libsymlink, &libStat) < 0) {
71 if (errno != ENOENT) {
72 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
73 return -1;
74 }
75 } else {
76 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +010077 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070078 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
79 return -1;
80 }
81 } else if (S_ISLNK(libStat.st_mode)) {
82 if (unlink(libsymlink) < 0) {
83 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
84 return -1;
85 }
86 }
87 }
88
Stephen Smalley26288202014-02-07 09:16:46 -050089 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070090 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
91 unlink(libsymlink);
92 unlink(pkgdir);
93 return -errno;
94 }
95
Stephen Smalley3a983892014-05-13 12:53:07 -040096 if (symlink(applibdir, libsymlink) < 0) {
97 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
98 strerror(errno));
99 unlink(pkgdir);
100 return -1;
101 }
102
Mike Lockwood94afecf2012-10-24 10:45:23 -0700103 if (chown(pkgdir, uid, gid) < 0) {
104 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
105 unlink(libsymlink);
106 unlink(pkgdir);
107 return -1;
108 }
109
110 return 0;
111}
112
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700113int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700114{
115 char pkgdir[PKG_PATH_MAX];
116
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700117 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700118 return -1;
119
Dave Allisond9370732014-01-30 14:19:23 -0800120 remove_profile_file(pkgname);
121
Mike Lockwood94afecf2012-10-24 10:45:23 -0700122 /* delete contents AND directory, no exceptions */
123 return delete_dir_contents(pkgdir, 1, NULL);
124}
125
126int renamepkg(const char *oldpkgname, const char *newpkgname)
127{
128 char oldpkgdir[PKG_PATH_MAX];
129 char newpkgdir[PKG_PATH_MAX];
130
131 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
132 return -1;
133 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
134 return -1;
135
136 if (rename(oldpkgdir, newpkgdir) < 0) {
137 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
138 return -errno;
139 }
140 return 0;
141}
142
143int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
144{
145 char pkgdir[PKG_PATH_MAX];
146 struct stat s;
147 int rc = 0;
148
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];
316
317 if (create_pkg_path(codecachedir, pkgname, CODE_CACHE_DIR_POSTFIX, userid))
318 return -1;
319
320 /* delete contents, not the directory, no exceptions */
321 return delete_dir_contents(codecachedir, 0, NULL);
322}
323
Mike Lockwood94afecf2012-10-24 10:45:23 -0700324/* Try to ensure free_size bytes of storage are available.
325 * Returns 0 on success.
326 * This is rather simple-minded because doing a full LRU would
327 * be potentially memory-intensive, and without atime it would
328 * also require that apps constantly modify file metadata even
329 * when just reading from the cache, which is pretty awful.
330 */
331int free_cache(int64_t free_size)
332{
333 cache_t* cache;
334 int64_t avail;
335 DIR *d;
336 struct dirent *de;
337 char tmpdir[PATH_MAX];
338 char *dirpos;
339
340 avail = data_disk_free();
341 if (avail < 0) return -1;
342
343 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
344 if (avail >= free_size) return 0;
345
346 cache = start_cache_collection();
347
348 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700349 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700350 //ALOGI("adding cache files from %s\n", tmpdir);
351 add_cache_files(cache, tmpdir, "cache");
352 }
353
354 // Search for other users and add any cache files from them.
355 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
356 SECONDARY_USER_PREFIX);
357 dirpos = tmpdir + strlen(tmpdir);
358 d = opendir(tmpdir);
359 if (d != NULL) {
360 while ((de = readdir(d))) {
361 if (de->d_type == DT_DIR) {
362 const char *name = de->d_name;
363 /* always skip "." and ".." */
364 if (name[0] == '.') {
365 if (name[1] == 0) continue;
366 if ((name[1] == '.') && (name[2] == 0)) continue;
367 }
368 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
369 strcpy(dirpos, name);
370 //ALOGI("adding cache files from %s\n", tmpdir);
371 add_cache_files(cache, tmpdir, "cache");
372 } else {
373 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
374 }
375 }
376 }
377 closedir(d);
378 }
379
380 // Collect cache files on external storage for all users (if it is mounted as part
381 // of the internal storage).
382 strcpy(tmpdir, android_media_dir.path);
383 dirpos = tmpdir + strlen(tmpdir);
384 d = opendir(tmpdir);
385 if (d != NULL) {
386 while ((de = readdir(d))) {
387 if (de->d_type == DT_DIR) {
388 const char *name = de->d_name;
389 /* skip any dir that doesn't start with a number, so not a user */
390 if (name[0] < '0' || name[0] > '9') {
391 continue;
392 }
393 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
394 strcpy(dirpos, name);
395 if (lookup_media_dir(tmpdir, "Android") == 0
396 && lookup_media_dir(tmpdir, "data") == 0) {
397 //ALOGI("adding cache files from %s\n", tmpdir);
398 add_cache_files(cache, tmpdir, "cache");
399 }
400 } else {
401 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
402 }
403 }
404 }
405 closedir(d);
406 }
407
408 clear_cache_files(cache, free_size);
409 finish_cache_collection(cache);
410
411 return data_disk_free() >= free_size ? 0 : -1;
412}
413
Narayan Kamath1b400322014-04-11 13:17:00 +0100414int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700415{
416 char src_dex[PKG_PATH_MAX];
417 char dst_dex[PKG_PATH_MAX];
418
419 if (validate_apk_path(src)) return -1;
420 if (validate_apk_path(dst)) return -1;
421
Narayan Kamath1b400322014-04-11 13:17:00 +0100422 if (create_cache_path(src_dex, src, instruction_set)) return -1;
423 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700424
425 ALOGV("move %s -> %s\n", src_dex, dst_dex);
426 if (rename(src_dex, dst_dex) < 0) {
427 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
428 return -1;
429 } else {
430 return 0;
431 }
432}
433
Narayan Kamath1b400322014-04-11 13:17:00 +0100434int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700435{
436 char dex_path[PKG_PATH_MAX];
437
438 if (validate_apk_path(path)) return -1;
Narayan Kamath1b400322014-04-11 13:17:00 +0100439 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700440
441 ALOGV("unlink %s\n", dex_path);
442 if (unlink(dex_path) < 0) {
443 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
444 return -1;
445 } else {
446 return 0;
447 }
448}
449
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700450int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700451 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100452 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
453 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700454{
455 DIR *d;
456 int dfd;
457 struct dirent *de;
458 struct stat s;
459 char path[PKG_PATH_MAX];
460
461 int64_t codesize = 0;
462 int64_t datasize = 0;
463 int64_t cachesize = 0;
464 int64_t asecsize = 0;
465
466 /* count the source apk as code -- but only if it's not
467 * on the /system partition and its not on the sdcard.
468 */
469 if (validate_system_app_path(apkpath) &&
470 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
471 if (stat(apkpath, &s) == 0) {
472 codesize += stat_size(&s);
473 }
474 }
475 /* count the forward locked apk as code if it is given
476 */
477 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
478 if (stat(fwdlock_apkpath, &s) == 0) {
479 codesize += stat_size(&s);
480 }
481 }
482 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100483 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700484 if (stat(path, &s) == 0) {
485 codesize += stat_size(&s);
486 }
487 }
488
489 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700490 if (libdirpath != NULL && libdirpath[0] != '!') {
491 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700492 if (d != NULL) {
493 dfd = dirfd(d);
494 codesize += calculate_dir_size(dfd);
495 closedir(d);
496 }
497 }
498
499 /* compute asec size if it is given
500 */
501 if (asecpath != NULL && asecpath[0] != '!') {
502 if (stat(asecpath, &s) == 0) {
503 asecsize += stat_size(&s);
504 }
505 }
506
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700507 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700508 goto done;
509 }
510
511 d = opendir(path);
512 if (d == NULL) {
513 goto done;
514 }
515 dfd = dirfd(d);
516
517 /* most stuff in the pkgdir is data, except for the "cache"
518 * directory and below, which is cache, and the "lib" directory
519 * and below, which is code...
520 */
521 while ((de = readdir(d))) {
522 const char *name = de->d_name;
523
524 if (de->d_type == DT_DIR) {
525 int subfd;
526 int64_t statsize = 0;
527 int64_t dirsize = 0;
528 /* always skip "." and ".." */
529 if (name[0] == '.') {
530 if (name[1] == 0) continue;
531 if ((name[1] == '.') && (name[2] == 0)) continue;
532 }
533 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
534 statsize = stat_size(&s);
535 }
536 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
537 if (subfd >= 0) {
538 dirsize = calculate_dir_size(subfd);
539 }
540 if(!strcmp(name,"lib")) {
541 codesize += dirsize + statsize;
542 } else if(!strcmp(name,"cache")) {
543 cachesize += dirsize + statsize;
544 } else {
545 datasize += dirsize + statsize;
546 }
547 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
548 // This is the symbolic link to the application's library
549 // code. We'll count this as code instead of data, since
550 // it is not something that the app creates.
551 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
552 codesize += stat_size(&s);
553 }
554 } else {
555 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
556 datasize += stat_size(&s);
557 }
558 }
559 }
560 closedir(d);
561done:
562 *_codesize = codesize;
563 *_datasize = datasize;
564 *_cachesize = cachesize;
565 *_asecsize = asecsize;
566 return 0;
567}
568
Narayan Kamath1b400322014-04-11 13:17:00 +0100569int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700570{
571 char *tmp;
572 int srclen;
573 int dstlen;
574
575 srclen = strlen(src);
576
577 /* demand that we are an absolute path */
578 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
579 return -1;
580 }
581
582 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
583 return -1;
584 }
585
Dave Allisond9370732014-01-30 14:19:23 -0800586 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100587 strlen(instruction_set) +
588 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800589
Mike Lockwood94afecf2012-10-24 10:45:23 -0700590 if (dstlen > PKG_PATH_MAX) {
591 return -1;
592 }
593
Narayan Kamath1b400322014-04-11 13:17:00 +0100594 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700595 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100596 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700597 src + 1, /* skip the leading / */
598 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800599
Narayan Kamath1b400322014-04-11 13:17:00 +0100600 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700601 if (*tmp == '/') {
602 *tmp = '@';
603 }
604 }
605
606 return 0;
607}
608
609static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800610 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700611{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800612 /* platform-specific flags affecting optimization and verification */
613 char dexopt_flags[PROPERTY_VALUE_MAX];
614 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
615 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
616
Mike Lockwood94afecf2012-10-24 10:45:23 -0700617 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
618 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
619 char zip_num[MAX_INT_LEN];
620 char odex_num[MAX_INT_LEN];
621
622 sprintf(zip_num, "%d", zip_fd);
623 sprintf(odex_num, "%d", odex_fd);
624
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700625 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700626 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
627 dexopt_flags, (char*) NULL);
628 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
629}
630
Alex Light43c5d302014-07-21 12:23:48 -0700631static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
632 const char* output_file_name, const char *pkgname, const char *instruction_set)
633{
634 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100635 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light43c5d302014-07-21 12:23:48 -0700636
637 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
638 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
639 ALOGE("Instruction set %s longer than max length of %d",
640 instruction_set, MAX_INSTRUCTION_SET_LEN);
641 return;
642 }
643
644 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
645 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
646 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
647 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
648 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
649 // The caller has already gotten all the locks we need.
650 const char* no_lock_arg = "--no-lock-output";
651 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
652 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
653 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
654 ALOGE("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
655 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
656
657 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
658 char* argv[7];
659 argv[0] = (char*) PATCHOAT_BIN;
660 argv[1] = (char*) patched_image_location_arg;
661 argv[2] = (char*) no_lock_arg;
662 argv[3] = instruction_set_arg;
663 argv[4] = output_oat_fd_arg;
664 argv[5] = input_oat_fd_arg;
665 argv[6] = NULL;
666
667 execv(PATCHOAT_BIN, (char* const *)argv);
668 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
669}
670
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700671static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Calin Juravleb1efac12014-08-21 19:05:20 +0100672 const char* output_file_name, const char *pkgname, const char *instruction_set,
673 bool vm_safe_mode)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700674{
Calin Juravle8fc73152014-08-19 18:48:50 +0100675 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
676
677 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
678 ALOGE("Instruction set %s longer than max length of %d",
679 instruction_set, MAX_INSTRUCTION_SET_LEN);
680 return;
681 }
682
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700683 char prop_buf[PROPERTY_VALUE_MAX];
684 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
685
686 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
687 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
688
689 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
690 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
691
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700692 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
693 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
694 dex2oat_compiler_filter_flag, NULL) > 0;
695
Calin Juravle8fc73152014-08-19 18:48:50 +0100696 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
697 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
698 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
699 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
700 dex2oat_isa_features, NULL) > 0;
701
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800702 char dex2oat_flags[PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100703 bool have_dex2oat_flags = property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, NULL) > 0;
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800704 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
705
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700706 // If we booting without the real /data, don't spend time compiling.
707 char vold_decrypt[PROPERTY_VALUE_MAX];
708 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
709 bool skip_compilation = (have_vold_decrypt &&
710 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
711 (strcmp(vold_decrypt, "1") == 0)));
712
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700713 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700714
Brian Carlstrom53e07762014-06-27 14:15:19 -0700715 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700716
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700717 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100718
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700719 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
720 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
721 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700722 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100723 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravle8fc73152014-08-19 18:48:50 +0100724 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100725 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
726 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700727 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
728 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700729 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700730
731 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
732 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
733 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
734 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100735 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle8fc73152014-08-19 18:48:50 +0100736 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Calin Juravle57c69c32014-06-06 14:42:16 +0100737
Calin Juravle4fdff462014-06-06 16:58:43 +0100738 bool have_profile_file = false;
739 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100740 if (profiler && (strcmp(pkgname, "*") != 0)) {
741 char profile_file[PKG_PATH_MAX];
742 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800743 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100744 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100745 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100746 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100747 have_profile_file = true;
748 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
749 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
750 "--top-k-profile-threshold=%s", prop_buf);
751 have_top_k_profile_threshold = true;
752 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100753 }
Dave Allisond9370732014-01-30 14:19:23 -0800754 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700755
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700756 if (have_dex2oat_Xms_flag) {
757 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
758 }
759 if (have_dex2oat_Xmx_flag) {
760 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
761 }
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700762 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700763 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700764 have_dex2oat_compiler_filter_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100765 } else if (vm_safe_mode) {
766 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700767 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700768 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
769 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700770
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700771 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100772
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700773 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravle8fc73152014-08-19 18:48:50 +0100774 + (have_dex2oat_isa_features ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100775 + (have_profile_file ? 1 : 0)
776 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700777 + (have_dex2oat_Xms_flag ? 2 : 0)
778 + (have_dex2oat_Xmx_flag ? 2 : 0)
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700779 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100780 + (have_dex2oat_flags ? 1 : 0)];
781 int i = 0;
782 argv[i++] = (char*)DEX2OAT_BIN;
783 argv[i++] = zip_fd_arg;
784 argv[i++] = zip_location_arg;
785 argv[i++] = oat_fd_arg;
786 argv[i++] = oat_location_arg;
787 argv[i++] = instruction_set_arg;
Calin Juravle8fc73152014-08-19 18:48:50 +0100788 if (have_dex2oat_isa_features) {
789 argv[i++] = instruction_set_features_arg;
790 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100791 if (have_profile_file) {
792 argv[i++] = profile_file_arg;
793 }
794 if (have_top_k_profile_threshold) {
795 argv[i++] = top_k_profile_threshold_arg;
796 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700797 if (have_dex2oat_Xms_flag) {
798 argv[i++] = (char*)RUNTIME_ARG;
799 argv[i++] = dex2oat_Xms_arg;
800 }
801 if (have_dex2oat_Xmx_flag) {
802 argv[i++] = (char*)RUNTIME_ARG;
803 argv[i++] = dex2oat_Xmx_arg;
804 }
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700805 if (have_dex2oat_compiler_filter_flag) {
806 argv[i++] = dex2oat_compiler_filter_arg;
807 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100808 if (have_dex2oat_flags) {
809 argv[i++] = dex2oat_flags;
810 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700811 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100812 argv[i] = NULL;
813
814 execv(DEX2OAT_BIN, (char* const *)argv);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700815 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
816}
817
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100818static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700819{
820 int status;
821 pid_t got_pid;
822
Mike Lockwood94afecf2012-10-24 10:45:23 -0700823 while (1) {
824 got_pid = waitpid(pid, &status, 0);
825 if (got_pid == -1 && errno == EINTR) {
826 printf("waitpid interrupted, retrying\n");
827 } else {
828 break;
829 }
830 }
831 if (got_pid != pid) {
832 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
833 (int) pid, (int) got_pid, strerror(errno));
834 return 1;
835 }
836
837 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700838 return 0;
839 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700840 return status; /* always nonzero */
841 }
842}
843
Calin Juravleb1efac12014-08-21 19:05:20 +0100844int dexopt(const char *apk_path, uid_t uid, bool is_public,
Alex Light43c5d302014-07-21 12:23:48 -0700845 const char *pkgname, const char *instruction_set,
Calin Juravleb1efac12014-08-21 19:05:20 +0100846 bool vm_safe_mode, bool is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700847{
848 struct utimbuf ut;
Alex Light43c5d302014-07-21 12:23:48 -0700849 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700850 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700851 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700852 char *end;
Alex Light43c5d302014-07-21 12:23:48 -0700853 const char *input_file;
854 char in_odex_path[PKG_PATH_MAX];
855 int res, input_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700856
Mike Lockwood94afecf2012-10-24 10:45:23 -0700857 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
858 return -1;
859 }
860
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800861 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700862 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700863
Alex Light43c5d302014-07-21 12:23:48 -0700864 if (is_patchoat && strncmp(persist_sys_dalvik_vm_lib, "libart", 6) != 0) {
865 /* We may only patch if we are libart */
866 ALOGE("Patching is only supported in libart\n");
867 return -1;
868 }
869
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700870 /* Before anything else: is there a .odex file? If so, we have
871 * precompiled the apk and there is nothing to do here.
Alex Light43c5d302014-07-21 12:23:48 -0700872 *
873 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700874 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800875 strcpy(out_path, apk_path);
876 end = strrchr(out_path, '.');
Alex Light43c5d302014-07-21 12:23:48 -0700877 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800878 strcpy(end, ".odex");
879 if (stat(out_path, &dex_stat) == 0) {
880 return 0;
881 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700882 }
883
Narayan Kamath1b400322014-04-11 13:17:00 +0100884 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700885 return -1;
886 }
887
Alex Light43c5d302014-07-21 12:23:48 -0700888 if (is_patchoat) {
889 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
890 strcpy(in_odex_path, apk_path);
891 end = strrchr(in_odex_path, '/');
892 if (end == NULL) {
893 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
894 return -1;
895 }
896 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
897 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
898 strcat(in_odex_path, apk_end);
899 end = strrchr(in_odex_path, '.');
900 if (end == NULL) {
901 return -1;
902 }
903 strcpy(end + 1, "odex");
904 input_file = in_odex_path;
905 } else {
906 input_file = apk_path;
907 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700908
Alex Light43c5d302014-07-21 12:23:48 -0700909 memset(&input_stat, 0, sizeof(input_stat));
910 stat(input_file, &input_stat);
911
912 input_fd = open(input_file, O_RDONLY, 0);
913 if (input_fd < 0) {
914 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700915 return -1;
916 }
917
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700918 unlink(out_path);
919 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
920 if (out_fd < 0) {
921 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700922 goto fail;
923 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700924 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700925 S_IRUSR|S_IWUSR|S_IRGRP |
926 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700927 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700928 goto fail;
929 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700930 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
931 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700932 goto fail;
933 }
934
Dave Allisond9370732014-01-30 14:19:23 -0800935 // Create profile file if there is a package name present.
936 if (strcmp(pkgname, "*") != 0) {
937 create_profile_file(pkgname, uid);
938 }
939
940
Alex Light43c5d302014-07-21 12:23:48 -0700941 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700942
943 pid_t pid;
944 pid = fork();
945 if (pid == 0) {
946 /* child -- drop privileges before continuing */
947 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700948 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700949 exit(64);
950 }
951 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700952 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700953 exit(65);
954 }
955 // drop capabilities
956 struct __user_cap_header_struct capheader;
957 struct __user_cap_data_struct capdata[2];
958 memset(&capheader, 0, sizeof(capheader));
959 memset(&capdata, 0, sizeof(capdata));
960 capheader.version = _LINUX_CAPABILITY_VERSION_3;
961 if (capset(&capheader, &capdata[0]) < 0) {
962 ALOGE("capset failed: %s\n", strerror(errno));
963 exit(66);
964 }
Brian Carlstrom3b14e5b2014-08-08 00:52:22 -0700965 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
966 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
967 exit(70);
968 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700969 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
970 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971 exit(67);
972 }
973
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700974 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700975 run_dexopt(input_fd, out_fd, input_file, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700976 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700977 if (is_patchoat) {
978 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
979 } else {
Calin Juravleb1efac12014-08-21 19:05:20 +0100980 run_dex2oat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set,
981 vm_safe_mode);
Alex Light43c5d302014-07-21 12:23:48 -0700982 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700983 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700984 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700985 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700986 exit(68); /* only get here on exec failure */
987 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100988 res = wait_child(pid);
989 if (res == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700990 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100991 } else {
Alex Light43c5d302014-07-21 12:23:48 -0700992 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700993 goto fail;
994 }
995 }
996
Alex Light43c5d302014-07-21 12:23:48 -0700997 ut.actime = input_stat.st_atime;
998 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700999 utime(out_path, &ut);
1000
1001 close(out_fd);
Alex Light43c5d302014-07-21 12:23:48 -07001002 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001003 return 0;
1004
1005fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001006 if (out_fd >= 0) {
1007 close(out_fd);
1008 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001009 }
Alex Light43c5d302014-07-21 12:23:48 -07001010 if (input_fd >= 0) {
1011 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001012 }
1013 return -1;
1014}
1015
1016void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1017 struct stat* statbuf)
1018{
1019 while (path[basepos] != 0) {
1020 if (path[basepos] == '/') {
1021 path[basepos] = 0;
1022 if (lstat(path, statbuf) < 0) {
1023 ALOGV("Making directory: %s\n", path);
1024 if (mkdir(path, mode) == 0) {
1025 chown(path, uid, gid);
1026 } else {
1027 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1028 }
1029 }
1030 path[basepos] = '/';
1031 basepos++;
1032 }
1033 basepos++;
1034 }
1035}
1036
1037int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1038 int dstuid, int dstgid, struct stat* statbuf)
1039{
1040 DIR *d;
1041 struct dirent *de;
1042 int res;
1043
1044 int srcend = strlen(srcpath);
1045 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001046
Mike Lockwood94afecf2012-10-24 10:45:23 -07001047 if (lstat(srcpath, statbuf) < 0) {
1048 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1049 return 1;
1050 }
Dave Allisond9370732014-01-30 14:19:23 -08001051
Mike Lockwood94afecf2012-10-24 10:45:23 -07001052 if ((statbuf->st_mode&S_IFDIR) == 0) {
1053 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1054 dstuid, dstgid, statbuf);
1055 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1056 if (rename(srcpath, dstpath) >= 0) {
1057 if (chown(dstpath, dstuid, dstgid) < 0) {
1058 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1059 unlink(dstpath);
1060 return 1;
1061 }
1062 } else {
1063 ALOGW("Unable to rename %s to %s: %s\n",
1064 srcpath, dstpath, strerror(errno));
1065 return 1;
1066 }
1067 return 0;
1068 }
1069
1070 d = opendir(srcpath);
1071 if (d == NULL) {
1072 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1073 return 1;
1074 }
1075
1076 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001077
Mike Lockwood94afecf2012-10-24 10:45:23 -07001078 while ((de = readdir(d))) {
1079 const char *name = de->d_name;
1080 /* always skip "." and ".." */
1081 if (name[0] == '.') {
1082 if (name[1] == 0) continue;
1083 if ((name[1] == '.') && (name[2] == 0)) continue;
1084 }
Dave Allisond9370732014-01-30 14:19:23 -08001085
Mike Lockwood94afecf2012-10-24 10:45:23 -07001086 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1087 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1088 continue;
1089 }
Dave Allisond9370732014-01-30 14:19:23 -08001090
Mike Lockwood94afecf2012-10-24 10:45:23 -07001091 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1092 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1093 continue;
1094 }
Dave Allisond9370732014-01-30 14:19:23 -08001095
Mike Lockwood94afecf2012-10-24 10:45:23 -07001096 srcpath[srcend] = dstpath[dstend] = '/';
1097 strcpy(srcpath+srcend+1, name);
1098 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001099
Mike Lockwood94afecf2012-10-24 10:45:23 -07001100 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1101 res = 1;
1102 }
Dave Allisond9370732014-01-30 14:19:23 -08001103
Mike Lockwood94afecf2012-10-24 10:45:23 -07001104 // Note: we will be leaving empty directories behind in srcpath,
1105 // but that is okay, the package manager will be erasing all of the
1106 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001107
Mike Lockwood94afecf2012-10-24 10:45:23 -07001108 srcpath[srcend] = dstpath[dstend] = 0;
1109 }
Dave Allisond9370732014-01-30 14:19:23 -08001110
Mike Lockwood94afecf2012-10-24 10:45:23 -07001111 closedir(d);
1112 return res;
1113}
1114
1115int movefiles()
1116{
1117 DIR *d;
1118 int dfd, subfd;
1119 struct dirent *de;
1120 struct stat s;
1121 char buf[PKG_PATH_MAX+1];
1122 int bufp, bufe, bufi, readlen;
1123
1124 char srcpkg[PKG_NAME_MAX];
1125 char dstpkg[PKG_NAME_MAX];
1126 char srcpath[PKG_PATH_MAX];
1127 char dstpath[PKG_PATH_MAX];
1128 int dstuid=-1, dstgid=-1;
1129 int hasspace;
1130
1131 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1132 if (d == NULL) {
1133 goto done;
1134 }
1135 dfd = dirfd(d);
1136
1137 /* Iterate through all files in the directory, executing the
1138 * file movements requested there-in.
1139 */
1140 while ((de = readdir(d))) {
1141 const char *name = de->d_name;
1142
1143 if (de->d_type == DT_DIR) {
1144 continue;
1145 } else {
1146 subfd = openat(dfd, name, O_RDONLY);
1147 if (subfd < 0) {
1148 ALOGW("Unable to open update commands at %s%s\n",
1149 UPDATE_COMMANDS_DIR_PREFIX, name);
1150 continue;
1151 }
Dave Allisond9370732014-01-30 14:19:23 -08001152
Mike Lockwood94afecf2012-10-24 10:45:23 -07001153 bufp = 0;
1154 bufe = 0;
1155 buf[PKG_PATH_MAX] = 0;
1156 srcpkg[0] = dstpkg[0] = 0;
1157 while (1) {
1158 bufi = bufp;
1159 while (bufi < bufe && buf[bufi] != '\n') {
1160 bufi++;
1161 }
1162 if (bufi < bufe) {
1163 buf[bufi] = 0;
1164 ALOGV("Processing line: %s\n", buf+bufp);
1165 hasspace = 0;
1166 while (bufp < bufi && isspace(buf[bufp])) {
1167 hasspace = 1;
1168 bufp++;
1169 }
1170 if (buf[bufp] == '#' || bufp == bufi) {
1171 // skip comments and empty lines.
1172 } else if (hasspace) {
1173 if (dstpkg[0] == 0) {
1174 ALOGW("Path before package line in %s%s: %s\n",
1175 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1176 } else if (srcpkg[0] == 0) {
1177 // Skip -- source package no longer exists.
1178 } else {
1179 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1180 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1181 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1182 movefileordir(srcpath, dstpath,
1183 strlen(dstpath)-strlen(buf+bufp),
1184 dstuid, dstgid, &s);
1185 }
1186 }
1187 } else {
1188 char* div = strchr(buf+bufp, ':');
1189 if (div == NULL) {
1190 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1191 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1192 } else {
1193 *div = 0;
1194 div++;
1195 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1196 strcpy(dstpkg, buf+bufp);
1197 } else {
1198 srcpkg[0] = dstpkg[0] = 0;
1199 ALOGW("Package name too long in %s%s: %s\n",
1200 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1201 }
1202 if (strlen(div) < PKG_NAME_MAX) {
1203 strcpy(srcpkg, div);
1204 } else {
1205 srcpkg[0] = dstpkg[0] = 0;
1206 ALOGW("Package name too long in %s%s: %s\n",
1207 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1208 }
1209 if (srcpkg[0] != 0) {
1210 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1211 if (lstat(srcpath, &s) < 0) {
1212 // Package no longer exists -- skip.
1213 srcpkg[0] = 0;
1214 }
1215 } else {
1216 srcpkg[0] = 0;
1217 ALOGW("Can't create path %s in %s%s\n",
1218 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1219 }
1220 if (srcpkg[0] != 0) {
1221 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1222 if (lstat(dstpath, &s) == 0) {
1223 dstuid = s.st_uid;
1224 dstgid = s.st_gid;
1225 } else {
1226 // Destination package doesn't
1227 // exist... due to original-package,
1228 // this is normal, so don't be
1229 // noisy about it.
1230 srcpkg[0] = 0;
1231 }
1232 } else {
1233 srcpkg[0] = 0;
1234 ALOGW("Can't create path %s in %s%s\n",
1235 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1236 }
1237 }
1238 ALOGV("Transfering from %s to %s: uid=%d\n",
1239 srcpkg, dstpkg, dstuid);
1240 }
1241 }
1242 }
1243 bufp = bufi+1;
1244 } else {
1245 if (bufp == 0) {
1246 if (bufp < bufe) {
1247 ALOGW("Line too long in %s%s, skipping: %s\n",
1248 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1249 }
1250 } else if (bufp < bufe) {
1251 memcpy(buf, buf+bufp, bufe-bufp);
1252 bufe -= bufp;
1253 bufp = 0;
1254 }
1255 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1256 if (readlen < 0) {
1257 ALOGW("Failure reading update commands in %s%s: %s\n",
1258 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1259 break;
1260 } else if (readlen == 0) {
1261 break;
1262 }
1263 bufe += readlen;
1264 buf[bufe] = 0;
1265 ALOGV("Read buf: %s\n", buf);
1266 }
1267 }
1268 close(subfd);
1269 }
1270 }
1271 closedir(d);
1272done:
1273 return 0;
1274}
1275
1276int linklib(const char* pkgname, const char* asecLibDir, int userId)
1277{
1278 char pkgdir[PKG_PATH_MAX];
1279 char libsymlink[PKG_PATH_MAX];
1280 struct stat s, libStat;
1281 int rc = 0;
1282
1283 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1284 ALOGE("cannot create package path\n");
1285 return -1;
1286 }
1287 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1288 ALOGE("cannot create package lib symlink origin path\n");
1289 return -1;
1290 }
1291
1292 if (stat(pkgdir, &s) < 0) return -1;
1293
1294 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1295 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1296 return -1;
1297 }
1298
1299 if (chmod(pkgdir, 0700) < 0) {
1300 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1301 rc = -1;
1302 goto out;
1303 }
1304
1305 if (lstat(libsymlink, &libStat) < 0) {
1306 if (errno != ENOENT) {
1307 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1308 rc = -1;
1309 goto out;
1310 }
1311 } else {
1312 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001313 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001314 rc = -1;
1315 goto out;
1316 }
1317 } else if (S_ISLNK(libStat.st_mode)) {
1318 if (unlink(libsymlink) < 0) {
1319 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1320 rc = -1;
1321 goto out;
1322 }
1323 }
1324 }
1325
1326 if (symlink(asecLibDir, libsymlink) < 0) {
1327 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1328 strerror(errno));
1329 rc = -errno;
1330 goto out;
1331 }
1332
1333out:
1334 if (chmod(pkgdir, s.st_mode) < 0) {
1335 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1336 rc = -errno;
1337 }
1338
1339 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1340 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1341 return -errno;
1342 }
1343
1344 return rc;
1345}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001346
1347static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1348{
1349 static const char *IDMAP_BIN = "/system/bin/idmap";
1350 static const size_t MAX_INT_LEN = 32;
1351 char idmap_str[MAX_INT_LEN];
1352
1353 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1354
1355 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1356 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1357}
1358
1359// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1360// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1361static int flatten_path(const char *prefix, const char *suffix,
1362 const char *overlay_path, char *idmap_path, size_t N)
1363{
1364 if (overlay_path == NULL || idmap_path == NULL) {
1365 return -1;
1366 }
1367 const size_t len_overlay_path = strlen(overlay_path);
1368 // will access overlay_path + 1 further below; requires absolute path
1369 if (len_overlay_path < 2 || *overlay_path != '/') {
1370 return -1;
1371 }
1372 const size_t len_idmap_root = strlen(prefix);
1373 const size_t len_suffix = strlen(suffix);
1374 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1375 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1376 // additions below would cause overflow
1377 return -1;
1378 }
1379 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1380 return -1;
1381 }
1382 memset(idmap_path, 0, N);
1383 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1384 char *ch = idmap_path + len_idmap_root;
1385 while (*ch != '\0') {
1386 if (*ch == '/') {
1387 *ch = '@';
1388 }
1389 ++ch;
1390 }
1391 return 0;
1392}
1393
1394int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1395{
1396 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1397
1398 int idmap_fd = -1;
1399 char idmap_path[PATH_MAX];
1400
1401 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1402 idmap_path, sizeof(idmap_path)) == -1) {
1403 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1404 goto fail;
1405 }
1406
1407 unlink(idmap_path);
1408 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1409 if (idmap_fd < 0) {
1410 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1411 goto fail;
1412 }
1413 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1414 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1415 goto fail;
1416 }
1417 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1418 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1419 goto fail;
1420 }
1421
1422 pid_t pid;
1423 pid = fork();
1424 if (pid == 0) {
1425 /* child -- drop privileges before continuing */
1426 if (setgid(uid) != 0) {
1427 ALOGE("setgid(%d) failed during idmap\n", uid);
1428 exit(1);
1429 }
1430 if (setuid(uid) != 0) {
1431 ALOGE("setuid(%d) failed during idmap\n", uid);
1432 exit(1);
1433 }
1434 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1435 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1436 exit(1);
1437 }
1438
1439 run_idmap(target_apk, overlay_apk, idmap_fd);
1440 exit(1); /* only if exec call to idmap failed */
1441 } else {
1442 int status = wait_child(pid);
1443 if (status != 0) {
1444 ALOGE("idmap failed, status=0x%04x\n", status);
1445 goto fail;
1446 }
1447 }
1448
1449 close(idmap_fd);
1450 return 0;
1451fail:
1452 if (idmap_fd >= 0) {
1453 close(idmap_fd);
1454 unlink(idmap_path);
1455 }
1456 return -1;
1457}
Robert Craige9887e42014-02-20 10:25:56 -05001458
Robert Craigda30dc72014-03-27 10:21:12 -04001459int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001460{
Robert Craigda30dc72014-03-27 10:21:12 -04001461 struct dirent *entry;
1462 DIR *d;
1463 struct stat s;
1464 char *userdir;
1465 char *primarydir;
1466 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001467 int ret = 0;
1468
Robert Craigda30dc72014-03-27 10:21:12 -04001469 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1470 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1471
1472 if (!pkgName || !seinfo) {
1473 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001474 return -1;
1475 }
1476
Robert Craigda30dc72014-03-27 10:21:12 -04001477 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1478 return -1;
1479 }
1480
1481 // Relabel for primary user.
1482 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1483 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001484 ret |= -1;
1485 }
1486
Robert Craigda30dc72014-03-27 10:21:12 -04001487 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1488 free(primarydir);
1489 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001490 }
1491
Robert Craigda30dc72014-03-27 10:21:12 -04001492 // Relabel package directory for all secondary users.
1493 d = opendir(userdir);
1494 if (d == NULL) {
1495 free(primarydir);
1496 free(userdir);
1497 return -1;
1498 }
1499
1500 while ((entry = readdir(d))) {
1501 if (entry->d_type != DT_DIR) {
1502 continue;
1503 }
1504
1505 const char *user = entry->d_name;
1506 // Ignore "." and ".."
1507 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1508 continue;
1509 }
1510
1511 // user directories start with a number
1512 if (user[0] < '0' || user[0] > '9') {
1513 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1514 continue;
1515 }
1516
1517 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1518 continue;
1519 }
1520
1521 if (stat(pkgdir, &s) < 0) {
1522 free(pkgdir);
1523 continue;
1524 }
1525
1526 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1527 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1528 ret |= -1;
1529 }
1530 free(pkgdir);
1531 }
1532
1533 closedir(d);
1534 free(primarydir);
1535 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001536 return ret;
1537}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001538
1539static int prune_dex_exclusion_predicate(const char *file_name, const int is_dir)
1540{
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001541 // Exclude all directories. The top level command will be
1542 // given a list of ISA specific directories that are assumed
1543 // to be flat.
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001544 if (is_dir) {
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001545 return 1;
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001546 }
1547
1548
1549 // Don't exclude regular files that start with the list
1550 // of prefixes.
1551 static const char data_app_prefix[] = "data@app@";
1552 static const char data_priv_app_prefix[] = "data@priv-app@";
1553 if (!strncmp(file_name, data_app_prefix, sizeof(data_app_prefix) - 1) ||
1554 !strncmp(file_name, data_priv_app_prefix, sizeof(data_priv_app_prefix) - 1)) {
1555 return 0;
1556 }
1557
1558 // Exclude all regular files that don't start with the prefix "data@app@" or
1559 // "data@priv-app@".
1560 return 1;
1561}
1562
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001563int prune_dex_cache(const char* subdir) {
1564 // "." is handled as a special case, and refers to
1565 // DALVIK_CACHE_PREFIX (usually /data/dalvik-cache).
1566 const bool is_dalvik_cache_root = !strcmp(subdir, ".");
1567
1568 // Don't allow the path to contain "." or ".." except for the
1569 // special case above. This is much stricter than we need to be,
1570 // but there's no good reason to support them.
1571 if (strchr(subdir, '.' ) != NULL && !is_dalvik_cache_root) {
1572 return -1;
1573 }
1574
1575 if (!is_dalvik_cache_root) {
1576 char full_path[PKG_PATH_MAX];
1577 snprintf(full_path, sizeof(full_path), "%s%s", DALVIK_CACHE_PREFIX, subdir);
1578 return delete_dir_contents(full_path, 0, &prune_dex_exclusion_predicate);
1579 }
1580
1581
1582 // When subdir == ".", clean the contents of the top level
1583 // dalvik-cache directory.
1584 return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate);
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001585}