blob: 1954d8dda5b9332620334b906b5884f2ba3052a2 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080018#include <sys/capability.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070019#include "installd.h"
Brian Carlstrom3b14e5b2014-08-08 00:52:22 -070020#include <cutils/sched_policy.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070021#include <diskusage/dirsize.h>
22#include <selinux/android.h>
Igor Murashkin9e87a802014-11-05 15:21:12 -080023#include <system/thread_defs.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070024
25/* Directory records that are used in execution of commands. */
26dir_rec_t android_data_dir;
27dir_rec_t android_asec_dir;
28dir_rec_t android_app_dir;
29dir_rec_t android_app_private_dir;
30dir_rec_t android_app_lib_dir;
31dir_rec_t android_media_dir;
32dir_rec_array_t android_system_dirs;
33
Robert Craig4d3fd4e2013-03-25 06:33:03 -040034int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070035{
36 char pkgdir[PKG_PATH_MAX];
37 char libsymlink[PKG_PATH_MAX];
38 char applibdir[PKG_PATH_MAX];
39 struct stat libStat;
40
41 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
42 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
43 return -1;
44 }
45
46 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
47 ALOGE("cannot create package path\n");
48 return -1;
49 }
50
51 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
52 ALOGE("cannot create package lib symlink origin path\n");
53 return -1;
54 }
55
56 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
57 ALOGE("cannot create package lib symlink dest path\n");
58 return -1;
59 }
60
Nick Kralevicha2d838a2013-01-09 16:00:35 -080061 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070062 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
63 return -1;
64 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080065 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070066 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
67 unlink(pkgdir);
68 return -1;
69 }
70
71 if (lstat(libsymlink, &libStat) < 0) {
72 if (errno != ENOENT) {
73 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
74 return -1;
75 }
76 } else {
77 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +010078 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070079 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
80 return -1;
81 }
82 } else if (S_ISLNK(libStat.st_mode)) {
83 if (unlink(libsymlink) < 0) {
84 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
85 return -1;
86 }
87 }
88 }
89
Stephen Smalley26288202014-02-07 09:16:46 -050090 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070091 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
92 unlink(libsymlink);
93 unlink(pkgdir);
94 return -errno;
95 }
96
Stephen Smalley3a983892014-05-13 12:53:07 -040097 if (symlink(applibdir, libsymlink) < 0) {
98 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
99 strerror(errno));
100 unlink(pkgdir);
101 return -1;
102 }
103
Mike Lockwood94afecf2012-10-24 10:45:23 -0700104 if (chown(pkgdir, uid, gid) < 0) {
105 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
106 unlink(libsymlink);
107 unlink(pkgdir);
108 return -1;
109 }
110
111 return 0;
112}
113
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700114int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700115{
116 char pkgdir[PKG_PATH_MAX];
117
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700118 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700119 return -1;
120
Dave Allisond9370732014-01-30 14:19:23 -0800121 remove_profile_file(pkgname);
122
Mike Lockwood94afecf2012-10-24 10:45:23 -0700123 /* delete contents AND directory, no exceptions */
124 return delete_dir_contents(pkgdir, 1, NULL);
125}
126
127int renamepkg(const char *oldpkgname, const char *newpkgname)
128{
129 char oldpkgdir[PKG_PATH_MAX];
130 char newpkgdir[PKG_PATH_MAX];
131
132 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
133 return -1;
134 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
135 return -1;
136
137 if (rename(oldpkgdir, newpkgdir) < 0) {
138 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
139 return -errno;
140 }
141 return 0;
142}
143
144int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
145{
146 char pkgdir[PKG_PATH_MAX];
147 struct stat s;
148 int rc = 0;
149
150 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
151 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
152 return -1;
153 }
154
155 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
156 ALOGE("cannot create package path\n");
157 return -1;
158 }
159
160 if (stat(pkgdir, &s) < 0) return -1;
161
162 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700163 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 -0700164 return -1;
165 }
166
167 if (chmod(pkgdir, 0751) < 0) {
168 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
169 unlink(pkgdir);
170 return -errno;
171 }
172 if (chown(pkgdir, uid, gid) < 0) {
173 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
174 unlink(pkgdir);
175 return -errno;
176 }
177
178 return 0;
179}
180
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700181int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700182{
183 char pkgdir[PKG_PATH_MAX];
184
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700185 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700186 return -1;
187
Jeff Sharkey3316fe42014-08-27 10:46:25 -0700188 return delete_dir_contents(pkgdir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700189}
190
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700191int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192{
193 char pkgdir[PKG_PATH_MAX];
194 char applibdir[PKG_PATH_MAX];
195 char libsymlink[PKG_PATH_MAX];
196 struct stat libStat;
197
198 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700199 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700200 return -1;
201 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700202 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203 ALOGE("cannot create package lib symlink origin path\n");
204 return -1;
205 }
206 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
207 ALOGE("cannot create package lib symlink dest path\n");
208 return -1;
209 }
210
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800211 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700212 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
213 return -errno;
214 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800215 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700216 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
217 unlink(pkgdir);
218 return -errno;
219 }
220
221 if (lstat(libsymlink, &libStat) < 0) {
222 if (errno != ENOENT) {
223 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
224 unlink(pkgdir);
225 return -1;
226 }
227 } else {
228 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100229 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700230 ALOGE("couldn't delete lib directory during install for non-primary: %s",
231 libsymlink);
232 unlink(pkgdir);
233 return -1;
234 }
235 } else if (S_ISLNK(libStat.st_mode)) {
236 if (unlink(libsymlink) < 0) {
237 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
238 libsymlink);
239 unlink(pkgdir);
240 return -1;
241 }
242 }
243 }
244
Stephen Smalley26288202014-02-07 09:16:46 -0500245 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700246 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
247 unlink(libsymlink);
248 unlink(pkgdir);
249 return -errno;
250 }
251
Stephen Smalley3a983892014-05-13 12:53:07 -0400252 if (symlink(applibdir, libsymlink) < 0) {
253 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
254 applibdir, strerror(errno));
255 unlink(pkgdir);
256 return -1;
257 }
258
Mike Lockwood94afecf2012-10-24 10:45:23 -0700259 if (chown(pkgdir, uid, uid) < 0) {
260 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
261 unlink(libsymlink);
262 unlink(pkgdir);
263 return -errno;
264 }
265
266 return 0;
267}
268
Robin Lee7c8bec02014-06-10 18:46:26 +0100269int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700270{
Robin Lee095c7632014-04-25 15:05:19 +0100271 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700272 return -1;
273 }
274
275 return 0;
276}
277
Robin Lee095c7632014-04-25 15:05:19 +0100278int delete_user(userid_t userid)
279{
280 int status = 0;
281
282 char data_path[PKG_PATH_MAX];
283 if ((create_user_path(data_path, userid) != 0)
284 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
285 status = -1;
286 }
287
288 char media_path[PATH_MAX];
289 if ((create_user_media_path(media_path, userid) != 0)
290 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
291 status = -1;
292 }
293
294 char config_path[PATH_MAX];
295 if ((create_user_config_path(config_path, userid) != 0)
296 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
297 status = -1;
298 }
299
300 return status;
301}
302
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700303int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700304{
305 char cachedir[PKG_PATH_MAX];
306
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700307 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700308 return -1;
309
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700310 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100311 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700312}
313
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700314int delete_code_cache(const char *pkgname, userid_t userid)
315{
316 char codecachedir[PKG_PATH_MAX];
Jeff Sharkey770180a2014-09-08 17:14:26 -0700317 struct stat s;
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700318
319 if (create_pkg_path(codecachedir, pkgname, CODE_CACHE_DIR_POSTFIX, userid))
320 return -1;
321
Jeff Sharkey770180a2014-09-08 17:14:26 -0700322 /* it's okay if code cache is missing */
323 if (lstat(codecachedir, &s) == -1 && errno == ENOENT) {
324 return 0;
325 }
326
Jeff Sharkeyc796b682014-07-15 21:49:51 -0700327 /* delete contents, not the directory, no exceptions */
328 return delete_dir_contents(codecachedir, 0, NULL);
329}
330
Mike Lockwood94afecf2012-10-24 10:45:23 -0700331/* Try to ensure free_size bytes of storage are available.
332 * Returns 0 on success.
333 * This is rather simple-minded because doing a full LRU would
334 * be potentially memory-intensive, and without atime it would
335 * also require that apps constantly modify file metadata even
336 * when just reading from the cache, which is pretty awful.
337 */
338int free_cache(int64_t free_size)
339{
340 cache_t* cache;
341 int64_t avail;
342 DIR *d;
343 struct dirent *de;
344 char tmpdir[PATH_MAX];
345 char *dirpos;
346
347 avail = data_disk_free();
348 if (avail < 0) return -1;
349
350 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
351 if (avail >= free_size) return 0;
352
353 cache = start_cache_collection();
354
355 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700356 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700357 //ALOGI("adding cache files from %s\n", tmpdir);
358 add_cache_files(cache, tmpdir, "cache");
359 }
360
361 // Search for other users and add any cache files from them.
362 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
363 SECONDARY_USER_PREFIX);
364 dirpos = tmpdir + strlen(tmpdir);
365 d = opendir(tmpdir);
366 if (d != NULL) {
367 while ((de = readdir(d))) {
368 if (de->d_type == DT_DIR) {
369 const char *name = de->d_name;
370 /* always skip "." and ".." */
371 if (name[0] == '.') {
372 if (name[1] == 0) continue;
373 if ((name[1] == '.') && (name[2] == 0)) continue;
374 }
375 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
376 strcpy(dirpos, name);
377 //ALOGI("adding cache files from %s\n", tmpdir);
378 add_cache_files(cache, tmpdir, "cache");
379 } else {
380 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
381 }
382 }
383 }
384 closedir(d);
385 }
386
387 // Collect cache files on external storage for all users (if it is mounted as part
388 // of the internal storage).
389 strcpy(tmpdir, android_media_dir.path);
390 dirpos = tmpdir + strlen(tmpdir);
391 d = opendir(tmpdir);
392 if (d != NULL) {
393 while ((de = readdir(d))) {
394 if (de->d_type == DT_DIR) {
395 const char *name = de->d_name;
396 /* skip any dir that doesn't start with a number, so not a user */
397 if (name[0] < '0' || name[0] > '9') {
398 continue;
399 }
400 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
401 strcpy(dirpos, name);
402 if (lookup_media_dir(tmpdir, "Android") == 0
403 && lookup_media_dir(tmpdir, "data") == 0) {
404 //ALOGI("adding cache files from %s\n", tmpdir);
405 add_cache_files(cache, tmpdir, "cache");
406 }
407 } else {
408 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
409 }
410 }
411 }
412 closedir(d);
413 }
414
415 clear_cache_files(cache, free_size);
416 finish_cache_collection(cache);
417
418 return data_disk_free() >= free_size ? 0 : -1;
419}
420
Narayan Kamath1b400322014-04-11 13:17:00 +0100421int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700422{
423 char src_dex[PKG_PATH_MAX];
424 char dst_dex[PKG_PATH_MAX];
425
Jeff Sharkey770180a2014-09-08 17:14:26 -0700426 if (validate_apk_path(src)) {
427 ALOGE("invalid apk path '%s' (bad prefix)\n", src);
428 return -1;
429 }
430 if (validate_apk_path(dst)) {
431 ALOGE("invalid apk path '%s' (bad prefix)\n", dst);
432 return -1;
433 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700434
Narayan Kamath1b400322014-04-11 13:17:00 +0100435 if (create_cache_path(src_dex, src, instruction_set)) return -1;
436 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700437
438 ALOGV("move %s -> %s\n", src_dex, dst_dex);
439 if (rename(src_dex, dst_dex) < 0) {
440 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
441 return -1;
442 } else {
443 return 0;
444 }
445}
446
Narayan Kamath1b400322014-04-11 13:17:00 +0100447int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700448{
449 char dex_path[PKG_PATH_MAX];
450
Jeff Sharkey770180a2014-09-08 17:14:26 -0700451 if (validate_apk_path(path) && validate_system_app_path(path)) {
452 ALOGE("invalid apk path '%s' (bad prefix)\n", path);
453 return -1;
454 }
455
Narayan Kamath1b400322014-04-11 13:17:00 +0100456 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700457
458 ALOGV("unlink %s\n", dex_path);
459 if (unlink(dex_path) < 0) {
Jeff Sharkey770180a2014-09-08 17:14:26 -0700460 if (errno != ENOENT) {
461 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
462 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700463 return -1;
464 } else {
465 return 0;
466 }
467}
468
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700469int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700470 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100471 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
472 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700473{
474 DIR *d;
475 int dfd;
476 struct dirent *de;
477 struct stat s;
478 char path[PKG_PATH_MAX];
479
480 int64_t codesize = 0;
481 int64_t datasize = 0;
482 int64_t cachesize = 0;
483 int64_t asecsize = 0;
484
485 /* count the source apk as code -- but only if it's not
486 * on the /system partition and its not on the sdcard.
487 */
488 if (validate_system_app_path(apkpath) &&
489 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
490 if (stat(apkpath, &s) == 0) {
491 codesize += stat_size(&s);
492 }
493 }
494 /* count the forward locked apk as code if it is given
495 */
496 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
497 if (stat(fwdlock_apkpath, &s) == 0) {
498 codesize += stat_size(&s);
499 }
500 }
501 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100502 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700503 if (stat(path, &s) == 0) {
504 codesize += stat_size(&s);
505 }
506 }
507
508 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700509 if (libdirpath != NULL && libdirpath[0] != '!') {
510 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700511 if (d != NULL) {
512 dfd = dirfd(d);
513 codesize += calculate_dir_size(dfd);
514 closedir(d);
515 }
516 }
517
518 /* compute asec size if it is given
519 */
520 if (asecpath != NULL && asecpath[0] != '!') {
521 if (stat(asecpath, &s) == 0) {
522 asecsize += stat_size(&s);
523 }
524 }
525
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700526 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700527 goto done;
528 }
529
530 d = opendir(path);
531 if (d == NULL) {
532 goto done;
533 }
534 dfd = dirfd(d);
535
536 /* most stuff in the pkgdir is data, except for the "cache"
537 * directory and below, which is cache, and the "lib" directory
538 * and below, which is code...
539 */
540 while ((de = readdir(d))) {
541 const char *name = de->d_name;
542
543 if (de->d_type == DT_DIR) {
544 int subfd;
545 int64_t statsize = 0;
546 int64_t dirsize = 0;
547 /* always skip "." and ".." */
548 if (name[0] == '.') {
549 if (name[1] == 0) continue;
550 if ((name[1] == '.') && (name[2] == 0)) continue;
551 }
552 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
553 statsize = stat_size(&s);
554 }
555 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
556 if (subfd >= 0) {
557 dirsize = calculate_dir_size(subfd);
558 }
559 if(!strcmp(name,"lib")) {
560 codesize += dirsize + statsize;
561 } else if(!strcmp(name,"cache")) {
562 cachesize += dirsize + statsize;
563 } else {
564 datasize += dirsize + statsize;
565 }
566 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
567 // This is the symbolic link to the application's library
568 // code. We'll count this as code instead of data, since
569 // it is not something that the app creates.
570 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
571 codesize += stat_size(&s);
572 }
573 } else {
574 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
575 datasize += stat_size(&s);
576 }
577 }
578 }
579 closedir(d);
580done:
581 *_codesize = codesize;
582 *_datasize = datasize;
583 *_cachesize = cachesize;
584 *_asecsize = asecsize;
585 return 0;
586}
587
Narayan Kamath1b400322014-04-11 13:17:00 +0100588int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700589{
590 char *tmp;
591 int srclen;
592 int dstlen;
593
594 srclen = strlen(src);
595
596 /* demand that we are an absolute path */
597 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
598 return -1;
599 }
600
601 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
602 return -1;
603 }
604
Dave Allisond9370732014-01-30 14:19:23 -0800605 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100606 strlen(instruction_set) +
607 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800608
Mike Lockwood94afecf2012-10-24 10:45:23 -0700609 if (dstlen > PKG_PATH_MAX) {
610 return -1;
611 }
612
Narayan Kamath1b400322014-04-11 13:17:00 +0100613 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700614 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100615 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700616 src + 1, /* skip the leading / */
617 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800618
Narayan Kamath1b400322014-04-11 13:17:00 +0100619 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700620 if (*tmp == '/') {
621 *tmp = '@';
622 }
623 }
624
625 return 0;
626}
627
628static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800629 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700630{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800631 /* platform-specific flags affecting optimization and verification */
632 char dexopt_flags[PROPERTY_VALUE_MAX];
633 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
634 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
635
Mike Lockwood94afecf2012-10-24 10:45:23 -0700636 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
637 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
638 char zip_num[MAX_INT_LEN];
639 char odex_num[MAX_INT_LEN];
640
641 sprintf(zip_num, "%d", zip_fd);
642 sprintf(odex_num, "%d", odex_fd);
643
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700644 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700645 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
646 dexopt_flags, (char*) NULL);
647 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
648}
649
Alex Light43c5d302014-07-21 12:23:48 -0700650static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
651 const char* output_file_name, const char *pkgname, const char *instruction_set)
652{
653 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravle8fc73152014-08-19 18:48:50 +0100654 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light43c5d302014-07-21 12:23:48 -0700655
656 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
657 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
658 ALOGE("Instruction set %s longer than max length of %d",
659 instruction_set, MAX_INSTRUCTION_SET_LEN);
660 return;
661 }
662
663 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
664 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
665 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
666 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
667 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
668 // The caller has already gotten all the locks we need.
669 const char* no_lock_arg = "--no-lock-output";
670 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
671 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
672 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
673 ALOGE("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
674 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
675
676 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
677 char* argv[7];
678 argv[0] = (char*) PATCHOAT_BIN;
679 argv[1] = (char*) patched_image_location_arg;
680 argv[2] = (char*) no_lock_arg;
681 argv[3] = instruction_set_arg;
682 argv[4] = output_oat_fd_arg;
683 argv[5] = input_oat_fd_arg;
684 argv[6] = NULL;
685
686 execv(PATCHOAT_BIN, (char* const *)argv);
687 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
688}
689
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700690static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Calin Juravleb1efac12014-08-21 19:05:20 +0100691 const char* output_file_name, const char *pkgname, const char *instruction_set,
692 bool vm_safe_mode)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700693{
Calin Juravle8fc73152014-08-19 18:48:50 +0100694 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
695
696 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
697 ALOGE("Instruction set %s longer than max length of %d",
698 instruction_set, MAX_INSTRUCTION_SET_LEN);
699 return;
700 }
701
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700702 char prop_buf[PROPERTY_VALUE_MAX];
703 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
704
705 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
706 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
707
708 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
709 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
710
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700711 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
712 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
713 dex2oat_compiler_filter_flag, NULL) > 0;
714
Calin Juravle8fc73152014-08-19 18:48:50 +0100715 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
716 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
717 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
718 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
719 dex2oat_isa_features, NULL) > 0;
720
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800721 char dex2oat_flags[PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100722 bool have_dex2oat_flags = property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, NULL) > 0;
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800723 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
724
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700725 // If we booting without the real /data, don't spend time compiling.
726 char vold_decrypt[PROPERTY_VALUE_MAX];
727 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
728 bool skip_compilation = (have_vold_decrypt &&
729 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
730 (strcmp(vold_decrypt, "1") == 0)));
731
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700732 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700733
Brian Carlstrom53e07762014-06-27 14:15:19 -0700734 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700735
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700736 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100737
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700738 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
739 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
740 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700741 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100742 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravle8fc73152014-08-19 18:48:50 +0100743 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100744 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
745 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700746 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
747 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700748 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700749
750 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
751 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
752 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
753 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100754 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle8fc73152014-08-19 18:48:50 +0100755 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Calin Juravle57c69c32014-06-06 14:42:16 +0100756
Calin Juravle4fdff462014-06-06 16:58:43 +0100757 bool have_profile_file = false;
758 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100759 if (profiler && (strcmp(pkgname, "*") != 0)) {
760 char profile_file[PKG_PATH_MAX];
761 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800762 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100763 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100764 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100765 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100766 have_profile_file = true;
767 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
768 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
769 "--top-k-profile-threshold=%s", prop_buf);
770 have_top_k_profile_threshold = true;
771 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100772 }
Dave Allisond9370732014-01-30 14:19:23 -0800773 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700774
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700775 if (have_dex2oat_Xms_flag) {
776 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
777 }
778 if (have_dex2oat_Xmx_flag) {
779 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
780 }
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700781 if (skip_compilation) {
Brian Carlstrome18987e2014-08-15 09:55:50 -0700782 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700783 have_dex2oat_compiler_filter_flag = true;
Calin Juravleb1efac12014-08-21 19:05:20 +0100784 } else if (vm_safe_mode) {
785 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
Calin Juravle97477d22014-08-27 16:10:03 +0100786 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom41cd9eb2014-07-30 14:37:11 -0700787 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700788 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
789 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700790
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700791 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100792
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700793 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravle8fc73152014-08-19 18:48:50 +0100794 + (have_dex2oat_isa_features ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100795 + (have_profile_file ? 1 : 0)
796 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700797 + (have_dex2oat_Xms_flag ? 2 : 0)
798 + (have_dex2oat_Xmx_flag ? 2 : 0)
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700799 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100800 + (have_dex2oat_flags ? 1 : 0)];
801 int i = 0;
802 argv[i++] = (char*)DEX2OAT_BIN;
803 argv[i++] = zip_fd_arg;
804 argv[i++] = zip_location_arg;
805 argv[i++] = oat_fd_arg;
806 argv[i++] = oat_location_arg;
807 argv[i++] = instruction_set_arg;
Calin Juravle8fc73152014-08-19 18:48:50 +0100808 if (have_dex2oat_isa_features) {
809 argv[i++] = instruction_set_features_arg;
810 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100811 if (have_profile_file) {
812 argv[i++] = profile_file_arg;
813 }
814 if (have_top_k_profile_threshold) {
815 argv[i++] = top_k_profile_threshold_arg;
816 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700817 if (have_dex2oat_Xms_flag) {
818 argv[i++] = (char*)RUNTIME_ARG;
819 argv[i++] = dex2oat_Xms_arg;
820 }
821 if (have_dex2oat_Xmx_flag) {
822 argv[i++] = (char*)RUNTIME_ARG;
823 argv[i++] = dex2oat_Xmx_arg;
824 }
Brian Carlstrom9a87db62014-07-28 19:13:28 -0700825 if (have_dex2oat_compiler_filter_flag) {
826 argv[i++] = dex2oat_compiler_filter_arg;
827 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100828 if (have_dex2oat_flags) {
829 argv[i++] = dex2oat_flags;
830 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700831 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100832 argv[i] = NULL;
833
834 execv(DEX2OAT_BIN, (char* const *)argv);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700835 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
836}
837
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100838static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700839{
840 int status;
841 pid_t got_pid;
842
Mike Lockwood94afecf2012-10-24 10:45:23 -0700843 while (1) {
844 got_pid = waitpid(pid, &status, 0);
845 if (got_pid == -1 && errno == EINTR) {
846 printf("waitpid interrupted, retrying\n");
847 } else {
848 break;
849 }
850 }
851 if (got_pid != pid) {
852 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
853 (int) pid, (int) got_pid, strerror(errno));
854 return 1;
855 }
856
857 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700858 return 0;
859 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700860 return status; /* always nonzero */
861 }
862}
863
Calin Juravleb1efac12014-08-21 19:05:20 +0100864int dexopt(const char *apk_path, uid_t uid, bool is_public,
Alex Light43c5d302014-07-21 12:23:48 -0700865 const char *pkgname, const char *instruction_set,
Calin Juravleb1efac12014-08-21 19:05:20 +0100866 bool vm_safe_mode, bool is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700867{
868 struct utimbuf ut;
Alex Light43c5d302014-07-21 12:23:48 -0700869 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700870 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700871 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700872 char *end;
Alex Light43c5d302014-07-21 12:23:48 -0700873 const char *input_file;
874 char in_odex_path[PKG_PATH_MAX];
875 int res, input_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700876
Mike Lockwood94afecf2012-10-24 10:45:23 -0700877 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
878 return -1;
879 }
880
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800881 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700882 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700883
Alex Light43c5d302014-07-21 12:23:48 -0700884 if (is_patchoat && strncmp(persist_sys_dalvik_vm_lib, "libart", 6) != 0) {
885 /* We may only patch if we are libart */
886 ALOGE("Patching is only supported in libart\n");
887 return -1;
888 }
889
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700890 /* Before anything else: is there a .odex file? If so, we have
891 * precompiled the apk and there is nothing to do here.
Alex Light43c5d302014-07-21 12:23:48 -0700892 *
893 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700894 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800895 strcpy(out_path, apk_path);
896 end = strrchr(out_path, '.');
Alex Light43c5d302014-07-21 12:23:48 -0700897 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800898 strcpy(end, ".odex");
899 if (stat(out_path, &dex_stat) == 0) {
900 return 0;
901 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700902 }
903
Narayan Kamath1b400322014-04-11 13:17:00 +0100904 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700905 return -1;
906 }
907
Alex Light43c5d302014-07-21 12:23:48 -0700908 if (is_patchoat) {
909 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
910 strcpy(in_odex_path, apk_path);
911 end = strrchr(in_odex_path, '/');
912 if (end == NULL) {
913 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
914 return -1;
915 }
916 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
917 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
918 strcat(in_odex_path, apk_end);
919 end = strrchr(in_odex_path, '.');
920 if (end == NULL) {
921 return -1;
922 }
923 strcpy(end + 1, "odex");
924 input_file = in_odex_path;
925 } else {
926 input_file = apk_path;
927 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700928
Alex Light43c5d302014-07-21 12:23:48 -0700929 memset(&input_stat, 0, sizeof(input_stat));
930 stat(input_file, &input_stat);
931
932 input_fd = open(input_file, O_RDONLY, 0);
933 if (input_fd < 0) {
934 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700935 return -1;
936 }
937
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700938 unlink(out_path);
939 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
940 if (out_fd < 0) {
941 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700942 goto fail;
943 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700944 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700945 S_IRUSR|S_IWUSR|S_IRGRP |
946 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700947 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700948 goto fail;
949 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700950 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
951 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700952 goto fail;
953 }
954
Dave Allisond9370732014-01-30 14:19:23 -0800955 // Create profile file if there is a package name present.
956 if (strcmp(pkgname, "*") != 0) {
957 create_profile_file(pkgname, uid);
958 }
959
960
Alex Light43c5d302014-07-21 12:23:48 -0700961 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700962
963 pid_t pid;
964 pid = fork();
965 if (pid == 0) {
966 /* child -- drop privileges before continuing */
967 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700968 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700969 exit(64);
970 }
971 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700972 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700973 exit(65);
974 }
975 // drop capabilities
976 struct __user_cap_header_struct capheader;
977 struct __user_cap_data_struct capdata[2];
978 memset(&capheader, 0, sizeof(capheader));
979 memset(&capdata, 0, sizeof(capdata));
980 capheader.version = _LINUX_CAPABILITY_VERSION_3;
981 if (capset(&capheader, &capdata[0]) < 0) {
982 ALOGE("capset failed: %s\n", strerror(errno));
983 exit(66);
984 }
Brian Carlstrom3b14e5b2014-08-08 00:52:22 -0700985 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
986 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
987 exit(70);
988 }
Igor Murashkin9e87a802014-11-05 15:21:12 -0800989 if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) {
990 ALOGE("setpriority failed: %s\n", strerror(errno));
991 exit(71);
992 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700993 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
994 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700995 exit(67);
996 }
997
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700998 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Alex Light43c5d302014-07-21 12:23:48 -0700999 run_dexopt(input_fd, out_fd, input_file, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -07001000 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Alex Light43c5d302014-07-21 12:23:48 -07001001 if (is_patchoat) {
1002 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
1003 } else {
Calin Juravleb1efac12014-08-21 19:05:20 +01001004 run_dex2oat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set,
1005 vm_safe_mode);
Alex Light43c5d302014-07-21 12:23:48 -07001006 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001007 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -07001008 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001009 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001010 exit(68); /* only get here on exec failure */
1011 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001012 res = wait_child(pid);
1013 if (res == 0) {
Alex Light43c5d302014-07-21 12:23:48 -07001014 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001015 } else {
Alex Light43c5d302014-07-21 12:23:48 -07001016 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001017 goto fail;
1018 }
1019 }
1020
Alex Light43c5d302014-07-21 12:23:48 -07001021 ut.actime = input_stat.st_atime;
1022 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001023 utime(out_path, &ut);
1024
1025 close(out_fd);
Alex Light43c5d302014-07-21 12:23:48 -07001026 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001027 return 0;
1028
1029fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001030 if (out_fd >= 0) {
1031 close(out_fd);
1032 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001033 }
Alex Light43c5d302014-07-21 12:23:48 -07001034 if (input_fd >= 0) {
1035 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001036 }
1037 return -1;
1038}
1039
1040void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1041 struct stat* statbuf)
1042{
1043 while (path[basepos] != 0) {
1044 if (path[basepos] == '/') {
1045 path[basepos] = 0;
1046 if (lstat(path, statbuf) < 0) {
1047 ALOGV("Making directory: %s\n", path);
1048 if (mkdir(path, mode) == 0) {
1049 chown(path, uid, gid);
1050 } else {
1051 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1052 }
1053 }
1054 path[basepos] = '/';
1055 basepos++;
1056 }
1057 basepos++;
1058 }
1059}
1060
1061int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1062 int dstuid, int dstgid, struct stat* statbuf)
1063{
1064 DIR *d;
1065 struct dirent *de;
1066 int res;
1067
1068 int srcend = strlen(srcpath);
1069 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001070
Mike Lockwood94afecf2012-10-24 10:45:23 -07001071 if (lstat(srcpath, statbuf) < 0) {
1072 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1073 return 1;
1074 }
Dave Allisond9370732014-01-30 14:19:23 -08001075
Mike Lockwood94afecf2012-10-24 10:45:23 -07001076 if ((statbuf->st_mode&S_IFDIR) == 0) {
1077 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1078 dstuid, dstgid, statbuf);
1079 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1080 if (rename(srcpath, dstpath) >= 0) {
1081 if (chown(dstpath, dstuid, dstgid) < 0) {
1082 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1083 unlink(dstpath);
1084 return 1;
1085 }
1086 } else {
1087 ALOGW("Unable to rename %s to %s: %s\n",
1088 srcpath, dstpath, strerror(errno));
1089 return 1;
1090 }
1091 return 0;
1092 }
1093
1094 d = opendir(srcpath);
1095 if (d == NULL) {
1096 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1097 return 1;
1098 }
1099
1100 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001101
Mike Lockwood94afecf2012-10-24 10:45:23 -07001102 while ((de = readdir(d))) {
1103 const char *name = de->d_name;
1104 /* always skip "." and ".." */
1105 if (name[0] == '.') {
1106 if (name[1] == 0) continue;
1107 if ((name[1] == '.') && (name[2] == 0)) continue;
1108 }
Dave Allisond9370732014-01-30 14:19:23 -08001109
Mike Lockwood94afecf2012-10-24 10:45:23 -07001110 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1111 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1112 continue;
1113 }
Dave Allisond9370732014-01-30 14:19:23 -08001114
Mike Lockwood94afecf2012-10-24 10:45:23 -07001115 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1116 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1117 continue;
1118 }
Dave Allisond9370732014-01-30 14:19:23 -08001119
Mike Lockwood94afecf2012-10-24 10:45:23 -07001120 srcpath[srcend] = dstpath[dstend] = '/';
1121 strcpy(srcpath+srcend+1, name);
1122 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001123
Mike Lockwood94afecf2012-10-24 10:45:23 -07001124 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1125 res = 1;
1126 }
Dave Allisond9370732014-01-30 14:19:23 -08001127
Mike Lockwood94afecf2012-10-24 10:45:23 -07001128 // Note: we will be leaving empty directories behind in srcpath,
1129 // but that is okay, the package manager will be erasing all of the
1130 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001131
Mike Lockwood94afecf2012-10-24 10:45:23 -07001132 srcpath[srcend] = dstpath[dstend] = 0;
1133 }
Dave Allisond9370732014-01-30 14:19:23 -08001134
Mike Lockwood94afecf2012-10-24 10:45:23 -07001135 closedir(d);
1136 return res;
1137}
1138
1139int movefiles()
1140{
1141 DIR *d;
1142 int dfd, subfd;
1143 struct dirent *de;
1144 struct stat s;
1145 char buf[PKG_PATH_MAX+1];
1146 int bufp, bufe, bufi, readlen;
1147
1148 char srcpkg[PKG_NAME_MAX];
1149 char dstpkg[PKG_NAME_MAX];
1150 char srcpath[PKG_PATH_MAX];
1151 char dstpath[PKG_PATH_MAX];
1152 int dstuid=-1, dstgid=-1;
1153 int hasspace;
1154
1155 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1156 if (d == NULL) {
1157 goto done;
1158 }
1159 dfd = dirfd(d);
1160
1161 /* Iterate through all files in the directory, executing the
1162 * file movements requested there-in.
1163 */
1164 while ((de = readdir(d))) {
1165 const char *name = de->d_name;
1166
1167 if (de->d_type == DT_DIR) {
1168 continue;
1169 } else {
1170 subfd = openat(dfd, name, O_RDONLY);
1171 if (subfd < 0) {
1172 ALOGW("Unable to open update commands at %s%s\n",
1173 UPDATE_COMMANDS_DIR_PREFIX, name);
1174 continue;
1175 }
Dave Allisond9370732014-01-30 14:19:23 -08001176
Mike Lockwood94afecf2012-10-24 10:45:23 -07001177 bufp = 0;
1178 bufe = 0;
1179 buf[PKG_PATH_MAX] = 0;
1180 srcpkg[0] = dstpkg[0] = 0;
1181 while (1) {
1182 bufi = bufp;
1183 while (bufi < bufe && buf[bufi] != '\n') {
1184 bufi++;
1185 }
1186 if (bufi < bufe) {
1187 buf[bufi] = 0;
1188 ALOGV("Processing line: %s\n", buf+bufp);
1189 hasspace = 0;
1190 while (bufp < bufi && isspace(buf[bufp])) {
1191 hasspace = 1;
1192 bufp++;
1193 }
1194 if (buf[bufp] == '#' || bufp == bufi) {
1195 // skip comments and empty lines.
1196 } else if (hasspace) {
1197 if (dstpkg[0] == 0) {
1198 ALOGW("Path before package line in %s%s: %s\n",
1199 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1200 } else if (srcpkg[0] == 0) {
1201 // Skip -- source package no longer exists.
1202 } else {
1203 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1204 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1205 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1206 movefileordir(srcpath, dstpath,
1207 strlen(dstpath)-strlen(buf+bufp),
1208 dstuid, dstgid, &s);
1209 }
1210 }
1211 } else {
1212 char* div = strchr(buf+bufp, ':');
1213 if (div == NULL) {
1214 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1215 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1216 } else {
1217 *div = 0;
1218 div++;
1219 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1220 strcpy(dstpkg, buf+bufp);
1221 } else {
1222 srcpkg[0] = dstpkg[0] = 0;
1223 ALOGW("Package name too long in %s%s: %s\n",
1224 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1225 }
1226 if (strlen(div) < PKG_NAME_MAX) {
1227 strcpy(srcpkg, div);
1228 } else {
1229 srcpkg[0] = dstpkg[0] = 0;
1230 ALOGW("Package name too long in %s%s: %s\n",
1231 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1232 }
1233 if (srcpkg[0] != 0) {
1234 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1235 if (lstat(srcpath, &s) < 0) {
1236 // Package no longer exists -- skip.
1237 srcpkg[0] = 0;
1238 }
1239 } else {
1240 srcpkg[0] = 0;
1241 ALOGW("Can't create path %s in %s%s\n",
1242 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1243 }
1244 if (srcpkg[0] != 0) {
1245 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1246 if (lstat(dstpath, &s) == 0) {
1247 dstuid = s.st_uid;
1248 dstgid = s.st_gid;
1249 } else {
1250 // Destination package doesn't
1251 // exist... due to original-package,
1252 // this is normal, so don't be
1253 // noisy about it.
1254 srcpkg[0] = 0;
1255 }
1256 } else {
1257 srcpkg[0] = 0;
1258 ALOGW("Can't create path %s in %s%s\n",
1259 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1260 }
1261 }
1262 ALOGV("Transfering from %s to %s: uid=%d\n",
1263 srcpkg, dstpkg, dstuid);
1264 }
1265 }
1266 }
1267 bufp = bufi+1;
1268 } else {
1269 if (bufp == 0) {
1270 if (bufp < bufe) {
1271 ALOGW("Line too long in %s%s, skipping: %s\n",
1272 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1273 }
1274 } else if (bufp < bufe) {
1275 memcpy(buf, buf+bufp, bufe-bufp);
1276 bufe -= bufp;
1277 bufp = 0;
1278 }
1279 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1280 if (readlen < 0) {
1281 ALOGW("Failure reading update commands in %s%s: %s\n",
1282 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1283 break;
1284 } else if (readlen == 0) {
1285 break;
1286 }
1287 bufe += readlen;
1288 buf[bufe] = 0;
1289 ALOGV("Read buf: %s\n", buf);
1290 }
1291 }
1292 close(subfd);
1293 }
1294 }
1295 closedir(d);
1296done:
1297 return 0;
1298}
1299
1300int linklib(const char* pkgname, const char* asecLibDir, int userId)
1301{
1302 char pkgdir[PKG_PATH_MAX];
1303 char libsymlink[PKG_PATH_MAX];
1304 struct stat s, libStat;
1305 int rc = 0;
1306
1307 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1308 ALOGE("cannot create package path\n");
1309 return -1;
1310 }
1311 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1312 ALOGE("cannot create package lib symlink origin path\n");
1313 return -1;
1314 }
1315
1316 if (stat(pkgdir, &s) < 0) return -1;
1317
1318 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1319 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1320 return -1;
1321 }
1322
1323 if (chmod(pkgdir, 0700) < 0) {
1324 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1325 rc = -1;
1326 goto out;
1327 }
1328
1329 if (lstat(libsymlink, &libStat) < 0) {
1330 if (errno != ENOENT) {
1331 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1332 rc = -1;
1333 goto out;
1334 }
1335 } else {
1336 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001337 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001338 rc = -1;
1339 goto out;
1340 }
1341 } else if (S_ISLNK(libStat.st_mode)) {
1342 if (unlink(libsymlink) < 0) {
1343 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1344 rc = -1;
1345 goto out;
1346 }
1347 }
1348 }
1349
1350 if (symlink(asecLibDir, libsymlink) < 0) {
1351 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1352 strerror(errno));
1353 rc = -errno;
1354 goto out;
1355 }
1356
1357out:
1358 if (chmod(pkgdir, s.st_mode) < 0) {
1359 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1360 rc = -errno;
1361 }
1362
1363 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1364 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1365 return -errno;
1366 }
1367
1368 return rc;
1369}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001370
1371static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1372{
1373 static const char *IDMAP_BIN = "/system/bin/idmap";
1374 static const size_t MAX_INT_LEN = 32;
1375 char idmap_str[MAX_INT_LEN];
1376
1377 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1378
1379 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1380 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1381}
1382
1383// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1384// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1385static int flatten_path(const char *prefix, const char *suffix,
1386 const char *overlay_path, char *idmap_path, size_t N)
1387{
1388 if (overlay_path == NULL || idmap_path == NULL) {
1389 return -1;
1390 }
1391 const size_t len_overlay_path = strlen(overlay_path);
1392 // will access overlay_path + 1 further below; requires absolute path
1393 if (len_overlay_path < 2 || *overlay_path != '/') {
1394 return -1;
1395 }
1396 const size_t len_idmap_root = strlen(prefix);
1397 const size_t len_suffix = strlen(suffix);
1398 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1399 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1400 // additions below would cause overflow
1401 return -1;
1402 }
1403 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1404 return -1;
1405 }
1406 memset(idmap_path, 0, N);
1407 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1408 char *ch = idmap_path + len_idmap_root;
1409 while (*ch != '\0') {
1410 if (*ch == '/') {
1411 *ch = '@';
1412 }
1413 ++ch;
1414 }
1415 return 0;
1416}
1417
1418int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1419{
1420 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1421
1422 int idmap_fd = -1;
1423 char idmap_path[PATH_MAX];
1424
1425 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1426 idmap_path, sizeof(idmap_path)) == -1) {
1427 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1428 goto fail;
1429 }
1430
1431 unlink(idmap_path);
1432 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1433 if (idmap_fd < 0) {
1434 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1435 goto fail;
1436 }
1437 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1438 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1439 goto fail;
1440 }
1441 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1442 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1443 goto fail;
1444 }
1445
1446 pid_t pid;
1447 pid = fork();
1448 if (pid == 0) {
1449 /* child -- drop privileges before continuing */
1450 if (setgid(uid) != 0) {
1451 ALOGE("setgid(%d) failed during idmap\n", uid);
1452 exit(1);
1453 }
1454 if (setuid(uid) != 0) {
1455 ALOGE("setuid(%d) failed during idmap\n", uid);
1456 exit(1);
1457 }
1458 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1459 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1460 exit(1);
1461 }
1462
1463 run_idmap(target_apk, overlay_apk, idmap_fd);
1464 exit(1); /* only if exec call to idmap failed */
1465 } else {
1466 int status = wait_child(pid);
1467 if (status != 0) {
1468 ALOGE("idmap failed, status=0x%04x\n", status);
1469 goto fail;
1470 }
1471 }
1472
1473 close(idmap_fd);
1474 return 0;
1475fail:
1476 if (idmap_fd >= 0) {
1477 close(idmap_fd);
1478 unlink(idmap_path);
1479 }
1480 return -1;
1481}
Robert Craige9887e42014-02-20 10:25:56 -05001482
Robert Craigda30dc72014-03-27 10:21:12 -04001483int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001484{
Robert Craigda30dc72014-03-27 10:21:12 -04001485 struct dirent *entry;
1486 DIR *d;
1487 struct stat s;
1488 char *userdir;
1489 char *primarydir;
1490 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001491 int ret = 0;
1492
Robert Craigda30dc72014-03-27 10:21:12 -04001493 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1494 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1495
1496 if (!pkgName || !seinfo) {
1497 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001498 return -1;
1499 }
1500
Robert Craigda30dc72014-03-27 10:21:12 -04001501 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1502 return -1;
1503 }
1504
1505 // Relabel for primary user.
1506 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1507 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001508 ret |= -1;
1509 }
1510
Robert Craigda30dc72014-03-27 10:21:12 -04001511 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1512 free(primarydir);
1513 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001514 }
1515
Robert Craigda30dc72014-03-27 10:21:12 -04001516 // Relabel package directory for all secondary users.
1517 d = opendir(userdir);
1518 if (d == NULL) {
1519 free(primarydir);
1520 free(userdir);
1521 return -1;
1522 }
1523
1524 while ((entry = readdir(d))) {
1525 if (entry->d_type != DT_DIR) {
1526 continue;
1527 }
1528
1529 const char *user = entry->d_name;
1530 // Ignore "." and ".."
1531 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1532 continue;
1533 }
1534
1535 // user directories start with a number
1536 if (user[0] < '0' || user[0] > '9') {
1537 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1538 continue;
1539 }
1540
1541 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1542 continue;
1543 }
1544
1545 if (stat(pkgdir, &s) < 0) {
1546 free(pkgdir);
1547 continue;
1548 }
1549
1550 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1551 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1552 ret |= -1;
1553 }
1554 free(pkgdir);
1555 }
1556
1557 closedir(d);
1558 free(primarydir);
1559 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001560 return ret;
1561}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001562