From 88ce4ff7a95ea2008fa28f12b880ee526e331440 Mon Sep 17 00:00:00 2001 From: Fyodor Kupolov Date: Tue, 3 Mar 2015 12:25:29 -0800 Subject: [PATCH] Support for storing OAT files in app directory Changes to installd: - dexopt now allows oat_dir param for custom output directory - Added helper method calculate_oat_file_path for calculating oat file output location based on oat_dir and apk_path. Bug: 19550105 Change-Id: I6b079207310583adeb6dad918a58034a059e34c4 --- cmds/installd/commands.c | 106 ++++++++++++++++++++++++++++++++------- cmds/installd/installd.c | 30 ++++++++--- cmds/installd/installd.h | 7 ++- 3 files changed, 116 insertions(+), 27 deletions(-) diff --git a/cmds/installd/commands.c b/cmds/installd/commands.c index 9fc3f40ef..54e7b4514 100644 --- a/cmds/installd/commands.c +++ b/cmds/installd/commands.c @@ -976,7 +976,7 @@ static bool calculate_odex_file_path(char path[PKG_PATH_MAX], int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgname, const char *instruction_set, - bool vm_safe_mode, bool is_patchoat, bool debuggable) + bool vm_safe_mode, bool is_patchoat, bool debuggable, const char* oat_dir) { struct utimbuf ut; struct stat input_stat, dex_stat; @@ -991,25 +991,22 @@ int dexopt(const char *apk_path, uid_t uid, bool is_public, // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run // without a swap file, if necessary. if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) { + ALOGE("apk_path too long '%s'\n", apk_path); return -1; } - /* Before anything else: is there a .odex file? If so, we have - * precompiled the apk and there is nothing to do here. - * - * We skip this if we are doing a patchoat. - */ - strcpy(out_path, apk_path); - end = strrchr(out_path, '.'); - if (end != NULL && !is_patchoat) { - strcpy(end, ".odex"); - if (stat(out_path, &dex_stat) == 0) { - return 0; + if (oat_dir != NULL && oat_dir[0] != '!') { + if (validate_apk_path(oat_dir)) { + ALOGE("invalid oat_dir '%s'\n", oat_dir); + return -1; + } + if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) { + return -1; + } + } else { + if (create_cache_path(out_path, apk_path, instruction_set)) { + return -1; } - } - - if (create_cache_path(out_path, apk_path, instruction_set)) { - return -1; } if (is_patchoat) { @@ -1115,8 +1112,14 @@ int dexopt(const char *apk_path, uid_t uid, bool is_public, if (is_patchoat) { run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set); } else { - run_dex2oat(input_fd, out_fd, input_file, out_path, swap_fd, pkgname, instruction_set, - vm_safe_mode, debuggable); + const char *input_file_name = strrchr(input_file, '/'); + if (input_file_name == NULL) { + input_file_name = input_file; + } else { + input_file_name++; + } + run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname, + instruction_set, vm_safe_mode, debuggable); } exit(68); /* only get here on exec failure */ } else { @@ -1689,3 +1692,70 @@ int restorecon_data(const char* pkgName, const char* seinfo, uid_t uid) return ret; } +int create_oat_dir(const char* oat_dir, const char* instruction_set) +{ + char oat_instr_dir[PKG_PATH_MAX]; + + if (validate_apk_path(oat_dir)) { + ALOGE("invalid apk path '%s' (bad prefix)\n", oat_dir); + return -1; + } + if ((mkdir(oat_dir, S_IRWXU|S_IRWXG|S_IXOTH) < 0) && (errno != EEXIST)) { + ALOGE("cannot create dir '%s': %s\n", oat_dir, strerror(errno)); + return -1; + } + if (chmod(oat_dir, S_IRWXU|S_IRWXG|S_IXOTH) < 0) { + ALOGE("cannot chmod dir '%s': %s\n", oat_dir, strerror(errno)); + return -1; + } + if (selinux_android_restorecon(oat_dir, 0)) { + ALOGE("cannot restorecon dir '%s': %s\n", oat_dir, strerror(errno)); + return -1; + } + snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); + if ((mkdir(oat_instr_dir, S_IRWXU|S_IRWXG|S_IXOTH) < 0) && (errno != EEXIST)) { + ALOGE("cannot create dir '%s': %s\n", oat_instr_dir, strerror(errno)); + return -1; + } + if (chmod(oat_instr_dir, S_IRWXU|S_IRWXG|S_IXOTH) < 0) { + ALOGE("cannot chmod dir '%s': %s\n", oat_dir, strerror(errno)); + return -1; + } + return 0; +} + +int rm_package_dir(const char* apk_path) +{ + if (validate_apk_path(apk_path)) { + ALOGE("invalid apk path '%s' (bad prefix)\n", apk_path); + return -1; + } + return delete_dir_contents(apk_path, 1 /* also_delete_dir */ , NULL /* exclusion_predicate */); +} + +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; + char *file_name_end; + + file_name_start = strrchr(apk_path, '/'); + if (file_name_start == NULL) { + ALOGE("apk_path '%s' has no '/'s in it\n", apk_path); + return -1; + } + file_name_end = strrchr(apk_path, '.'); + if (file_name_end < file_name_start) { + ALOGE("apk_path '%s' has no extension\n", apk_path); + return -1; + } + + // Calculate file_name + int file_name_len = file_name_end - file_name_start - 1; + char file_name[file_name_len + 1]; + memcpy(file_name, file_name_start + 1, file_name_len); + file_name[file_name_len] = '\0'; + + // /oat//.odex + snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name); + return 0; +} diff --git a/cmds/installd/installd.c b/cmds/installd/installd.c index 8f9417051..5b7117514 100644 --- a/cmds/installd/installd.c +++ b/cmds/installd/installd.c @@ -23,7 +23,7 @@ #define BUFFER_MAX 1024 /* input buffer for commands */ -#define TOKEN_MAX 8 /* max number of arguments in buffer */ +#define TOKEN_MAX 16 /* max number of arguments in buffer */ #define REPLY_MAX 256 /* largest reply allowed */ static int do_ping(char **arg __unused, char reply[REPLY_MAX] __unused) @@ -38,10 +38,10 @@ static int do_install(char **arg, char reply[REPLY_MAX] __unused) static int do_dexopt(char **arg, char reply[REPLY_MAX] __unused) { - /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate, - debuggable */ - return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0, - atoi(arg[6])); + /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate + debuggable, outputPath */ + return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0, + atoi(arg[6]), arg[7]); } static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused) @@ -154,8 +154,20 @@ static int do_restorecon_data(char **arg, char reply[REPLY_MAX] __attribute__((u static int do_patchoat(char **arg, char reply[REPLY_MAX] __unused) { /* apk_path, uid, is_public, pkgname, instruction_set, vm_safe_mode, should_relocate, - debuggable */ - return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1, 0); + debuggable, outputPath */ + return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], 0, 1, 0, "!"); +} + +static int do_create_oat_dir(char **arg, char reply[REPLY_MAX] __unused) +{ + /* oat_dir, instruction_set */ + return create_oat_dir(arg[0], arg[1]); +} + +static int do_rm_package_dir(char **arg, char reply[REPLY_MAX] __unused) +{ + /* oat_dir */ + return rm_package_dir(arg[0]); } struct cmdinfo { @@ -167,7 +179,7 @@ struct cmdinfo { struct cmdinfo cmds[] = { { "ping", 0, do_ping }, { "install", 4, do_install }, - { "dexopt", 7, do_dexopt }, + { "dexopt", 8, do_dexopt }, { "markbootcomplete", 1, do_mark_boot_complete }, { "movedex", 3, do_move_dex }, { "rmdex", 2, do_rm_dex }, @@ -187,6 +199,8 @@ struct cmdinfo cmds[] = { { "idmap", 3, do_idmap }, { "restorecondata", 3, do_restorecon_data }, { "patchoat", 5, do_patchoat }, + { "createoatdir", 2, do_create_oat_dir }, + { "rmpackagedir", 1, do_rm_package_dir}, }; static int readx(int s, void *_buf, int count) diff --git a/cmds/installd/installd.h b/cmds/installd/installd.h index 47577d60f..9c30a3592 100644 --- a/cmds/installd/installd.h +++ b/cmds/installd/installd.h @@ -220,9 +220,14 @@ int get_size(const char *pkgname, userid_t userid, const char *apkpath, const ch int64_t *codesize, int64_t *datasize, int64_t *cachesize, int64_t *asecsize); int free_cache(int64_t free_size); int dexopt(const char *apk_path, uid_t uid, bool is_public, const char *pkgName, - const char *instruction_set, bool vm_safe_mode, bool should_relocate, bool debuggable); + const char *instruction_set, bool vm_safe_mode, bool should_relocate, bool debuggable, + const char* outputPath); int mark_boot_complete(const char *instruction_set); int movefiles(); int linklib(const char* target, const char* source, int userId); int idmap(const char *target_path, const char *overlay_path, uid_t uid); int restorecon_data(); +int create_oat_dir(const char* oat_dir, const char *instruction_set); +int rm_package_dir(const char* apk_path); +int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, + const char *instruction_set); \ No newline at end of file