blob: d54c545930c87b947376cd741d3571f09cdb6588 [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"
20#include <diskusage/dirsize.h>
21#include <selinux/android.h>
22
23/* Directory records that are used in execution of commands. */
24dir_rec_t android_data_dir;
25dir_rec_t android_asec_dir;
26dir_rec_t android_app_dir;
27dir_rec_t android_app_private_dir;
28dir_rec_t android_app_lib_dir;
29dir_rec_t android_media_dir;
30dir_rec_array_t android_system_dirs;
31
Robert Craig4d3fd4e2013-03-25 06:33:03 -040032int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070033{
34 char pkgdir[PKG_PATH_MAX];
35 char libsymlink[PKG_PATH_MAX];
36 char applibdir[PKG_PATH_MAX];
37 struct stat libStat;
38
39 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
40 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
41 return -1;
42 }
43
44 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
45 ALOGE("cannot create package path\n");
46 return -1;
47 }
48
49 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
50 ALOGE("cannot create package lib symlink origin path\n");
51 return -1;
52 }
53
54 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
55 ALOGE("cannot create package lib symlink dest path\n");
56 return -1;
57 }
58
Nick Kralevicha2d838a2013-01-09 16:00:35 -080059 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070060 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
61 return -1;
62 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080063 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070064 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
65 unlink(pkgdir);
66 return -1;
67 }
68
69 if (lstat(libsymlink, &libStat) < 0) {
70 if (errno != ENOENT) {
71 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
72 return -1;
73 }
74 } else {
75 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +010076 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070077 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
78 return -1;
79 }
80 } else if (S_ISLNK(libStat.st_mode)) {
81 if (unlink(libsymlink) < 0) {
82 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
83 return -1;
84 }
85 }
86 }
87
Stephen Smalley26288202014-02-07 09:16:46 -050088 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070089 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
90 unlink(libsymlink);
91 unlink(pkgdir);
92 return -errno;
93 }
94
Stephen Smalley3a983892014-05-13 12:53:07 -040095 if (symlink(applibdir, libsymlink) < 0) {
96 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
97 strerror(errno));
98 unlink(pkgdir);
99 return -1;
100 }
101
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102 if (chown(pkgdir, uid, gid) < 0) {
103 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
104 unlink(libsymlink);
105 unlink(pkgdir);
106 return -1;
107 }
108
109 return 0;
110}
111
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700112int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700113{
114 char pkgdir[PKG_PATH_MAX];
115
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700116 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117 return -1;
118
Dave Allisond9370732014-01-30 14:19:23 -0800119 remove_profile_file(pkgname);
120
Mike Lockwood94afecf2012-10-24 10:45:23 -0700121 /* delete contents AND directory, no exceptions */
122 return delete_dir_contents(pkgdir, 1, NULL);
123}
124
125int renamepkg(const char *oldpkgname, const char *newpkgname)
126{
127 char oldpkgdir[PKG_PATH_MAX];
128 char newpkgdir[PKG_PATH_MAX];
129
130 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
131 return -1;
132 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
133 return -1;
134
135 if (rename(oldpkgdir, newpkgdir) < 0) {
136 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
137 return -errno;
138 }
139 return 0;
140}
141
142int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
143{
144 char pkgdir[PKG_PATH_MAX];
145 struct stat s;
146 int rc = 0;
147
148 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
149 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
150 return -1;
151 }
152
153 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
154 ALOGE("cannot create package path\n");
155 return -1;
156 }
157
158 if (stat(pkgdir, &s) < 0) return -1;
159
160 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700161 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 -0700162 return -1;
163 }
164
165 if (chmod(pkgdir, 0751) < 0) {
166 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
167 unlink(pkgdir);
168 return -errno;
169 }
170 if (chown(pkgdir, uid, gid) < 0) {
171 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
172 unlink(pkgdir);
173 return -errno;
174 }
175
176 return 0;
177}
178
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100179static int lib_dir_matcher(const char* file_name, const int is_dir) {
180 return is_dir && !strcmp(file_name, "lib");
181}
182
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700183int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184{
185 char pkgdir[PKG_PATH_MAX];
186
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700187 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188 return -1;
189
190 /* delete contents, excluding "lib", but not the directory itself */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100191 return delete_dir_contents(pkgdir, 0, &lib_dir_matcher);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192}
193
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700194int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195{
196 char pkgdir[PKG_PATH_MAX];
197 char applibdir[PKG_PATH_MAX];
198 char libsymlink[PKG_PATH_MAX];
199 struct stat libStat;
200
201 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700202 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203 return -1;
204 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700205 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206 ALOGE("cannot create package lib symlink origin path\n");
207 return -1;
208 }
209 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
210 ALOGE("cannot create package lib symlink dest path\n");
211 return -1;
212 }
213
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800214 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
216 return -errno;
217 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800218 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
220 unlink(pkgdir);
221 return -errno;
222 }
223
224 if (lstat(libsymlink, &libStat) < 0) {
225 if (errno != ENOENT) {
226 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
227 unlink(pkgdir);
228 return -1;
229 }
230 } else {
231 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100232 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 ALOGE("couldn't delete lib directory during install for non-primary: %s",
234 libsymlink);
235 unlink(pkgdir);
236 return -1;
237 }
238 } else if (S_ISLNK(libStat.st_mode)) {
239 if (unlink(libsymlink) < 0) {
240 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
241 libsymlink);
242 unlink(pkgdir);
243 return -1;
244 }
245 }
246 }
247
Stephen Smalley26288202014-02-07 09:16:46 -0500248 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
250 unlink(libsymlink);
251 unlink(pkgdir);
252 return -errno;
253 }
254
Stephen Smalley3a983892014-05-13 12:53:07 -0400255 if (symlink(applibdir, libsymlink) < 0) {
256 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
257 applibdir, strerror(errno));
258 unlink(pkgdir);
259 return -1;
260 }
261
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 if (chown(pkgdir, uid, uid) < 0) {
263 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
264 unlink(libsymlink);
265 unlink(pkgdir);
266 return -errno;
267 }
268
269 return 0;
270}
271
Robin Lee7c8bec02014-06-10 18:46:26 +0100272int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273{
Robin Lee095c7632014-04-25 15:05:19 +0100274 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275 return -1;
276 }
277
278 return 0;
279}
280
Robin Lee095c7632014-04-25 15:05:19 +0100281int delete_user(userid_t userid)
282{
283 int status = 0;
284
285 char data_path[PKG_PATH_MAX];
286 if ((create_user_path(data_path, userid) != 0)
287 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
288 status = -1;
289 }
290
291 char media_path[PATH_MAX];
292 if ((create_user_media_path(media_path, userid) != 0)
293 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
294 status = -1;
295 }
296
297 char config_path[PATH_MAX];
298 if ((create_user_config_path(config_path, userid) != 0)
299 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
300 status = -1;
301 }
302
303 return status;
304}
305
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700306int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307{
308 char cachedir[PKG_PATH_MAX];
309
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700310 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 return -1;
312
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700313 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100314 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700315}
316
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700317int delete_code_cache(const char *pkgname, userid_t userid)
318{
319 char codecachedir[PKG_PATH_MAX];
320
321 if (create_pkg_path(codecachedir, pkgname, CODE_CACHE_DIR_POSTFIX, userid))
322 return -1;
323
324 /* delete contents, not the directory, no exceptions */
325 return delete_dir_contents(codecachedir, 0, NULL);
326}
327
Mike Lockwood94afecf2012-10-24 10:45:23 -0700328/* Try to ensure free_size bytes of storage are available.
329 * Returns 0 on success.
330 * This is rather simple-minded because doing a full LRU would
331 * be potentially memory-intensive, and without atime it would
332 * also require that apps constantly modify file metadata even
333 * when just reading from the cache, which is pretty awful.
334 */
335int free_cache(int64_t free_size)
336{
337 cache_t* cache;
338 int64_t avail;
339 DIR *d;
340 struct dirent *de;
341 char tmpdir[PATH_MAX];
342 char *dirpos;
343
344 avail = data_disk_free();
345 if (avail < 0) return -1;
346
347 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
348 if (avail >= free_size) return 0;
349
350 cache = start_cache_collection();
351
352 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700353 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700354 //ALOGI("adding cache files from %s\n", tmpdir);
355 add_cache_files(cache, tmpdir, "cache");
356 }
357
358 // Search for other users and add any cache files from them.
359 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
360 SECONDARY_USER_PREFIX);
361 dirpos = tmpdir + strlen(tmpdir);
362 d = opendir(tmpdir);
363 if (d != NULL) {
364 while ((de = readdir(d))) {
365 if (de->d_type == DT_DIR) {
366 const char *name = de->d_name;
367 /* always skip "." and ".." */
368 if (name[0] == '.') {
369 if (name[1] == 0) continue;
370 if ((name[1] == '.') && (name[2] == 0)) continue;
371 }
372 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
373 strcpy(dirpos, name);
374 //ALOGI("adding cache files from %s\n", tmpdir);
375 add_cache_files(cache, tmpdir, "cache");
376 } else {
377 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
378 }
379 }
380 }
381 closedir(d);
382 }
383
384 // Collect cache files on external storage for all users (if it is mounted as part
385 // of the internal storage).
386 strcpy(tmpdir, android_media_dir.path);
387 dirpos = tmpdir + strlen(tmpdir);
388 d = opendir(tmpdir);
389 if (d != NULL) {
390 while ((de = readdir(d))) {
391 if (de->d_type == DT_DIR) {
392 const char *name = de->d_name;
393 /* skip any dir that doesn't start with a number, so not a user */
394 if (name[0] < '0' || name[0] > '9') {
395 continue;
396 }
397 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
398 strcpy(dirpos, name);
399 if (lookup_media_dir(tmpdir, "Android") == 0
400 && lookup_media_dir(tmpdir, "data") == 0) {
401 //ALOGI("adding cache files from %s\n", tmpdir);
402 add_cache_files(cache, tmpdir, "cache");
403 }
404 } else {
405 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
406 }
407 }
408 }
409 closedir(d);
410 }
411
412 clear_cache_files(cache, free_size);
413 finish_cache_collection(cache);
414
415 return data_disk_free() >= free_size ? 0 : -1;
416}
417
Narayan Kamath1b400322014-04-11 13:17:00 +0100418int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700419{
420 char src_dex[PKG_PATH_MAX];
421 char dst_dex[PKG_PATH_MAX];
422
423 if (validate_apk_path(src)) return -1;
424 if (validate_apk_path(dst)) return -1;
425
Narayan Kamath1b400322014-04-11 13:17:00 +0100426 if (create_cache_path(src_dex, src, instruction_set)) return -1;
427 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700428
429 ALOGV("move %s -> %s\n", src_dex, dst_dex);
430 if (rename(src_dex, dst_dex) < 0) {
431 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
432 return -1;
433 } else {
434 return 0;
435 }
436}
437
Narayan Kamath1b400322014-04-11 13:17:00 +0100438int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700439{
440 char dex_path[PKG_PATH_MAX];
441
442 if (validate_apk_path(path)) return -1;
Narayan Kamath1b400322014-04-11 13:17:00 +0100443 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700444
445 ALOGV("unlink %s\n", dex_path);
446 if (unlink(dex_path) < 0) {
447 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
448 return -1;
449 } else {
450 return 0;
451 }
452}
453
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700454int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700455 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100456 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
457 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700458{
459 DIR *d;
460 int dfd;
461 struct dirent *de;
462 struct stat s;
463 char path[PKG_PATH_MAX];
464
465 int64_t codesize = 0;
466 int64_t datasize = 0;
467 int64_t cachesize = 0;
468 int64_t asecsize = 0;
469
470 /* count the source apk as code -- but only if it's not
471 * on the /system partition and its not on the sdcard.
472 */
473 if (validate_system_app_path(apkpath) &&
474 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
475 if (stat(apkpath, &s) == 0) {
476 codesize += stat_size(&s);
477 }
478 }
479 /* count the forward locked apk as code if it is given
480 */
481 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
482 if (stat(fwdlock_apkpath, &s) == 0) {
483 codesize += stat_size(&s);
484 }
485 }
486 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100487 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700488 if (stat(path, &s) == 0) {
489 codesize += stat_size(&s);
490 }
491 }
492
493 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700494 if (libdirpath != NULL && libdirpath[0] != '!') {
495 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700496 if (d != NULL) {
497 dfd = dirfd(d);
498 codesize += calculate_dir_size(dfd);
499 closedir(d);
500 }
501 }
502
503 /* compute asec size if it is given
504 */
505 if (asecpath != NULL && asecpath[0] != '!') {
506 if (stat(asecpath, &s) == 0) {
507 asecsize += stat_size(&s);
508 }
509 }
510
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700511 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700512 goto done;
513 }
514
515 d = opendir(path);
516 if (d == NULL) {
517 goto done;
518 }
519 dfd = dirfd(d);
520
521 /* most stuff in the pkgdir is data, except for the "cache"
522 * directory and below, which is cache, and the "lib" directory
523 * and below, which is code...
524 */
525 while ((de = readdir(d))) {
526 const char *name = de->d_name;
527
528 if (de->d_type == DT_DIR) {
529 int subfd;
530 int64_t statsize = 0;
531 int64_t dirsize = 0;
532 /* always skip "." and ".." */
533 if (name[0] == '.') {
534 if (name[1] == 0) continue;
535 if ((name[1] == '.') && (name[2] == 0)) continue;
536 }
537 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
538 statsize = stat_size(&s);
539 }
540 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
541 if (subfd >= 0) {
542 dirsize = calculate_dir_size(subfd);
543 }
544 if(!strcmp(name,"lib")) {
545 codesize += dirsize + statsize;
546 } else if(!strcmp(name,"cache")) {
547 cachesize += dirsize + statsize;
548 } else {
549 datasize += dirsize + statsize;
550 }
551 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
552 // This is the symbolic link to the application's library
553 // code. We'll count this as code instead of data, since
554 // it is not something that the app creates.
555 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
556 codesize += stat_size(&s);
557 }
558 } else {
559 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
560 datasize += stat_size(&s);
561 }
562 }
563 }
564 closedir(d);
565done:
566 *_codesize = codesize;
567 *_datasize = datasize;
568 *_cachesize = cachesize;
569 *_asecsize = asecsize;
570 return 0;
571}
572
Narayan Kamath1b400322014-04-11 13:17:00 +0100573int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700574{
575 char *tmp;
576 int srclen;
577 int dstlen;
578
579 srclen = strlen(src);
580
581 /* demand that we are an absolute path */
582 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
583 return -1;
584 }
585
586 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
587 return -1;
588 }
589
Dave Allisond9370732014-01-30 14:19:23 -0800590 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100591 strlen(instruction_set) +
592 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800593
Mike Lockwood94afecf2012-10-24 10:45:23 -0700594 if (dstlen > PKG_PATH_MAX) {
595 return -1;
596 }
597
Narayan Kamath1b400322014-04-11 13:17:00 +0100598 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700599 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100600 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700601 src + 1, /* skip the leading / */
602 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800603
Narayan Kamath1b400322014-04-11 13:17:00 +0100604 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700605 if (*tmp == '/') {
606 *tmp = '@';
607 }
608 }
609
610 return 0;
611}
612
613static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800614 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700615{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800616 /* platform-specific flags affecting optimization and verification */
617 char dexopt_flags[PROPERTY_VALUE_MAX];
618 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
619 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
620
Mike Lockwood94afecf2012-10-24 10:45:23 -0700621 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
622 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
623 char zip_num[MAX_INT_LEN];
624 char odex_num[MAX_INT_LEN];
625
626 sprintf(zip_num, "%d", zip_fd);
627 sprintf(odex_num, "%d", odex_fd);
628
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700629 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700630 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
631 dexopt_flags, (char*) NULL);
632 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
633}
634
Alex Light43c5d302014-07-21 12:23:48 -0700635static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
636 const char* output_file_name, const char *pkgname, const char *instruction_set)
637{
638 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
639 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
640
641 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
642 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
643 ALOGE("Instruction set %s longer than max length of %d",
644 instruction_set, MAX_INSTRUCTION_SET_LEN);
645 return;
646 }
647
648 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
649 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
650 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
651 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
652 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
653 // The caller has already gotten all the locks we need.
654 const char* no_lock_arg = "--no-lock-output";
655 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
656 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
657 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
658 ALOGE("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
659 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
660
661 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
662 char* argv[7];
663 argv[0] = (char*) PATCHOAT_BIN;
664 argv[1] = (char*) patched_image_location_arg;
665 argv[2] = (char*) no_lock_arg;
666 argv[3] = instruction_set_arg;
667 argv[4] = output_oat_fd_arg;
668 argv[5] = input_oat_fd_arg;
669 argv[6] = NULL;
670
671 execv(PATCHOAT_BIN, (char* const *)argv);
672 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
673}
674
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700675static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Narayan Kamath1b400322014-04-11 13:17:00 +0100676 const char* output_file_name, const char *pkgname, const char *instruction_set)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700677{
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700678 char prop_buf[PROPERTY_VALUE_MAX];
679 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
680
681 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
682 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
683
684 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
685 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
686
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800687 char dex2oat_flags[PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100688 bool have_dex2oat_flags = property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, NULL) > 0;
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800689 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
690
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700691 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700692
Brian Carlstrom53e07762014-06-27 14:15:19 -0700693 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700694
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700695 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100696 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
697
698 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
699 ALOGE("Instruction set %s longer than max length of %d",
700 instruction_set, MAX_INSTRUCTION_SET_LEN);
701 return;
702 }
703
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700704 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
705 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
706 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700707 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100708 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravle4fdff462014-06-06 16:58:43 +0100709 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
710 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700711 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
712 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700713
714 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
715 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
716 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
717 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100718 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle57c69c32014-06-06 14:42:16 +0100719
Calin Juravle4fdff462014-06-06 16:58:43 +0100720 bool have_profile_file = false;
721 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100722 if (profiler && (strcmp(pkgname, "*") != 0)) {
723 char profile_file[PKG_PATH_MAX];
724 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800725 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100726 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100727 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100728 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100729 have_profile_file = true;
730 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
731 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
732 "--top-k-profile-threshold=%s", prop_buf);
733 have_top_k_profile_threshold = true;
734 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100735 }
Dave Allisond9370732014-01-30 14:19:23 -0800736 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700737
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700738 if (have_dex2oat_Xms_flag) {
739 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
740 }
741 if (have_dex2oat_Xmx_flag) {
742 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
743 }
744
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700745 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100746
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700747 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravle4fdff462014-06-06 16:58:43 +0100748 + (have_profile_file ? 1 : 0)
749 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700750 + (have_dex2oat_Xms_flag ? 2 : 0)
751 + (have_dex2oat_Xmx_flag ? 2 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100752 + (have_dex2oat_flags ? 1 : 0)];
753 int i = 0;
754 argv[i++] = (char*)DEX2OAT_BIN;
755 argv[i++] = zip_fd_arg;
756 argv[i++] = zip_location_arg;
757 argv[i++] = oat_fd_arg;
758 argv[i++] = oat_location_arg;
759 argv[i++] = instruction_set_arg;
760 if (have_profile_file) {
761 argv[i++] = profile_file_arg;
762 }
763 if (have_top_k_profile_threshold) {
764 argv[i++] = top_k_profile_threshold_arg;
765 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700766 if (have_dex2oat_Xms_flag) {
767 argv[i++] = (char*)RUNTIME_ARG;
768 argv[i++] = dex2oat_Xms_arg;
769 }
770 if (have_dex2oat_Xmx_flag) {
771 argv[i++] = (char*)RUNTIME_ARG;
772 argv[i++] = dex2oat_Xmx_arg;
773 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100774 if (have_dex2oat_flags) {
775 argv[i++] = dex2oat_flags;
776 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700777 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100778 argv[i] = NULL;
779
780 execv(DEX2OAT_BIN, (char* const *)argv);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700781 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
782}
783
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100784static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700785{
786 int status;
787 pid_t got_pid;
788
Mike Lockwood94afecf2012-10-24 10:45:23 -0700789 while (1) {
790 got_pid = waitpid(pid, &status, 0);
791 if (got_pid == -1 && errno == EINTR) {
792 printf("waitpid interrupted, retrying\n");
793 } else {
794 break;
795 }
796 }
797 if (got_pid != pid) {
798 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
799 (int) pid, (int) got_pid, strerror(errno));
800 return 1;
801 }
802
803 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700804 return 0;
805 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700806 return status; /* always nonzero */
807 }
808}
809
Dave Allisond9370732014-01-30 14:19:23 -0800810int dexopt(const char *apk_path, uid_t uid, int is_public,
Alex Light43c5d302014-07-21 12:23:48 -0700811 const char *pkgname, const char *instruction_set,
812 int is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700813{
814 struct utimbuf ut;
Alex Light43c5d302014-07-21 12:23:48 -0700815 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700816 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700817 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700818 char *end;
Alex Light43c5d302014-07-21 12:23:48 -0700819 const char *input_file;
820 char in_odex_path[PKG_PATH_MAX];
821 int res, input_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700822
Mike Lockwood94afecf2012-10-24 10:45:23 -0700823 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
824 return -1;
825 }
826
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800827 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700828 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700829
Alex Light43c5d302014-07-21 12:23:48 -0700830 if (is_patchoat && strncmp(persist_sys_dalvik_vm_lib, "libart", 6) != 0) {
831 /* We may only patch if we are libart */
832 ALOGE("Patching is only supported in libart\n");
833 return -1;
834 }
835
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700836 /* Before anything else: is there a .odex file? If so, we have
837 * precompiled the apk and there is nothing to do here.
Alex Light43c5d302014-07-21 12:23:48 -0700838 *
839 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700840 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800841 strcpy(out_path, apk_path);
842 end = strrchr(out_path, '.');
Alex Light43c5d302014-07-21 12:23:48 -0700843 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800844 strcpy(end, ".odex");
845 if (stat(out_path, &dex_stat) == 0) {
846 return 0;
847 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700848 }
849
Narayan Kamath1b400322014-04-11 13:17:00 +0100850 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700851 return -1;
852 }
853
Alex Light43c5d302014-07-21 12:23:48 -0700854 if (is_patchoat) {
855 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
856 strcpy(in_odex_path, apk_path);
857 end = strrchr(in_odex_path, '/');
858 if (end == NULL) {
859 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
860 return -1;
861 }
862 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
863 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
864 strcat(in_odex_path, apk_end);
865 end = strrchr(in_odex_path, '.');
866 if (end == NULL) {
867 return -1;
868 }
869 strcpy(end + 1, "odex");
870 input_file = in_odex_path;
871 } else {
872 input_file = apk_path;
873 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700874
Alex Light43c5d302014-07-21 12:23:48 -0700875 memset(&input_stat, 0, sizeof(input_stat));
876 stat(input_file, &input_stat);
877
878 input_fd = open(input_file, O_RDONLY, 0);
879 if (input_fd < 0) {
880 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700881 return -1;
882 }
883
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700884 unlink(out_path);
885 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
886 if (out_fd < 0) {
887 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700888 goto fail;
889 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700890 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700891 S_IRUSR|S_IWUSR|S_IRGRP |
892 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700893 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700894 goto fail;
895 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700896 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
897 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700898 goto fail;
899 }
900
Dave Allisond9370732014-01-30 14:19:23 -0800901 // Create profile file if there is a package name present.
902 if (strcmp(pkgname, "*") != 0) {
903 create_profile_file(pkgname, uid);
904 }
905
906
Alex Light43c5d302014-07-21 12:23:48 -0700907 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700908
909 pid_t pid;
910 pid = fork();
911 if (pid == 0) {
912 /* child -- drop privileges before continuing */
913 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700914 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700915 exit(64);
916 }
917 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700918 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700919 exit(65);
920 }
921 // drop capabilities
922 struct __user_cap_header_struct capheader;
923 struct __user_cap_data_struct capdata[2];
924 memset(&capheader, 0, sizeof(capheader));
925 memset(&capdata, 0, sizeof(capdata));
926 capheader.version = _LINUX_CAPABILITY_VERSION_3;
927 if (capset(&capheader, &capdata[0]) < 0) {
928 ALOGE("capset failed: %s\n", strerror(errno));
929 exit(66);
930 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700931 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
932 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700933 exit(67);
934 }
935
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700936 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700937 run_dexopt(input_fd, out_fd, input_file, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700938 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700939 if (is_patchoat) {
940 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
941 } else {
942 run_dex2oat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
943 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700944 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700945 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700946 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700947 exit(68); /* only get here on exec failure */
948 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100949 res = wait_child(pid);
950 if (res == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700951 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100952 } else {
Alex Light43c5d302014-07-21 12:23:48 -0700953 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700954 goto fail;
955 }
956 }
957
Alex Light43c5d302014-07-21 12:23:48 -0700958 ut.actime = input_stat.st_atime;
959 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700960 utime(out_path, &ut);
961
962 close(out_fd);
Alex Light43c5d302014-07-21 12:23:48 -0700963 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700964 return 0;
965
966fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700967 if (out_fd >= 0) {
968 close(out_fd);
969 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700970 }
Alex Light43c5d302014-07-21 12:23:48 -0700971 if (input_fd >= 0) {
972 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700973 }
974 return -1;
975}
976
977void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
978 struct stat* statbuf)
979{
980 while (path[basepos] != 0) {
981 if (path[basepos] == '/') {
982 path[basepos] = 0;
983 if (lstat(path, statbuf) < 0) {
984 ALOGV("Making directory: %s\n", path);
985 if (mkdir(path, mode) == 0) {
986 chown(path, uid, gid);
987 } else {
988 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
989 }
990 }
991 path[basepos] = '/';
992 basepos++;
993 }
994 basepos++;
995 }
996}
997
998int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
999 int dstuid, int dstgid, struct stat* statbuf)
1000{
1001 DIR *d;
1002 struct dirent *de;
1003 int res;
1004
1005 int srcend = strlen(srcpath);
1006 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001007
Mike Lockwood94afecf2012-10-24 10:45:23 -07001008 if (lstat(srcpath, statbuf) < 0) {
1009 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1010 return 1;
1011 }
Dave Allisond9370732014-01-30 14:19:23 -08001012
Mike Lockwood94afecf2012-10-24 10:45:23 -07001013 if ((statbuf->st_mode&S_IFDIR) == 0) {
1014 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1015 dstuid, dstgid, statbuf);
1016 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1017 if (rename(srcpath, dstpath) >= 0) {
1018 if (chown(dstpath, dstuid, dstgid) < 0) {
1019 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1020 unlink(dstpath);
1021 return 1;
1022 }
1023 } else {
1024 ALOGW("Unable to rename %s to %s: %s\n",
1025 srcpath, dstpath, strerror(errno));
1026 return 1;
1027 }
1028 return 0;
1029 }
1030
1031 d = opendir(srcpath);
1032 if (d == NULL) {
1033 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1034 return 1;
1035 }
1036
1037 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001038
Mike Lockwood94afecf2012-10-24 10:45:23 -07001039 while ((de = readdir(d))) {
1040 const char *name = de->d_name;
1041 /* always skip "." and ".." */
1042 if (name[0] == '.') {
1043 if (name[1] == 0) continue;
1044 if ((name[1] == '.') && (name[2] == 0)) continue;
1045 }
Dave Allisond9370732014-01-30 14:19:23 -08001046
Mike Lockwood94afecf2012-10-24 10:45:23 -07001047 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1048 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1049 continue;
1050 }
Dave Allisond9370732014-01-30 14:19:23 -08001051
Mike Lockwood94afecf2012-10-24 10:45:23 -07001052 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1053 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1054 continue;
1055 }
Dave Allisond9370732014-01-30 14:19:23 -08001056
Mike Lockwood94afecf2012-10-24 10:45:23 -07001057 srcpath[srcend] = dstpath[dstend] = '/';
1058 strcpy(srcpath+srcend+1, name);
1059 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001060
Mike Lockwood94afecf2012-10-24 10:45:23 -07001061 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1062 res = 1;
1063 }
Dave Allisond9370732014-01-30 14:19:23 -08001064
Mike Lockwood94afecf2012-10-24 10:45:23 -07001065 // Note: we will be leaving empty directories behind in srcpath,
1066 // but that is okay, the package manager will be erasing all of the
1067 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001068
Mike Lockwood94afecf2012-10-24 10:45:23 -07001069 srcpath[srcend] = dstpath[dstend] = 0;
1070 }
Dave Allisond9370732014-01-30 14:19:23 -08001071
Mike Lockwood94afecf2012-10-24 10:45:23 -07001072 closedir(d);
1073 return res;
1074}
1075
1076int movefiles()
1077{
1078 DIR *d;
1079 int dfd, subfd;
1080 struct dirent *de;
1081 struct stat s;
1082 char buf[PKG_PATH_MAX+1];
1083 int bufp, bufe, bufi, readlen;
1084
1085 char srcpkg[PKG_NAME_MAX];
1086 char dstpkg[PKG_NAME_MAX];
1087 char srcpath[PKG_PATH_MAX];
1088 char dstpath[PKG_PATH_MAX];
1089 int dstuid=-1, dstgid=-1;
1090 int hasspace;
1091
1092 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1093 if (d == NULL) {
1094 goto done;
1095 }
1096 dfd = dirfd(d);
1097
1098 /* Iterate through all files in the directory, executing the
1099 * file movements requested there-in.
1100 */
1101 while ((de = readdir(d))) {
1102 const char *name = de->d_name;
1103
1104 if (de->d_type == DT_DIR) {
1105 continue;
1106 } else {
1107 subfd = openat(dfd, name, O_RDONLY);
1108 if (subfd < 0) {
1109 ALOGW("Unable to open update commands at %s%s\n",
1110 UPDATE_COMMANDS_DIR_PREFIX, name);
1111 continue;
1112 }
Dave Allisond9370732014-01-30 14:19:23 -08001113
Mike Lockwood94afecf2012-10-24 10:45:23 -07001114 bufp = 0;
1115 bufe = 0;
1116 buf[PKG_PATH_MAX] = 0;
1117 srcpkg[0] = dstpkg[0] = 0;
1118 while (1) {
1119 bufi = bufp;
1120 while (bufi < bufe && buf[bufi] != '\n') {
1121 bufi++;
1122 }
1123 if (bufi < bufe) {
1124 buf[bufi] = 0;
1125 ALOGV("Processing line: %s\n", buf+bufp);
1126 hasspace = 0;
1127 while (bufp < bufi && isspace(buf[bufp])) {
1128 hasspace = 1;
1129 bufp++;
1130 }
1131 if (buf[bufp] == '#' || bufp == bufi) {
1132 // skip comments and empty lines.
1133 } else if (hasspace) {
1134 if (dstpkg[0] == 0) {
1135 ALOGW("Path before package line in %s%s: %s\n",
1136 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1137 } else if (srcpkg[0] == 0) {
1138 // Skip -- source package no longer exists.
1139 } else {
1140 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1141 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1142 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1143 movefileordir(srcpath, dstpath,
1144 strlen(dstpath)-strlen(buf+bufp),
1145 dstuid, dstgid, &s);
1146 }
1147 }
1148 } else {
1149 char* div = strchr(buf+bufp, ':');
1150 if (div == NULL) {
1151 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1152 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1153 } else {
1154 *div = 0;
1155 div++;
1156 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1157 strcpy(dstpkg, buf+bufp);
1158 } else {
1159 srcpkg[0] = dstpkg[0] = 0;
1160 ALOGW("Package name too long in %s%s: %s\n",
1161 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1162 }
1163 if (strlen(div) < PKG_NAME_MAX) {
1164 strcpy(srcpkg, div);
1165 } else {
1166 srcpkg[0] = dstpkg[0] = 0;
1167 ALOGW("Package name too long in %s%s: %s\n",
1168 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1169 }
1170 if (srcpkg[0] != 0) {
1171 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1172 if (lstat(srcpath, &s) < 0) {
1173 // Package no longer exists -- skip.
1174 srcpkg[0] = 0;
1175 }
1176 } else {
1177 srcpkg[0] = 0;
1178 ALOGW("Can't create path %s in %s%s\n",
1179 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1180 }
1181 if (srcpkg[0] != 0) {
1182 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1183 if (lstat(dstpath, &s) == 0) {
1184 dstuid = s.st_uid;
1185 dstgid = s.st_gid;
1186 } else {
1187 // Destination package doesn't
1188 // exist... due to original-package,
1189 // this is normal, so don't be
1190 // noisy about it.
1191 srcpkg[0] = 0;
1192 }
1193 } else {
1194 srcpkg[0] = 0;
1195 ALOGW("Can't create path %s in %s%s\n",
1196 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1197 }
1198 }
1199 ALOGV("Transfering from %s to %s: uid=%d\n",
1200 srcpkg, dstpkg, dstuid);
1201 }
1202 }
1203 }
1204 bufp = bufi+1;
1205 } else {
1206 if (bufp == 0) {
1207 if (bufp < bufe) {
1208 ALOGW("Line too long in %s%s, skipping: %s\n",
1209 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1210 }
1211 } else if (bufp < bufe) {
1212 memcpy(buf, buf+bufp, bufe-bufp);
1213 bufe -= bufp;
1214 bufp = 0;
1215 }
1216 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1217 if (readlen < 0) {
1218 ALOGW("Failure reading update commands in %s%s: %s\n",
1219 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1220 break;
1221 } else if (readlen == 0) {
1222 break;
1223 }
1224 bufe += readlen;
1225 buf[bufe] = 0;
1226 ALOGV("Read buf: %s\n", buf);
1227 }
1228 }
1229 close(subfd);
1230 }
1231 }
1232 closedir(d);
1233done:
1234 return 0;
1235}
1236
1237int linklib(const char* pkgname, const char* asecLibDir, int userId)
1238{
1239 char pkgdir[PKG_PATH_MAX];
1240 char libsymlink[PKG_PATH_MAX];
1241 struct stat s, libStat;
1242 int rc = 0;
1243
1244 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1245 ALOGE("cannot create package path\n");
1246 return -1;
1247 }
1248 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1249 ALOGE("cannot create package lib symlink origin path\n");
1250 return -1;
1251 }
1252
1253 if (stat(pkgdir, &s) < 0) return -1;
1254
1255 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1256 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1257 return -1;
1258 }
1259
1260 if (chmod(pkgdir, 0700) < 0) {
1261 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1262 rc = -1;
1263 goto out;
1264 }
1265
1266 if (lstat(libsymlink, &libStat) < 0) {
1267 if (errno != ENOENT) {
1268 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1269 rc = -1;
1270 goto out;
1271 }
1272 } else {
1273 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001274 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001275 rc = -1;
1276 goto out;
1277 }
1278 } else if (S_ISLNK(libStat.st_mode)) {
1279 if (unlink(libsymlink) < 0) {
1280 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1281 rc = -1;
1282 goto out;
1283 }
1284 }
1285 }
1286
1287 if (symlink(asecLibDir, libsymlink) < 0) {
1288 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1289 strerror(errno));
1290 rc = -errno;
1291 goto out;
1292 }
1293
1294out:
1295 if (chmod(pkgdir, s.st_mode) < 0) {
1296 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1297 rc = -errno;
1298 }
1299
1300 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1301 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1302 return -errno;
1303 }
1304
1305 return rc;
1306}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001307
1308static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1309{
1310 static const char *IDMAP_BIN = "/system/bin/idmap";
1311 static const size_t MAX_INT_LEN = 32;
1312 char idmap_str[MAX_INT_LEN];
1313
1314 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1315
1316 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1317 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1318}
1319
1320// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1321// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1322static int flatten_path(const char *prefix, const char *suffix,
1323 const char *overlay_path, char *idmap_path, size_t N)
1324{
1325 if (overlay_path == NULL || idmap_path == NULL) {
1326 return -1;
1327 }
1328 const size_t len_overlay_path = strlen(overlay_path);
1329 // will access overlay_path + 1 further below; requires absolute path
1330 if (len_overlay_path < 2 || *overlay_path != '/') {
1331 return -1;
1332 }
1333 const size_t len_idmap_root = strlen(prefix);
1334 const size_t len_suffix = strlen(suffix);
1335 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1336 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1337 // additions below would cause overflow
1338 return -1;
1339 }
1340 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1341 return -1;
1342 }
1343 memset(idmap_path, 0, N);
1344 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1345 char *ch = idmap_path + len_idmap_root;
1346 while (*ch != '\0') {
1347 if (*ch == '/') {
1348 *ch = '@';
1349 }
1350 ++ch;
1351 }
1352 return 0;
1353}
1354
1355int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1356{
1357 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1358
1359 int idmap_fd = -1;
1360 char idmap_path[PATH_MAX];
1361
1362 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1363 idmap_path, sizeof(idmap_path)) == -1) {
1364 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1365 goto fail;
1366 }
1367
1368 unlink(idmap_path);
1369 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1370 if (idmap_fd < 0) {
1371 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1372 goto fail;
1373 }
1374 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1375 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1376 goto fail;
1377 }
1378 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1379 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1380 goto fail;
1381 }
1382
1383 pid_t pid;
1384 pid = fork();
1385 if (pid == 0) {
1386 /* child -- drop privileges before continuing */
1387 if (setgid(uid) != 0) {
1388 ALOGE("setgid(%d) failed during idmap\n", uid);
1389 exit(1);
1390 }
1391 if (setuid(uid) != 0) {
1392 ALOGE("setuid(%d) failed during idmap\n", uid);
1393 exit(1);
1394 }
1395 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1396 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1397 exit(1);
1398 }
1399
1400 run_idmap(target_apk, overlay_apk, idmap_fd);
1401 exit(1); /* only if exec call to idmap failed */
1402 } else {
1403 int status = wait_child(pid);
1404 if (status != 0) {
1405 ALOGE("idmap failed, status=0x%04x\n", status);
1406 goto fail;
1407 }
1408 }
1409
1410 close(idmap_fd);
1411 return 0;
1412fail:
1413 if (idmap_fd >= 0) {
1414 close(idmap_fd);
1415 unlink(idmap_path);
1416 }
1417 return -1;
1418}
Robert Craige9887e42014-02-20 10:25:56 -05001419
Robert Craigda30dc72014-03-27 10:21:12 -04001420int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001421{
Robert Craigda30dc72014-03-27 10:21:12 -04001422 struct dirent *entry;
1423 DIR *d;
1424 struct stat s;
1425 char *userdir;
1426 char *primarydir;
1427 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001428 int ret = 0;
1429
Robert Craigda30dc72014-03-27 10:21:12 -04001430 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1431 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1432
1433 if (!pkgName || !seinfo) {
1434 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001435 return -1;
1436 }
1437
Robert Craigda30dc72014-03-27 10:21:12 -04001438 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1439 return -1;
1440 }
1441
1442 // Relabel for primary user.
1443 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1444 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001445 ret |= -1;
1446 }
1447
Robert Craigda30dc72014-03-27 10:21:12 -04001448 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1449 free(primarydir);
1450 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001451 }
1452
Robert Craigda30dc72014-03-27 10:21:12 -04001453 // Relabel package directory for all secondary users.
1454 d = opendir(userdir);
1455 if (d == NULL) {
1456 free(primarydir);
1457 free(userdir);
1458 return -1;
1459 }
1460
1461 while ((entry = readdir(d))) {
1462 if (entry->d_type != DT_DIR) {
1463 continue;
1464 }
1465
1466 const char *user = entry->d_name;
1467 // Ignore "." and ".."
1468 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1469 continue;
1470 }
1471
1472 // user directories start with a number
1473 if (user[0] < '0' || user[0] > '9') {
1474 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1475 continue;
1476 }
1477
1478 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1479 continue;
1480 }
1481
1482 if (stat(pkgdir, &s) < 0) {
1483 free(pkgdir);
1484 continue;
1485 }
1486
1487 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1488 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1489 ret |= -1;
1490 }
1491 free(pkgdir);
1492 }
1493
1494 closedir(d);
1495 free(primarydir);
1496 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001497 return ret;
1498}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001499
1500static int prune_dex_exclusion_predicate(const char *file_name, const int is_dir)
1501{
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001502 // Exclude all directories. The top level command will be
1503 // given a list of ISA specific directories that are assumed
1504 // to be flat.
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001505 if (is_dir) {
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001506 return 1;
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001507 }
1508
1509
1510 // Don't exclude regular files that start with the list
1511 // of prefixes.
1512 static const char data_app_prefix[] = "data@app@";
1513 static const char data_priv_app_prefix[] = "data@priv-app@";
1514 if (!strncmp(file_name, data_app_prefix, sizeof(data_app_prefix) - 1) ||
1515 !strncmp(file_name, data_priv_app_prefix, sizeof(data_priv_app_prefix) - 1)) {
1516 return 0;
1517 }
1518
1519 // Exclude all regular files that don't start with the prefix "data@app@" or
1520 // "data@priv-app@".
1521 return 1;
1522}
1523
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001524int prune_dex_cache(const char* subdir) {
1525 // "." is handled as a special case, and refers to
1526 // DALVIK_CACHE_PREFIX (usually /data/dalvik-cache).
1527 const bool is_dalvik_cache_root = !strcmp(subdir, ".");
1528
1529 // Don't allow the path to contain "." or ".." except for the
1530 // special case above. This is much stricter than we need to be,
1531 // but there's no good reason to support them.
1532 if (strchr(subdir, '.' ) != NULL && !is_dalvik_cache_root) {
1533 return -1;
1534 }
1535
1536 if (!is_dalvik_cache_root) {
1537 char full_path[PKG_PATH_MAX];
1538 snprintf(full_path, sizeof(full_path), "%s%s", DALVIK_CACHE_PREFIX, subdir);
1539 return delete_dir_contents(full_path, 0, &prune_dex_exclusion_predicate);
1540 }
1541
1542
1543 // When subdir == ".", clean the contents of the top level
1544 // dalvik-cache directory.
1545 return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate);
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001546}