diff options
Diffstat (limited to 'cmds/installd/commands.cpp')
-rw-r--r-- | cmds/installd/commands.cpp | 307 |
1 files changed, 212 insertions, 95 deletions
diff --git a/cmds/installd/commands.cpp b/cmds/installd/commands.cpp index de6fd96..7090b36 100644 --- a/cmds/installd/commands.cpp +++ b/cmds/installd/commands.cpp @@ -50,7 +50,7 @@ int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const c return -1; } - std::string _pkgdir(create_package_data_path(uuid, pkgname, 0)); + std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname)); const char* pkgdir = _pkgdir.c_str(); if (mkdir(pkgdir, 0751) < 0) { @@ -80,7 +80,7 @@ int install(const char *uuid, const char *pkgname, uid_t uid, gid_t gid, const c int uninstall(const char *uuid, const char *pkgname, userid_t userid) { - std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); + std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname)); const char* pkgdir = _pkgdir.c_str(); remove_profile_file(pkgname); @@ -115,7 +115,7 @@ int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid) return -1; } - std::string _pkgdir(create_package_data_path(uuid, pkgname, 0)); + std::string _pkgdir(create_data_user_package_path(uuid, 0, pkgname)); const char* pkgdir = _pkgdir.c_str(); if (stat(pkgdir, &s) < 0) return -1; @@ -141,7 +141,7 @@ int fix_uid(const char *uuid, const char *pkgname, uid_t uid, gid_t gid) int delete_user_data(const char *uuid, const char *pkgname, userid_t userid) { - std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); + std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname)); const char* pkgdir = _pkgdir.c_str(); return delete_dir_contents(pkgdir, 0, NULL); @@ -149,7 +149,7 @@ int delete_user_data(const char *uuid, const char *pkgname, userid_t userid) int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t userid, const char* seinfo) { - std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); + std::string _pkgdir(create_data_user_package_path(uuid, userid, pkgname)); const char* pkgdir = _pkgdir.c_str(); if (mkdir(pkgdir, 0751) < 0) { @@ -177,15 +177,48 @@ int make_user_data(const char *uuid, const char *pkgname, uid_t uid, userid_t us return 0; } -int move_user_data(const char *from_uuid, const char *to_uuid, - const char *package_name, appid_t appid, const char* seinfo) { +int copy_complete_app(const char *from_uuid, const char *to_uuid, + const char *package_name, const char *data_app_name, appid_t appid, + const char* seinfo) { std::vector<userid_t> users = get_known_users(from_uuid); - // Copy package private data for all known users + // Copy app + { + std::string from(create_data_app_package_path(from_uuid, data_app_name)); + std::string to(create_data_app_package_path(to_uuid, data_app_name)); + std::string to_parent(create_data_app_path(to_uuid)); + + char *argv[] = { + (char*) kCpPath, + (char*) "-F", /* delete any existing destination file first (--remove-destination) */ + (char*) "-p", /* preserve timestamps, ownership, and permissions */ + (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ + (char*) "-P", /* Do not follow symlinks [default] */ + (char*) "-d", /* don't dereference symlinks */ + (char*) from.c_str(), + (char*) to_parent.c_str() + }; + + LOG(DEBUG) << "Copying " << from << " to " << to; + int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); + + if (rc != 0) { + LOG(ERROR) << "Failed copying " << from << " to " << to + << ": status " << rc; + goto fail; + } + + if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { + LOG(ERROR) << "Failed to restorecon " << to; + goto fail; + } + } + + // Copy private data for all known users for (auto user : users) { - std::string from(create_package_data_path(from_uuid, package_name, user)); - std::string to(create_package_data_path(to_uuid, package_name, user)); - std::string to_user(create_data_user_path(to_uuid, user)); + std::string from(create_data_user_package_path(from_uuid, user, package_name)); + std::string to(create_data_user_package_path(to_uuid, user, package_name)); + std::string to_parent(create_data_user_path(to_uuid, user)); // Data source may not exist for all users; that's okay if (access(from.c_str(), F_OK) != 0) { @@ -213,7 +246,7 @@ int move_user_data(const char *from_uuid, const char *to_uuid, (char*) "-P", /* Do not follow symlinks [default] */ (char*) "-d", /* don't dereference symlinks */ (char*) from.c_str(), - (char*) to_user.c_str() + (char*) to_parent.c_str() }; LOG(DEBUG) << "Copying " << from << " to " << to; @@ -224,26 +257,28 @@ int move_user_data(const char *from_uuid, const char *to_uuid, << ": status " << rc; goto fail; } - - if (restorecon_data(to_uuid, package_name, seinfo, uid) != 0) { - LOG(ERROR) << "Failed to restorecon " << to; - goto fail; - } } - // Copy successful, so delete old data - for (auto user : users) { - std::string from(create_package_data_path(from_uuid, package_name, user)); - if (delete_dir_contents(from.c_str(), 1, NULL) != 0) { - LOG(WARNING) << "Failed to delete " << from; - } + if (restorecon_data(to_uuid, package_name, seinfo, multiuser_get_uid(0, appid)) != 0) { + LOG(ERROR) << "Failed to restorecon"; + goto fail; } + + // We let the framework scan the new location and persist that before + // deleting the data in the old location; this ordering ensures that + // we can recover from things like battery pulls. return 0; fail: // Nuke everything we might have already copied + { + std::string to(create_data_app_package_path(to_uuid, data_app_name)); + if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { + LOG(WARNING) << "Failed to rollback " << to; + } + } for (auto user : users) { - std::string to(create_package_data_path(to_uuid, package_name, user)); + std::string to(create_data_user_package_path(to_uuid, user, package_name)); if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { LOG(WARNING) << "Failed to rollback " << to; } @@ -289,7 +324,7 @@ int delete_user(const char *uuid, userid_t userid) int delete_cache(const char *uuid, const char *pkgname, userid_t userid) { std::string _cachedir( - create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX); + create_data_user_package_path(uuid, userid, pkgname) + CACHE_DIR_POSTFIX); const char* cachedir = _cachedir.c_str(); /* delete contents, not the directory, no exceptions */ @@ -299,7 +334,7 @@ int delete_cache(const char *uuid, const char *pkgname, userid_t userid) int delete_code_cache(const char *uuid, const char *pkgname, userid_t userid) { std::string _codecachedir( - create_package_data_path(uuid, pkgname, userid) + CACHE_DIR_POSTFIX); + create_data_user_package_path(uuid, userid, pkgname) + CODE_CACHE_DIR_POSTFIX); const char* codecachedir = _codecachedir.c_str(); struct stat s; @@ -454,7 +489,7 @@ int rm_dex(const char *path, const char *instruction_set) } } -int get_size(const char *uuid, const char *pkgname, userid_t userid, const char *apkpath, +int get_size(const char *uuid, const char *pkgname, int userid, const char *apkpath, const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath, const char *instruction_set, int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize, int64_t* _asecsize) @@ -470,30 +505,38 @@ int get_size(const char *uuid, const char *pkgname, userid_t userid, const char 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. - */ + /* 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); + if (S_ISDIR(s.st_mode)) { + d = opendir(apkpath); + if (d != NULL) { + dfd = dirfd(d); + codesize += calculate_dir_size(dfd); + closedir(d); + } + } } } - /* count the forward locked apk as code if it is given - */ + + /* 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 */ + + /* count the cached dexfile as code */ if (!create_cache_path(path, apkpath, instruction_set)) { if (stat(path, &s) == 0) { codesize += stat_size(&s); } } - /* add in size of any libraries */ + /* add in size of any libraries */ if (libdirpath != NULL && libdirpath[0] != '!') { d = opendir(libdirpath); if (d != NULL) { @@ -503,68 +546,76 @@ int get_size(const char *uuid, const char *pkgname, userid_t userid, const char } } - /* compute asec size if it is given - */ + /* compute asec size if it is given */ if (asecpath != NULL && asecpath[0] != '!') { if (stat(asecpath, &s) == 0) { asecsize += stat_size(&s); } } - std::string _pkgdir(create_package_data_path(uuid, pkgname, userid)); - const char* pkgdir = _pkgdir.c_str(); - - d = opendir(pkgdir); - if (d == NULL) { - goto done; + std::vector<userid_t> users; + if (userid == -1) { + users = get_known_users(uuid); + } else { + users.push_back(userid); } - 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; + for (auto user : users) { + std::string _pkgdir(create_data_user_package_path(uuid, user, pkgname)); + const char* pkgdir = _pkgdir.c_str(); - 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; + d = opendir(pkgdir); + if (d == NULL) { + PLOG(WARNING) << "Failed to open " << pkgdir; + continue; + } + 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 { - 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); + if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { + datasize += stat_size(&s); + } } } + closedir(d); } - closedir(d); -done: *_codesize = codesize; *_datasize = datasize; *_cachesize = cachesize; @@ -684,6 +735,15 @@ static void run_patchoat(int input_fd, int oat_fd, const char* input_file_name, ALOGE("execv(%s) failed: %s\n", PATCHOAT_BIN, strerror(errno)); } +static bool check_boolean_property(const char* property_name, bool default_value = false) { + char tmp_property_value[PROPERTY_VALUE_MAX]; + bool have_property = property_get(property_name, tmp_property_value, nullptr) > 0; + if (!have_property) { + return default_value; + } + return strcmp(tmp_property_value, "true") == 0; +} + static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, const char* output_file_name, int swap_fd, const char *pkgname, const char *instruction_set, bool vm_safe_mode, bool debuggable) @@ -744,9 +804,8 @@ static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, (strcmp(vold_decrypt, "trigger_restart_min_framework") == 0 || (strcmp(vold_decrypt, "1") == 0))); - char use_jit_property[PROPERTY_VALUE_MAX]; - bool have_jit_property = property_get("debug.usejit", use_jit_property, NULL) > 0; - bool use_jit = have_jit_property && strcmp(use_jit_property, "true") == 0; + bool use_jit = check_boolean_property("debug.usejit"); + bool generate_debug_info = check_boolean_property("debug.generate-debug-info"); static const char* DEX2OAT_BIN = "/system/bin/dex2oat"; @@ -839,6 +898,7 @@ static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, + (have_dex2oat_threads_flag ? 1 : 0) + (have_dex2oat_swap_fd ? 1 : 0) + (have_dex2oat_relocation_skip_flag ? 2 : 0) + + (generate_debug_info ? 1 : 0) + (debuggable ? 1 : 0) + dex2oat_flags_count]; int i = 0; @@ -877,6 +937,9 @@ static void run_dex2oat(int zip_fd, int oat_fd, const char* input_file_name, if (have_dex2oat_swap_fd) { argv[i++] = dex2oat_swap_fd; } + if (generate_debug_info) { + argv[i++] = "--generate-debug-info"; + } if (debuggable) { argv[i++] = "--debuggable"; } @@ -921,20 +984,49 @@ static int wait_child(pid_t pid) } /* - * Whether dexopt should use a swap file when compiling an APK. If kAlwaysProvideSwapFile, do this - * on all devices (dex2oat will make a more informed decision itself, anyways). Otherwise, only do - * this on a low-mem device. + * Whether dexopt should use a swap file when compiling an APK. + * + * If kAlwaysProvideSwapFile, do this on all devices (dex2oat will make a more informed decision + * itself, anyways). + * + * Otherwise, read "dalvik.vm.dex2oat-swap". If the property exists, return whether it is "true". + * + * Otherwise, return true if this is a low-mem device. + * + * Otherwise, return default value. */ -static bool kAlwaysProvideSwapFile = true; +static bool kAlwaysProvideSwapFile = false; +static bool kDefaultProvideSwapFile = true; static bool ShouldUseSwapFileForDexopt() { if (kAlwaysProvideSwapFile) { return true; } - char low_mem_buf[PROPERTY_VALUE_MAX]; - property_get("ro.config.low_ram", low_mem_buf, ""); - return (strcmp(low_mem_buf, "true") == 0); + // Check the "override" property. If it exists, return value == "true". + char dex2oat_prop_buf[PROPERTY_VALUE_MAX]; + if (property_get("dalvik.vm.dex2oat-swap", dex2oat_prop_buf, "") > 0) { + if (strcmp(dex2oat_prop_buf, "true") == 0) { + return true; + } else { + return false; + } + } + + // Shortcut for default value. This is an implementation optimization for the process sketched + // above. If the default value is true, we can avoid to check whether this is a low-mem device, + // as low-mem is never returning false. The compiler will optimize this away if it can. + if (kDefaultProvideSwapFile) { + return true; + } + + bool is_low_mem = check_boolean_property("ro.config.low_ram"); + if (is_low_mem) { + return true; + } + + // Default value must be false here. + return kDefaultProvideSwapFile; } /* @@ -1448,7 +1540,7 @@ int linklib(const char* uuid, const char* pkgname, const char* asecLibDir, int u struct stat s, libStat; int rc = 0; - std::string _pkgdir(create_package_data_path(uuid, pkgname, userId)); + std::string _pkgdir(create_data_user_package_path(uuid, userId, pkgname)); std::string _libsymlink(_pkgdir + PKG_LIB_POSTFIX); const char* pkgdir = _pkgdir.c_str(); @@ -1639,7 +1731,7 @@ int restorecon_data(const char* uuid, const char* pkgName, // Special case for owner on internal storage if (uuid == nullptr) { - std::string path(create_package_data_path(nullptr, pkgName, 0)); + std::string path(create_data_user_package_path(nullptr, 0, pkgName)); if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, flags) < 0) { PLOG(ERROR) << "restorecon failed for " << path; @@ -1717,6 +1809,31 @@ int rm_package_dir(const char* apk_path) return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */); } +int link_file(const char* relative_path, const char* from_base, const char* to_base) { + char from_path[PKG_PATH_MAX]; + char to_path[PKG_PATH_MAX]; + snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); + snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); + + if (validate_apk_path_subdirs(from_path)) { + ALOGE("invalid app data sub-path '%s' (bad prefix)\n", from_path); + return -1; + } + + if (validate_apk_path_subdirs(to_path)) { + ALOGE("invalid app data sub-path '%s' (bad prefix)\n", to_path); + return -1; + } + + const int ret = link(from_path, to_path); + if (ret < 0) { + ALOGE("link(%s, %s) failed : %s", from_path, to_path, strerror(errno)); + return -1; + } + + return 0; +} + int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, const char *instruction_set) { char *file_name_start; |