blob: a441370e3565ee45bec5cb91d078f80ca8ba303a [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 Carlstrom0378aaf2014-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
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100180static int lib_dir_matcher(const char* file_name, const int is_dir) {
181 return is_dir && !strcmp(file_name, "lib");
182}
183
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700184int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700185{
186 char pkgdir[PKG_PATH_MAX];
187
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700188 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189 return -1;
190
191 /* delete contents, excluding "lib", but not the directory itself */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100192 return delete_dir_contents(pkgdir, 0, &lib_dir_matcher);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193}
194
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700195int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196{
197 char pkgdir[PKG_PATH_MAX];
198 char applibdir[PKG_PATH_MAX];
199 char libsymlink[PKG_PATH_MAX];
200 struct stat libStat;
201
202 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700203 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700204 return -1;
205 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700206 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700207 ALOGE("cannot create package lib symlink origin path\n");
208 return -1;
209 }
210 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
211 ALOGE("cannot create package lib symlink dest path\n");
212 return -1;
213 }
214
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800215 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
217 return -errno;
218 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800219 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700220 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
221 unlink(pkgdir);
222 return -errno;
223 }
224
225 if (lstat(libsymlink, &libStat) < 0) {
226 if (errno != ENOENT) {
227 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
228 unlink(pkgdir);
229 return -1;
230 }
231 } else {
232 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100233 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700234 ALOGE("couldn't delete lib directory during install for non-primary: %s",
235 libsymlink);
236 unlink(pkgdir);
237 return -1;
238 }
239 } else if (S_ISLNK(libStat.st_mode)) {
240 if (unlink(libsymlink) < 0) {
241 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
242 libsymlink);
243 unlink(pkgdir);
244 return -1;
245 }
246 }
247 }
248
Stephen Smalley26288202014-02-07 09:16:46 -0500249 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700250 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
251 unlink(libsymlink);
252 unlink(pkgdir);
253 return -errno;
254 }
255
Stephen Smalley3a983892014-05-13 12:53:07 -0400256 if (symlink(applibdir, libsymlink) < 0) {
257 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
258 applibdir, strerror(errno));
259 unlink(pkgdir);
260 return -1;
261 }
262
Mike Lockwood94afecf2012-10-24 10:45:23 -0700263 if (chown(pkgdir, uid, uid) < 0) {
264 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
265 unlink(libsymlink);
266 unlink(pkgdir);
267 return -errno;
268 }
269
270 return 0;
271}
272
Robin Lee7c8bec02014-06-10 18:46:26 +0100273int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700274{
Robin Lee095c7632014-04-25 15:05:19 +0100275 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700276 return -1;
277 }
278
279 return 0;
280}
281
Robin Lee095c7632014-04-25 15:05:19 +0100282int delete_user(userid_t userid)
283{
284 int status = 0;
285
286 char data_path[PKG_PATH_MAX];
287 if ((create_user_path(data_path, userid) != 0)
288 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
289 status = -1;
290 }
291
292 char media_path[PATH_MAX];
293 if ((create_user_media_path(media_path, userid) != 0)
294 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
295 status = -1;
296 }
297
298 char config_path[PATH_MAX];
299 if ((create_user_config_path(config_path, userid) != 0)
300 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
301 status = -1;
302 }
303
304 return status;
305}
306
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700307int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700308{
309 char cachedir[PKG_PATH_MAX];
310
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700311 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700312 return -1;
313
314 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100315 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700316}
317
318/* Try to ensure free_size bytes of storage are available.
319 * Returns 0 on success.
320 * This is rather simple-minded because doing a full LRU would
321 * be potentially memory-intensive, and without atime it would
322 * also require that apps constantly modify file metadata even
323 * when just reading from the cache, which is pretty awful.
324 */
325int free_cache(int64_t free_size)
326{
327 cache_t* cache;
328 int64_t avail;
329 DIR *d;
330 struct dirent *de;
331 char tmpdir[PATH_MAX];
332 char *dirpos;
333
334 avail = data_disk_free();
335 if (avail < 0) return -1;
336
337 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
338 if (avail >= free_size) return 0;
339
340 cache = start_cache_collection();
341
342 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700343 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700344 //ALOGI("adding cache files from %s\n", tmpdir);
345 add_cache_files(cache, tmpdir, "cache");
346 }
347
348 // Search for other users and add any cache files from them.
349 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
350 SECONDARY_USER_PREFIX);
351 dirpos = tmpdir + strlen(tmpdir);
352 d = opendir(tmpdir);
353 if (d != NULL) {
354 while ((de = readdir(d))) {
355 if (de->d_type == DT_DIR) {
356 const char *name = de->d_name;
357 /* always skip "." and ".." */
358 if (name[0] == '.') {
359 if (name[1] == 0) continue;
360 if ((name[1] == '.') && (name[2] == 0)) continue;
361 }
362 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
363 strcpy(dirpos, name);
364 //ALOGI("adding cache files from %s\n", tmpdir);
365 add_cache_files(cache, tmpdir, "cache");
366 } else {
367 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
368 }
369 }
370 }
371 closedir(d);
372 }
373
374 // Collect cache files on external storage for all users (if it is mounted as part
375 // of the internal storage).
376 strcpy(tmpdir, android_media_dir.path);
377 dirpos = tmpdir + strlen(tmpdir);
378 d = opendir(tmpdir);
379 if (d != NULL) {
380 while ((de = readdir(d))) {
381 if (de->d_type == DT_DIR) {
382 const char *name = de->d_name;
383 /* skip any dir that doesn't start with a number, so not a user */
384 if (name[0] < '0' || name[0] > '9') {
385 continue;
386 }
387 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
388 strcpy(dirpos, name);
389 if (lookup_media_dir(tmpdir, "Android") == 0
390 && lookup_media_dir(tmpdir, "data") == 0) {
391 //ALOGI("adding cache files from %s\n", tmpdir);
392 add_cache_files(cache, tmpdir, "cache");
393 }
394 } else {
395 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
396 }
397 }
398 }
399 closedir(d);
400 }
401
402 clear_cache_files(cache, free_size);
403 finish_cache_collection(cache);
404
405 return data_disk_free() >= free_size ? 0 : -1;
406}
407
Narayan Kamath1b400322014-04-11 13:17:00 +0100408int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700409{
410 char src_dex[PKG_PATH_MAX];
411 char dst_dex[PKG_PATH_MAX];
412
413 if (validate_apk_path(src)) return -1;
414 if (validate_apk_path(dst)) return -1;
415
Narayan Kamath1b400322014-04-11 13:17:00 +0100416 if (create_cache_path(src_dex, src, instruction_set)) return -1;
417 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700418
419 ALOGV("move %s -> %s\n", src_dex, dst_dex);
420 if (rename(src_dex, dst_dex) < 0) {
421 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
422 return -1;
423 } else {
424 return 0;
425 }
426}
427
Narayan Kamath1b400322014-04-11 13:17:00 +0100428int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700429{
430 char dex_path[PKG_PATH_MAX];
431
432 if (validate_apk_path(path)) return -1;
Narayan Kamath1b400322014-04-11 13:17:00 +0100433 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700434
435 ALOGV("unlink %s\n", dex_path);
436 if (unlink(dex_path) < 0) {
437 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
438 return -1;
439 } else {
440 return 0;
441 }
442}
443
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700444int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700445 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100446 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
447 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700448{
449 DIR *d;
450 int dfd;
451 struct dirent *de;
452 struct stat s;
453 char path[PKG_PATH_MAX];
454
455 int64_t codesize = 0;
456 int64_t datasize = 0;
457 int64_t cachesize = 0;
458 int64_t asecsize = 0;
459
460 /* count the source apk as code -- but only if it's not
461 * on the /system partition and its not on the sdcard.
462 */
463 if (validate_system_app_path(apkpath) &&
464 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
465 if (stat(apkpath, &s) == 0) {
466 codesize += stat_size(&s);
467 }
468 }
469 /* count the forward locked apk as code if it is given
470 */
471 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
472 if (stat(fwdlock_apkpath, &s) == 0) {
473 codesize += stat_size(&s);
474 }
475 }
476 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100477 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700478 if (stat(path, &s) == 0) {
479 codesize += stat_size(&s);
480 }
481 }
482
483 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700484 if (libdirpath != NULL && libdirpath[0] != '!') {
485 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700486 if (d != NULL) {
487 dfd = dirfd(d);
488 codesize += calculate_dir_size(dfd);
489 closedir(d);
490 }
491 }
492
493 /* compute asec size if it is given
494 */
495 if (asecpath != NULL && asecpath[0] != '!') {
496 if (stat(asecpath, &s) == 0) {
497 asecsize += stat_size(&s);
498 }
499 }
500
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700501 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700502 goto done;
503 }
504
505 d = opendir(path);
506 if (d == NULL) {
507 goto done;
508 }
509 dfd = dirfd(d);
510
511 /* most stuff in the pkgdir is data, except for the "cache"
512 * directory and below, which is cache, and the "lib" directory
513 * and below, which is code...
514 */
515 while ((de = readdir(d))) {
516 const char *name = de->d_name;
517
518 if (de->d_type == DT_DIR) {
519 int subfd;
520 int64_t statsize = 0;
521 int64_t dirsize = 0;
522 /* always skip "." and ".." */
523 if (name[0] == '.') {
524 if (name[1] == 0) continue;
525 if ((name[1] == '.') && (name[2] == 0)) continue;
526 }
527 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
528 statsize = stat_size(&s);
529 }
530 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
531 if (subfd >= 0) {
532 dirsize = calculate_dir_size(subfd);
533 }
534 if(!strcmp(name,"lib")) {
535 codesize += dirsize + statsize;
536 } else if(!strcmp(name,"cache")) {
537 cachesize += dirsize + statsize;
538 } else {
539 datasize += dirsize + statsize;
540 }
541 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
542 // This is the symbolic link to the application's library
543 // code. We'll count this as code instead of data, since
544 // it is not something that the app creates.
545 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
546 codesize += stat_size(&s);
547 }
548 } else {
549 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
550 datasize += stat_size(&s);
551 }
552 }
553 }
554 closedir(d);
555done:
556 *_codesize = codesize;
557 *_datasize = datasize;
558 *_cachesize = cachesize;
559 *_asecsize = asecsize;
560 return 0;
561}
562
Narayan Kamath1b400322014-04-11 13:17:00 +0100563int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700564{
565 char *tmp;
566 int srclen;
567 int dstlen;
568
569 srclen = strlen(src);
570
571 /* demand that we are an absolute path */
572 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
573 return -1;
574 }
575
576 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
577 return -1;
578 }
579
Dave Allisond9370732014-01-30 14:19:23 -0800580 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100581 strlen(instruction_set) +
582 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800583
Mike Lockwood94afecf2012-10-24 10:45:23 -0700584 if (dstlen > PKG_PATH_MAX) {
585 return -1;
586 }
587
Narayan Kamath1b400322014-04-11 13:17:00 +0100588 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700589 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100590 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700591 src + 1, /* skip the leading / */
592 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800593
Narayan Kamath1b400322014-04-11 13:17:00 +0100594 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700595 if (*tmp == '/') {
596 *tmp = '@';
597 }
598 }
599
600 return 0;
601}
602
603static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800604 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700605{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800606 /* platform-specific flags affecting optimization and verification */
607 char dexopt_flags[PROPERTY_VALUE_MAX];
608 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
609 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
610
Mike Lockwood94afecf2012-10-24 10:45:23 -0700611 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
612 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
613 char zip_num[MAX_INT_LEN];
614 char odex_num[MAX_INT_LEN];
615
616 sprintf(zip_num, "%d", zip_fd);
617 sprintf(odex_num, "%d", odex_fd);
618
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700619 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700620 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
621 dexopt_flags, (char*) NULL);
622 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
623}
624
Alex Light7365a102014-07-21 12:23:48 -0700625static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
626 const char* output_file_name, const char *pkgname, const char *instruction_set)
627{
628 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
629 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
630
631 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
632 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
633 ALOGE("Instruction set %s longer than max length of %d",
634 instruction_set, MAX_INSTRUCTION_SET_LEN);
635 return;
636 }
637
638 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
639 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
640 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
641 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
642 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
643 // The caller has already gotten all the locks we need.
644 const char* no_lock_arg = "--no-lock-output";
645 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
646 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
647 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700648 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700649 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
650
651 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
652 char* argv[7];
653 argv[0] = (char*) PATCHOAT_BIN;
654 argv[1] = (char*) patched_image_location_arg;
655 argv[2] = (char*) no_lock_arg;
656 argv[3] = instruction_set_arg;
657 argv[4] = output_oat_fd_arg;
658 argv[5] = input_oat_fd_arg;
659 argv[6] = NULL;
660
661 execv(PATCHOAT_BIN, (char* const *)argv);
662 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
663}
664
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700665static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Narayan Kamath1b400322014-04-11 13:17:00 +0100666 const char* output_file_name, const char *pkgname, const char *instruction_set)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700667{
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700668 char prop_buf[PROPERTY_VALUE_MAX];
669 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
670
671 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
672 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
673
674 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
675 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
676
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700677 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
678 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
679 dex2oat_compiler_filter_flag, NULL) > 0;
680
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800681 char dex2oat_flags[PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100682 bool have_dex2oat_flags = property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, NULL) > 0;
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800683 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
684
Brian Carlstrom538998f2014-07-30 14:37:11 -0700685 // If we booting without the real /data, don't spend time compiling.
686 char vold_decrypt[PROPERTY_VALUE_MAX];
687 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
688 bool skip_compilation = (have_vold_decrypt &&
689 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
690 (strcmp(vold_decrypt, "1") == 0)));
691
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700692 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700693
Brian Carlstrom53e07762014-06-27 14:15:19 -0700694 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700695
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700696 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100697 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
698
699 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
700 ALOGE("Instruction set %s longer than max length of %d",
701 instruction_set, MAX_INSTRUCTION_SET_LEN);
702 return;
703 }
704
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700705 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
706 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
707 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700708 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100709 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravle4fdff462014-06-06 16:58:43 +0100710 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
711 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700712 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
713 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700714 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700715
716 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
717 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
718 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
719 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100720 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle57c69c32014-06-06 14:42:16 +0100721
Calin Juravle4fdff462014-06-06 16:58:43 +0100722 bool have_profile_file = false;
723 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100724 if (profiler && (strcmp(pkgname, "*") != 0)) {
725 char profile_file[PKG_PATH_MAX];
726 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800727 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100728 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100729 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100730 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100731 have_profile_file = true;
732 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
733 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
734 "--top-k-profile-threshold=%s", prop_buf);
735 have_top_k_profile_threshold = true;
736 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100737 }
Dave Allisond9370732014-01-30 14:19:23 -0800738 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700739
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700740 if (have_dex2oat_Xms_flag) {
741 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
742 }
743 if (have_dex2oat_Xmx_flag) {
744 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
745 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700746 if (skip_compilation) {
Brian Carlstromf7765c42014-08-15 09:55:50 -0700747 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700748 have_dex2oat_compiler_filter_flag = true;
749 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700750 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
751 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700752
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700753 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100754
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700755 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravle4fdff462014-06-06 16:58:43 +0100756 + (have_profile_file ? 1 : 0)
757 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700758 + (have_dex2oat_Xms_flag ? 2 : 0)
759 + (have_dex2oat_Xmx_flag ? 2 : 0)
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700760 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100761 + (have_dex2oat_flags ? 1 : 0)];
762 int i = 0;
763 argv[i++] = (char*)DEX2OAT_BIN;
764 argv[i++] = zip_fd_arg;
765 argv[i++] = zip_location_arg;
766 argv[i++] = oat_fd_arg;
767 argv[i++] = oat_location_arg;
768 argv[i++] = instruction_set_arg;
769 if (have_profile_file) {
770 argv[i++] = profile_file_arg;
771 }
772 if (have_top_k_profile_threshold) {
773 argv[i++] = top_k_profile_threshold_arg;
774 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700775 if (have_dex2oat_Xms_flag) {
776 argv[i++] = (char*)RUNTIME_ARG;
777 argv[i++] = dex2oat_Xms_arg;
778 }
779 if (have_dex2oat_Xmx_flag) {
780 argv[i++] = (char*)RUNTIME_ARG;
781 argv[i++] = dex2oat_Xmx_arg;
782 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700783 if (have_dex2oat_compiler_filter_flag) {
784 argv[i++] = dex2oat_compiler_filter_arg;
785 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100786 if (have_dex2oat_flags) {
787 argv[i++] = dex2oat_flags;
788 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700789 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100790 argv[i] = NULL;
791
792 execv(DEX2OAT_BIN, (char* const *)argv);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700793 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
794}
795
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100796static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700797{
798 int status;
799 pid_t got_pid;
800
Mike Lockwood94afecf2012-10-24 10:45:23 -0700801 while (1) {
802 got_pid = waitpid(pid, &status, 0);
803 if (got_pid == -1 && errno == EINTR) {
804 printf("waitpid interrupted, retrying\n");
805 } else {
806 break;
807 }
808 }
809 if (got_pid != pid) {
810 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
811 (int) pid, (int) got_pid, strerror(errno));
812 return 1;
813 }
814
815 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700816 return 0;
817 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700818 return status; /* always nonzero */
819 }
820}
821
Dave Allisond9370732014-01-30 14:19:23 -0800822int dexopt(const char *apk_path, uid_t uid, int is_public,
Alex Light7365a102014-07-21 12:23:48 -0700823 const char *pkgname, const char *instruction_set,
824 int is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700825{
826 struct utimbuf ut;
Alex Light7365a102014-07-21 12:23:48 -0700827 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700828 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700829 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700830 char *end;
Alex Light7365a102014-07-21 12:23:48 -0700831 const char *input_file;
832 char in_odex_path[PKG_PATH_MAX];
833 int res, input_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700834
Mike Lockwood94afecf2012-10-24 10:45:23 -0700835 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
836 return -1;
837 }
838
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800839 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700840 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700841
Alex Light7365a102014-07-21 12:23:48 -0700842 if (is_patchoat && strncmp(persist_sys_dalvik_vm_lib, "libart", 6) != 0) {
843 /* We may only patch if we are libart */
844 ALOGE("Patching is only supported in libart\n");
845 return -1;
846 }
847
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700848 /* Before anything else: is there a .odex file? If so, we have
849 * precompiled the apk and there is nothing to do here.
Alex Light7365a102014-07-21 12:23:48 -0700850 *
851 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700852 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800853 strcpy(out_path, apk_path);
854 end = strrchr(out_path, '.');
Alex Light7365a102014-07-21 12:23:48 -0700855 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800856 strcpy(end, ".odex");
857 if (stat(out_path, &dex_stat) == 0) {
858 return 0;
859 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700860 }
861
Narayan Kamath1b400322014-04-11 13:17:00 +0100862 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700863 return -1;
864 }
865
Alex Light7365a102014-07-21 12:23:48 -0700866 if (is_patchoat) {
867 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
868 strcpy(in_odex_path, apk_path);
869 end = strrchr(in_odex_path, '/');
870 if (end == NULL) {
871 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
872 return -1;
873 }
874 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
875 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
876 strcat(in_odex_path, apk_end);
877 end = strrchr(in_odex_path, '.');
878 if (end == NULL) {
879 return -1;
880 }
881 strcpy(end + 1, "odex");
882 input_file = in_odex_path;
883 } else {
884 input_file = apk_path;
885 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700886
Alex Light7365a102014-07-21 12:23:48 -0700887 memset(&input_stat, 0, sizeof(input_stat));
888 stat(input_file, &input_stat);
889
890 input_fd = open(input_file, O_RDONLY, 0);
891 if (input_fd < 0) {
892 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700893 return -1;
894 }
895
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700896 unlink(out_path);
897 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
898 if (out_fd < 0) {
899 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700900 goto fail;
901 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700902 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700903 S_IRUSR|S_IWUSR|S_IRGRP |
904 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700905 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700906 goto fail;
907 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700908 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
909 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700910 goto fail;
911 }
912
Dave Allisond9370732014-01-30 14:19:23 -0800913 // Create profile file if there is a package name present.
914 if (strcmp(pkgname, "*") != 0) {
915 create_profile_file(pkgname, uid);
916 }
917
918
Alex Light7365a102014-07-21 12:23:48 -0700919 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700920
921 pid_t pid;
922 pid = fork();
923 if (pid == 0) {
924 /* child -- drop privileges before continuing */
925 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700926 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700927 exit(64);
928 }
929 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700930 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700931 exit(65);
932 }
933 // drop capabilities
934 struct __user_cap_header_struct capheader;
935 struct __user_cap_data_struct capdata[2];
936 memset(&capheader, 0, sizeof(capheader));
937 memset(&capdata, 0, sizeof(capdata));
938 capheader.version = _LINUX_CAPABILITY_VERSION_3;
939 if (capset(&capheader, &capdata[0]) < 0) {
940 ALOGE("capset failed: %s\n", strerror(errno));
941 exit(66);
942 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -0700943 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
944 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
945 exit(70);
946 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700947 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
948 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700949 exit(67);
950 }
951
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700952 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Alex Light7365a102014-07-21 12:23:48 -0700953 run_dexopt(input_fd, out_fd, input_file, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700954 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Alex Light7365a102014-07-21 12:23:48 -0700955 if (is_patchoat) {
956 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
957 } else {
958 run_dex2oat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
959 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700960 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700961 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700962 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700963 exit(68); /* only get here on exec failure */
964 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100965 res = wait_child(pid);
966 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -0700967 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100968 } else {
Alex Light7365a102014-07-21 12:23:48 -0700969 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700970 goto fail;
971 }
972 }
973
Alex Light7365a102014-07-21 12:23:48 -0700974 ut.actime = input_stat.st_atime;
975 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700976 utime(out_path, &ut);
977
978 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -0700979 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700980 return 0;
981
982fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700983 if (out_fd >= 0) {
984 close(out_fd);
985 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700986 }
Alex Light7365a102014-07-21 12:23:48 -0700987 if (input_fd >= 0) {
988 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700989 }
990 return -1;
991}
992
993void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
994 struct stat* statbuf)
995{
996 while (path[basepos] != 0) {
997 if (path[basepos] == '/') {
998 path[basepos] = 0;
999 if (lstat(path, statbuf) < 0) {
1000 ALOGV("Making directory: %s\n", path);
1001 if (mkdir(path, mode) == 0) {
1002 chown(path, uid, gid);
1003 } else {
1004 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1005 }
1006 }
1007 path[basepos] = '/';
1008 basepos++;
1009 }
1010 basepos++;
1011 }
1012}
1013
1014int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1015 int dstuid, int dstgid, struct stat* statbuf)
1016{
1017 DIR *d;
1018 struct dirent *de;
1019 int res;
1020
1021 int srcend = strlen(srcpath);
1022 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001023
Mike Lockwood94afecf2012-10-24 10:45:23 -07001024 if (lstat(srcpath, statbuf) < 0) {
1025 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1026 return 1;
1027 }
Dave Allisond9370732014-01-30 14:19:23 -08001028
Mike Lockwood94afecf2012-10-24 10:45:23 -07001029 if ((statbuf->st_mode&S_IFDIR) == 0) {
1030 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1031 dstuid, dstgid, statbuf);
1032 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1033 if (rename(srcpath, dstpath) >= 0) {
1034 if (chown(dstpath, dstuid, dstgid) < 0) {
1035 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1036 unlink(dstpath);
1037 return 1;
1038 }
1039 } else {
1040 ALOGW("Unable to rename %s to %s: %s\n",
1041 srcpath, dstpath, strerror(errno));
1042 return 1;
1043 }
1044 return 0;
1045 }
1046
1047 d = opendir(srcpath);
1048 if (d == NULL) {
1049 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1050 return 1;
1051 }
1052
1053 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001054
Mike Lockwood94afecf2012-10-24 10:45:23 -07001055 while ((de = readdir(d))) {
1056 const char *name = de->d_name;
1057 /* always skip "." and ".." */
1058 if (name[0] == '.') {
1059 if (name[1] == 0) continue;
1060 if ((name[1] == '.') && (name[2] == 0)) continue;
1061 }
Dave Allisond9370732014-01-30 14:19:23 -08001062
Mike Lockwood94afecf2012-10-24 10:45:23 -07001063 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1064 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1065 continue;
1066 }
Dave Allisond9370732014-01-30 14:19:23 -08001067
Mike Lockwood94afecf2012-10-24 10:45:23 -07001068 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1069 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1070 continue;
1071 }
Dave Allisond9370732014-01-30 14:19:23 -08001072
Mike Lockwood94afecf2012-10-24 10:45:23 -07001073 srcpath[srcend] = dstpath[dstend] = '/';
1074 strcpy(srcpath+srcend+1, name);
1075 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001076
Mike Lockwood94afecf2012-10-24 10:45:23 -07001077 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1078 res = 1;
1079 }
Dave Allisond9370732014-01-30 14:19:23 -08001080
Mike Lockwood94afecf2012-10-24 10:45:23 -07001081 // Note: we will be leaving empty directories behind in srcpath,
1082 // but that is okay, the package manager will be erasing all of the
1083 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001084
Mike Lockwood94afecf2012-10-24 10:45:23 -07001085 srcpath[srcend] = dstpath[dstend] = 0;
1086 }
Dave Allisond9370732014-01-30 14:19:23 -08001087
Mike Lockwood94afecf2012-10-24 10:45:23 -07001088 closedir(d);
1089 return res;
1090}
1091
1092int movefiles()
1093{
1094 DIR *d;
1095 int dfd, subfd;
1096 struct dirent *de;
1097 struct stat s;
1098 char buf[PKG_PATH_MAX+1];
1099 int bufp, bufe, bufi, readlen;
1100
1101 char srcpkg[PKG_NAME_MAX];
1102 char dstpkg[PKG_NAME_MAX];
1103 char srcpath[PKG_PATH_MAX];
1104 char dstpath[PKG_PATH_MAX];
1105 int dstuid=-1, dstgid=-1;
1106 int hasspace;
1107
1108 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1109 if (d == NULL) {
1110 goto done;
1111 }
1112 dfd = dirfd(d);
1113
1114 /* Iterate through all files in the directory, executing the
1115 * file movements requested there-in.
1116 */
1117 while ((de = readdir(d))) {
1118 const char *name = de->d_name;
1119
1120 if (de->d_type == DT_DIR) {
1121 continue;
1122 } else {
1123 subfd = openat(dfd, name, O_RDONLY);
1124 if (subfd < 0) {
1125 ALOGW("Unable to open update commands at %s%s\n",
1126 UPDATE_COMMANDS_DIR_PREFIX, name);
1127 continue;
1128 }
Dave Allisond9370732014-01-30 14:19:23 -08001129
Mike Lockwood94afecf2012-10-24 10:45:23 -07001130 bufp = 0;
1131 bufe = 0;
1132 buf[PKG_PATH_MAX] = 0;
1133 srcpkg[0] = dstpkg[0] = 0;
1134 while (1) {
1135 bufi = bufp;
1136 while (bufi < bufe && buf[bufi] != '\n') {
1137 bufi++;
1138 }
1139 if (bufi < bufe) {
1140 buf[bufi] = 0;
1141 ALOGV("Processing line: %s\n", buf+bufp);
1142 hasspace = 0;
1143 while (bufp < bufi && isspace(buf[bufp])) {
1144 hasspace = 1;
1145 bufp++;
1146 }
1147 if (buf[bufp] == '#' || bufp == bufi) {
1148 // skip comments and empty lines.
1149 } else if (hasspace) {
1150 if (dstpkg[0] == 0) {
1151 ALOGW("Path before package line in %s%s: %s\n",
1152 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1153 } else if (srcpkg[0] == 0) {
1154 // Skip -- source package no longer exists.
1155 } else {
1156 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1157 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1158 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1159 movefileordir(srcpath, dstpath,
1160 strlen(dstpath)-strlen(buf+bufp),
1161 dstuid, dstgid, &s);
1162 }
1163 }
1164 } else {
1165 char* div = strchr(buf+bufp, ':');
1166 if (div == NULL) {
1167 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1168 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1169 } else {
1170 *div = 0;
1171 div++;
1172 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1173 strcpy(dstpkg, buf+bufp);
1174 } else {
1175 srcpkg[0] = dstpkg[0] = 0;
1176 ALOGW("Package name too long in %s%s: %s\n",
1177 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1178 }
1179 if (strlen(div) < PKG_NAME_MAX) {
1180 strcpy(srcpkg, div);
1181 } else {
1182 srcpkg[0] = dstpkg[0] = 0;
1183 ALOGW("Package name too long in %s%s: %s\n",
1184 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1185 }
1186 if (srcpkg[0] != 0) {
1187 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1188 if (lstat(srcpath, &s) < 0) {
1189 // Package no longer exists -- skip.
1190 srcpkg[0] = 0;
1191 }
1192 } else {
1193 srcpkg[0] = 0;
1194 ALOGW("Can't create path %s in %s%s\n",
1195 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1196 }
1197 if (srcpkg[0] != 0) {
1198 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1199 if (lstat(dstpath, &s) == 0) {
1200 dstuid = s.st_uid;
1201 dstgid = s.st_gid;
1202 } else {
1203 // Destination package doesn't
1204 // exist... due to original-package,
1205 // this is normal, so don't be
1206 // noisy about it.
1207 srcpkg[0] = 0;
1208 }
1209 } else {
1210 srcpkg[0] = 0;
1211 ALOGW("Can't create path %s in %s%s\n",
1212 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1213 }
1214 }
1215 ALOGV("Transfering from %s to %s: uid=%d\n",
1216 srcpkg, dstpkg, dstuid);
1217 }
1218 }
1219 }
1220 bufp = bufi+1;
1221 } else {
1222 if (bufp == 0) {
1223 if (bufp < bufe) {
1224 ALOGW("Line too long in %s%s, skipping: %s\n",
1225 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1226 }
1227 } else if (bufp < bufe) {
1228 memcpy(buf, buf+bufp, bufe-bufp);
1229 bufe -= bufp;
1230 bufp = 0;
1231 }
1232 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1233 if (readlen < 0) {
1234 ALOGW("Failure reading update commands in %s%s: %s\n",
1235 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1236 break;
1237 } else if (readlen == 0) {
1238 break;
1239 }
1240 bufe += readlen;
1241 buf[bufe] = 0;
1242 ALOGV("Read buf: %s\n", buf);
1243 }
1244 }
1245 close(subfd);
1246 }
1247 }
1248 closedir(d);
1249done:
1250 return 0;
1251}
1252
1253int linklib(const char* pkgname, const char* asecLibDir, int userId)
1254{
1255 char pkgdir[PKG_PATH_MAX];
1256 char libsymlink[PKG_PATH_MAX];
1257 struct stat s, libStat;
1258 int rc = 0;
1259
1260 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1261 ALOGE("cannot create package path\n");
1262 return -1;
1263 }
1264 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1265 ALOGE("cannot create package lib symlink origin path\n");
1266 return -1;
1267 }
1268
1269 if (stat(pkgdir, &s) < 0) return -1;
1270
1271 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1272 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1273 return -1;
1274 }
1275
1276 if (chmod(pkgdir, 0700) < 0) {
1277 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1278 rc = -1;
1279 goto out;
1280 }
1281
1282 if (lstat(libsymlink, &libStat) < 0) {
1283 if (errno != ENOENT) {
1284 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1285 rc = -1;
1286 goto out;
1287 }
1288 } else {
1289 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001290 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001291 rc = -1;
1292 goto out;
1293 }
1294 } else if (S_ISLNK(libStat.st_mode)) {
1295 if (unlink(libsymlink) < 0) {
1296 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1297 rc = -1;
1298 goto out;
1299 }
1300 }
1301 }
1302
1303 if (symlink(asecLibDir, libsymlink) < 0) {
1304 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1305 strerror(errno));
1306 rc = -errno;
1307 goto out;
1308 }
1309
1310out:
1311 if (chmod(pkgdir, s.st_mode) < 0) {
1312 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1313 rc = -errno;
1314 }
1315
1316 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1317 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1318 return -errno;
1319 }
1320
1321 return rc;
1322}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001323
1324static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1325{
1326 static const char *IDMAP_BIN = "/system/bin/idmap";
1327 static const size_t MAX_INT_LEN = 32;
1328 char idmap_str[MAX_INT_LEN];
1329
1330 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1331
1332 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1333 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1334}
1335
1336// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1337// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1338static int flatten_path(const char *prefix, const char *suffix,
1339 const char *overlay_path, char *idmap_path, size_t N)
1340{
1341 if (overlay_path == NULL || idmap_path == NULL) {
1342 return -1;
1343 }
1344 const size_t len_overlay_path = strlen(overlay_path);
1345 // will access overlay_path + 1 further below; requires absolute path
1346 if (len_overlay_path < 2 || *overlay_path != '/') {
1347 return -1;
1348 }
1349 const size_t len_idmap_root = strlen(prefix);
1350 const size_t len_suffix = strlen(suffix);
1351 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1352 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1353 // additions below would cause overflow
1354 return -1;
1355 }
1356 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1357 return -1;
1358 }
1359 memset(idmap_path, 0, N);
1360 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1361 char *ch = idmap_path + len_idmap_root;
1362 while (*ch != '\0') {
1363 if (*ch == '/') {
1364 *ch = '@';
1365 }
1366 ++ch;
1367 }
1368 return 0;
1369}
1370
1371int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1372{
1373 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1374
1375 int idmap_fd = -1;
1376 char idmap_path[PATH_MAX];
1377
1378 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1379 idmap_path, sizeof(idmap_path)) == -1) {
1380 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1381 goto fail;
1382 }
1383
1384 unlink(idmap_path);
1385 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1386 if (idmap_fd < 0) {
1387 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1388 goto fail;
1389 }
1390 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1391 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1392 goto fail;
1393 }
1394 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1395 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1396 goto fail;
1397 }
1398
1399 pid_t pid;
1400 pid = fork();
1401 if (pid == 0) {
1402 /* child -- drop privileges before continuing */
1403 if (setgid(uid) != 0) {
1404 ALOGE("setgid(%d) failed during idmap\n", uid);
1405 exit(1);
1406 }
1407 if (setuid(uid) != 0) {
1408 ALOGE("setuid(%d) failed during idmap\n", uid);
1409 exit(1);
1410 }
1411 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1412 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1413 exit(1);
1414 }
1415
1416 run_idmap(target_apk, overlay_apk, idmap_fd);
1417 exit(1); /* only if exec call to idmap failed */
1418 } else {
1419 int status = wait_child(pid);
1420 if (status != 0) {
1421 ALOGE("idmap failed, status=0x%04x\n", status);
1422 goto fail;
1423 }
1424 }
1425
1426 close(idmap_fd);
1427 return 0;
1428fail:
1429 if (idmap_fd >= 0) {
1430 close(idmap_fd);
1431 unlink(idmap_path);
1432 }
1433 return -1;
1434}
Robert Craige9887e42014-02-20 10:25:56 -05001435
Robert Craigda30dc72014-03-27 10:21:12 -04001436int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001437{
Robert Craigda30dc72014-03-27 10:21:12 -04001438 struct dirent *entry;
1439 DIR *d;
1440 struct stat s;
1441 char *userdir;
1442 char *primarydir;
1443 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001444 int ret = 0;
1445
Robert Craigda30dc72014-03-27 10:21:12 -04001446 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1447 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1448
1449 if (!pkgName || !seinfo) {
1450 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001451 return -1;
1452 }
1453
Robert Craigda30dc72014-03-27 10:21:12 -04001454 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1455 return -1;
1456 }
1457
1458 // Relabel for primary user.
1459 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1460 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001461 ret |= -1;
1462 }
1463
Robert Craigda30dc72014-03-27 10:21:12 -04001464 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1465 free(primarydir);
1466 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001467 }
1468
Robert Craigda30dc72014-03-27 10:21:12 -04001469 // Relabel package directory for all secondary users.
1470 d = opendir(userdir);
1471 if (d == NULL) {
1472 free(primarydir);
1473 free(userdir);
1474 return -1;
1475 }
1476
1477 while ((entry = readdir(d))) {
1478 if (entry->d_type != DT_DIR) {
1479 continue;
1480 }
1481
1482 const char *user = entry->d_name;
1483 // Ignore "." and ".."
1484 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1485 continue;
1486 }
1487
1488 // user directories start with a number
1489 if (user[0] < '0' || user[0] > '9') {
1490 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1491 continue;
1492 }
1493
1494 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1495 continue;
1496 }
1497
1498 if (stat(pkgdir, &s) < 0) {
1499 free(pkgdir);
1500 continue;
1501 }
1502
1503 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1504 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1505 ret |= -1;
1506 }
1507 free(pkgdir);
1508 }
1509
1510 closedir(d);
1511 free(primarydir);
1512 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001513 return ret;
1514}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001515
1516static int prune_dex_exclusion_predicate(const char *file_name, const int is_dir)
1517{
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001518 // Exclude all directories. The top level command will be
1519 // given a list of ISA specific directories that are assumed
1520 // to be flat.
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001521 if (is_dir) {
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001522 return 1;
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001523 }
1524
1525
1526 // Don't exclude regular files that start with the list
1527 // of prefixes.
1528 static const char data_app_prefix[] = "data@app@";
1529 static const char data_priv_app_prefix[] = "data@priv-app@";
1530 if (!strncmp(file_name, data_app_prefix, sizeof(data_app_prefix) - 1) ||
1531 !strncmp(file_name, data_priv_app_prefix, sizeof(data_priv_app_prefix) - 1)) {
1532 return 0;
1533 }
1534
1535 // Exclude all regular files that don't start with the prefix "data@app@" or
1536 // "data@priv-app@".
1537 return 1;
1538}
1539
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001540int prune_dex_cache(const char* subdir) {
1541 // "." is handled as a special case, and refers to
1542 // DALVIK_CACHE_PREFIX (usually /data/dalvik-cache).
1543 const bool is_dalvik_cache_root = !strcmp(subdir, ".");
1544
1545 // Don't allow the path to contain "." or ".." except for the
1546 // special case above. This is much stricter than we need to be,
1547 // but there's no good reason to support them.
1548 if (strchr(subdir, '.' ) != NULL && !is_dalvik_cache_root) {
1549 return -1;
1550 }
1551
1552 if (!is_dalvik_cache_root) {
1553 char full_path[PKG_PATH_MAX];
1554 snprintf(full_path, sizeof(full_path), "%s%s", DALVIK_CACHE_PREFIX, subdir);
1555 return delete_dir_contents(full_path, 0, &prune_dex_exclusion_predicate);
1556 }
1557
1558
1559 // When subdir == ".", clean the contents of the top level
1560 // dalvik-cache directory.
1561 return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate);
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001562}