int install(const char *pkgname, uid_t uid, gid_t gid) { char pkgdir[PKG_PATH_MAX]; char libdir[PKG_PATH_MAX]; if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { LOGE("invalid uid/gid: %d %d\n", uid, gid); return -1; } if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { LOGE("cannot create package path\n"); return -1; } if (create_pkg_path(libdir, pkgname, PKG_LIB_POSTFIX, 0)) { LOGE("cannot create package lib path\n"); return -1; } if (mkdir(pkgdir, 0751) < 0) { LOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); return -errno; } if (chmod(pkgdir, 0751) < 0) { LOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } if (chown(pkgdir, uid, gid) < 0) { LOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } if (mkdir(libdir, 0755) < 0) { LOGE("cannot create dir '%s': %s\n", libdir, strerror(errno)); unlink(pkgdir); return -errno; } if (chmod(libdir, 0755) < 0) { LOGE("cannot chmod dir '%s': %s\n", libdir, strerror(errno)); unlink(libdir); unlink(pkgdir); return -errno; } if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) { LOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno)); unlink(libdir); unlink(pkgdir); return -errno; } if (chown(pkgdir, uid, gid) < 0) { LOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(libdir); unlink(pkgdir); return -errno; } return 0; }
int uninstall(const char *pkgname, int encrypted_fs_flag) { char pkgdir[PKG_PATH_MAX]; if (encrypted_fs_flag == USE_UNENCRYPTED_FS) { if (create_pkg_path(pkgdir, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) return -1; } else { if (create_pkg_path(pkgdir, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) return -1; } /* delete contents AND directory, no exceptions */ return delete_dir_contents(pkgdir, 1, 0); }
int delete_cache(const char *pkgname, int encrypted_fs_flag) { char cachedir[PKG_PATH_MAX]; if (encrypted_fs_flag == USE_UNENCRYPTED_FS) { if (create_pkg_path(cachedir, CACHE_DIR_PREFIX, pkgname, CACHE_DIR_POSTFIX)) return -1; } else { if (create_pkg_path(cachedir, CACHE_SEC_DIR_PREFIX, pkgname, CACHE_DIR_POSTFIX)) return -1; } /* delete contents, not the directory, no exceptions */ return delete_dir_contents(cachedir, 0, 0); }
int delete_user_data(const char *pkgname, int encrypted_fs_flag) { char pkgdir[PKG_PATH_MAX]; if (encrypted_fs_flag == USE_UNENCRYPTED_FS) { if (create_pkg_path(pkgdir, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) return -1; } else { if (create_pkg_path(pkgdir, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) return -1; } /* delete contents, excluding "lib", but not the directory itself */ return delete_dir_contents(pkgdir, 0, "lib"); }
int renamepkg(const char *oldpkgname, const char *newpkgname) { char oldpkgdir[PKG_PATH_MAX]; char newpkgdir[PKG_PATH_MAX]; if (create_pkg_path(oldpkgdir, oldpkgname, PKG_DIR_POSTFIX, 0)) return -1; if (create_pkg_path(newpkgdir, newpkgname, PKG_DIR_POSTFIX, 0)) return -1; if (rename(oldpkgdir, newpkgdir) < 0) { ALOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno)); return -errno; } return 0; }
int install(const char *pkgname, int encrypted_fs_flag, uid_t uid, gid_t gid) { char pkgdir[PKG_PATH_MAX]; char libdir[PKG_PATH_MAX]; LOGI("installer: dir: %s pkg: %s", pkgdir, pkgname); if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { LOGE("invalid uid/gid: %d %d\n", uid, gid); return -1; } if (encrypted_fs_flag == USE_UNENCRYPTED_FS) { if (create_pkg_path(pkgdir, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) return -1; if (create_pkg_path(libdir, PKG_LIB_PREFIX, pkgname, PKG_LIB_POSTFIX)) return -1; } else { if (create_pkg_path(pkgdir, PKG_SEC_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) return -1; if (create_pkg_path(libdir, PKG_SEC_LIB_PREFIX, pkgname, PKG_LIB_POSTFIX)) return -1; } if (mkdir(pkgdir, 0751) < 0) { LOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); return -errno; } if (chown(pkgdir, uid, gid) < 0) { LOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } if (mkdir(libdir, 0755) < 0) { LOGE("cannot create dir '%s': %s\n", libdir, strerror(errno)); unlink(pkgdir); return -errno; } if (chown(libdir, AID_SYSTEM, AID_SYSTEM) < 0) { LOGE("cannot chown dir '%s': %s\n", libdir, strerror(errno)); unlink(libdir); unlink(pkgdir); return -errno; } return 0; }
TEST_F(UtilsTest, CreatePkgPath_PrimaryUser) { char path[PKG_PATH_MAX]; EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 0)) << "Should return error because postfix is too long."; EXPECT_STREQ(TEST_DATA_DIR PRIMARY_USER_PREFIX "com.example.package", path) << "Package path should be in /data/data/"; }
TEST_F(UtilsTest, CreatePkgPath_SecondaryUser) { char path[PKG_PATH_MAX]; EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 1)) << "Should successfully create package path."; EXPECT_STREQ(TEST_DATA_DIR SECONDARY_USER_PREFIX "1/com.example.package", path) << "Package path should be in /data/user/"; }
int uninstall(const char *pkgname, uid_t persona) { char pkgdir[PKG_PATH_MAX]; if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) return -1; /* delete contents AND directory, no exceptions */ return delete_dir_contents(pkgdir, 1, NULL); }
int delete_cache(const char *pkgname, uid_t persona) { char cachedir[PKG_PATH_MAX]; if (create_pkg_path(cachedir, pkgname, CACHE_DIR_POSTFIX, persona)) return -1; /* delete contents, not the directory, no exceptions */ return delete_dir_contents(cachedir, 0, 0); }
int delete_user_data(const char *pkgname, uid_t persona) { char pkgdir[PKG_PATH_MAX]; if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) return -1; /* delete contents, excluding "lib", but not the directory itself */ return delete_dir_contents(pkgdir, 0, "lib"); }
TEST_F(UtilsTest, CreatePkgPath_LongPostfixFail) { char path[PKG_PATH_MAX]; // Create long packagename of "aaaaa..." size_t postfixSize = PKG_PATH_MAX; char postfix[postfixSize + 1]; memset(postfix, 'a', postfixSize); postfix[postfixSize] = '\0'; EXPECT_EQ(-1, create_pkg_path(path, "com.example.package", postfix, 0)) << "Should return error because postfix is too long."; }
TEST_F(UtilsTest, CreatePkgPath_LongPkgNameFail) { char path[PKG_PATH_MAX]; // Create long packagename of "aaaaa..." size_t pkgnameSize = PKG_NAME_MAX + 1; char pkgname[pkgnameSize + 1]; memset(pkgname, 'a', pkgnameSize); pkgname[pkgnameSize] = '\0'; EXPECT_EQ(-1, create_pkg_path(path, pkgname, "", 0)) << "Should return error because package name is too long."; }
int renamepkg(const char *oldpkgname, const char *newpkgname, int encrypted_fs_flag) { char oldpkgdir[PKG_PATH_MAX]; char newpkgdir[PKG_PATH_MAX]; if (encrypted_fs_flag == USE_UNENCRYPTED_FS) { if (create_pkg_path(oldpkgdir, PKG_DIR_PREFIX, oldpkgname, PKG_DIR_POSTFIX)) return -1; if (create_pkg_path(newpkgdir, PKG_DIR_PREFIX, newpkgname, PKG_DIR_POSTFIX)) return -1; } else { if (create_pkg_path(oldpkgdir, PKG_SEC_DIR_PREFIX, oldpkgname, PKG_DIR_POSTFIX)) return -1; if (create_pkg_path(newpkgdir, PKG_SEC_DIR_PREFIX, newpkgname, PKG_DIR_POSTFIX)) return -1; } if (rename(oldpkgdir, newpkgdir) < 0) { LOGE("cannot rename dir '%s' to '%s': %s\n", oldpkgdir, newpkgdir, strerror(errno)); return -errno; } return 0; }
int clone_persona_data(uid_t src_persona, uid_t target_persona, int copy) { char src_data_dir[PKG_PATH_MAX]; char pkg_path[PKG_PATH_MAX]; DIR *d; struct dirent *de; struct stat s; uid_t uid; if (create_persona_path(src_data_dir, src_persona)) { return -1; } d = opendir(src_data_dir); if (d != NULL) { while ((de = readdir(d))) { const char *name = de->d_name; if (de->d_type == DT_DIR) { int subfd; /* always skip "." and ".." */ if (name[0] == '.') { if (name[1] == 0) continue; if ((name[1] == '.') && (name[2] == 0)) continue; } /* Create the full path to the package's data dir */ create_pkg_path(pkg_path, name, PKG_DIR_POSTFIX, src_persona); /* Get the file stat */ if (stat(pkg_path, &s) < 0) continue; /* Get the uid of the package */ ALOGI("Adding datadir for uid = %lu\n", s.st_uid); uid = (uid_t) s.st_uid % PER_USER_RANGE; /* Create the directory for the target */ make_user_data(name, uid + target_persona * PER_USER_RANGE, target_persona, NULL); } } closedir(d); } if (ensure_media_user_dirs((userid_t) target_persona) == -1) { return -1; } return 0; }
TEST_F(UtilsTest, CreatePkgPath_LongPkgNameSuccess) { char path[PKG_PATH_MAX]; // Create long packagename of "aaaaa..." size_t pkgnameSize = PKG_NAME_MAX; char pkgname[pkgnameSize + 1]; memset(pkgname, 'a', pkgnameSize); pkgname[pkgnameSize] = '\0'; EXPECT_EQ(0, create_pkg_path(path, pkgname, "", 0)) << "Should successfully be able to create package name."; const char *prefix = TEST_DATA_DIR PRIMARY_USER_PREFIX; size_t offset = strlen(prefix); EXPECT_STREQ(pkgname, path + offset) << "Package path should be a really long string of a's"; }
int make_user_data(const char *pkgname, uid_t uid, uid_t persona) { char pkgdir[PKG_PATH_MAX]; char real_libdir[PKG_PATH_MAX]; // Create the data dir for the package if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) { return -1; } if (mkdir(pkgdir, 0751) < 0) { ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); return -errno; } if (chown(pkgdir, uid, uid) < 0) { ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } return 0; }
int fix_uid(const char *pkgname, uid_t uid, gid_t gid) { char pkgdir[PKG_PATH_MAX]; struct stat s; int rc = 0; if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { ALOGE("invalid uid/gid: %d %d\n", uid, gid); return -1; } if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { ALOGE("cannot create package path\n"); return -1; } if (stat(pkgdir, &s) < 0) return -1; if (s.st_uid != 0 || s.st_gid != 0) { ALOGE("fixing uid of non-root pkg: %s %lu %lu\n", pkgdir, s.st_uid, s.st_gid); return -1; } if (chmod(pkgdir, 0751) < 0) { ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } if (chown(pkgdir, uid, gid) < 0) { ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } return 0; }
int protect(char *pkgname, gid_t gid) { struct stat s; char pkgpath[PKG_PATH_MAX]; if (gid < AID_SYSTEM) return -1; if (create_pkg_path(pkgpath, PROTECTED_DIR_PREFIX, pkgname, ".apk")) return -1; if (stat(pkgpath, &s) < 0) return -1; if (chown(pkgpath, s.st_uid, gid) < 0) { LOGE("failed to chgrp '%s': %s\n", pkgpath, strerror(errno)); return -1; } if (chmod(pkgpath, S_IRUSR|S_IWUSR|S_IRGRP) < 0) { LOGE("failed to chmod '%s': %s\n", pkgpath, strerror(errno)); return -1; } return 0; }
int movefiles() { DIR *d; int dfd, subfd; struct dirent *de; struct stat s; char buf[PKG_PATH_MAX+1]; int bufp, bufe, bufi, readlen; char srcpkg[PKG_NAME_MAX]; char dstpkg[PKG_NAME_MAX]; char srcpath[PKG_PATH_MAX]; char dstpath[PKG_PATH_MAX]; int dstuid=-1, dstgid=-1; int hasspace; d = opendir(UPDATE_COMMANDS_DIR_PREFIX); if (d == NULL) { goto done; } dfd = dirfd(d); /* Iterate through all files in the directory, executing the * file movements requested there-in. */ while ((de = readdir(d))) { const char *name = de->d_name; if (de->d_type == DT_DIR) { continue; } else { subfd = openat(dfd, name, O_RDONLY); if (subfd < 0) { ALOGW("Unable to open update commands at %s%s\n", UPDATE_COMMANDS_DIR_PREFIX, name); continue; } bufp = 0; bufe = 0; buf[PKG_PATH_MAX] = 0; srcpkg[0] = dstpkg[0] = 0; while (1) { bufi = bufp; while (bufi < bufe && buf[bufi] != '\n') { bufi++; } if (bufi < bufe) { buf[bufi] = 0; ALOGV("Processing line: %s\n", buf+bufp); hasspace = 0; while (bufp < bufi && isspace(buf[bufp])) { hasspace = 1; bufp++; } if (buf[bufp] == '#' || bufp == bufi) { // skip comments and empty lines. } else if (hasspace) { if (dstpkg[0] == 0) { ALOGW("Path before package line in %s%s: %s\n", UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); } else if (srcpkg[0] == 0) { // Skip -- source package no longer exists. } else { ALOGV("Move file: %s (from %s to %s)\n", buf+bufp, srcpkg, dstpkg); if (!create_move_path(srcpath, srcpkg, buf+bufp, 0) && !create_move_path(dstpath, dstpkg, buf+bufp, 0)) { movefileordir(srcpath, dstpath, strlen(dstpath)-strlen(buf+bufp), dstuid, dstgid, &s); } } } else { char* div = strchr(buf+bufp, ':'); if (div == NULL) { ALOGW("Bad package spec in %s%s; no ':' sep: %s\n", UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); } else { *div = 0; div++; if (strlen(buf+bufp) < PKG_NAME_MAX) { strcpy(dstpkg, buf+bufp); } else { srcpkg[0] = dstpkg[0] = 0; ALOGW("Package name too long in %s%s: %s\n", UPDATE_COMMANDS_DIR_PREFIX, name, buf+bufp); } if (strlen(div) < PKG_NAME_MAX) { strcpy(srcpkg, div); } else { srcpkg[0] = dstpkg[0] = 0; ALOGW("Package name too long in %s%s: %s\n", UPDATE_COMMANDS_DIR_PREFIX, name, div); } if (srcpkg[0] != 0) { if (!create_pkg_path(srcpath, srcpkg, PKG_DIR_POSTFIX, 0)) { if (lstat(srcpath, &s) < 0) { // Package no longer exists -- skip. srcpkg[0] = 0; } } else { srcpkg[0] = 0; ALOGW("Can't create path %s in %s%s\n", div, UPDATE_COMMANDS_DIR_PREFIX, name); } if (srcpkg[0] != 0) { if (!create_pkg_path(dstpath, dstpkg, PKG_DIR_POSTFIX, 0)) { if (lstat(dstpath, &s) == 0) { dstuid = s.st_uid; dstgid = s.st_gid; } else { // Destination package doesn't // exist... due to original-package, // this is normal, so don't be // noisy about it. srcpkg[0] = 0; } } else { srcpkg[0] = 0; ALOGW("Can't create path %s in %s%s\n", div, UPDATE_COMMANDS_DIR_PREFIX, name); } } ALOGV("Transfering from %s to %s: uid=%d\n", srcpkg, dstpkg, dstuid); } } } bufp = bufi+1; } else { if (bufp == 0) { if (bufp < bufe) { ALOGW("Line too long in %s%s, skipping: %s\n", UPDATE_COMMANDS_DIR_PREFIX, name, buf); } } else if (bufp < bufe) { memcpy(buf, buf+bufp, bufe-bufp); bufe -= bufp; bufp = 0; } readlen = read(subfd, buf+bufe, PKG_PATH_MAX-bufe); if (readlen < 0) { ALOGW("Failure reading update commands in %s%s: %s\n", UPDATE_COMMANDS_DIR_PREFIX, name, strerror(errno)); break; } else if (readlen == 0) { break; } bufe += readlen; buf[bufe] = 0; ALOGV("Read buf: %s\n", buf); } } close(subfd); } } closedir(d); done: return 0; }
int get_size(const char *pkgname, int persona, const char *apkpath, const char *fwdlock_apkpath, const char *asecpath, int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize, int64_t* _asecsize) { DIR *d; int dfd; struct dirent *de; struct stat s; char path[PKG_PATH_MAX]; int64_t codesize = 0; int64_t datasize = 0; int64_t cachesize = 0; int64_t asecsize = 0; /* count the source apk as code -- but only if it's not * on the /system partition and its not on the sdcard. */ if (validate_system_app_path(apkpath) && strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) { if (stat(apkpath, &s) == 0) { codesize += stat_size(&s); } } /* count the forward locked apk as code if it is given */ if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { if (stat(fwdlock_apkpath, &s) == 0) { codesize += stat_size(&s); } } /* count the cached dexfile as code */ if (!create_cache_path(path, apkpath)) { if (stat(path, &s) == 0) { codesize += stat_size(&s); } } /* add in size of any libraries */ if (!create_pkg_path_in_dir(path, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) { d = opendir(path); if (d != NULL) { dfd = dirfd(d); codesize += calculate_dir_size(dfd); closedir(d); } } /* compute asec size if it is given */ if (asecpath != NULL && asecpath[0] != '!') { if (stat(asecpath, &s) == 0) { asecsize += stat_size(&s); } } if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, persona)) { goto done; } d = opendir(path); if (d == NULL) { goto done; } dfd = dirfd(d); /* most stuff in the pkgdir is data, except for the "cache" * directory and below, which is cache, and the "lib" directory * and below, which is code... */ while ((de = readdir(d))) { const char *name = de->d_name; if (de->d_type == DT_DIR) { int subfd; int64_t statsize = 0; int64_t dirsize = 0; /* always skip "." and ".." */ if (name[0] == '.') { if (name[1] == 0) continue; if ((name[1] == '.') && (name[2] == 0)) continue; } if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { statsize = stat_size(&s); } subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); if (subfd >= 0) { dirsize = calculate_dir_size(subfd); } if(!strcmp(name,"lib")) { codesize += dirsize + statsize; } else if(!strcmp(name,"cache")) { cachesize += dirsize + statsize; } else { datasize += dirsize + statsize; } } else if (de->d_type == DT_LNK && !strcmp(name,"lib")) { // This is the symbolic link to the application's library // code. We'll count this as code instead of data, since // it is not something that the app creates. if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { codesize += stat_size(&s); } } else { if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { datasize += stat_size(&s); } } } closedir(d); done: *_codesize = codesize; *_datasize = datasize; *_cachesize = cachesize; *_asecsize = asecsize; return 0; }
int linklib(const char* pkgname, const char* asecLibDir, int userId) { char pkgdir[PKG_PATH_MAX]; char libsymlink[PKG_PATH_MAX]; struct stat s, libStat; int rc = 0; if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, userId)) { ALOGE("cannot create package path\n"); return -1; } if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, userId)) { ALOGE("cannot create package lib symlink origin path\n"); return -1; } if (stat(pkgdir, &s) < 0) return -1; if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { ALOGE("failed to chown '%s': %s\n", pkgdir, strerror(errno)); return -1; } if (chmod(pkgdir, 0700) < 0) { ALOGE("linklib() 1: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); rc = -1; goto out; } if (lstat(libsymlink, &libStat) < 0) { if (errno != ENOENT) { ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); rc = -1; goto out; } } else { if (S_ISDIR(libStat.st_mode)) { if (delete_dir_contents(libsymlink, 1, 0) < 0) { rc = -1; goto out; } } else if (S_ISLNK(libStat.st_mode)) { if (unlink(libsymlink) < 0) { ALOGE("couldn't unlink lib dir: %s\n", strerror(errno)); rc = -1; goto out; } } } if (symlink(asecLibDir, libsymlink) < 0) { ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, asecLibDir, strerror(errno)); rc = -errno; goto out; } out: if (chmod(pkgdir, s.st_mode) < 0) { ALOGE("linklib() 2: failed to chmod '%s': %s\n", pkgdir, strerror(errno)); rc = -errno; } if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { ALOGE("failed to chown '%s' : %s\n", pkgdir, strerror(errno)); return -errno; } return rc; }
int install(const char *pkgname, uid_t uid, gid_t gid, const char *seinfo) { char pkgdir[PKG_PATH_MAX]; char libsymlink[PKG_PATH_MAX]; char applibdir[PKG_PATH_MAX]; struct stat libStat; if ((uid < AID_SYSTEM) || (gid < AID_SYSTEM)) { ALOGE("invalid uid/gid: %d %d\n", uid, gid); return -1; } if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, 0)) { ALOGE("cannot create package path\n"); return -1; } if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, 0)) { ALOGE("cannot create package lib symlink origin path\n"); return -1; } if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) { ALOGE("cannot create package lib symlink dest path\n"); return -1; } if (mkdir(pkgdir, 0751) < 0) { ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); return -1; } if (chmod(pkgdir, 0751) < 0) { ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -1; } if (lstat(libsymlink, &libStat) < 0) { if (errno != ENOENT) { ALOGE("couldn't stat lib dir: %s\n", strerror(errno)); return -1; } } else { if (S_ISDIR(libStat.st_mode)) { if (delete_dir_contents(libsymlink, 1, 0) < 0) { ALOGE("couldn't delete lib directory during install for: %s", libsymlink); return -1; } } else if (S_ISLNK(libStat.st_mode)) { if (unlink(libsymlink) < 0) { ALOGE("couldn't unlink lib directory during install for: %s", libsymlink); return -1; } } } if (symlink(applibdir, libsymlink) < 0) { ALOGE("couldn't symlink directory '%s' -> '%s': %s\n", libsymlink, applibdir, strerror(errno)); unlink(pkgdir); return -1; } if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) { ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); unlink(libsymlink); unlink(pkgdir); return -errno; } if (chown(pkgdir, uid, gid) < 0) { ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(libsymlink); unlink(pkgdir); return -1; } return 0; }
int get_size(const char *pkgname, const char *apkpath, const char *fwdlock_apkpath, int *_codesize, int *_datasize, int *_cachesize) { DIR *d; int dfd; struct dirent *de; struct stat s; char path[PKG_PATH_MAX]; int codesize = 0; int datasize = 0; int cachesize = 0; /* count the source apk as code -- but only if it's not * on the /system partition */ if (strncmp(apkpath, "/system", 7) != 0) { if (stat(apkpath, &s) == 0) { codesize += stat_size(&s); } } /* count the forward locked apk as code if it is given */ if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') { if (stat(fwdlock_apkpath, &s) == 0) { codesize += stat_size(&s); } } /* count the cached dexfile as code */ if (!create_cache_path(path, apkpath)) { if (stat(path, &s) == 0) { codesize += stat_size(&s); } } if (create_pkg_path(path, PKG_DIR_PREFIX, pkgname, PKG_DIR_POSTFIX)) { goto done; } d = opendir(path); if (d == NULL) { goto done; } dfd = dirfd(d); /* most stuff in the pkgdir is data, except for the "cache" * directory and below, which is cache, and the "lib" directory * and below, which is code... */ while ((de = readdir(d))) { const char *name = de->d_name; if (de->d_type == DT_DIR) { int subfd; /* always skip "." and ".." */ if (name[0] == '.') { if (name[1] == 0) continue; if ((name[1] == '.') && (name[2] == 0)) continue; } subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); if (subfd >= 0) { int size = calculate_dir_size(subfd); if (!strcmp(name,"lib")) { codesize += size; } else if(!strcmp(name,"cache")) { cachesize += size; } else { datasize += size; } } } else { if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { datasize += stat_size(&s); } } } closedir(d); done: *_codesize = codesize; *_datasize = datasize; *_cachesize = cachesize; return 0; }
int make_user_data(const char *pkgname, uid_t uid, uid_t persona, const char* seinfo) { char pkgdir[PKG_PATH_MAX]; char applibdir[PKG_PATH_MAX]; char libsymlink[PKG_PATH_MAX]; struct stat libStat; // Create the data dir for the package if (create_pkg_path(pkgdir, pkgname, PKG_DIR_POSTFIX, persona)) { return -1; } if (create_pkg_path(libsymlink, pkgname, PKG_LIB_POSTFIX, persona)) { ALOGE("cannot create package lib symlink origin path\n"); return -1; } if (create_pkg_path_in_dir(applibdir, &android_app_lib_dir, pkgname, PKG_DIR_POSTFIX)) { ALOGE("cannot create package lib symlink dest path\n"); return -1; } if (mkdir(pkgdir, 0751) < 0) { ALOGE("cannot create dir '%s': %s\n", pkgdir, strerror(errno)); return -errno; } if (chmod(pkgdir, 0751) < 0) { ALOGE("cannot chmod dir '%s': %s\n", pkgdir, strerror(errno)); unlink(pkgdir); return -errno; } if (lstat(libsymlink, &libStat) < 0) { if (errno != ENOENT) { ALOGE("couldn't stat lib dir for non-primary: %s\n", strerror(errno)); unlink(pkgdir); return -1; } } else { if (S_ISDIR(libStat.st_mode)) { if (delete_dir_contents(libsymlink, 1, 0) < 0) { ALOGE("couldn't delete lib directory during install for non-primary: %s", libsymlink); unlink(pkgdir); return -1; } } else if (S_ISLNK(libStat.st_mode)) { if (unlink(libsymlink) < 0) { ALOGE("couldn't unlink lib directory during install for non-primary: %s", libsymlink); unlink(pkgdir); return -1; } } } if (symlink(applibdir, libsymlink) < 0) { ALOGE("couldn't symlink directory for non-primary '%s' -> '%s': %s\n", libsymlink, applibdir, strerror(errno)); unlink(pkgdir); return -1; } if (selinux_android_setfilecon2(pkgdir, pkgname, seinfo, uid) < 0) { ALOGE("cannot setfilecon dir '%s': %s\n", pkgdir, strerror(errno)); unlink(libsymlink); unlink(pkgdir); return -errno; } if (chown(pkgdir, uid, uid) < 0) { ALOGE("cannot chown dir '%s': %s\n", pkgdir, strerror(errno)); unlink(libsymlink); unlink(pkgdir); return -errno; } return 0; }