2 ** Copyright 2008, The Android Open Source Project
4 ** 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
8 ** http://www.apache.org/licenses/LICENSE-2.0
10 ** 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
14 ** limitations under the License.
17 #include <sys/capability.h>
19 #include <diskusage/dirsize.h>
20 #include <selinux/android.h>
22 /* Directory records that are used in execution of commands. */
23 dir_rec_t android_data_dir;
24 dir_rec_t android_asec_dir;
25 dir_rec_t android_app_dir;
26 dir_rec_t android_app_private_dir;
27 dir_rec_t android_app_lib_dir;
28 dir_rec_t android_media_dir;
29 dir_rec_array_t android_system_dirs;
31 int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo)
33 char pkgdir[PKG_PATH_MAX];
34 char libsymlink[PKG_PATH_MAX];
35 char applibdir[PKG_PATH_MAX];
38 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
39 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
43 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
44 ALOGE("cannot create package path\n");
48 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) {
49 ALOGE("cannot create package lib symlink origin path\n");
53 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
54 ALOGE("cannot create package lib symlink dest path\n");
58 if (mkdir(pkgdir, 0751) < 0) {
59 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
62 if (chmod(pkgdir, 0751) < 0) {
63 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
68 if (lstat(libsymlink, &libStat) < 0) {
69 if (errno != ENOENT) {
70 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
74 if (S_ISDIR(libStat.st_mode)) {
75 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
76 ALOGE("couldn't delete lib directory during install for: %s", libsymlink);
79 } else if (S_ISLNK(libStat.st_mode)) {
80 if (unlink(libsymlink) < 0) {
81 ALOGE("couldn't unlink lib directory during install for: %s", libsymlink);
87 if (symlink(applibdir, libsymlink) < 0) {
88 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir,
94 if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) {
95 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
101 if (chown(pkgdir, uid, gid) < 0) {
102 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
111 int uninstall(const char *pkgname, uid_t persona)
113 char pkgdir[PKG_PATH_MAX];
115 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
118 /* delete contents AND directory, no exceptions */
119 return delete_dir_contents(pkgdir, 1, NULL);
122 int renamepkg(const char *oldpkgname, const char *newpkgname)
124 char oldpkgdir[PKG_PATH_MAX];
125 char newpkgdir[PKG_PATH_MAX];
127 if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0))
129 if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0))
132 if (rename(oldpkgdir, newpkgdir) < 0) {
133 ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno));
139 int fix_uid(const char *pkgname, uid_t uid, gid_t gid)
141 char pkgdir[PKG_PATH_MAX];
145 if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) {
146 ALOGE("invalid uid/gid: %d %d\n", uid, gid);
150 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) {
151 ALOGE("cannot create package path\n");
155 if (stat(pkgdir, &s) < 0) return -1;
157 if (s.st_uid != 0 || s.st_gid != 0) {
158 ALOGE("fixing uid of non-root pkg: %s %lu %lu\n", pkgdir, s.st_uid, s.st_gid);
162 if (chmod(pkgdir, 0751) < 0) {
163 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
167 if (chown(pkgdir, uid, gid) < 0) {
168 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
176 int delete_user_data(const char *pkgname, uid_t persona)
178 char pkgdir[PKG_PATH_MAX];
180 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona))
183 /* delete contents, excluding "lib", but not the directory itself */
184 return delete_dir_contents(pkgdir, 0, "lib");
187 int make_user_data(const char *pkgname, uid_t uid, uid_t persona)
189 char pkgdir[PKG_PATH_MAX];
190 char applibdir[PKG_PATH_MAX];
191 char libsymlink[PKG_PATH_MAX];
194 // Create the data dir for the package
195 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) {
198 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, persona)) {
199 ALOGE("cannot create package lib symlink origin path\n");
202 if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) {
203 ALOGE("cannot create package lib symlink dest path\n");
207 if (mkdir(pkgdir, 0751) < 0) {
208 ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno));
211 if (chmod(pkgdir, 0751) < 0) {
212 ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno));
217 if (lstat(libsymlink, &libStat) < 0) {
218 if (errno != ENOENT) {
219 ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno));
224 if (S_ISDIR(libStat.st_mode)) {
225 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
226 ALOGE("couldn't delete lib directory during install for non-primary: %s",
231 } else if (S_ISLNK(libStat.st_mode)) {
232 if (unlink(libsymlink) < 0) {
233 ALOGE("couldn't unlink lib directory during install for non-primary: %s",
241 if (symlink(applibdir, libsymlink) < 0) {
242 ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink,
243 applibdir, strerror(errno));
248 if (selinux_android_setfilecon(pkgdir, pkgname, uid) < 0) {
249 ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno));
255 if (chown(pkgdir, uid, uid) < 0) {
256 ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno));
265 int delete_persona(uid_t persona)
267 char data_path[PKG_PATH_MAX];
268 if (create_persona_path(data_path, persona)) {
271 if (delete_dir_contents(data_path, 1, NULL)) {
275 char media_path[PATH_MAX];
276 if (create_persona_media_path(media_path, (userid_t) persona) == -1) {
279 if (delete_dir_contents(media_path, 1, NULL) == -1) {
286 int delete_cache(const char *pkgname, uid_t persona)
288 char cachedir[PKG_PATH_MAX];
290 if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona))
293 /* delete contents, not the directory, no exceptions */
294 return delete_dir_contents(cachedir, 0, 0);
297 /* Try to ensure free_size bytes of storage are available.
298 * Returns 0 on success.
299 * This is rather simple-minded because doing a full LRU would
300 * be potentially memory-intensive, and without atime it would
301 * also require that apps constantly modify file metadata even
302 * when just reading from the cache, which is pretty awful.
304 int free_cache(int64_t free_size)
310 char tmpdir[PATH_MAX];
313 avail = data_disk_free();
314 if (avail < 0) return -1;
316 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", free_size, avail);
317 if (avail >= free_size) return 0;
319 cache = start_cache_collection();
321 // Collect cache files for primary user.
322 if (create_persona_path(tmpdir, 0) == 0) {
323 //ALOGI("adding cache files from %s\n", tmpdir);
324 add_cache_files(cache, tmpdir, "cache");
327 // Search for other users and add any cache files from them.
328 snprintf(tmpdir, sizeof(tmpdir), "%s%s", android_data_dir.path,
329 SECONDARY_USER_PREFIX);
330 dirpos = tmpdir + strlen(tmpdir);
333 while ((de = readdir(d))) {
334 if (de->d_type == DT_DIR) {
335 const char *name = de->d_name;
336 /* always skip "." and ".." */
337 if (name[0] == '.') {
338 if (name[1] == 0) continue;
339 if ((name[1] == '.') && (name[2] == 0)) continue;
341 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
342 strcpy(dirpos, name);
343 //ALOGI("adding cache files from %s\n", tmpdir);
344 add_cache_files(cache, tmpdir, "cache");
346 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
353 // Collect cache files on external storage for all users (if it is mounted as part
354 // of the internal storage).
355 strcpy(tmpdir, android_media_dir.path);
356 dirpos = tmpdir + strlen(tmpdir);
359 while ((de = readdir(d))) {
360 if (de->d_type == DT_DIR) {
361 const char *name = de->d_name;
362 /* skip any dir that doesn't start with a number, so not a user */
363 if (name[0] < '0' || name[0] > '9') {
366 if ((strlen(name)+(dirpos-tmpdir)) < (sizeof(tmpdir)-1)) {
367 strcpy(dirpos, name);
368 if (lookup_media_dir(tmpdir, "Android") == 0
369 && lookup_media_dir(tmpdir, "data") == 0) {
370 //ALOGI("adding cache files from %s\n", tmpdir);
371 add_cache_files(cache, tmpdir, "cache");
374 ALOGW("Path exceeds limit: %s%s", tmpdir, name);
381 clear_cache_files(cache, free_size);
382 finish_cache_collection(cache);
384 return data_disk_free() >= free_size ? 0 : -1;
387 int move_dex(const char *src, const char *dst)
389 char src_dex[PKG_PATH_MAX];
390 char dst_dex[PKG_PATH_MAX];
392 if (validate_apk_path(src)) return -1;
393 if (validate_apk_path(dst)) return -1;
395 if (create_cache_path(src_dex, src)) return -1;
396 if (create_cache_path(dst_dex, dst)) return -1;
398 ALOGV("move %s -> %s\n", src_dex, dst_dex);
399 if (rename(src_dex, dst_dex) < 0) {
400 ALOGE("Couldn't move %s: %s\n", src_dex, strerror(errno));
407 int rm_dex(const char *path)
409 char dex_path[PKG_PATH_MAX];
411 if (validate_apk_path(path)) return -1;
412 if (create_cache_path(dex_path, path)) return -1;
414 ALOGV("unlink %s\n", dex_path);
415 if (unlink(dex_path) < 0) {
416 ALOGE("Couldn't unlink %s: %s\n", dex_path, strerror(errno));
423 int get_size(const char *pkgname, int persona, const char *apkpath,
424 const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
425 int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
432 char path[PKG_PATH_MAX];
434 int64_t codesize = 0;
435 int64_t datasize = 0;
436 int64_t cachesize = 0;
437 int64_t asecsize = 0;
439 /* count the source apk as code -- but only if it's not
440 * on the /system partition and its not on the sdcard.
442 if (validate_system_app_path(apkpath) &&
443 strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
444 if (stat(apkpath, &s) == 0) {
445 codesize += stat_size(&s);
448 /* count the forward locked apk as code if it is given
450 if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
451 if (stat(fwdlock_apkpath, &s) == 0) {
452 codesize += stat_size(&s);
455 /* count the cached dexfile as code */
456 if (!create_cache_path(path, apkpath)) {
457 if (stat(path, &s) == 0) {
458 codesize += stat_size(&s);
462 /* add in size of any libraries */
463 if (libdirpath != NULL && libdirpath[0] != '!') {
464 d = opendir(libdirpath);
467 codesize += calculate_dir_size(dfd);
472 /* compute asec size if it is given
474 if (asecpath != NULL && asecpath[0] != '!') {
475 if (stat(asecpath, &s) == 0) {
476 asecsize += stat_size(&s);
480 if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) {
490 /* most stuff in the pkgdir is data, except for the "cache"
491 * directory and below, which is cache, and the "lib" directory
492 * and below, which is code...
494 while ((de = readdir(d))) {
495 const char *name = de->d_name;
497 if (de->d_type == DT_DIR) {
499 int64_t statsize = 0;
501 /* always skip "." and ".." */
502 if (name[0] == '.') {
503 if (name[1] == 0) continue;
504 if ((name[1] == '.') && (name[2] == 0)) continue;
506 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
507 statsize = stat_size(&s);
509 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
511 dirsize = calculate_dir_size(subfd);
513 if(!strcmp(name,"lib")) {
514 codesize += dirsize + statsize;
515 } else if(!strcmp(name,"cache")) {
516 cachesize += dirsize + statsize;
518 datasize += dirsize + statsize;
520 } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
521 // This is the symbolic link to the application's library
522 // code. We'll count this as code instead of data, since
523 // it is not something that the app creates.
524 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
525 codesize += stat_size(&s);
528 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
529 datasize += stat_size(&s);
535 *_codesize = codesize;
536 *_datasize = datasize;
537 *_cachesize = cachesize;
538 *_asecsize = asecsize;
543 int create_cache_path(char path[PKG_PATH_MAX], const char *src)
549 srclen = strlen(src);
551 /* demand that we are an absolute path */
552 if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
556 if (srclen > PKG_PATH_MAX) { // XXX: PKG_NAME_MAX?
560 dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
561 strlen(DALVIK_CACHE_POSTFIX) + 1;
563 if (dstlen > PKG_PATH_MAX) {
567 sprintf(path,"%s%s%s",
569 src + 1, /* skip the leading / */
570 DALVIK_CACHE_POSTFIX);
572 for(tmp = path + strlen(DALVIK_CACHE_PREFIX); *tmp; tmp++) {
581 static void run_dexopt(int zip_fd, int odex_fd, const char* input_file_name,
582 const char* output_file_name, const char* dexopt_flags)
584 static const char* DEX_OPT_BIN = "/system/bin/dexopt";
585 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
586 char zip_num[MAX_INT_LEN];
587 char odex_num[MAX_INT_LEN];
589 sprintf(zip_num, "%d", zip_fd);
590 sprintf(odex_num, "%d", odex_fd);
592 ALOGV("Running %s in=%s out=%s\n", DEX_OPT_BIN, input_file_name, output_file_name);
593 execl(DEX_OPT_BIN, DEX_OPT_BIN, "--zip", zip_num, odex_num, input_file_name,
594 dexopt_flags, (char*) NULL);
595 ALOGE("execl(%s) failed: %s\n", DEX_OPT_BIN, strerror(errno));
598 static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name,
599 const char* output_file_name, const char* dexopt_flags)
601 static const char* DEX2OAT_BIN = "/system/bin/dex2oat";
602 static const int MAX_INT_LEN = 12; // '-'+10dig+'\0' -OR- 0x+8dig
603 char zip_fd_arg[strlen("--zip-fd=") + MAX_INT_LEN];
604 char zip_location_arg[strlen("--zip-location=") + PKG_PATH_MAX];
605 char oat_fd_arg[strlen("--oat-fd=") + MAX_INT_LEN];
606 char oat_location_arg[strlen("--oat-name=") + PKG_PATH_MAX];
608 sprintf(zip_fd_arg, "--zip-fd=%d", zip_fd);
609 sprintf(zip_location_arg, "--zip-location=%s", input_file_name);
610 sprintf(oat_fd_arg, "--oat-fd=%d", oat_fd);
611 sprintf(oat_location_arg, "--oat-location=%s", output_file_name);
613 ALOGV("Running %s in=%s out=%s\n", DEX2OAT_BIN, input_file_name, output_file_name);
614 execl(DEX2OAT_BIN, DEX2OAT_BIN,
615 zip_fd_arg, zip_location_arg,
616 oat_fd_arg, oat_location_arg,
618 ALOGE("execl(%s) failed: %s\n", DEX2OAT_BIN, strerror(errno));
621 static int wait_dexopt(pid_t pid, const char* apk_path)
627 * Wait for the optimization process to finish.
630 got_pid = waitpid(pid, &status, 0);
631 if (got_pid == -1 && errno == EINTR) {
632 printf("waitpid interrupted, retrying\n");
637 if (got_pid != pid) {
638 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
639 (int) pid, (int) got_pid, strerror(errno));
643 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
644 ALOGV("DexInv: --- END '%s' (success) ---\n", apk_path);
647 ALOGW("DexInv: --- END '%s' --- status=0x%04x, process failed\n",
649 return status; /* always nonzero */
653 int dexopt(const char *apk_path, uid_t uid, int is_public)
656 struct stat apk_stat, dex_stat;
657 char out_path[PKG_PATH_MAX];
658 char dexopt_flags[PROPERTY_VALUE_MAX];
659 char persist_sys_dalvik_vm_lib[PROPERTY_VALUE_MAX];
661 int res, zip_fd=-1, out_fd=-1;
663 if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
667 /* platform-specific flags affecting optimization and verification */
668 property_get("dalvik.vm.dexopt-flags", dexopt_flags, "");
669 ALOGV("dalvik.vm.dexopt_flags=%s\n", dexopt_flags);
671 /* The command to run depend ones the value of persist.sys.dalvik.vm.lib */
672 property_get("persist.sys.dalvik.vm.lib", persist_sys_dalvik_vm_lib, "libdvm.so");
674 /* Before anything else: is there a .odex file? If so, we have
675 * precompiled the apk and there is nothing to do here.
677 sprintf(out_path, "%s%s", apk_path, ".odex");
678 if (stat(out_path, &dex_stat) == 0) {
682 if (create_cache_path(out_path, apk_path)) {
686 memset(&apk_stat, 0, sizeof(apk_stat));
687 stat(apk_path, &apk_stat);
689 zip_fd = open(apk_path, O_RDONLY, 0);
691 ALOGE("installd cannot open '%s' for input during dexopt\n", apk_path);
696 out_fd = open(out_path, O_RDWR | O_CREAT | O_EXCL, 0644);
698 ALOGE("installd cannot open '%s' for output during dexopt\n", out_path);
702 S_IRUSR|S_IWUSR|S_IRGRP |
703 (is_public ? S_IROTH : 0)) < 0) {
704 ALOGE("installd cannot chmod '%s' during dexopt\n", out_path);
707 if (fchown(out_fd, AID_SYSTEM, uid) < 0) {
708 ALOGE("installd cannot chown '%s' during dexopt\n", out_path);
712 ALOGV("DexInv: --- BEGIN '%s' ---\n", apk_path);
717 /* child -- drop privileges before continuing */
718 if (setgid(uid) != 0) {
719 ALOGE("setgid(%d) failed in installd during dexopt\n", uid);
722 if (setuid(uid) != 0) {
723 ALOGE("setuid(%d) failed in installd during dexopt\n", uid);
727 struct __user_cap_header_struct capheader;
728 struct __user_cap_data_struct capdata[2];
729 memset(&capheader, 0, sizeof(capheader));
730 memset(&capdata, 0, sizeof(capdata));
731 capheader.version = _LINUX_CAPABILITY_VERSION_3;
732 if (capset(&capheader, &capdata[0]) < 0) {
733 ALOGE("capset failed: %s\n", strerror(errno));
736 if (flock(out_fd, LOCK_EX | LOCK_NB) != 0) {
737 ALOGE("flock(%s) failed: %s\n", out_path, strerror(errno));
741 if (strncmp(persist_sys_dalvik_vm_lib, "libdvm", 6) == 0) {
742 run_dexopt(zip_fd, out_fd, apk_path, out_path, dexopt_flags);
743 } else if (strncmp(persist_sys_dalvik_vm_lib, "libart", 6) == 0) {
744 run_dex2oat(zip_fd, out_fd, apk_path, out_path, dexopt_flags);
746 exit(69); /* Unexpected persist.sys.dalvik.vm.lib value */
748 exit(68); /* only get here on exec failure */
750 res = wait_dexopt(pid, apk_path);
752 ALOGE("dexopt in='%s' out='%s' res=%d\n", apk_path, out_path, res);
757 ut.actime = apk_stat.st_atime;
758 ut.modtime = apk_stat.st_mtime;
759 utime(out_path, &ut);
776 void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
777 struct stat* statbuf)
779 while (path[basepos] != 0) {
780 if (path[basepos] == '/') {
782 if (lstat(path, statbuf) < 0) {
783 ALOGV("Making directory: %s\n", path);
784 if (mkdir(path, mode) == 0) {
785 chown(path, uid, gid);
787 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
797 int movefileordir(char* srcpath, char* dstpath, int dstbasepos,
798 int dstuid, int dstgid, struct stat* statbuf)
804 int srcend = strlen(srcpath);
805 int dstend = strlen(dstpath);
807 if (lstat(srcpath, statbuf) < 0) {
808 ALOGW("Unable to stat %s: %s\n", srcpath, strerror(errno));
812 if ((statbuf->st_mode&S_IFDIR) == 0) {
813 mkinnerdirs(dstpath, dstbasepos, S_IRWXU|S_IRWXG|S_IXOTH,
814 dstuid, dstgid, statbuf);
815 ALOGV("Renaming %s to %s (uid %d)\n", srcpath, dstpath, dstuid);
816 if (rename(srcpath, dstpath) >= 0) {
817 if (chown(dstpath, dstuid, dstgid) < 0) {
818 ALOGE("cannot chown %s: %s\n", dstpath, strerror(errno));
823 ALOGW("Unable to rename %s to %s: %s\n",
824 srcpath, dstpath, strerror(errno));
830 d = opendir(srcpath);
832 ALOGW("Unable to opendir %s: %s\n", srcpath, strerror(errno));
838 while ((de = readdir(d))) {
839 const char *name = de->d_name;
840 /* always skip "." and ".." */
841 if (name[0] == '.') {
842 if (name[1] == 0) continue;
843 if ((name[1] == '.') && (name[2] == 0)) continue;
846 if ((srcend+strlen(name)) >= (PKG_PATH_MAX-2)) {
847 ALOGW("Source path too long; skipping: %s/%s\n", srcpath, name);
851 if ((dstend+strlen(name)) >= (PKG_PATH_MAX-2)) {
852 ALOGW("Destination path too long; skipping: %s/%s\n", dstpath, name);
856 srcpath[srcend] = dstpath[dstend] = '/';
857 strcpy(srcpath+srcend+1, name);
858 strcpy(dstpath+dstend+1, name);
860 if (movefileordir(srcpath, dstpath, dstbasepos, dstuid, dstgid, statbuf) != 0) {
864 // Note: we will be leaving empty directories behind in srcpath,
865 // but that is okay, the package manager will be erasing all of the
866 // data associated with .apks that disappear.
868 srcpath[srcend] = dstpath[dstend] = 0;
881 char buf[PKG_PATH_MAX+1];
882 int bufp, bufe, bufi, readlen;
884 char srcpkg[PKG_NAME_MAX];
885 char dstpkg[PKG_NAME_MAX];
886 char srcpath[PKG_PATH_MAX];
887 char dstpath[PKG_PATH_MAX];
888 int dstuid=-1, dstgid=-1;
891 d = opendir(UPDATE_COMMANDS_DIR_PREFIX);
897 /* Iterate through all files in the directory, executing the
898 * file movements requested there-in.
900 while ((de = readdir(d))) {
901 const char *name = de->d_name;
903 if (de->d_type == DT_DIR) {
906 subfd = openat(dfd, name, O_RDONLY);
908 ALOGW("Unable to open update commands at %s%s\n",
909 UPDATE_COMMANDS_DIR_PREFIX, name);
915 buf[PKG_PATH_MAX] = 0;
916 srcpkg[0] = dstpkg[0] = 0;
919 while (bufi < bufe && buf[bufi] != '\n') {
924 ALOGV("Processing line: %s\n", buf+bufp);
926 while (bufp < bufi && isspace(buf[bufp])) {
930 if (buf[bufp] == '#' || bufp == bufi) {
931 // skip comments and empty lines.
932 } else if (hasspace) {
933 if (dstpkg[0] == 0) {
934 ALOGW("Path before package line in %s%s: %s\n",
935 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
936 } else if (srcpkg[0] == 0) {
937 // Skip -- source package no longer exists.
939 ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg);
940 if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) &&
941 !create_move_path(dstpath, dstpkg, buf+bufp, 0)) {
942 movefileordir(srcpath, dstpath,
943 strlen(dstpath)-strlen(buf+bufp),
948 char* div = strchr(buf+bufp, ':');
950 ALOGW("Bad package spec in %s%s; no ':' sep: %s\n",
951 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
955 if (strlen(buf+bufp) < PKG_NAME_MAX) {
956 strcpy(dstpkg, buf+bufp);
958 srcpkg[0] = dstpkg[0] = 0;
959 ALOGW("Package name too long in %s%s: %s\n",
960 UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp);
962 if (strlen(div) < PKG_NAME_MAX) {
965 srcpkg[0] = dstpkg[0] = 0;
966 ALOGW("Package name too long in %s%s: %s\n",
967 UPDATE_COMMANDS_DIR_PREFIX, name, div);
969 if (srcpkg[0] != 0) {
970 if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) {
971 if (lstat(srcpath, &s) < 0) {
972 // Package no longer exists -- skip.
977 ALOGW("Can't create path %s in %s%s\n",
978 div, UPDATE_COMMANDS_DIR_PREFIX, name);
980 if (srcpkg[0] != 0) {
981 if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) {
982 if (lstat(dstpath, &s) == 0) {
986 // Destination package doesn't
987 // exist... due to original-package,
988 // this is normal, so don't be
994 ALOGW("Can't create path %s in %s%s\n",
995 div, UPDATE_COMMANDS_DIR_PREFIX, name);
998 ALOGV("Transfering from %s to %s: uid=%d\n",
999 srcpkg, dstpkg, dstuid);
1007 ALOGW("Line too long in %s%s, skipping: %s\n",
1008 UPDATE_COMMANDS_DIR_PREFIX, name, buf);
1010 } else if (bufp < bufe) {
1011 memcpy(buf, buf+bufp, bufe-bufp);
1015 readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe);
1017 ALOGW("Failure reading update commands in %s%s: %s\n",
1018 UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno));
1020 } else if (readlen == 0) {
1025 ALOGV("Read buf: %s\n", buf);
1036 int linklib(const char* pkgname, const char* asecLibDir, int userId)
1038 char pkgdir[PKG_PATH_MAX];
1039 char libsymlink[PKG_PATH_MAX];
1040 struct stat s, libStat;
1043 if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) {
1044 ALOGE("cannot create package path\n");
1047 if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) {
1048 ALOGE("cannot create package lib symlink origin path\n");
1052 if (stat(pkgdir, &s) < 0) return -1;
1054 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
1055 ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno));
1059 if (chmod(pkgdir, 0700) < 0) {
1060 ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1065 if (lstat(libsymlink, &libStat) < 0) {
1066 if (errno != ENOENT) {
1067 ALOGE("couldn't stat lib dir: %s\n", strerror(errno));
1072 if (S_ISDIR(libStat.st_mode)) {
1073 if (delete_dir_contents(libsymlink, 1, 0) < 0) {
1077 } else if (S_ISLNK(libStat.st_mode)) {
1078 if (unlink(libsymlink) < 0) {
1079 ALOGE("couldn't unlink lib dir: %s\n", strerror(errno));
1086 if (symlink(asecLibDir, libsymlink) < 0) {
1087 ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir,
1094 if (chmod(pkgdir, s.st_mode) < 0) {
1095 ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno));
1099 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
1100 ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno));