blob: 0a307c9d8b9d6660eac18e926ab978e7aade5d6f [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;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700147
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
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700179int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180{
181 char pkgdir[PKG_PATH_MAX];
182
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700183 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184 return -1;
185
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700186 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700187}
188
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700189int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700190{
191 char pkgdir[PKG_PATH_MAX];
192 char applibdir[PKG_PATH_MAX];
193 char libsymlink[PKG_PATH_MAX];
194 struct stat libStat;
195
196 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700197 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700198 return -1;
199 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700200 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700201 ALOGE("cannot create package lib symlink origin path\n");
202 return -1;
203 }
204 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
205 ALOGE("cannot create package lib symlink dest path\n");
206 return -1;
207 }
208
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800209 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700210 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
211 return -errno;
212 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800213 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700214 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
215 unlink(pkgdir);
216 return -errno;
217 }
218
219 if (lstat(libsymlink, &libStat) < 0) {
220 if (errno != ENOENT) {
221 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
222 unlink(pkgdir);
223 return -1;
224 }
225 } else {
226 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100227 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700228 ALOGE("couldn't delete lib directory during install for non-primary: %s",
229 libsymlink);
230 unlink(pkgdir);
231 return -1;
232 }
233 } else if (S_ISLNK(libStat.st_mode)) {
234 if (unlink(libsymlink) < 0) {
235 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
236 libsymlink);
237 unlink(pkgdir);
238 return -1;
239 }
240 }
241 }
242
Stephen Smalley26288202014-02-07 09:16:46 -0500243 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700244 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
245 unlink(libsymlink);
246 unlink(pkgdir);
247 return -errno;
248 }
249
Stephen Smalley3a983892014-05-13 12:53:07 -0400250 if (symlink(applibdir, libsymlink) < 0) {
251 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
252 applibdir, strerror(errno));
253 unlink(pkgdir);
254 return -1;
255 }
256
Mike Lockwood94afecf2012-10-24 10:45:23 -0700257 if (chown(pkgdir, uid, uid) < 0) {
258 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
259 unlink(libsymlink);
260 unlink(pkgdir);
261 return -errno;
262 }
263
264 return 0;
265}
266
Robin Lee7c8bec02014-06-10 18:46:26 +0100267int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700268{
Robin Lee095c7632014-04-25 15:05:19 +0100269 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700270 return -1;
271 }
272
273 return 0;
274}
275
Robin Lee095c7632014-04-25 15:05:19 +0100276int delete_user(userid_t userid)
277{
278 int status = 0;
279
280 char data_path[PKG_PATH_MAX];
281 if ((create_user_path(data_path, userid) != 0)
282 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
283 status = -1;
284 }
285
286 char media_path[PATH_MAX];
287 if ((create_user_media_path(media_path, userid) != 0)
288 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
289 status = -1;
290 }
291
292 char config_path[PATH_MAX];
293 if ((create_user_config_path(config_path, userid) != 0)
294 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
295 status = -1;
296 }
297
298 return status;
299}
300
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700301int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700302{
303 char cachedir[PKG_PATH_MAX];
304
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700305 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700306 return -1;
307
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700308 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100309 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700310}
311
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700312int delete_code_cache(const char *pkgname, userid_t userid)
313{
314 char codecachedir[PKG_PATH_MAX];
Jeff Sharkey770180a2014-09-08 17:14:26 -0700315 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700316
317 if (create_pkg_path(codecachedir, pkgname, CODE_CACHE_DIR_POSTFIX, userid))
318 return -1;
319
Jeff Sharkey770180a2014-09-08 17:14:26 -0700320 /* it's okay if code cache is missing */
321 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
322 return 0;
323 }
324
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700325 /* delete contents, not the directory, no exceptions */
326 return delete_dir_contents(codecachedir, 0, NULL);
327}
328
Mike Lockwood94afecf2012-10-24 10:45:23 -0700329/* Try to ensure free_size bytes of storage are available.
330 * Returns 0 on success.
331 * This is rather simple-minded because doing a full LRU would
332 * be potentially memory-intensive, and without atime it would
333 * also require that apps constantly modify file metadata even
334 * when just reading from the cache, which is pretty awful.
335 */
336int free_cache(int64_t free_size)
337{
338 cache_t* cache;
339 int64_t avail;
340 DIR *d;
341 struct dirent *de;
342 char tmpdir[PATH_MAX];
343 char *dirpos;
344
345 avail = data_disk_free();
346 if (avail < 0) return -1;
347
348 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
349 if (avail >= free_size) return 0;
350
351 cache = start_cache_collection();
352
353 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700354 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700355 //ALOGI("adding cache files from %s\n", tmpdir);
356 add_cache_files(cache, tmpdir, "cache");
357 }
358
359 // Search for other users and add any cache files from them.
360 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
361 SECONDARY_USER_PREFIX);
362 dirpos = tmpdir + strlen(tmpdir);
363 d = opendir(tmpdir);
364 if (d != NULL) {
365 while ((de = readdir(d))) {
366 if (de->d_type == DT_DIR) {
367 const char *name = de->d_name;
368 /* always skip "." and ".." */
369 if (name[0] == '.') {
370 if (name[1] == 0) continue;
371 if ((name[1] == '.') && (name[2] == 0)) continue;
372 }
373 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
374 strcpy(dirpos, name);
375 //ALOGI("adding cache files from %s\n", tmpdir);
376 add_cache_files(cache, tmpdir, "cache");
377 } else {
378 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
379 }
380 }
381 }
382 closedir(d);
383 }
384
385 // Collect cache files on external storage for all users (if it is mounted as part
386 // of the internal storage).
387 strcpy(tmpdir, android_media_dir.path);
388 dirpos = tmpdir + strlen(tmpdir);
389 d = opendir(tmpdir);
390 if (d != NULL) {
391 while ((de = readdir(d))) {
392 if (de->d_type == DT_DIR) {
393 const char *name = de->d_name;
394 /* skip any dir that doesn't start with a number, so not a user */
395 if (name[0] < '0' || name[0] > '9') {
396 continue;
397 }
398 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
399 strcpy(dirpos, name);
400 if (lookup_media_dir(tmpdir, "Android") == 0
401 && lookup_media_dir(tmpdir, "data") == 0) {
402 //ALOGI("adding cache files from %s\n", tmpdir);
403 add_cache_files(cache, tmpdir, "cache");
404 }
405 } else {
406 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
407 }
408 }
409 }
410 closedir(d);
411 }
412
413 clear_cache_files(cache, free_size);
414 finish_cache_collection(cache);
415
416 return data_disk_free() >= free_size ? 0 : -1;
417}
418
Narayan Kamath1b400322014-04-11 13:17:00 +0100419int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700420{
421 char src_dex[PKG_PATH_MAX];
422 char dst_dex[PKG_PATH_MAX];
423
Jeff Sharkey770180a2014-09-08 17:14:26 -0700424 if (validate_apk_path(src)) {
425 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
426 return -1;
427 }
428 if (validate_apk_path(dst)) {
429 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
430 return -1;
431 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700432
Narayan Kamath1b400322014-04-11 13:17:00 +0100433 if (create_cache_path(src_dex, src, instruction_set)) return -1;
434 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700435
436 ALOGV("move %s -> %s\n", src_dex, dst_dex);
437 if (rename(src_dex, dst_dex) < 0) {
438 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
439 return -1;
440 } else {
441 return 0;
442 }
443}
444
Narayan Kamath1b400322014-04-11 13:17:00 +0100445int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700446{
447 char dex_path[PKG_PATH_MAX];
448
Jeff Sharkey770180a2014-09-08 17:14:26 -0700449 if (validate_apk_path(path) && validate_system_app_path(path)) {
450 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
451 return -1;
452 }
453
Narayan Kamath1b400322014-04-11 13:17:00 +0100454 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700455
456 ALOGV("unlink %s\n", dex_path);
457 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700458 if (errno != ENOENT) {
459 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
460 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700461 return -1;
462 } else {
463 return 0;
464 }
465}
466
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700467int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700468 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100469 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
470 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700471{
472 DIR *d;
473 int dfd;
474 struct dirent *de;
475 struct stat s;
476 char path[PKG_PATH_MAX];
477
478 int64_t codesize = 0;
479 int64_t datasize = 0;
480 int64_t cachesize = 0;
481 int64_t asecsize = 0;
482
483 /* count the source apk as code -- but only if it's not
484 * on the /system partition and its not on the sdcard.
485 */
486 if (validate_system_app_path(apkpath) &&
487 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
488 if (stat(apkpath, &s) == 0) {
489 codesize += stat_size(&s);
490 }
491 }
492 /* count the forward locked apk as code if it is given
493 */
494 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
495 if (stat(fwdlock_apkpath, &s) == 0) {
496 codesize += stat_size(&s);
497 }
498 }
499 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100500 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700501 if (stat(path, &s) == 0) {
502 codesize += stat_size(&s);
503 }
504 }
505
506 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700507 if (libdirpath != NULL && libdirpath[0] != '!') {
508 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700509 if (d != NULL) {
510 dfd = dirfd(d);
511 codesize += calculate_dir_size(dfd);
512 closedir(d);
513 }
514 }
515
516 /* compute asec size if it is given
517 */
518 if (asecpath != NULL && asecpath[0] != '!') {
519 if (stat(asecpath, &s) == 0) {
520 asecsize += stat_size(&s);
521 }
522 }
523
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700524 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700525 goto done;
526 }
527
528 d = opendir(path);
529 if (d == NULL) {
530 goto done;
531 }
532 dfd = dirfd(d);
533
534 /* most stuff in the pkgdir is data, except for the "cache"
535 * directory and below, which is cache, and the "lib" directory
536 * and below, which is code...
537 */
538 while ((de = readdir(d))) {
539 const char *name = de->d_name;
540
541 if (de->d_type == DT_DIR) {
542 int subfd;
543 int64_t statsize = 0;
544 int64_t dirsize = 0;
545 /* always skip "." and ".." */
546 if (name[0] == '.') {
547 if (name[1] == 0) continue;
548 if ((name[1] == '.') && (name[2] == 0)) continue;
549 }
550 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
551 statsize = stat_size(&s);
552 }
553 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
554 if (subfd >= 0) {
555 dirsize = calculate_dir_size(subfd);
556 }
557 if(!strcmp(name,"lib")) {
558 codesize += dirsize + statsize;
559 } else if(!strcmp(name,"cache")) {
560 cachesize += dirsize + statsize;
561 } else {
562 datasize += dirsize + statsize;
563 }
564 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
565 // This is the symbolic link to the application's library
566 // code. We'll count this as code instead of data, since
567 // it is not something that the app creates.
568 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
569 codesize += stat_size(&s);
570 }
571 } else {
572 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
573 datasize += stat_size(&s);
574 }
575 }
576 }
577 closedir(d);
578done:
579 *_codesize = codesize;
580 *_datasize = datasize;
581 *_cachesize = cachesize;
582 *_asecsize = asecsize;
583 return 0;
584}
585
Narayan Kamath1b400322014-04-11 13:17:00 +0100586int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700587{
588 char *tmp;
589 int srclen;
590 int dstlen;
591
592 srclen = strlen(src);
593
594 /* demand that we are an absolute path */
595 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
596 return -1;
597 }
598
599 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
600 return -1;
601 }
602
Dave Allisond9370732014-01-30 14:19:23 -0800603 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100604 strlen(instruction_set) +
605 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800606
Mike Lockwood94afecf2012-10-24 10:45:23 -0700607 if (dstlen > PKG_PATH_MAX) {
608 return -1;
609 }
610
Narayan Kamath1b400322014-04-11 13:17:00 +0100611 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700612 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100613 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700614 src + 1, /* skip the leading / */
615 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800616
Narayan Kamath1b400322014-04-11 13:17:00 +0100617 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700618 if (*tmp == '/') {
619 *tmp = '@';
620 }
621 }
622
623 return 0;
624}
625
626static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800627 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700628{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800629 /* platform-specific flags affecting optimization and verification */
630 char dexopt_flags[PROPERTY_VALUE_MAX];
631 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
632 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
633
Mike Lockwood94afecf2012-10-24 10:45:23 -0700634 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
635 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
636 char zip_num[MAX_INT_LEN];
637 char odex_num[MAX_INT_LEN];
638
639 sprintf(zip_num, "%d", zip_fd);
640 sprintf(odex_num, "%d", odex_fd);
641
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700642 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700643 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
644 dexopt_flags, (char*) NULL);
645 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
646}
647
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700648static int split_count(const char *str)
649{
650 char *ctx;
651 int count = 0;
652 char buf[PROPERTY_VALUE_MAX];
653
654 strncpy(buf, str, sizeof(buf));
655 char *pBuf = buf;
656
657 while(strtok_r(pBuf, " ", &ctx) != NULL) {
658 count++;
659 pBuf = NULL;
660 }
661
662 return count;
663}
664
665static int split(char *buf, char **argv)
666{
667 char *ctx;
668 int count = 0;
669 char *tok;
670 char *pBuf = buf;
671
672 while((tok = strtok_r(pBuf, " ", &ctx)) != NULL) {
673 argv[count++] = tok;
674 pBuf = NULL;
675 }
676
677 return count;
678}
679
Alex Light7365a102014-07-21 12:23:48 -0700680static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700681 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700682{
683 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100684 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700685
686 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
687 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
688 ALOGE("Instruction set %s longer than max length of %d",
689 instruction_set, MAX_INSTRUCTION_SET_LEN);
690 return;
691 }
692
693 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
694 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
695 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
696 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
697 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
698 // The caller has already gotten all the locks we need.
699 const char* no_lock_arg = "--no-lock-output";
700 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
701 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
702 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700703 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700704 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
705
706 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
707 char* argv[7];
708 argv[0] = (char*) PATCHOAT_BIN;
709 argv[1] = (char*) patched_image_location_arg;
710 argv[2] = (char*) no_lock_arg;
711 argv[3] = instruction_set_arg;
712 argv[4] = output_oat_fd_arg;
713 argv[5] = input_oat_fd_arg;
714 argv[6] = NULL;
715
716 execv(PATCHOAT_BIN, (char* const *)argv);
717 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
718}
719
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700720static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Calin Juravleb1efac12014-08-21 19:05:20 +0100721 const char* output_file_name, const char *pkgname, const char *instruction_set,
722 bool vm_safe_mode)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700723{
Calin Juravle8fc73152014-08-19 18:48:50 +0100724 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
725
726 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
727 ALOGE("Instruction set %s longer than max length of %d",
728 instruction_set, MAX_INSTRUCTION_SET_LEN);
729 return;
730 }
731
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700732 char prop_buf[PROPERTY_VALUE_MAX];
733 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
734
735 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
736 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
737
738 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
739 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
740
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700741 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
742 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
743 dex2oat_compiler_filter_flag, NULL) > 0;
744
Calin Juravle8fc73152014-08-19 18:48:50 +0100745 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
746 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
747 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
748 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
749 dex2oat_isa_features, NULL) > 0;
750
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800751 char dex2oat_flags[PROPERTY_VALUE_MAX];
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700752 int dex2oat_flags_count = property_get("dalvik.vm.dex2oat-flags",
753 dex2oat_flags, NULL) <= 0 ? 0 : split_count(dex2oat_flags);
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800754 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
755
Brian Carlstrom538998f2014-07-30 14:37:11 -0700756 // If we booting without the real /data, don't spend time compiling.
757 char vold_decrypt[PROPERTY_VALUE_MAX];
758 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
759 bool skip_compilation = (have_vold_decrypt &&
760 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
761 (strcmp(vold_decrypt, "1") == 0)));
762
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700763 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700764
Brian Carlstrom53e07762014-06-27 14:15:19 -0700765 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700766
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700767 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100768
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700769 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
770 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
771 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700772 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100773 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravle8fc73152014-08-19 18:48:50 +0100774 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100775 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
776 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700777 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
778 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700779 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700780
781 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
782 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
783 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
784 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100785 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle8fc73152014-08-19 18:48:50 +0100786 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Calin Juravle57c69c32014-06-06 14:42:16 +0100787
Calin Juravle4fdff462014-06-06 16:58:43 +0100788 bool have_profile_file = false;
789 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100790 if (profiler && (strcmp(pkgname, "*") != 0)) {
791 char profile_file[PKG_PATH_MAX];
792 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800793 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100794 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100795 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100796 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100797 have_profile_file = true;
798 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
799 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
800 "--top-k-profile-threshold=%s", prop_buf);
801 have_top_k_profile_threshold = true;
802 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100803 }
Dave Allisond9370732014-01-30 14:19:23 -0800804 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700805
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700806 if (have_dex2oat_Xms_flag) {
807 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
808 }
809 if (have_dex2oat_Xmx_flag) {
810 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
811 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700812 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700813 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700814 have_dex2oat_compiler_filter_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100815 } else if (vm_safe_mode) {
816 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100817 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700818 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700819 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
820 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700821
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700822 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100823
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700824 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravle8fc73152014-08-19 18:48:50 +0100825 + (have_dex2oat_isa_features ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100826 + (have_profile_file ? 1 : 0)
827 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700828 + (have_dex2oat_Xms_flag ? 2 : 0)
829 + (have_dex2oat_Xmx_flag ? 2 : 0)
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700830 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700831 + dex2oat_flags_count];
Calin Juravle4fdff462014-06-06 16:58:43 +0100832 int i = 0;
833 argv[i++] = (char*)DEX2OAT_BIN;
834 argv[i++] = zip_fd_arg;
835 argv[i++] = zip_location_arg;
836 argv[i++] = oat_fd_arg;
837 argv[i++] = oat_location_arg;
838 argv[i++] = instruction_set_arg;
Calin Juravle8fc73152014-08-19 18:48:50 +0100839 if (have_dex2oat_isa_features) {
840 argv[i++] = instruction_set_features_arg;
841 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100842 if (have_profile_file) {
843 argv[i++] = profile_file_arg;
844 }
845 if (have_top_k_profile_threshold) {
846 argv[i++] = top_k_profile_threshold_arg;
847 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700848 if (have_dex2oat_Xms_flag) {
849 argv[i++] = (char*)RUNTIME_ARG;
850 argv[i++] = dex2oat_Xms_arg;
851 }
852 if (have_dex2oat_Xmx_flag) {
853 argv[i++] = (char*)RUNTIME_ARG;
854 argv[i++] = dex2oat_Xmx_arg;
855 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700856 if (have_dex2oat_compiler_filter_flag) {
857 argv[i++] = dex2oat_compiler_filter_arg;
858 }
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700859 if (dex2oat_flags_count) {
860 i += split(dex2oat_flags, argv + i);
Calin Juravle4fdff462014-06-06 16:58:43 +0100861 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700862 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100863 argv[i] = NULL;
864
865 execv(DEX2OAT_BIN, (char* const *)argv);
Yevgeny Roubanb0d8d002014-09-08 17:02:10 +0700866 ALOGE("execv(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700867}
868
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100869static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700870{
871 int status;
872 pid_t got_pid;
873
Mike Lockwood94afecf2012-10-24 10:45:23 -0700874 while (1) {
875 got_pid = waitpid(pid, &status, 0);
876 if (got_pid == -1 && errno == EINTR) {
877 printf("waitpid interrupted, retrying\n");
878 } else {
879 break;
880 }
881 }
882 if (got_pid != pid) {
883 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
884 (int) pid, (int) got_pid, strerror(errno));
885 return 1;
886 }
887
888 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700889 return 0;
890 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700891 return status; /* always nonzero */
892 }
893}
894
Calin Juravleb1efac12014-08-21 19:05:20 +0100895int dexopt(const char *apk_path, uid_t uid, bool is_public,
Alex Light7365a102014-07-21 12:23:48 -0700896 const char *pkgname, const char *instruction_set,
Calin Juravleb1efac12014-08-21 19:05:20 +0100897 bool vm_safe_mode, bool is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700898{
899 struct utimbuf ut;
Alex Light7365a102014-07-21 12:23:48 -0700900 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700901 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700902 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700903 char *end;
Alex Light7365a102014-07-21 12:23:48 -0700904 const char *input_file;
905 char in_odex_path[PKG_PATH_MAX];
906 int res, input_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700907
Mike Lockwood94afecf2012-10-24 10:45:23 -0700908 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
909 return -1;
910 }
911
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800912 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700913 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700914
Alex Light7365a102014-07-21 12:23:48 -0700915 if (is_patchoat && strncmp(persist_sys_dalvik_vm_lib, "libart", 6) != 0) {
916 /* We may only patch if we are libart */
917 ALOGE("Patching is only supported in libart\n");
918 return -1;
919 }
920
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700921 /* Before anything else: is there a .odex file? If so, we have
922 * precompiled the apk and there is nothing to do here.
Alex Light7365a102014-07-21 12:23:48 -0700923 *
924 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700925 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800926 strcpy(out_path, apk_path);
927 end = strrchr(out_path, '.');
Alex Light7365a102014-07-21 12:23:48 -0700928 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800929 strcpy(end, ".odex");
930 if (stat(out_path, &dex_stat) == 0) {
931 return 0;
932 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700933 }
934
Narayan Kamath1b400322014-04-11 13:17:00 +0100935 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700936 return -1;
937 }
938
Alex Light7365a102014-07-21 12:23:48 -0700939 if (is_patchoat) {
940 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
941 strcpy(in_odex_path, apk_path);
942 end = strrchr(in_odex_path, '/');
943 if (end == NULL) {
944 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
945 return -1;
946 }
947 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
948 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
949 strcat(in_odex_path, apk_end);
950 end = strrchr(in_odex_path, '.');
951 if (end == NULL) {
952 return -1;
953 }
954 strcpy(end + 1, "odex");
955 input_file = in_odex_path;
956 } else {
957 input_file = apk_path;
958 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700959
Alex Light7365a102014-07-21 12:23:48 -0700960 memset(&input_stat, 0, sizeof(input_stat));
961 stat(input_file, &input_stat);
962
963 input_fd = open(input_file, O_RDONLY, 0);
964 if (input_fd < 0) {
965 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700966 return -1;
967 }
968
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700969 unlink(out_path);
970 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
971 if (out_fd < 0) {
972 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700973 goto fail;
974 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700975 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700976 S_IRUSR|S_IWUSR|S_IRGRP |
977 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700978 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700979 goto fail;
980 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700981 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
982 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700983 goto fail;
984 }
985
Dave Allisond9370732014-01-30 14:19:23 -0800986 // Create profile file if there is a package name present.
987 if (strcmp(pkgname, "*") != 0) {
988 create_profile_file(pkgname, uid);
989 }
990
991
Alex Light7365a102014-07-21 12:23:48 -0700992 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700993
994 pid_t pid;
995 pid = fork();
996 if (pid == 0) {
997 /* child -- drop privileges before continuing */
998 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700999 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001000 exit(64);
1001 }
1002 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001003 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001004 exit(65);
1005 }
1006 // drop capabilities
1007 struct __user_cap_header_struct capheader;
1008 struct __user_cap_data_struct capdata[2];
1009 memset(&capheader, 0, sizeof(capheader));
1010 memset(&capdata, 0, sizeof(capdata));
1011 capheader.version = _LINUX_CAPABILITY_VERSION_3;
1012 if (capset(&capheader, &capdata[0]) < 0) {
1013 ALOGE("capset failed: %s\n", strerror(errno));
1014 exit(66);
1015 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -07001016 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
1017 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
1018 exit(70);
1019 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001020 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
1021 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -07001022 exit(67);
1023 }
1024
Brian Carlstrome7a8b172013-06-30 13:17:38 -07001025 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001026 run_dexopt(input_fd, out_fd, input_file, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -07001027 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001028 if (is_patchoat) {
1029 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
1030 } else {
Calin Juravleb1efac12014-08-21 19:05:20 +01001031 run_dex2oat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set,
1032 vm_safe_mode);
Alex Light7365a102014-07-21 12:23:48 -07001033 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001034 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -07001035 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001036 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001037 exit(68); /* only get here on exec failure */
1038 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001039 res = wait_child(pid);
1040 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -07001041 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001042 } else {
Alex Light7365a102014-07-21 12:23:48 -07001043 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001044 goto fail;
1045 }
1046 }
1047
Alex Light7365a102014-07-21 12:23:48 -07001048 ut.actime = input_stat.st_atime;
1049 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001050 utime(out_path, &ut);
1051
1052 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -07001053 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001054 return 0;
1055
1056fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001057 if (out_fd >= 0) {
1058 close(out_fd);
1059 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001060 }
Alex Light7365a102014-07-21 12:23:48 -07001061 if (input_fd >= 0) {
1062 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001063 }
1064 return -1;
1065}
1066
1067void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1068 struct stat* statbuf)
1069{
1070 while (path[basepos] != 0) {
1071 if (path[basepos] == '/') {
1072 path[basepos] = 0;
1073 if (lstat(path, statbuf) < 0) {
1074 ALOGV("Making directory: %s\n", path);
1075 if (mkdir(path, mode) == 0) {
1076 chown(path, uid, gid);
1077 } else {
1078 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1079 }
1080 }
1081 path[basepos] = '/';
1082 basepos++;
1083 }
1084 basepos++;
1085 }
1086}
1087
1088int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1089 int dstuid, int dstgid, struct stat* statbuf)
1090{
1091 DIR *d;
1092 struct dirent *de;
1093 int res;
1094
1095 int srcend = strlen(srcpath);
1096 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001097
Mike Lockwood94afecf2012-10-24 10:45:23 -07001098 if (lstat(srcpath, statbuf) < 0) {
1099 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1100 return 1;
1101 }
Dave Allisond9370732014-01-30 14:19:23 -08001102
Mike Lockwood94afecf2012-10-24 10:45:23 -07001103 if ((statbuf->st_mode&S_IFDIR) == 0) {
1104 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1105 dstuid, dstgid, statbuf);
1106 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1107 if (rename(srcpath, dstpath) >= 0) {
1108 if (chown(dstpath, dstuid, dstgid) < 0) {
1109 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1110 unlink(dstpath);
1111 return 1;
1112 }
1113 } else {
1114 ALOGW("Unable to rename %s to %s: %s\n",
1115 srcpath, dstpath, strerror(errno));
1116 return 1;
1117 }
1118 return 0;
1119 }
1120
1121 d = opendir(srcpath);
1122 if (d == NULL) {
1123 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1124 return 1;
1125 }
1126
1127 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001128
Mike Lockwood94afecf2012-10-24 10:45:23 -07001129 while ((de = readdir(d))) {
1130 const char *name = de->d_name;
1131 /* always skip "." and ".." */
1132 if (name[0] == '.') {
1133 if (name[1] == 0) continue;
1134 if ((name[1] == '.') && (name[2] == 0)) continue;
1135 }
Dave Allisond9370732014-01-30 14:19:23 -08001136
Mike Lockwood94afecf2012-10-24 10:45:23 -07001137 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1138 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1139 continue;
1140 }
Dave Allisond9370732014-01-30 14:19:23 -08001141
Mike Lockwood94afecf2012-10-24 10:45:23 -07001142 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1143 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1144 continue;
1145 }
Dave Allisond9370732014-01-30 14:19:23 -08001146
Mike Lockwood94afecf2012-10-24 10:45:23 -07001147 srcpath[srcend] = dstpath[dstend] = '/';
1148 strcpy(srcpath+srcend+1, name);
1149 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001150
Mike Lockwood94afecf2012-10-24 10:45:23 -07001151 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1152 res = 1;
1153 }
Dave Allisond9370732014-01-30 14:19:23 -08001154
Mike Lockwood94afecf2012-10-24 10:45:23 -07001155 // Note: we will be leaving empty directories behind in srcpath,
1156 // but that is okay, the package manager will be erasing all of the
1157 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001158
Mike Lockwood94afecf2012-10-24 10:45:23 -07001159 srcpath[srcend] = dstpath[dstend] = 0;
1160 }
Dave Allisond9370732014-01-30 14:19:23 -08001161
Mike Lockwood94afecf2012-10-24 10:45:23 -07001162 closedir(d);
1163 return res;
1164}
1165
1166int movefiles()
1167{
1168 DIR *d;
1169 int dfd, subfd;
1170 struct dirent *de;
1171 struct stat s;
1172 char buf[PKG_PATH_MAX+1];
1173 int bufp, bufe, bufi, readlen;
1174
1175 char srcpkg[PKG_NAME_MAX];
1176 char dstpkg[PKG_NAME_MAX];
1177 char srcpath[PKG_PATH_MAX];
1178 char dstpath[PKG_PATH_MAX];
1179 int dstuid=-1, dstgid=-1;
1180 int hasspace;
1181
1182 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1183 if (d == NULL) {
1184 goto done;
1185 }
1186 dfd = dirfd(d);
1187
1188 /* Iterate through all files in the directory, executing the
1189 * file movements requested there-in.
1190 */
1191 while ((de = readdir(d))) {
1192 const char *name = de->d_name;
1193
1194 if (de->d_type == DT_DIR) {
1195 continue;
1196 } else {
1197 subfd = openat(dfd, name, O_RDONLY);
1198 if (subfd < 0) {
1199 ALOGW("Unable to open update commands at %s%s\n",
1200 UPDATE_COMMANDS_DIR_PREFIX, name);
1201 continue;
1202 }
Dave Allisond9370732014-01-30 14:19:23 -08001203
Mike Lockwood94afecf2012-10-24 10:45:23 -07001204 bufp = 0;
1205 bufe = 0;
1206 buf[PKG_PATH_MAX] = 0;
1207 srcpkg[0] = dstpkg[0] = 0;
1208 while (1) {
1209 bufi = bufp;
1210 while (bufi < bufe && buf[bufi] != '\n') {
1211 bufi++;
1212 }
1213 if (bufi < bufe) {
1214 buf[bufi] = 0;
1215 ALOGV("Processing line: %s\n", buf+bufp);
1216 hasspace = 0;
1217 while (bufp < bufi && isspace(buf[bufp])) {
1218 hasspace = 1;
1219 bufp++;
1220 }
1221 if (buf[bufp] == '#' || bufp == bufi) {
1222 // skip comments and empty lines.
1223 } else if (hasspace) {
1224 if (dstpkg[0] == 0) {
1225 ALOGW("Path before package line in %s%s: %s\n",
1226 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1227 } else if (srcpkg[0] == 0) {
1228 // Skip -- source package no longer exists.
1229 } else {
1230 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1231 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1232 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1233 movefileordir(srcpath, dstpath,
1234 strlen(dstpath)-strlen(buf+bufp),
1235 dstuid, dstgid, &s);
1236 }
1237 }
1238 } else {
1239 char* div = strchr(buf+bufp, ':');
1240 if (div == NULL) {
1241 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1242 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1243 } else {
1244 *div = 0;
1245 div++;
1246 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1247 strcpy(dstpkg, buf+bufp);
1248 } else {
1249 srcpkg[0] = dstpkg[0] = 0;
1250 ALOGW("Package name too long in %s%s: %s\n",
1251 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1252 }
1253 if (strlen(div) < PKG_NAME_MAX) {
1254 strcpy(srcpkg, div);
1255 } else {
1256 srcpkg[0] = dstpkg[0] = 0;
1257 ALOGW("Package name too long in %s%s: %s\n",
1258 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1259 }
1260 if (srcpkg[0] != 0) {
1261 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1262 if (lstat(srcpath, &s) < 0) {
1263 // Package no longer exists -- skip.
1264 srcpkg[0] = 0;
1265 }
1266 } else {
1267 srcpkg[0] = 0;
1268 ALOGW("Can't create path %s in %s%s\n",
1269 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1270 }
1271 if (srcpkg[0] != 0) {
1272 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1273 if (lstat(dstpath, &s) == 0) {
1274 dstuid = s.st_uid;
1275 dstgid = s.st_gid;
1276 } else {
1277 // Destination package doesn't
1278 // exist... due to original-package,
1279 // this is normal, so don't be
1280 // noisy about it.
1281 srcpkg[0] = 0;
1282 }
1283 } else {
1284 srcpkg[0] = 0;
1285 ALOGW("Can't create path %s in %s%s\n",
1286 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1287 }
1288 }
1289 ALOGV("Transfering from %s to %s: uid=%d\n",
1290 srcpkg, dstpkg, dstuid);
1291 }
1292 }
1293 }
1294 bufp = bufi+1;
1295 } else {
1296 if (bufp == 0) {
1297 if (bufp < bufe) {
1298 ALOGW("Line too long in %s%s, skipping: %s\n",
1299 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1300 }
1301 } else if (bufp < bufe) {
1302 memcpy(buf, buf+bufp, bufe-bufp);
1303 bufe -= bufp;
1304 bufp = 0;
1305 }
1306 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1307 if (readlen < 0) {
1308 ALOGW("Failure reading update commands in %s%s: %s\n",
1309 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1310 break;
1311 } else if (readlen == 0) {
1312 break;
1313 }
1314 bufe += readlen;
1315 buf[bufe] = 0;
1316 ALOGV("Read buf: %s\n", buf);
1317 }
1318 }
1319 close(subfd);
1320 }
1321 }
1322 closedir(d);
1323done:
1324 return 0;
1325}
1326
1327int linklib(const char* pkgname, const char* asecLibDir, int userId)
1328{
1329 char pkgdir[PKG_PATH_MAX];
1330 char libsymlink[PKG_PATH_MAX];
1331 struct stat s, libStat;
1332 int rc = 0;
1333
1334 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1335 ALOGE("cannot create package path\n");
1336 return -1;
1337 }
1338 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1339 ALOGE("cannot create package lib symlink origin path\n");
1340 return -1;
1341 }
1342
1343 if (stat(pkgdir, &s) < 0) return -1;
1344
1345 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1346 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1347 return -1;
1348 }
1349
1350 if (chmod(pkgdir, 0700) < 0) {
1351 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1352 rc = -1;
1353 goto out;
1354 }
1355
1356 if (lstat(libsymlink, &libStat) < 0) {
1357 if (errno != ENOENT) {
1358 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1359 rc = -1;
1360 goto out;
1361 }
1362 } else {
1363 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001364 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001365 rc = -1;
1366 goto out;
1367 }
1368 } else if (S_ISLNK(libStat.st_mode)) {
1369 if (unlink(libsymlink) < 0) {
1370 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1371 rc = -1;
1372 goto out;
1373 }
1374 }
1375 }
1376
1377 if (symlink(asecLibDir, libsymlink) < 0) {
1378 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1379 strerror(errno));
1380 rc = -errno;
1381 goto out;
1382 }
1383
1384out:
1385 if (chmod(pkgdir, s.st_mode) < 0) {
1386 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1387 rc = -errno;
1388 }
1389
1390 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1391 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1392 return -errno;
1393 }
1394
1395 return rc;
1396}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001397
1398static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1399{
1400 static const char *IDMAP_BIN = "/system/bin/idmap";
1401 static const size_t MAX_INT_LEN = 32;
1402 char idmap_str[MAX_INT_LEN];
1403
1404 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1405
1406 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1407 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1408}
1409
1410// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1411// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1412static int flatten_path(const char *prefix, const char *suffix,
1413 const char *overlay_path, char *idmap_path, size_t N)
1414{
1415 if (overlay_path == NULL || idmap_path == NULL) {
1416 return -1;
1417 }
1418 const size_t len_overlay_path = strlen(overlay_path);
1419 // will access overlay_path + 1 further below; requires absolute path
1420 if (len_overlay_path < 2 || *overlay_path != '/') {
1421 return -1;
1422 }
1423 const size_t len_idmap_root = strlen(prefix);
1424 const size_t len_suffix = strlen(suffix);
1425 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1426 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1427 // additions below would cause overflow
1428 return -1;
1429 }
1430 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1431 return -1;
1432 }
1433 memset(idmap_path, 0, N);
1434 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1435 char *ch = idmap_path + len_idmap_root;
1436 while (*ch != '\0') {
1437 if (*ch == '/') {
1438 *ch = '@';
1439 }
1440 ++ch;
1441 }
1442 return 0;
1443}
1444
1445int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1446{
1447 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1448
1449 int idmap_fd = -1;
1450 char idmap_path[PATH_MAX];
1451
1452 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1453 idmap_path, sizeof(idmap_path)) == -1) {
1454 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1455 goto fail;
1456 }
1457
1458 unlink(idmap_path);
1459 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1460 if (idmap_fd < 0) {
1461 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1462 goto fail;
1463 }
1464 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1465 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1466 goto fail;
1467 }
1468 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1469 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1470 goto fail;
1471 }
1472
1473 pid_t pid;
1474 pid = fork();
1475 if (pid == 0) {
1476 /* child -- drop privileges before continuing */
1477 if (setgid(uid) != 0) {
1478 ALOGE("setgid(%d) failed during idmap\n", uid);
1479 exit(1);
1480 }
1481 if (setuid(uid) != 0) {
1482 ALOGE("setuid(%d) failed during idmap\n", uid);
1483 exit(1);
1484 }
1485 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1486 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1487 exit(1);
1488 }
1489
1490 run_idmap(target_apk, overlay_apk, idmap_fd);
1491 exit(1); /* only if exec call to idmap failed */
1492 } else {
1493 int status = wait_child(pid);
1494 if (status != 0) {
1495 ALOGE("idmap failed, status=0x%04x\n", status);
1496 goto fail;
1497 }
1498 }
1499
1500 close(idmap_fd);
1501 return 0;
1502fail:
1503 if (idmap_fd >= 0) {
1504 close(idmap_fd);
1505 unlink(idmap_path);
1506 }
1507 return -1;
1508}
Robert Craige9887e42014-02-20 10:25:56 -05001509
Robert Craigda30dc72014-03-27 10:21:12 -04001510int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001511{
Robert Craigda30dc72014-03-27 10:21:12 -04001512 struct dirent *entry;
1513 DIR *d;
1514 struct stat s;
1515 char *userdir;
1516 char *primarydir;
1517 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001518 int ret = 0;
1519
Robert Craigda30dc72014-03-27 10:21:12 -04001520 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1521 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1522
1523 if (!pkgName || !seinfo) {
1524 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001525 return -1;
1526 }
1527
Robert Craigda30dc72014-03-27 10:21:12 -04001528 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1529 return -1;
1530 }
1531
1532 // Relabel for primary user.
1533 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1534 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001535 ret |= -1;
1536 }
1537
Robert Craigda30dc72014-03-27 10:21:12 -04001538 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1539 free(primarydir);
1540 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001541 }
1542
Robert Craigda30dc72014-03-27 10:21:12 -04001543 // Relabel package directory for all secondary users.
1544 d = opendir(userdir);
1545 if (d == NULL) {
1546 free(primarydir);
1547 free(userdir);
1548 return -1;
1549 }
1550
1551 while ((entry = readdir(d))) {
1552 if (entry->d_type != DT_DIR) {
1553 continue;
1554 }
1555
1556 const char *user = entry->d_name;
1557 // Ignore "." and ".."
1558 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1559 continue;
1560 }
1561
1562 // user directories start with a number
1563 if (user[0] < '0' || user[0] > '9') {
1564 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1565 continue;
1566 }
1567
1568 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1569 continue;
1570 }
1571
1572 if (stat(pkgdir, &s) < 0) {
1573 free(pkgdir);
1574 continue;
1575 }
1576
Stephen Smalley8ac2a642014-09-08 15:51:55 -04001577 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, s.st_uid, flags) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001578 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1579 ret |= -1;
1580 }
1581 free(pkgdir);
1582 }
1583
1584 closedir(d);
1585 free(primarydir);
1586 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001587 return ret;
1588}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001589