blob: 570a6c1c8c1d8226c65b8bdc6799d31b92b1c7d2 [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
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100179static int lib_dir_matcher(const char* file_name, const int is_dir) {
180 return is_dir && !strcmp(file_name, "lib");
181}
182
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700183int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184{
185 char pkgdir[PKG_PATH_MAX];
186
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700187 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188 return -1;
189
190 /* delete contents, excluding "lib", but not the directory itself */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100191 return delete_dir_contents(pkgdir, 0, &lib_dir_matcher);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192}
193
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700194int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195{
196 char pkgdir[PKG_PATH_MAX];
197 char applibdir[PKG_PATH_MAX];
198 char libsymlink[PKG_PATH_MAX];
199 struct stat libStat;
200
201 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700202 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203 return -1;
204 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700205 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206 ALOGE("cannot create package lib symlink origin path\n");
207 return -1;
208 }
209 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
210 ALOGE("cannot create package lib symlink dest path\n");
211 return -1;
212 }
213
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800214 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
216 return -errno;
217 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800218 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
220 unlink(pkgdir);
221 return -errno;
222 }
223
224 if (lstat(libsymlink, &libStat) < 0) {
225 if (errno != ENOENT) {
226 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
227 unlink(pkgdir);
228 return -1;
229 }
230 } else {
231 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100232 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 ALOGE("couldn't delete lib directory during install for non-primary: %s",
234 libsymlink);
235 unlink(pkgdir);
236 return -1;
237 }
238 } else if (S_ISLNK(libStat.st_mode)) {
239 if (unlink(libsymlink) < 0) {
240 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
241 libsymlink);
242 unlink(pkgdir);
243 return -1;
244 }
245 }
246 }
247
Stephen Smalley26288202014-02-07 09:16:46 -0500248 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
250 unlink(libsymlink);
251 unlink(pkgdir);
252 return -errno;
253 }
254
Stephen Smalley3a983892014-05-13 12:53:07 -0400255 if (symlink(applibdir, libsymlink) < 0) {
256 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
257 applibdir, strerror(errno));
258 unlink(pkgdir);
259 return -1;
260 }
261
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 if (chown(pkgdir, uid, uid) < 0) {
263 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
264 unlink(libsymlink);
265 unlink(pkgdir);
266 return -errno;
267 }
268
269 return 0;
270}
271
Robin Lee7c8bec02014-06-10 18:46:26 +0100272int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273{
Robin Lee095c7632014-04-25 15:05:19 +0100274 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275 return -1;
276 }
277
278 return 0;
279}
280
Robin Lee095c7632014-04-25 15:05:19 +0100281int delete_user(userid_t userid)
282{
283 int status = 0;
284
285 char data_path[PKG_PATH_MAX];
286 if ((create_user_path(data_path, userid) != 0)
287 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
288 status = -1;
289 }
290
291 char media_path[PATH_MAX];
292 if ((create_user_media_path(media_path, userid) != 0)
293 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
294 status = -1;
295 }
296
297 char config_path[PATH_MAX];
298 if ((create_user_config_path(config_path, userid) != 0)
299 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
300 status = -1;
301 }
302
303 return status;
304}
305
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700306int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307{
308 char cachedir[PKG_PATH_MAX];
309
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700310 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 return -1;
312
313 /* delete contents, not the directory, no exceptions */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100314 return delete_dir_contents(cachedir, 0, NULL);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700315}
316
317/* Try to ensure free_size bytes of storage are available.
318 * Returns 0 on success.
319 * This is rather simple-minded because doing a full LRU would
320 * be potentially memory-intensive, and without atime it would
321 * also require that apps constantly modify file metadata even
322 * when just reading from the cache, which is pretty awful.
323 */
324int free_cache(int64_t free_size)
325{
326 cache_t* cache;
327 int64_t avail;
328 DIR *d;
329 struct dirent *de;
330 char tmpdir[PATH_MAX];
331 char *dirpos;
332
333 avail = data_disk_free();
334 if (avail < 0) return -1;
335
336 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
337 if (avail >= free_size) return 0;
338
339 cache = start_cache_collection();
340
341 // Collect cache files for primary user.
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700342 if (create_user_path(tmpdir, 0) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700343 //ALOGI("adding cache files from %s\n", tmpdir);
344 add_cache_files(cache, tmpdir, "cache");
345 }
346
347 // Search for other users and add any cache files from them.
348 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
349 SECONDARY_USER_PREFIX);
350 dirpos = tmpdir + strlen(tmpdir);
351 d = opendir(tmpdir);
352 if (d != NULL) {
353 while ((de = readdir(d))) {
354 if (de->d_type == DT_DIR) {
355 const char *name = de->d_name;
356 /* always skip "." and ".." */
357 if (name[0] == '.') {
358 if (name[1] == 0) continue;
359 if ((name[1] == '.') && (name[2] == 0)) continue;
360 }
361 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
362 strcpy(dirpos, name);
363 //ALOGI("adding cache files from %s\n", tmpdir);
364 add_cache_files(cache, tmpdir, "cache");
365 } else {
366 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
367 }
368 }
369 }
370 closedir(d);
371 }
372
373 // Collect cache files on external storage for all users (if it is mounted as part
374 // of the internal storage).
375 strcpy(tmpdir, android_media_dir.path);
376 dirpos = tmpdir + strlen(tmpdir);
377 d = opendir(tmpdir);
378 if (d != NULL) {
379 while ((de = readdir(d))) {
380 if (de->d_type == DT_DIR) {
381 const char *name = de->d_name;
382 /* skip any dir that doesn't start with a number, so not a user */
383 if (name[0] < '0' || name[0] > '9') {
384 continue;
385 }
386 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
387 strcpy(dirpos, name);
388 if (lookup_media_dir(tmpdir, "Android") == 0
389 && lookup_media_dir(tmpdir, "data") == 0) {
390 //ALOGI("adding cache files from %s\n", tmpdir);
391 add_cache_files(cache, tmpdir, "cache");
392 }
393 } else {
394 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
395 }
396 }
397 }
398 closedir(d);
399 }
400
401 clear_cache_files(cache, free_size);
402 finish_cache_collection(cache);
403
404 return data_disk_free() >= free_size ? 0 : -1;
405}
406
Narayan Kamath1b400322014-04-11 13:17:00 +0100407int move_dex(const char *src, const char *dst, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700408{
409 char src_dex[PKG_PATH_MAX];
410 char dst_dex[PKG_PATH_MAX];
411
412 if (validate_apk_path(src)) return -1;
413 if (validate_apk_path(dst)) return -1;
414
Narayan Kamath1b400322014-04-11 13:17:00 +0100415 if (create_cache_path(src_dex, src, instruction_set)) return -1;
416 if (create_cache_path(dst_dex, dst, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700417
418 ALOGV("move %s -> %s\n", src_dex, dst_dex);
419 if (rename(src_dex, dst_dex) < 0) {
420 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
421 return -1;
422 } else {
423 return 0;
424 }
425}
426
Narayan Kamath1b400322014-04-11 13:17:00 +0100427int rm_dex(const char *path, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700428{
429 char dex_path[PKG_PATH_MAX];
430
431 if (validate_apk_path(path)) return -1;
Narayan Kamath1b400322014-04-11 13:17:00 +0100432 if (create_cache_path(dex_path, path, instruction_set)) return -1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700433
434 ALOGV("unlink %s\n", dex_path);
435 if (unlink(dex_path) < 0) {
436 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
437 return -1;
438 } else {
439 return 0;
440 }
441}
442
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700443int get_size(const char *pkgname, userid_t userid, const char *apkpath,
Dianne Hackborn8b417802013-05-01 18:55:10 -0700444 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
Narayan Kamath1b400322014-04-11 13:17:00 +0100445 const char *instruction_set, int64_t *_codesize, int64_t *_datasize,
446 int64_t *_cachesize, int64_t* _asecsize)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700447{
448 DIR *d;
449 int dfd;
450 struct dirent *de;
451 struct stat s;
452 char path[PKG_PATH_MAX];
453
454 int64_t codesize = 0;
455 int64_t datasize = 0;
456 int64_t cachesize = 0;
457 int64_t asecsize = 0;
458
459 /* count the source apk as code -- but only if it's not
460 * on the /system partition and its not on the sdcard.
461 */
462 if (validate_system_app_path(apkpath) &&
463 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
464 if (stat(apkpath, &s) == 0) {
465 codesize += stat_size(&s);
466 }
467 }
468 /* count the forward locked apk as code if it is given
469 */
470 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
471 if (stat(fwdlock_apkpath, &s) == 0) {
472 codesize += stat_size(&s);
473 }
474 }
475 /* count the cached dexfile as code */
Narayan Kamath1b400322014-04-11 13:17:00 +0100476 if (!create_cache_path(path, apkpath, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700477 if (stat(path, &s) == 0) {
478 codesize += stat_size(&s);
479 }
480 }
481
482 /* add in size of any libraries */
Dianne Hackborn8b417802013-05-01 18:55:10 -0700483 if (libdirpath != NULL && libdirpath[0] != '!') {
484 d = opendir(libdirpath);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700485 if (d != NULL) {
486 dfd = dirfd(d);
487 codesize += calculate_dir_size(dfd);
488 closedir(d);
489 }
490 }
491
492 /* compute asec size if it is given
493 */
494 if (asecpath != NULL && asecpath[0] != '!') {
495 if (stat(asecpath, &s) == 0) {
496 asecsize += stat_size(&s);
497 }
498 }
499
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700500 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700501 goto done;
502 }
503
504 d = opendir(path);
505 if (d == NULL) {
506 goto done;
507 }
508 dfd = dirfd(d);
509
510 /* most stuff in the pkgdir is data, except for the "cache"
511 * directory and below, which is cache, and the "lib" directory
512 * and below, which is code...
513 */
514 while ((de = readdir(d))) {
515 const char *name = de->d_name;
516
517 if (de->d_type == DT_DIR) {
518 int subfd;
519 int64_t statsize = 0;
520 int64_t dirsize = 0;
521 /* always skip "." and ".." */
522 if (name[0] == '.') {
523 if (name[1] == 0) continue;
524 if ((name[1] == '.') && (name[2] == 0)) continue;
525 }
526 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
527 statsize = stat_size(&s);
528 }
529 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
530 if (subfd >= 0) {
531 dirsize = calculate_dir_size(subfd);
532 }
533 if(!strcmp(name,"lib")) {
534 codesize += dirsize + statsize;
535 } else if(!strcmp(name,"cache")) {
536 cachesize += dirsize + statsize;
537 } else {
538 datasize += dirsize + statsize;
539 }
540 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
541 // This is the symbolic link to the application's library
542 // code. We'll count this as code instead of data, since
543 // it is not something that the app creates.
544 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
545 codesize += stat_size(&s);
546 }
547 } else {
548 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
549 datasize += stat_size(&s);
550 }
551 }
552 }
553 closedir(d);
554done:
555 *_codesize = codesize;
556 *_datasize = datasize;
557 *_cachesize = cachesize;
558 *_asecsize = asecsize;
559 return 0;
560}
561
Narayan Kamath1b400322014-04-11 13:17:00 +0100562int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700563{
564 char *tmp;
565 int srclen;
566 int dstlen;
567
568 srclen = strlen(src);
569
570 /* demand that we are an absolute path */
571 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
572 return -1;
573 }
574
575 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
576 return -1;
577 }
578
Dave Allisond9370732014-01-30 14:19:23 -0800579 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100580 strlen(instruction_set) +
581 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800582
Mike Lockwood94afecf2012-10-24 10:45:23 -0700583 if (dstlen > PKG_PATH_MAX) {
584 return -1;
585 }
586
Narayan Kamath1b400322014-04-11 13:17:00 +0100587 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700588 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100589 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700590 src + 1, /* skip the leading / */
591 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800592
Narayan Kamath1b400322014-04-11 13:17:00 +0100593 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700594 if (*tmp == '/') {
595 *tmp = '@';
596 }
597 }
598
599 return 0;
600}
601
602static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800603 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700604{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800605 /* platform-specific flags affecting optimization and verification */
606 char dexopt_flags[PROPERTY_VALUE_MAX];
607 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
608 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
609
Mike Lockwood94afecf2012-10-24 10:45:23 -0700610 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
611 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
612 char zip_num[MAX_INT_LEN];
613 char odex_num[MAX_INT_LEN];
614
615 sprintf(zip_num, "%d", zip_fd);
616 sprintf(odex_num, "%d", odex_fd);
617
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700618 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700619 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
620 dexopt_flags, (char*) NULL);
621 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
622}
623
Alex Light7365a102014-07-21 12:23:48 -0700624static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name,
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700625 const char* output_file_name, const char *pkgname __unused, const char *instruction_set)
Alex Light7365a102014-07-21 12:23:48 -0700626{
627 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100628 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
Alex Light7365a102014-07-21 12:23:48 -0700629
630 static const char* PATCHOAT_BIN = "/system/bin/patchoat";
631 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
632 ALOGE("Instruction set %s longer than max length of %d",
633 instruction_set, MAX_INSTRUCTION_SET_LEN);
634 return;
635 }
636
637 /* input_file_name/input_fd should be the .odex/.oat file that is precompiled. I think*/
638 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
639 char output_oat_fd_arg[strlen("--output-oat-fd=") + MAX_INT_LEN];
640 char input_oat_fd_arg[strlen("--input-oat-fd=") + MAX_INT_LEN];
641 const char* patched_image_location_arg = "--patched-image-location=/system/framework/boot.art";
642 // The caller has already gotten all the locks we need.
643 const char* no_lock_arg = "--no-lock-output";
644 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
645 sprintf(output_oat_fd_arg, "--output-oat-fd=%d", oat_fd);
646 sprintf(input_oat_fd_arg, "--input-oat-fd=%d", input_fd);
Alex Lighta7915d42014-08-11 10:07:02 -0700647 ALOGV("Running %s isa=%s in-fd=%d (%s) out-fd=%d (%s)\n",
Alex Light7365a102014-07-21 12:23:48 -0700648 PATCHOAT_BIN, instruction_set, input_fd, input_file_name, oat_fd, output_file_name);
649
650 /* patchoat, patched-image-location, no-lock, isa, input-fd, output-fd */
651 char* argv[7];
652 argv[0] = (char*) PATCHOAT_BIN;
653 argv[1] = (char*) patched_image_location_arg;
654 argv[2] = (char*) no_lock_arg;
655 argv[3] = instruction_set_arg;
656 argv[4] = output_oat_fd_arg;
657 argv[5] = input_oat_fd_arg;
658 argv[6] = NULL;
659
660 execv(PATCHOAT_BIN, (char* const *)argv);
661 ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno));
662}
663
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700664static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Calin Juravle4f60ac22014-08-21 19:05:20 +0100665 const char* output_file_name, const char *pkgname, const char *instruction_set,
666 bool vm_safe_mode)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700667{
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100668 static const unsigned int MAX_INSTRUCTION_SET_LEN = 7;
669
670 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
671 ALOGE("Instruction set %s longer than max length of %d",
672 instruction_set, MAX_INSTRUCTION_SET_LEN);
673 return;
674 }
675
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700676 char prop_buf[PROPERTY_VALUE_MAX];
677 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
678
679 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
680 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
681
682 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
683 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
684
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700685 char dex2oat_compiler_filter_flag[PROPERTY_VALUE_MAX];
686 bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
687 dex2oat_compiler_filter_flag, NULL) > 0;
688
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100689 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
690 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
691 char dex2oat_isa_features[PROPERTY_VALUE_MAX];
692 bool have_dex2oat_isa_features = property_get(dex2oat_isa_features_key,
693 dex2oat_isa_features, NULL) > 0;
694
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800695 char dex2oat_flags[PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100696 bool have_dex2oat_flags = property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, NULL) > 0;
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800697 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
698
Brian Carlstrom538998f2014-07-30 14:37:11 -0700699 // If we booting without the real /data, don't spend time compiling.
700 char vold_decrypt[PROPERTY_VALUE_MAX];
701 bool have_vold_decrypt = property_get("vold.decrypt", vold_decrypt, "") > 0;
702 bool skip_compilation = (have_vold_decrypt &&
703 (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 ||
704 (strcmp(vold_decrypt, "1") == 0)));
705
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700706 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700707
Brian Carlstrom53e07762014-06-27 14:15:19 -0700708 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700709
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700710 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100711
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700712 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
713 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
714 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700715 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100716 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100717 char instruction_set_features_arg[strlen("--instruction-set-features=") + PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100718 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
719 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700720 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
721 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700722 char dex2oat_compiler_filter_arg[strlen("--compiler-filter=") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700723
724 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
725 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
726 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
727 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100728 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100729 sprintf(instruction_set_features_arg, "--instruction-set-features=%s", dex2oat_isa_features);
Calin Juravle57c69c32014-06-06 14:42:16 +0100730
Calin Juravle4fdff462014-06-06 16:58:43 +0100731 bool have_profile_file = false;
732 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100733 if (profiler && (strcmp(pkgname, "*") != 0)) {
734 char profile_file[PKG_PATH_MAX];
735 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800736 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100737 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100738 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100739 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100740 have_profile_file = true;
741 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
742 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
743 "--top-k-profile-threshold=%s", prop_buf);
744 have_top_k_profile_threshold = true;
745 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100746 }
Dave Allisond9370732014-01-30 14:19:23 -0800747 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700748
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700749 if (have_dex2oat_Xms_flag) {
750 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
751 }
752 if (have_dex2oat_Xmx_flag) {
753 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
754 }
Brian Carlstrom538998f2014-07-30 14:37:11 -0700755 if (skip_compilation) {
Brian Carlstromf7765c42014-08-15 09:55:50 -0700756 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=verify-none");
Brian Carlstrom538998f2014-07-30 14:37:11 -0700757 have_dex2oat_compiler_filter_flag = true;
Calin Juravle4f60ac22014-08-21 19:05:20 +0100758 } else if (vm_safe_mode) {
759 strcpy(dex2oat_compiler_filter_arg, "--compiler-filter=interpret-only");
760 have_dex2oat_compiler_filter_flag = true;
Brian Carlstrom538998f2014-07-30 14:37:11 -0700761 } else if (have_dex2oat_compiler_filter_flag) {
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700762 sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
763 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700764
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700765 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100766
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700767 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100768 + (have_dex2oat_isa_features ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100769 + (have_profile_file ? 1 : 0)
770 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700771 + (have_dex2oat_Xms_flag ? 2 : 0)
772 + (have_dex2oat_Xmx_flag ? 2 : 0)
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700773 + (have_dex2oat_compiler_filter_flag ? 1 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100774 + (have_dex2oat_flags ? 1 : 0)];
775 int i = 0;
776 argv[i++] = (char*)DEX2OAT_BIN;
777 argv[i++] = zip_fd_arg;
778 argv[i++] = zip_location_arg;
779 argv[i++] = oat_fd_arg;
780 argv[i++] = oat_location_arg;
781 argv[i++] = instruction_set_arg;
Calin Juravlee9eb12c2014-08-19 18:48:50 +0100782 if (have_dex2oat_isa_features) {
783 argv[i++] = instruction_set_features_arg;
784 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100785 if (have_profile_file) {
786 argv[i++] = profile_file_arg;
787 }
788 if (have_top_k_profile_threshold) {
789 argv[i++] = top_k_profile_threshold_arg;
790 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700791 if (have_dex2oat_Xms_flag) {
792 argv[i++] = (char*)RUNTIME_ARG;
793 argv[i++] = dex2oat_Xms_arg;
794 }
795 if (have_dex2oat_Xmx_flag) {
796 argv[i++] = (char*)RUNTIME_ARG;
797 argv[i++] = dex2oat_Xmx_arg;
798 }
Brian Carlstromcf51ba12014-07-28 19:13:28 -0700799 if (have_dex2oat_compiler_filter_flag) {
800 argv[i++] = dex2oat_compiler_filter_arg;
801 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100802 if (have_dex2oat_flags) {
803 argv[i++] = dex2oat_flags;
804 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700805 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100806 argv[i] = NULL;
807
808 execv(DEX2OAT_BIN, (char* const *)argv);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700809 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
810}
811
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100812static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700813{
814 int status;
815 pid_t got_pid;
816
Mike Lockwood94afecf2012-10-24 10:45:23 -0700817 while (1) {
818 got_pid = waitpid(pid, &status, 0);
819 if (got_pid == -1 && errno == EINTR) {
820 printf("waitpid interrupted, retrying\n");
821 } else {
822 break;
823 }
824 }
825 if (got_pid != pid) {
826 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
827 (int) pid, (int) got_pid, strerror(errno));
828 return 1;
829 }
830
831 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700832 return 0;
833 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700834 return status; /* always nonzero */
835 }
836}
837
Calin Juravle4f60ac22014-08-21 19:05:20 +0100838int dexopt(const char *apk_path, uid_t uid, bool is_public,
Alex Light7365a102014-07-21 12:23:48 -0700839 const char *pkgname, const char *instruction_set,
Calin Juravle4f60ac22014-08-21 19:05:20 +0100840 bool vm_safe_mode, bool is_patchoat)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700841{
842 struct utimbuf ut;
Alex Light7365a102014-07-21 12:23:48 -0700843 struct stat input_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700844 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700845 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700846 char *end;
Alex Light7365a102014-07-21 12:23:48 -0700847 const char *input_file;
848 char in_odex_path[PKG_PATH_MAX];
849 int res, input_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700850
Mike Lockwood94afecf2012-10-24 10:45:23 -0700851 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
852 return -1;
853 }
854
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800855 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700856 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700857
Alex Light7365a102014-07-21 12:23:48 -0700858 if (is_patchoat && strncmp(persist_sys_dalvik_vm_lib, "libart", 6) != 0) {
859 /* We may only patch if we are libart */
860 ALOGE("Patching is only supported in libart\n");
861 return -1;
862 }
863
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700864 /* Before anything else: is there a .odex file? If so, we have
865 * precompiled the apk and there is nothing to do here.
Alex Light7365a102014-07-21 12:23:48 -0700866 *
867 * We skip this if we are doing a patchoat.
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700868 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800869 strcpy(out_path, apk_path);
870 end = strrchr(out_path, '.');
Alex Light7365a102014-07-21 12:23:48 -0700871 if (end != NULL && !is_patchoat) {
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800872 strcpy(end, ".odex");
873 if (stat(out_path, &dex_stat) == 0) {
874 return 0;
875 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700876 }
877
Narayan Kamath1b400322014-04-11 13:17:00 +0100878 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700879 return -1;
880 }
881
Alex Light7365a102014-07-21 12:23:48 -0700882 if (is_patchoat) {
883 /* /system/framework/whatever.jar -> /system/framework/<isa>/whatever.odex */
884 strcpy(in_odex_path, apk_path);
885 end = strrchr(in_odex_path, '/');
886 if (end == NULL) {
887 ALOGE("apk_path '%s' has no '/'s in it?!\n", apk_path);
888 return -1;
889 }
890 const char *apk_end = apk_path + (end - in_odex_path); // strrchr(apk_path, '/');
891 strcpy(end + 1, instruction_set); // in_odex_path now is /system/framework/<isa>\0
892 strcat(in_odex_path, apk_end);
893 end = strrchr(in_odex_path, '.');
894 if (end == NULL) {
895 return -1;
896 }
897 strcpy(end + 1, "odex");
898 input_file = in_odex_path;
899 } else {
900 input_file = apk_path;
901 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700902
Alex Light7365a102014-07-21 12:23:48 -0700903 memset(&input_stat, 0, sizeof(input_stat));
904 stat(input_file, &input_stat);
905
906 input_fd = open(input_file, O_RDONLY, 0);
907 if (input_fd < 0) {
908 ALOGE("installd cannot open '%s' for input during dexopt\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700909 return -1;
910 }
911
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700912 unlink(out_path);
913 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
914 if (out_fd < 0) {
915 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700916 goto fail;
917 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700918 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700919 S_IRUSR|S_IWUSR|S_IRGRP |
920 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700921 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700922 goto fail;
923 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700924 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
925 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700926 goto fail;
927 }
928
Dave Allisond9370732014-01-30 14:19:23 -0800929 // Create profile file if there is a package name present.
930 if (strcmp(pkgname, "*") != 0) {
931 create_profile_file(pkgname, uid);
932 }
933
934
Alex Light7365a102014-07-21 12:23:48 -0700935 ALOGV("DexInv: --- BEGIN '%s' ---\n", input_file);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700936
937 pid_t pid;
938 pid = fork();
939 if (pid == 0) {
940 /* child -- drop privileges before continuing */
941 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700942 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700943 exit(64);
944 }
945 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700946 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700947 exit(65);
948 }
949 // drop capabilities
950 struct __user_cap_header_struct capheader;
951 struct __user_cap_data_struct capdata[2];
952 memset(&capheader, 0, sizeof(capheader));
953 memset(&capdata, 0, sizeof(capdata));
954 capheader.version = _LINUX_CAPABILITY_VERSION_3;
955 if (capset(&capheader, &capdata[0]) < 0) {
956 ALOGE("capset failed: %s\n", strerror(errno));
957 exit(66);
958 }
Brian Carlstrom0378aaf2014-08-08 00:52:22 -0700959 if (set_sched_policy(0, SP_BACKGROUND) < 0) {
960 ALOGE("set_sched_policy failed: %s\n", strerror(errno));
961 exit(70);
962 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700963 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
964 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700965 exit(67);
966 }
967
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700968 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Alex Light7365a102014-07-21 12:23:48 -0700969 run_dexopt(input_fd, out_fd, input_file, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700970 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Alex Light7365a102014-07-21 12:23:48 -0700971 if (is_patchoat) {
972 run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
973 } else {
Calin Juravle4f60ac22014-08-21 19:05:20 +0100974 run_dex2oat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set,
975 vm_safe_mode);
Alex Light7365a102014-07-21 12:23:48 -0700976 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700977 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700978 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700979 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700980 exit(68); /* only get here on exec failure */
981 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100982 res = wait_child(pid);
983 if (res == 0) {
Alex Light7365a102014-07-21 12:23:48 -0700984 ALOGV("DexInv: --- END '%s' (success) ---\n", input_file);
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100985 } else {
Alex Light7365a102014-07-21 12:23:48 -0700986 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", input_file, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700987 goto fail;
988 }
989 }
990
Alex Light7365a102014-07-21 12:23:48 -0700991 ut.actime = input_stat.st_atime;
992 ut.modtime = input_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700993 utime(out_path, &ut);
994
995 close(out_fd);
Alex Light7365a102014-07-21 12:23:48 -0700996 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700997 return 0;
998
999fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -07001000 if (out_fd >= 0) {
1001 close(out_fd);
1002 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001003 }
Alex Light7365a102014-07-21 12:23:48 -07001004 if (input_fd >= 0) {
1005 close(input_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001006 }
1007 return -1;
1008}
1009
1010void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1011 struct stat* statbuf)
1012{
1013 while (path[basepos] != 0) {
1014 if (path[basepos] == '/') {
1015 path[basepos] = 0;
1016 if (lstat(path, statbuf) < 0) {
1017 ALOGV("Making directory: %s\n", path);
1018 if (mkdir(path, mode) == 0) {
1019 chown(path, uid, gid);
1020 } else {
1021 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1022 }
1023 }
1024 path[basepos] = '/';
1025 basepos++;
1026 }
1027 basepos++;
1028 }
1029}
1030
1031int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
1032 int dstuid, int dstgid, struct stat* statbuf)
1033{
1034 DIR *d;
1035 struct dirent *de;
1036 int res;
1037
1038 int srcend = strlen(srcpath);
1039 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -08001040
Mike Lockwood94afecf2012-10-24 10:45:23 -07001041 if (lstat(srcpath, statbuf) < 0) {
1042 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
1043 return 1;
1044 }
Dave Allisond9370732014-01-30 14:19:23 -08001045
Mike Lockwood94afecf2012-10-24 10:45:23 -07001046 if ((statbuf->st_mode&S_IFDIR) == 0) {
1047 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
1048 dstuid, dstgid, statbuf);
1049 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
1050 if (rename(srcpath, dstpath) >= 0) {
1051 if (chown(dstpath, dstuid, dstgid) < 0) {
1052 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
1053 unlink(dstpath);
1054 return 1;
1055 }
1056 } else {
1057 ALOGW("Unable to rename %s to %s: %s\n",
1058 srcpath, dstpath, strerror(errno));
1059 return 1;
1060 }
1061 return 0;
1062 }
1063
1064 d = opendir(srcpath);
1065 if (d == NULL) {
1066 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
1067 return 1;
1068 }
1069
1070 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -08001071
Mike Lockwood94afecf2012-10-24 10:45:23 -07001072 while ((de = readdir(d))) {
1073 const char *name = de->d_name;
1074 /* always skip "." and ".." */
1075 if (name[0] == '.') {
1076 if (name[1] == 0) continue;
1077 if ((name[1] == '.') && (name[2] == 0)) continue;
1078 }
Dave Allisond9370732014-01-30 14:19:23 -08001079
Mike Lockwood94afecf2012-10-24 10:45:23 -07001080 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1081 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
1082 continue;
1083 }
Dave Allisond9370732014-01-30 14:19:23 -08001084
Mike Lockwood94afecf2012-10-24 10:45:23 -07001085 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
1086 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
1087 continue;
1088 }
Dave Allisond9370732014-01-30 14:19:23 -08001089
Mike Lockwood94afecf2012-10-24 10:45:23 -07001090 srcpath[srcend] = dstpath[dstend] = '/';
1091 strcpy(srcpath+srcend+1, name);
1092 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -08001093
Mike Lockwood94afecf2012-10-24 10:45:23 -07001094 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
1095 res = 1;
1096 }
Dave Allisond9370732014-01-30 14:19:23 -08001097
Mike Lockwood94afecf2012-10-24 10:45:23 -07001098 // Note: we will be leaving empty directories behind in srcpath,
1099 // but that is okay, the package manager will be erasing all of the
1100 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -08001101
Mike Lockwood94afecf2012-10-24 10:45:23 -07001102 srcpath[srcend] = dstpath[dstend] = 0;
1103 }
Dave Allisond9370732014-01-30 14:19:23 -08001104
Mike Lockwood94afecf2012-10-24 10:45:23 -07001105 closedir(d);
1106 return res;
1107}
1108
1109int movefiles()
1110{
1111 DIR *d;
1112 int dfd, subfd;
1113 struct dirent *de;
1114 struct stat s;
1115 char buf[PKG_PATH_MAX+1];
1116 int bufp, bufe, bufi, readlen;
1117
1118 char srcpkg[PKG_NAME_MAX];
1119 char dstpkg[PKG_NAME_MAX];
1120 char srcpath[PKG_PATH_MAX];
1121 char dstpath[PKG_PATH_MAX];
1122 int dstuid=-1, dstgid=-1;
1123 int hasspace;
1124
1125 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1126 if (d == NULL) {
1127 goto done;
1128 }
1129 dfd = dirfd(d);
1130
1131 /* Iterate through all files in the directory, executing the
1132 * file movements requested there-in.
1133 */
1134 while ((de = readdir(d))) {
1135 const char *name = de->d_name;
1136
1137 if (de->d_type == DT_DIR) {
1138 continue;
1139 } else {
1140 subfd = openat(dfd, name, O_RDONLY);
1141 if (subfd < 0) {
1142 ALOGW("Unable to open update commands at %s%s\n",
1143 UPDATE_COMMANDS_DIR_PREFIX, name);
1144 continue;
1145 }
Dave Allisond9370732014-01-30 14:19:23 -08001146
Mike Lockwood94afecf2012-10-24 10:45:23 -07001147 bufp = 0;
1148 bufe = 0;
1149 buf[PKG_PATH_MAX] = 0;
1150 srcpkg[0] = dstpkg[0] = 0;
1151 while (1) {
1152 bufi = bufp;
1153 while (bufi < bufe && buf[bufi] != '\n') {
1154 bufi++;
1155 }
1156 if (bufi < bufe) {
1157 buf[bufi] = 0;
1158 ALOGV("Processing line: %s\n", buf+bufp);
1159 hasspace = 0;
1160 while (bufp < bufi && isspace(buf[bufp])) {
1161 hasspace = 1;
1162 bufp++;
1163 }
1164 if (buf[bufp] == '#' || bufp == bufi) {
1165 // skip comments and empty lines.
1166 } else if (hasspace) {
1167 if (dstpkg[0] == 0) {
1168 ALOGW("Path before package line in %s%s: %s\n",
1169 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1170 } else if (srcpkg[0] == 0) {
1171 // Skip -- source package no longer exists.
1172 } else {
1173 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1174 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1175 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1176 movefileordir(srcpath, dstpath,
1177 strlen(dstpath)-strlen(buf+bufp),
1178 dstuid, dstgid, &s);
1179 }
1180 }
1181 } else {
1182 char* div = strchr(buf+bufp, ':');
1183 if (div == NULL) {
1184 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1185 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1186 } else {
1187 *div = 0;
1188 div++;
1189 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1190 strcpy(dstpkg, buf+bufp);
1191 } else {
1192 srcpkg[0] = dstpkg[0] = 0;
1193 ALOGW("Package name too long in %s%s: %s\n",
1194 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1195 }
1196 if (strlen(div) < PKG_NAME_MAX) {
1197 strcpy(srcpkg, div);
1198 } else {
1199 srcpkg[0] = dstpkg[0] = 0;
1200 ALOGW("Package name too long in %s%s: %s\n",
1201 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1202 }
1203 if (srcpkg[0] != 0) {
1204 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1205 if (lstat(srcpath, &s) < 0) {
1206 // Package no longer exists -- skip.
1207 srcpkg[0] = 0;
1208 }
1209 } else {
1210 srcpkg[0] = 0;
1211 ALOGW("Can't create path %s in %s%s\n",
1212 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1213 }
1214 if (srcpkg[0] != 0) {
1215 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1216 if (lstat(dstpath, &s) == 0) {
1217 dstuid = s.st_uid;
1218 dstgid = s.st_gid;
1219 } else {
1220 // Destination package doesn't
1221 // exist... due to original-package,
1222 // this is normal, so don't be
1223 // noisy about it.
1224 srcpkg[0] = 0;
1225 }
1226 } else {
1227 srcpkg[0] = 0;
1228 ALOGW("Can't create path %s in %s%s\n",
1229 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1230 }
1231 }
1232 ALOGV("Transfering from %s to %s: uid=%d\n",
1233 srcpkg, dstpkg, dstuid);
1234 }
1235 }
1236 }
1237 bufp = bufi+1;
1238 } else {
1239 if (bufp == 0) {
1240 if (bufp < bufe) {
1241 ALOGW("Line too long in %s%s, skipping: %s\n",
1242 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1243 }
1244 } else if (bufp < bufe) {
1245 memcpy(buf, buf+bufp, bufe-bufp);
1246 bufe -= bufp;
1247 bufp = 0;
1248 }
1249 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1250 if (readlen < 0) {
1251 ALOGW("Failure reading update commands in %s%s: %s\n",
1252 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1253 break;
1254 } else if (readlen == 0) {
1255 break;
1256 }
1257 bufe += readlen;
1258 buf[bufe] = 0;
1259 ALOGV("Read buf: %s\n", buf);
1260 }
1261 }
1262 close(subfd);
1263 }
1264 }
1265 closedir(d);
1266done:
1267 return 0;
1268}
1269
1270int linklib(const char* pkgname, const char* asecLibDir, int userId)
1271{
1272 char pkgdir[PKG_PATH_MAX];
1273 char libsymlink[PKG_PATH_MAX];
1274 struct stat s, libStat;
1275 int rc = 0;
1276
1277 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1278 ALOGE("cannot create package path\n");
1279 return -1;
1280 }
1281 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1282 ALOGE("cannot create package lib symlink origin path\n");
1283 return -1;
1284 }
1285
1286 if (stat(pkgdir, &s) < 0) return -1;
1287
1288 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1289 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1290 return -1;
1291 }
1292
1293 if (chmod(pkgdir, 0700) < 0) {
1294 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1295 rc = -1;
1296 goto out;
1297 }
1298
1299 if (lstat(libsymlink, &libStat) < 0) {
1300 if (errno != ENOENT) {
1301 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1302 rc = -1;
1303 goto out;
1304 }
1305 } else {
1306 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001307 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001308 rc = -1;
1309 goto out;
1310 }
1311 } else if (S_ISLNK(libStat.st_mode)) {
1312 if (unlink(libsymlink) < 0) {
1313 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1314 rc = -1;
1315 goto out;
1316 }
1317 }
1318 }
1319
1320 if (symlink(asecLibDir, libsymlink) < 0) {
1321 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1322 strerror(errno));
1323 rc = -errno;
1324 goto out;
1325 }
1326
1327out:
1328 if (chmod(pkgdir, s.st_mode) < 0) {
1329 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1330 rc = -errno;
1331 }
1332
1333 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1334 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1335 return -errno;
1336 }
1337
1338 return rc;
1339}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001340
1341static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1342{
1343 static const char *IDMAP_BIN = "/system/bin/idmap";
1344 static const size_t MAX_INT_LEN = 32;
1345 char idmap_str[MAX_INT_LEN];
1346
1347 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1348
1349 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1350 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1351}
1352
1353// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1354// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1355static int flatten_path(const char *prefix, const char *suffix,
1356 const char *overlay_path, char *idmap_path, size_t N)
1357{
1358 if (overlay_path == NULL || idmap_path == NULL) {
1359 return -1;
1360 }
1361 const size_t len_overlay_path = strlen(overlay_path);
1362 // will access overlay_path + 1 further below; requires absolute path
1363 if (len_overlay_path < 2 || *overlay_path != '/') {
1364 return -1;
1365 }
1366 const size_t len_idmap_root = strlen(prefix);
1367 const size_t len_suffix = strlen(suffix);
1368 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1369 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1370 // additions below would cause overflow
1371 return -1;
1372 }
1373 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1374 return -1;
1375 }
1376 memset(idmap_path, 0, N);
1377 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1378 char *ch = idmap_path + len_idmap_root;
1379 while (*ch != '\0') {
1380 if (*ch == '/') {
1381 *ch = '@';
1382 }
1383 ++ch;
1384 }
1385 return 0;
1386}
1387
1388int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1389{
1390 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1391
1392 int idmap_fd = -1;
1393 char idmap_path[PATH_MAX];
1394
1395 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1396 idmap_path, sizeof(idmap_path)) == -1) {
1397 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1398 goto fail;
1399 }
1400
1401 unlink(idmap_path);
1402 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1403 if (idmap_fd < 0) {
1404 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1405 goto fail;
1406 }
1407 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1408 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1409 goto fail;
1410 }
1411 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1412 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1413 goto fail;
1414 }
1415
1416 pid_t pid;
1417 pid = fork();
1418 if (pid == 0) {
1419 /* child -- drop privileges before continuing */
1420 if (setgid(uid) != 0) {
1421 ALOGE("setgid(%d) failed during idmap\n", uid);
1422 exit(1);
1423 }
1424 if (setuid(uid) != 0) {
1425 ALOGE("setuid(%d) failed during idmap\n", uid);
1426 exit(1);
1427 }
1428 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1429 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1430 exit(1);
1431 }
1432
1433 run_idmap(target_apk, overlay_apk, idmap_fd);
1434 exit(1); /* only if exec call to idmap failed */
1435 } else {
1436 int status = wait_child(pid);
1437 if (status != 0) {
1438 ALOGE("idmap failed, status=0x%04x\n", status);
1439 goto fail;
1440 }
1441 }
1442
1443 close(idmap_fd);
1444 return 0;
1445fail:
1446 if (idmap_fd >= 0) {
1447 close(idmap_fd);
1448 unlink(idmap_path);
1449 }
1450 return -1;
1451}
Robert Craige9887e42014-02-20 10:25:56 -05001452
Robert Craigda30dc72014-03-27 10:21:12 -04001453int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001454{
Robert Craigda30dc72014-03-27 10:21:12 -04001455 struct dirent *entry;
1456 DIR *d;
1457 struct stat s;
1458 char *userdir;
1459 char *primarydir;
1460 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001461 int ret = 0;
1462
Robert Craigda30dc72014-03-27 10:21:12 -04001463 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1464 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1465
1466 if (!pkgName || !seinfo) {
1467 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001468 return -1;
1469 }
1470
Robert Craigda30dc72014-03-27 10:21:12 -04001471 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1472 return -1;
1473 }
1474
1475 // Relabel for primary user.
1476 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1477 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001478 ret |= -1;
1479 }
1480
Robert Craigda30dc72014-03-27 10:21:12 -04001481 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1482 free(primarydir);
1483 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001484 }
1485
Robert Craigda30dc72014-03-27 10:21:12 -04001486 // Relabel package directory for all secondary users.
1487 d = opendir(userdir);
1488 if (d == NULL) {
1489 free(primarydir);
1490 free(userdir);
1491 return -1;
1492 }
1493
1494 while ((entry = readdir(d))) {
1495 if (entry->d_type != DT_DIR) {
1496 continue;
1497 }
1498
1499 const char *user = entry->d_name;
1500 // Ignore "." and ".."
1501 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1502 continue;
1503 }
1504
1505 // user directories start with a number
1506 if (user[0] < '0' || user[0] > '9') {
1507 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1508 continue;
1509 }
1510
1511 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1512 continue;
1513 }
1514
1515 if (stat(pkgdir, &s) < 0) {
1516 free(pkgdir);
1517 continue;
1518 }
1519
Stephen Smalley8ac2a642014-09-08 15:51:55 -04001520 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, s.st_uid, flags) < 0) {
Robert Craigda30dc72014-03-27 10:21:12 -04001521 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1522 ret |= -1;
1523 }
1524 free(pkgdir);
1525 }
1526
1527 closedir(d);
1528 free(primarydir);
1529 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001530 return ret;
1531}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001532