blob: 41a8f30227b2fdc69af845d7f21585f2e5beb48f [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
Mike Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
Mike Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070017#include <inttypes.h>
Nick Kralevichd7471292013-02-28 16:59:13 -080018#include <sys/capability.h>
Mike Lockwood94afecf2012-10-24 10:45:23 -070019#include "installd.h"
20#include <diskusage/dirsize.h>
21#include <selinux/android.h>
22
23/* Directory records that are used in execution of commands. */
24dir_rec_t android_data_dir;
25dir_rec_t android_asec_dir;
26dir_rec_t android_app_dir;
27dir_rec_t android_app_private_dir;
28dir_rec_t android_app_lib_dir;
29dir_rec_t android_media_dir;
30dir_rec_array_t android_system_dirs;
31
Robert Craig4d3fd4e2013-03-25 06:33:03 -040032int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -070033{
34 char pkgdir[PKG_PATH_MAX];
35 char libsymlink[PKG_PATH_MAX];
36 char applibdir[PKG_PATH_MAX];
37 struct stat libStat;
38
39 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
40 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
41 return -1;
42 }
43
44 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
45 ALOGE("cannot create package path\n");
46 return -1;
47 }
48
49 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
50 ALOGE("cannot create package lib symlink origin path\n");
51 return -1;
52 }
53
54 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
55 ALOGE("cannot create package lib symlink dest path\n");
56 return -1;
57 }
58
Nick Kralevicha2d838a2013-01-09 16:00:35 -080059 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070060 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
61 return -1;
62 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -080063 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070064 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
65 unlink(pkgdir);
66 return -1;
67 }
68
69 if (lstat(libsymlink, &libStat) < 0) {
70 if (errno != ENOENT) {
71 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
72 return -1;
73 }
74 } else {
75 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +010076 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070077 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
78 return -1;
79 }
80 } else if (S_ISLNK(libStat.st_mode)) {
81 if (unlink(libsymlink) < 0) {
82 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
83 return -1;
84 }
85 }
86 }
87
Stephen Smalley26288202014-02-07 09:16:46 -050088 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070089 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
90 unlink(libsymlink);
91 unlink(pkgdir);
92 return -errno;
93 }
94
Stephen Smalley3a983892014-05-13 12:53:07 -040095 if (symlink(applibdir, libsymlink) < 0) {
96 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
97 strerror(errno));
98 unlink(pkgdir);
99 return -1;
100 }
101
Mike Lockwood94afecf2012-10-24 10:45:23 -0700102 if (chown(pkgdir, uid, gid) < 0) {
103 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
104 unlink(libsymlink);
105 unlink(pkgdir);
106 return -1;
107 }
108
109 return 0;
110}
111
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700112int uninstall(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700113{
114 char pkgdir[PKG_PATH_MAX];
115
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700116 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700117 return -1;
118
Dave Allisond9370732014-01-30 14:19:23 -0800119 remove_profile_file(pkgname);
120
Mike Lockwood94afecf2012-10-24 10:45:23 -0700121 /* delete contents AND directory, no exceptions */
122 return delete_dir_contents(pkgdir, 1, NULL);
123}
124
125int renamepkg(const char *oldpkgname, const char *newpkgname)
126{
127 char oldpkgdir[PKG_PATH_MAX];
128 char newpkgdir[PKG_PATH_MAX];
129
130 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
131 return -1;
132 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
133 return -1;
134
135 if (rename(oldpkgdir, newpkgdir) < 0) {
136 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
137 return -errno;
138 }
139 return 0;
140}
141
142int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
143{
144 char pkgdir[PKG_PATH_MAX];
145 struct stat s;
146 int rc = 0;
147
148 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
149 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
150 return -1;
151 }
152
153 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
154 ALOGE("cannot create package path\n");
155 return -1;
156 }
157
158 if (stat(pkgdir, &s) < 0) return -1;
159
160 if (s.st_uid != 0 || s.st_gid != 0) {
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700161 ALOGE("fixing uid of non-root pkg: %s %" PRIu32 " %" PRIu32 "\n", pkgdir, s.st_uid, s.st_gid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700162 return -1;
163 }
164
165 if (chmod(pkgdir, 0751) < 0) {
166 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
167 unlink(pkgdir);
168 return -errno;
169 }
170 if (chown(pkgdir, uid, gid) < 0) {
171 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
172 unlink(pkgdir);
173 return -errno;
174 }
175
176 return 0;
177}
178
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100179static int lib_dir_matcher(const char* file_name, const int is_dir) {
180 return is_dir && !strcmp(file_name, "lib");
181}
182
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700183int delete_user_data(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700184{
185 char pkgdir[PKG_PATH_MAX];
186
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700187 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700188 return -1;
189
190 /* delete contents, excluding "lib", but not the directory itself */
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100191 return delete_dir_contents(pkgdir, 0, &lib_dir_matcher);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192}
193
Nick Kraleviche4e91c42013-09-20 12:45:20 -0700194int make_user_data(const char *pkgname, uid_t uid, userid_t userid, const char* seinfo)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195{
196 char pkgdir[PKG_PATH_MAX];
197 char applibdir[PKG_PATH_MAX];
198 char libsymlink[PKG_PATH_MAX];
199 struct stat libStat;
200
201 // Create the data dir for the package
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700202 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700203 return -1;
204 }
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700205 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userid)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700206 ALOGE("cannot create package lib symlink origin path\n");
207 return -1;
208 }
209 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
210 ALOGE("cannot create package lib symlink dest path\n");
211 return -1;
212 }
213
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800214 if (mkdir(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700215 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
216 return -errno;
217 }
Nick Kralevicha2d838a2013-01-09 16:00:35 -0800218 if (chmod(pkgdir, 0751) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700219 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
220 unlink(pkgdir);
221 return -errno;
222 }
223
224 if (lstat(libsymlink, &libStat) < 0) {
225 if (errno != ENOENT) {
226 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
227 unlink(pkgdir);
228 return -1;
229 }
230 } else {
231 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100232 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700233 ALOGE("couldn't delete lib directory during install for non-primary: %s",
234 libsymlink);
235 unlink(pkgdir);
236 return -1;
237 }
238 } else if (S_ISLNK(libStat.st_mode)) {
239 if (unlink(libsymlink) < 0) {
240 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
241 libsymlink);
242 unlink(pkgdir);
243 return -1;
244 }
245 }
246 }
247
Stephen Smalley26288202014-02-07 09:16:46 -0500248 if (selinux_android_setfilecon(pkgdir, pkgname, seinfo, uid) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700249 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
250 unlink(libsymlink);
251 unlink(pkgdir);
252 return -errno;
253 }
254
Stephen Smalley3a983892014-05-13 12:53:07 -0400255 if (symlink(applibdir, libsymlink) < 0) {
256 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
257 applibdir, strerror(errno));
258 unlink(pkgdir);
259 return -1;
260 }
261
Mike Lockwood94afecf2012-10-24 10:45:23 -0700262 if (chown(pkgdir, uid, uid) < 0) {
263 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
264 unlink(libsymlink);
265 unlink(pkgdir);
266 return -errno;
267 }
268
269 return 0;
270}
271
Robin Lee7c8bec02014-06-10 18:46:26 +0100272int make_user_config(userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700273{
Robin Lee095c7632014-04-25 15:05:19 +0100274 if (ensure_config_user_dirs(userid) == -1) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700275 return -1;
276 }
277
278 return 0;
279}
280
Robin Lee095c7632014-04-25 15:05:19 +0100281int delete_user(userid_t userid)
282{
283 int status = 0;
284
285 char data_path[PKG_PATH_MAX];
286 if ((create_user_path(data_path, userid) != 0)
287 || (delete_dir_contents(data_path, 1, NULL) != 0)) {
288 status = -1;
289 }
290
291 char media_path[PATH_MAX];
292 if ((create_user_media_path(media_path, userid) != 0)
293 || (delete_dir_contents(media_path, 1, NULL) != 0)) {
294 status = -1;
295 }
296
297 char config_path[PATH_MAX];
298 if ((create_user_config_path(config_path, userid) != 0)
299 || (delete_dir_contents(config_path, 1, NULL) != 0)) {
300 status = -1;
301 }
302
303 return status;
304}
305
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700306int delete_cache(const char *pkgname, userid_t userid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700307{
308 char cachedir[PKG_PATH_MAX];
309
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700310 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, userid))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700311 return -1;
312
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
562
Narayan Kamath1b400322014-04-11 13:17:00 +0100563int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700564{
565 char *tmp;
566 int srclen;
567 int dstlen;
568
569 srclen = strlen(src);
570
571 /* demand that we are an absolute path */
572 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
573 return -1;
574 }
575
576 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
577 return -1;
578 }
579
Dave Allisond9370732014-01-30 14:19:23 -0800580 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
Narayan Kamath1b400322014-04-11 13:17:00 +0100581 strlen(instruction_set) +
582 strlen(DALVIK_CACHE_POSTFIX) + 2;
Dave Allisond9370732014-01-30 14:19:23 -0800583
Mike Lockwood94afecf2012-10-24 10:45:23 -0700584 if (dstlen > PKG_PATH_MAX) {
585 return -1;
586 }
587
Narayan Kamath1b400322014-04-11 13:17:00 +0100588 sprintf(path,"%s%s/%s%s",
Mike Lockwood94afecf2012-10-24 10:45:23 -0700589 DALVIK_CACHE_PREFIX,
Narayan Kamath1b400322014-04-11 13:17:00 +0100590 instruction_set,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700591 src + 1, /* skip the leading / */
592 DALVIK_CACHE_POSTFIX);
Dave Allisond9370732014-01-30 14:19:23 -0800593
Narayan Kamath1b400322014-04-11 13:17:00 +0100594 for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700595 if (*tmp == '/') {
596 *tmp = '@';
597 }
598 }
599
600 return 0;
601}
602
603static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800604 const char* output_file_name)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700605{
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800606 /* platform-specific flags affecting optimization and verification */
607 char dexopt_flags[PROPERTY_VALUE_MAX];
608 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
609 ALOGV("dalvik.vm.dexopt-flags=%s\n", dexopt_flags);
610
Mike Lockwood94afecf2012-10-24 10:45:23 -0700611 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
612 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
613 char zip_num[MAX_INT_LEN];
614 char odex_num[MAX_INT_LEN];
615
616 sprintf(zip_num, "%d", zip_fd);
617 sprintf(odex_num, "%d", odex_fd);
618
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700619 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700620 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
621 dexopt_flags, (char*) NULL);
622 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
623}
624
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700625static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
Narayan Kamath1b400322014-04-11 13:17:00 +0100626 const char* output_file_name, const char *pkgname, const char *instruction_set)
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700627{
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700628 char prop_buf[PROPERTY_VALUE_MAX];
629 bool profiler = (property_get("dalvik.vm.profiler", prop_buf, "0") > 0) && (prop_buf[0] == '1');
630
631 char dex2oat_Xms_flag[PROPERTY_VALUE_MAX];
632 bool have_dex2oat_Xms_flag = property_get("dalvik.vm.dex2oat-Xms", dex2oat_Xms_flag, NULL) > 0;
633
634 char dex2oat_Xmx_flag[PROPERTY_VALUE_MAX];
635 bool have_dex2oat_Xmx_flag = property_get("dalvik.vm.dex2oat-Xmx", dex2oat_Xmx_flag, NULL) > 0;
636
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800637 char dex2oat_flags[PROPERTY_VALUE_MAX];
Calin Juravle4fdff462014-06-06 16:58:43 +0100638 bool have_dex2oat_flags = property_get("dalvik.vm.dex2oat-flags", dex2oat_flags, NULL) > 0;
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800639 ALOGV("dalvik.vm.dex2oat-flags=%s\n", dex2oat_flags);
640
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700641 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700642
Brian Carlstrom53e07762014-06-27 14:15:19 -0700643 static const char* RUNTIME_ARG = "--runtime-arg";
Brian Carlstrom53e07762014-06-27 14:15:19 -0700644
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700645 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
Narayan Kamath1b400322014-04-11 13:17:00 +0100646 static const unsigned int MAX_INSTRUCTION_SET_LEN = 32;
647
648 if (strlen(instruction_set) >= MAX_INSTRUCTION_SET_LEN) {
649 ALOGE("Instruction set %s longer than max length of %d",
650 instruction_set, MAX_INSTRUCTION_SET_LEN);
651 return;
652 }
653
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700654 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
655 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
656 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
Brian Carlstrom7195fcc2014-06-16 13:28:03 -0700657 char oat_location_arg[strlen("--oat-location=") + PKG_PATH_MAX];
Narayan Kamath1b400322014-04-11 13:17:00 +0100658 char instruction_set_arg[strlen("--instruction-set=") + MAX_INSTRUCTION_SET_LEN];
Calin Juravle4fdff462014-06-06 16:58:43 +0100659 char profile_file_arg[strlen("--profile-file=") + PKG_PATH_MAX];
660 char top_k_profile_threshold_arg[strlen("--top-k-profile-threshold=") + PROPERTY_VALUE_MAX];
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700661 char dex2oat_Xms_arg[strlen("-Xms") + PROPERTY_VALUE_MAX];
662 char dex2oat_Xmx_arg[strlen("-Xmx") + PROPERTY_VALUE_MAX];
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700663
664 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
665 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
666 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
667 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
Narayan Kamath1b400322014-04-11 13:17:00 +0100668 sprintf(instruction_set_arg, "--instruction-set=%s", instruction_set);
Calin Juravle57c69c32014-06-06 14:42:16 +0100669
Calin Juravle4fdff462014-06-06 16:58:43 +0100670 bool have_profile_file = false;
671 bool have_top_k_profile_threshold = false;
Calin Juravle57c69c32014-06-06 14:42:16 +0100672 if (profiler && (strcmp(pkgname, "*") != 0)) {
673 char profile_file[PKG_PATH_MAX];
674 snprintf(profile_file, sizeof(profile_file), "%s/%s",
Dave Allisond9370732014-01-30 14:19:23 -0800675 DALVIK_CACHE_PREFIX "profiles", pkgname);
Calin Juravle57c69c32014-06-06 14:42:16 +0100676 struct stat st;
Calin Juravle4fdff462014-06-06 16:58:43 +0100677 if ((stat(profile_file, &st) == 0) && (st.st_size > 0)) {
Calin Juravle57c69c32014-06-06 14:42:16 +0100678 sprintf(profile_file_arg, "--profile-file=%s", profile_file);
Calin Juravle4fdff462014-06-06 16:58:43 +0100679 have_profile_file = true;
680 if (property_get("dalvik.vm.profile.top-k-thr", prop_buf, NULL) > 0) {
681 snprintf(top_k_profile_threshold_arg, sizeof(top_k_profile_threshold_arg),
682 "--top-k-profile-threshold=%s", prop_buf);
683 have_top_k_profile_threshold = true;
684 }
Calin Juravle57c69c32014-06-06 14:42:16 +0100685 }
Dave Allisond9370732014-01-30 14:19:23 -0800686 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700687
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700688 if (have_dex2oat_Xms_flag) {
689 sprintf(dex2oat_Xms_arg, "-Xms%s", dex2oat_Xms_flag);
690 }
691 if (have_dex2oat_Xmx_flag) {
692 sprintf(dex2oat_Xmx_arg, "-Xmx%s", dex2oat_Xmx_flag);
693 }
694
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700695 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
Calin Juravle4fdff462014-06-06 16:58:43 +0100696
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700697 char* argv[7 // program name, mandatory arguments and the final NULL
Calin Juravle4fdff462014-06-06 16:58:43 +0100698 + (have_profile_file ? 1 : 0)
699 + (have_top_k_profile_threshold ? 1 : 0)
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700700 + (have_dex2oat_Xms_flag ? 2 : 0)
701 + (have_dex2oat_Xmx_flag ? 2 : 0)
Calin Juravle4fdff462014-06-06 16:58:43 +0100702 + (have_dex2oat_flags ? 1 : 0)];
703 int i = 0;
704 argv[i++] = (char*)DEX2OAT_BIN;
705 argv[i++] = zip_fd_arg;
706 argv[i++] = zip_location_arg;
707 argv[i++] = oat_fd_arg;
708 argv[i++] = oat_location_arg;
709 argv[i++] = instruction_set_arg;
710 if (have_profile_file) {
711 argv[i++] = profile_file_arg;
712 }
713 if (have_top_k_profile_threshold) {
714 argv[i++] = top_k_profile_threshold_arg;
715 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700716 if (have_dex2oat_Xms_flag) {
717 argv[i++] = (char*)RUNTIME_ARG;
718 argv[i++] = dex2oat_Xms_arg;
719 }
720 if (have_dex2oat_Xmx_flag) {
721 argv[i++] = (char*)RUNTIME_ARG;
722 argv[i++] = dex2oat_Xmx_arg;
723 }
Calin Juravle4fdff462014-06-06 16:58:43 +0100724 if (have_dex2oat_flags) {
725 argv[i++] = dex2oat_flags;
726 }
Brian Carlstrome46a75a2014-06-27 16:03:06 -0700727 // Do not add after dex2oat_flags, they should override others for debugging.
Calin Juravle4fdff462014-06-06 16:58:43 +0100728 argv[i] = NULL;
729
730 execv(DEX2OAT_BIN, (char* const *)argv);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700731 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
732}
733
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100734static int wait_child(pid_t pid)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700735{
736 int status;
737 pid_t got_pid;
738
Mike Lockwood94afecf2012-10-24 10:45:23 -0700739 while (1) {
740 got_pid = waitpid(pid, &status, 0);
741 if (got_pid == -1 && errno == EINTR) {
742 printf("waitpid interrupted, retrying\n");
743 } else {
744 break;
745 }
746 }
747 if (got_pid != pid) {
748 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
749 (int) pid, (int) got_pid, strerror(errno));
750 return 1;
751 }
752
753 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700754 return 0;
755 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700756 return status; /* always nonzero */
757 }
758}
759
Dave Allisond9370732014-01-30 14:19:23 -0800760int dexopt(const char *apk_path, uid_t uid, int is_public,
Narayan Kamath1b400322014-04-11 13:17:00 +0100761 const char *pkgname, const char *instruction_set)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700762{
763 struct utimbuf ut;
764 struct stat apk_stat, dex_stat;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700765 char out_path[PKG_PATH_MAX];
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700766 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
Mike Lockwood94afecf2012-10-24 10:45:23 -0700767 char *end;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700768 int res, zip_fd=-1, out_fd=-1;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700769
Mike Lockwood94afecf2012-10-24 10:45:23 -0700770 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
771 return -1;
772 }
773
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800774 /* The command to run depend on the value of persist.sys.dalvik.vm.lib */
Brian Carlstrom856bc782014-05-28 14:31:47 -0700775 property_get("persist.sys.dalvik.vm.lib.2", persist_sys_dalvik_vm_lib, "libart.so");
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700776
777 /* Before anything else: is there a .odex file? If so, we have
778 * precompiled the apk and there is nothing to do here.
779 */
Chih-Wei Huang0e8ae162014-04-28 15:47:45 +0800780 strcpy(out_path, apk_path);
781 end = strrchr(out_path, '.');
782 if (end != NULL) {
783 strcpy(end, ".odex");
784 if (stat(out_path, &dex_stat) == 0) {
785 return 0;
786 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700787 }
788
Narayan Kamath1b400322014-04-11 13:17:00 +0100789 if (create_cache_path(out_path, apk_path, instruction_set)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700790 return -1;
791 }
792
793 memset(&apk_stat, 0, sizeof(apk_stat));
794 stat(apk_path, &apk_stat);
795
796 zip_fd = open(apk_path, O_RDONLY, 0);
797 if (zip_fd < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700798 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700799 return -1;
800 }
801
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700802 unlink(out_path);
803 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
804 if (out_fd < 0) {
805 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700806 goto fail;
807 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700808 if (fchmod(out_fd,
Mike Lockwood94afecf2012-10-24 10:45:23 -0700809 S_IRUSR|S_IWUSR|S_IRGRP |
810 (is_public ? S_IROTH : 0)) < 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700811 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700812 goto fail;
813 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700814 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
815 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700816 goto fail;
817 }
818
Dave Allisond9370732014-01-30 14:19:23 -0800819 // Create profile file if there is a package name present.
820 if (strcmp(pkgname, "*") != 0) {
821 create_profile_file(pkgname, uid);
822 }
823
824
Mike Lockwood94afecf2012-10-24 10:45:23 -0700825 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
826
827 pid_t pid;
828 pid = fork();
829 if (pid == 0) {
830 /* child -- drop privileges before continuing */
831 if (setgid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700832 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700833 exit(64);
834 }
835 if (setuid(uid) != 0) {
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700836 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700837 exit(65);
838 }
839 // drop capabilities
840 struct __user_cap_header_struct capheader;
841 struct __user_cap_data_struct capdata[2];
842 memset(&capheader, 0, sizeof(capheader));
843 memset(&capdata, 0, sizeof(capdata));
844 capheader.version = _LINUX_CAPABILITY_VERSION_3;
845 if (capset(&capheader, &capdata[0]) < 0) {
846 ALOGE("capset failed: %s\n", strerror(errno));
847 exit(66);
848 }
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700849 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
850 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700851 exit(67);
852 }
853
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700854 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
Brian Carlstrom0ae8e392014-02-10 16:42:52 -0800855 run_dexopt(zip_fd, out_fd, apk_path, out_path);
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700856 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
Narayan Kamath1b400322014-04-11 13:17:00 +0100857 run_dex2oat(zip_fd, out_fd, apk_path, out_path, pkgname, instruction_set);
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700858 } else {
Brian Carlstrome7a8b172013-06-30 13:17:38 -0700859 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700860 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700861 exit(68); /* only get here on exec failure */
862 } else {
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +0100863 res = wait_child(pid);
864 if (res == 0) {
865 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
866 } else {
867 ALOGE("DexInv: --- END '%s' --- status=0x%04x, process failed\n", apk_path, res);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700868 goto fail;
869 }
870 }
871
872 ut.actime = apk_stat.st_atime;
873 ut.modtime = apk_stat.st_mtime;
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700874 utime(out_path, &ut);
875
876 close(out_fd);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700877 close(zip_fd);
878 return 0;
879
880fail:
Brian Carlstrom1705fc42013-03-21 18:20:22 -0700881 if (out_fd >= 0) {
882 close(out_fd);
883 unlink(out_path);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700884 }
885 if (zip_fd >= 0) {
886 close(zip_fd);
887 }
888 return -1;
889}
890
891void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
892 struct stat* statbuf)
893{
894 while (path[basepos] != 0) {
895 if (path[basepos] == '/') {
896 path[basepos] = 0;
897 if (lstat(path, statbuf) < 0) {
898 ALOGV("Making directory: %s\n", path);
899 if (mkdir(path, mode) == 0) {
900 chown(path, uid, gid);
901 } else {
902 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
903 }
904 }
905 path[basepos] = '/';
906 basepos++;
907 }
908 basepos++;
909 }
910}
911
912int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
913 int dstuid, int dstgid, struct stat* statbuf)
914{
915 DIR *d;
916 struct dirent *de;
917 int res;
918
919 int srcend = strlen(srcpath);
920 int dstend = strlen(dstpath);
Dave Allisond9370732014-01-30 14:19:23 -0800921
Mike Lockwood94afecf2012-10-24 10:45:23 -0700922 if (lstat(srcpath, statbuf) < 0) {
923 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
924 return 1;
925 }
Dave Allisond9370732014-01-30 14:19:23 -0800926
Mike Lockwood94afecf2012-10-24 10:45:23 -0700927 if ((statbuf->st_mode&S_IFDIR) == 0) {
928 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
929 dstuid, dstgid, statbuf);
930 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
931 if (rename(srcpath, dstpath) >= 0) {
932 if (chown(dstpath, dstuid, dstgid) < 0) {
933 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
934 unlink(dstpath);
935 return 1;
936 }
937 } else {
938 ALOGW("Unable to rename %s to %s: %s\n",
939 srcpath, dstpath, strerror(errno));
940 return 1;
941 }
942 return 0;
943 }
944
945 d = opendir(srcpath);
946 if (d == NULL) {
947 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
948 return 1;
949 }
950
951 res = 0;
Dave Allisond9370732014-01-30 14:19:23 -0800952
Mike Lockwood94afecf2012-10-24 10:45:23 -0700953 while ((de = readdir(d))) {
954 const char *name = de->d_name;
955 /* always skip "." and ".." */
956 if (name[0] == '.') {
957 if (name[1] == 0) continue;
958 if ((name[1] == '.') && (name[2] == 0)) continue;
959 }
Dave Allisond9370732014-01-30 14:19:23 -0800960
Mike Lockwood94afecf2012-10-24 10:45:23 -0700961 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
962 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
963 continue;
964 }
Dave Allisond9370732014-01-30 14:19:23 -0800965
Mike Lockwood94afecf2012-10-24 10:45:23 -0700966 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
967 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
968 continue;
969 }
Dave Allisond9370732014-01-30 14:19:23 -0800970
Mike Lockwood94afecf2012-10-24 10:45:23 -0700971 srcpath[srcend] = dstpath[dstend] = '/';
972 strcpy(srcpath+srcend+1, name);
973 strcpy(dstpath+dstend+1, name);
Dave Allisond9370732014-01-30 14:19:23 -0800974
Mike Lockwood94afecf2012-10-24 10:45:23 -0700975 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
976 res = 1;
977 }
Dave Allisond9370732014-01-30 14:19:23 -0800978
Mike Lockwood94afecf2012-10-24 10:45:23 -0700979 // Note: we will be leaving empty directories behind in srcpath,
980 // but that is okay, the package manager will be erasing all of the
981 // data associated with .apks that disappear.
Dave Allisond9370732014-01-30 14:19:23 -0800982
Mike Lockwood94afecf2012-10-24 10:45:23 -0700983 srcpath[srcend] = dstpath[dstend] = 0;
984 }
Dave Allisond9370732014-01-30 14:19:23 -0800985
Mike Lockwood94afecf2012-10-24 10:45:23 -0700986 closedir(d);
987 return res;
988}
989
990int movefiles()
991{
992 DIR *d;
993 int dfd, subfd;
994 struct dirent *de;
995 struct stat s;
996 char buf[PKG_PATH_MAX+1];
997 int bufp, bufe, bufi, readlen;
998
999 char srcpkg[PKG_NAME_MAX];
1000 char dstpkg[PKG_NAME_MAX];
1001 char srcpath[PKG_PATH_MAX];
1002 char dstpath[PKG_PATH_MAX];
1003 int dstuid=-1, dstgid=-1;
1004 int hasspace;
1005
1006 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
1007 if (d == NULL) {
1008 goto done;
1009 }
1010 dfd = dirfd(d);
1011
1012 /* Iterate through all files in the directory, executing the
1013 * file movements requested there-in.
1014 */
1015 while ((de = readdir(d))) {
1016 const char *name = de->d_name;
1017
1018 if (de->d_type == DT_DIR) {
1019 continue;
1020 } else {
1021 subfd = openat(dfd, name, O_RDONLY);
1022 if (subfd < 0) {
1023 ALOGW("Unable to open update commands at %s%s\n",
1024 UPDATE_COMMANDS_DIR_PREFIX, name);
1025 continue;
1026 }
Dave Allisond9370732014-01-30 14:19:23 -08001027
Mike Lockwood94afecf2012-10-24 10:45:23 -07001028 bufp = 0;
1029 bufe = 0;
1030 buf[PKG_PATH_MAX] = 0;
1031 srcpkg[0] = dstpkg[0] = 0;
1032 while (1) {
1033 bufi = bufp;
1034 while (bufi < bufe && buf[bufi] != '\n') {
1035 bufi++;
1036 }
1037 if (bufi < bufe) {
1038 buf[bufi] = 0;
1039 ALOGV("Processing line: %s\n", buf+bufp);
1040 hasspace = 0;
1041 while (bufp < bufi && isspace(buf[bufp])) {
1042 hasspace = 1;
1043 bufp++;
1044 }
1045 if (buf[bufp] == '#' || bufp == bufi) {
1046 // skip comments and empty lines.
1047 } else if (hasspace) {
1048 if (dstpkg[0] == 0) {
1049 ALOGW("Path before package line in %s%s: %s\n",
1050 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1051 } else if (srcpkg[0] == 0) {
1052 // Skip -- source package no longer exists.
1053 } else {
1054 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
1055 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
1056 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
1057 movefileordir(srcpath, dstpath,
1058 strlen(dstpath)-strlen(buf+bufp),
1059 dstuid, dstgid, &s);
1060 }
1061 }
1062 } else {
1063 char* div = strchr(buf+bufp, ':');
1064 if (div == NULL) {
1065 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
1066 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1067 } else {
1068 *div = 0;
1069 div++;
1070 if (strlen(buf+bufp) < PKG_NAME_MAX) {
1071 strcpy(dstpkg, buf+bufp);
1072 } else {
1073 srcpkg[0] = dstpkg[0] = 0;
1074 ALOGW("Package name too long in %s%s: %s\n",
1075 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
1076 }
1077 if (strlen(div) < PKG_NAME_MAX) {
1078 strcpy(srcpkg, div);
1079 } else {
1080 srcpkg[0] = dstpkg[0] = 0;
1081 ALOGW("Package name too long in %s%s: %s\n",
1082 UPDATE_COMMANDS_DIR_PREFIX, name, div);
1083 }
1084 if (srcpkg[0] != 0) {
1085 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
1086 if (lstat(srcpath, &s) < 0) {
1087 // Package no longer exists -- skip.
1088 srcpkg[0] = 0;
1089 }
1090 } else {
1091 srcpkg[0] = 0;
1092 ALOGW("Can't create path %s in %s%s\n",
1093 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1094 }
1095 if (srcpkg[0] != 0) {
1096 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
1097 if (lstat(dstpath, &s) == 0) {
1098 dstuid = s.st_uid;
1099 dstgid = s.st_gid;
1100 } else {
1101 // Destination package doesn't
1102 // exist... due to original-package,
1103 // this is normal, so don't be
1104 // noisy about it.
1105 srcpkg[0] = 0;
1106 }
1107 } else {
1108 srcpkg[0] = 0;
1109 ALOGW("Can't create path %s in %s%s\n",
1110 div, UPDATE_COMMANDS_DIR_PREFIX, name);
1111 }
1112 }
1113 ALOGV("Transfering from %s to %s: uid=%d\n",
1114 srcpkg, dstpkg, dstuid);
1115 }
1116 }
1117 }
1118 bufp = bufi+1;
1119 } else {
1120 if (bufp == 0) {
1121 if (bufp < bufe) {
1122 ALOGW("Line too long in %s%s, skipping: %s\n",
1123 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1124 }
1125 } else if (bufp < bufe) {
1126 memcpy(buf, buf+bufp, bufe-bufp);
1127 bufe -= bufp;
1128 bufp = 0;
1129 }
1130 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1131 if (readlen < 0) {
1132 ALOGW("Failure reading update commands in %s%s: %s\n",
1133 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1134 break;
1135 } else if (readlen == 0) {
1136 break;
1137 }
1138 bufe += readlen;
1139 buf[bufe] = 0;
1140 ALOGV("Read buf: %s\n", buf);
1141 }
1142 }
1143 close(subfd);
1144 }
1145 }
1146 closedir(d);
1147done:
1148 return 0;
1149}
1150
1151int linklib(const char* pkgname, const char* asecLibDir, int userId)
1152{
1153 char pkgdir[PKG_PATH_MAX];
1154 char libsymlink[PKG_PATH_MAX];
1155 struct stat s, libStat;
1156 int rc = 0;
1157
1158 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1159 ALOGE("cannot create package path\n");
1160 return -1;
1161 }
1162 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1163 ALOGE("cannot create package lib symlink origin path\n");
1164 return -1;
1165 }
1166
1167 if (stat(pkgdir, &s) < 0) return -1;
1168
1169 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1170 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1171 return -1;
1172 }
1173
1174 if (chmod(pkgdir, 0700) < 0) {
1175 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1176 rc = -1;
1177 goto out;
1178 }
1179
1180 if (lstat(libsymlink, &libStat) < 0) {
1181 if (errno != ENOENT) {
1182 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1183 rc = -1;
1184 goto out;
1185 }
1186 } else {
1187 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001188 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Mike Lockwood94afecf2012-10-24 10:45:23 -07001189 rc = -1;
1190 goto out;
1191 }
1192 } else if (S_ISLNK(libStat.st_mode)) {
1193 if (unlink(libsymlink) < 0) {
1194 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1195 rc = -1;
1196 goto out;
1197 }
1198 }
1199 }
1200
1201 if (symlink(asecLibDir, libsymlink) < 0) {
1202 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1203 strerror(errno));
1204 rc = -errno;
1205 goto out;
1206 }
1207
1208out:
1209 if (chmod(pkgdir, s.st_mode) < 0) {
1210 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1211 rc = -errno;
1212 }
1213
1214 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1215 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));
1216 return -errno;
1217 }
1218
1219 return rc;
1220}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001221
1222static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1223{
1224 static const char *IDMAP_BIN = "/system/bin/idmap";
1225 static const size_t MAX_INT_LEN = 32;
1226 char idmap_str[MAX_INT_LEN];
1227
1228 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1229
1230 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1231 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1232}
1233
1234// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1235// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1236static int flatten_path(const char *prefix, const char *suffix,
1237 const char *overlay_path, char *idmap_path, size_t N)
1238{
1239 if (overlay_path == NULL || idmap_path == NULL) {
1240 return -1;
1241 }
1242 const size_t len_overlay_path = strlen(overlay_path);
1243 // will access overlay_path + 1 further below; requires absolute path
1244 if (len_overlay_path < 2 || *overlay_path != '/') {
1245 return -1;
1246 }
1247 const size_t len_idmap_root = strlen(prefix);
1248 const size_t len_suffix = strlen(suffix);
1249 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1250 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1251 // additions below would cause overflow
1252 return -1;
1253 }
1254 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1255 return -1;
1256 }
1257 memset(idmap_path, 0, N);
1258 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1259 char *ch = idmap_path + len_idmap_root;
1260 while (*ch != '\0') {
1261 if (*ch == '/') {
1262 *ch = '@';
1263 }
1264 ++ch;
1265 }
1266 return 0;
1267}
1268
1269int idmap(const char *target_apk, const char *overlay_apk, uid_t uid)
1270{
1271 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1272
1273 int idmap_fd = -1;
1274 char idmap_path[PATH_MAX];
1275
1276 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1277 idmap_path, sizeof(idmap_path)) == -1) {
1278 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1279 goto fail;
1280 }
1281
1282 unlink(idmap_path);
1283 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1284 if (idmap_fd < 0) {
1285 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1286 goto fail;
1287 }
1288 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1289 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1290 goto fail;
1291 }
1292 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1293 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1294 goto fail;
1295 }
1296
1297 pid_t pid;
1298 pid = fork();
1299 if (pid == 0) {
1300 /* child -- drop privileges before continuing */
1301 if (setgid(uid) != 0) {
1302 ALOGE("setgid(%d) failed during idmap\n", uid);
1303 exit(1);
1304 }
1305 if (setuid(uid) != 0) {
1306 ALOGE("setuid(%d) failed during idmap\n", uid);
1307 exit(1);
1308 }
1309 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1310 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1311 exit(1);
1312 }
1313
1314 run_idmap(target_apk, overlay_apk, idmap_fd);
1315 exit(1); /* only if exec call to idmap failed */
1316 } else {
1317 int status = wait_child(pid);
1318 if (status != 0) {
1319 ALOGE("idmap failed, status=0x%04x\n", status);
1320 goto fail;
1321 }
1322 }
1323
1324 close(idmap_fd);
1325 return 0;
1326fail:
1327 if (idmap_fd >= 0) {
1328 close(idmap_fd);
1329 unlink(idmap_path);
1330 }
1331 return -1;
1332}
Robert Craige9887e42014-02-20 10:25:56 -05001333
Robert Craigda30dc72014-03-27 10:21:12 -04001334int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid)
Robert Craige9887e42014-02-20 10:25:56 -05001335{
Robert Craigda30dc72014-03-27 10:21:12 -04001336 struct dirent *entry;
1337 DIR *d;
1338 struct stat s;
1339 char *userdir;
1340 char *primarydir;
1341 char *pkgdir;
Robert Craige9887e42014-02-20 10:25:56 -05001342 int ret = 0;
1343
Robert Craigda30dc72014-03-27 10:21:12 -04001344 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
1345 unsigned int flags = SELINUX_ANDROID_RESTORECON_RECURSE;
1346
1347 if (!pkgName || !seinfo) {
1348 ALOGE("Package name or seinfo tag is null when trying to restorecon.");
Robert Craige9887e42014-02-20 10:25:56 -05001349 return -1;
1350 }
1351
Robert Craigda30dc72014-03-27 10:21:12 -04001352 if (asprintf(&primarydir, "%s%s%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgName) < 0) {
1353 return -1;
1354 }
1355
1356 // Relabel for primary user.
1357 if (selinux_android_restorecon_pkgdir(primarydir, seinfo, uid, flags) < 0) {
1358 ALOGE("restorecon failed for %s: %s\n", primarydir, strerror(errno));
Robert Craige9887e42014-02-20 10:25:56 -05001359 ret |= -1;
1360 }
1361
Robert Craigda30dc72014-03-27 10:21:12 -04001362 if (asprintf(&userdir, "%s%s", android_data_dir.path, SECONDARY_USER_PREFIX) < 0) {
1363 free(primarydir);
1364 return -1;
Robert Craige9887e42014-02-20 10:25:56 -05001365 }
1366
Robert Craigda30dc72014-03-27 10:21:12 -04001367 // Relabel package directory for all secondary users.
1368 d = opendir(userdir);
1369 if (d == NULL) {
1370 free(primarydir);
1371 free(userdir);
1372 return -1;
1373 }
1374
1375 while ((entry = readdir(d))) {
1376 if (entry->d_type != DT_DIR) {
1377 continue;
1378 }
1379
1380 const char *user = entry->d_name;
1381 // Ignore "." and ".."
1382 if (!strcmp(user, ".") || !strcmp(user, "..")) {
1383 continue;
1384 }
1385
1386 // user directories start with a number
1387 if (user[0] < '0' || user[0] > '9') {
1388 ALOGE("Expecting numbered directory during restorecon. Instead got '%s'.", user);
1389 continue;
1390 }
1391
1392 if (asprintf(&pkgdir, "%s%s/%s", userdir, user, pkgName) < 0) {
1393 continue;
1394 }
1395
1396 if (stat(pkgdir, &s) < 0) {
1397 free(pkgdir);
1398 continue;
1399 }
1400
1401 if (selinux_android_restorecon_pkgdir(pkgdir, seinfo, uid, flags) < 0) {
1402 ALOGE("restorecon failed for %s: %s\n", pkgdir, strerror(errno));
1403 ret |= -1;
1404 }
1405 free(pkgdir);
1406 }
1407
1408 closedir(d);
1409 free(primarydir);
1410 free(userdir);
Robert Craige9887e42014-02-20 10:25:56 -05001411 return ret;
1412}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001413
1414static int prune_dex_exclusion_predicate(const char *file_name, const int is_dir)
1415{
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001416 // Exclude all directories. The top level command will be
1417 // given a list of ISA specific directories that are assumed
1418 // to be flat.
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001419 if (is_dir) {
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001420 return 1;
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001421 }
1422
1423
1424 // Don't exclude regular files that start with the list
1425 // of prefixes.
1426 static const char data_app_prefix[] = "data@app@";
1427 static const char data_priv_app_prefix[] = "data@priv-app@";
1428 if (!strncmp(file_name, data_app_prefix, sizeof(data_app_prefix) - 1) ||
1429 !strncmp(file_name, data_priv_app_prefix, sizeof(data_priv_app_prefix) - 1)) {
1430 return 0;
1431 }
1432
1433 // Exclude all regular files that don't start with the prefix "data@app@" or
1434 // "data@priv-app@".
1435 return 1;
1436}
1437
Narayan Kamath1e57e4a2014-06-17 12:54:16 +01001438int prune_dex_cache(const char* subdir) {
1439 // "." is handled as a special case, and refers to
1440 // DALVIK_CACHE_PREFIX (usually /data/dalvik-cache).
1441 const bool is_dalvik_cache_root = !strcmp(subdir, ".");
1442
1443 // Don't allow the path to contain "." or ".." except for the
1444 // special case above. This is much stricter than we need to be,
1445 // but there's no good reason to support them.
1446 if (strchr(subdir, '.' ) != NULL && !is_dalvik_cache_root) {
1447 return -1;
1448 }
1449
1450 if (!is_dalvik_cache_root) {
1451 char full_path[PKG_PATH_MAX];
1452 snprintf(full_path, sizeof(full_path), "%s%s", DALVIK_CACHE_PREFIX, subdir);
1453 return delete_dir_contents(full_path, 0, &prune_dex_exclusion_predicate);
1454 }
1455
1456
1457 // When subdir == ".", clean the contents of the top level
1458 // dalvik-cache directory.
1459 return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate);
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001460}