installd: Add support for SELF_PATCHOAT_NEEDED.

Change-Id: Ib9a6373f98474f1242367b5285086251a9d580e5
diff --git a/cmds/installd/commands.cpp b/cmds/installd/commands.cpp
index 5f72ae4..0200112 100644
--- a/cmds/installd/commands.cpp
+++ b/cmds/installd/commands.cpp
@@ -988,8 +988,8 @@
 }
 
 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, const char* oat_dir)
+           const char *pkgname, const char *instruction_set, int dexopt_needed,
+           bool vm_safe_mode, bool debuggable, const char* oat_dir)
 {
     struct utimbuf ut;
     struct stat input_stat;
@@ -1021,13 +1021,25 @@
         }
     }
 
-    if (is_patchoat) {
-        if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
-          return -1;
-        }
-        input_file = in_odex_path;
-    } else {
-        input_file = apk_path;
+    switch (dexopt_needed) {
+        case DEXOPT_DEX2OAT_NEEDED:
+            input_file = apk_path;
+            break;
+
+        case DEXOPT_PATCHOAT_NEEDED:
+            if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
+                return -1;
+            }
+            input_file = in_odex_path;
+            break;
+
+        case DEXOPT_SELF_PATCHOAT_NEEDED:
+            input_file = out_path;
+            break;
+
+        default:
+            ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
+            exit(72);
     }
 
     memset(&input_stat, 0, sizeof(input_stat));
@@ -1062,7 +1074,7 @@
     }
 
     // Create a swap file if necessary.
-    if (!is_patchoat && ShouldUseSwapFileForDexopt()) {
+    if (ShouldUseSwapFileForDexopt()) {
         // Make sure there really is enough space.
         size_t out_len = strlen(out_path);
         if (out_len + strlen(".swap") + 1 <= PKG_PATH_MAX) {
@@ -1121,9 +1133,10 @@
             exit(67);
         }
 
-        if (is_patchoat) {
+        if (dexopt_needed == DEXOPT_PATCHOAT_NEEDED
+            || dexopt_needed == DEXOPT_SELF_PATCHOAT_NEEDED) {
             run_patchoat(input_fd, out_fd, input_file, out_path, pkgname, instruction_set);
-        } else {
+        } else if (dexopt_needed == DEXOPT_DEX2OAT_NEEDED) {
             const char *input_file_name = strrchr(input_file, '/');
             if (input_file_name == NULL) {
                 input_file_name = input_file;
@@ -1132,6 +1145,9 @@
             }
             run_dex2oat(input_fd, out_fd, input_file_name, out_path, swap_fd, pkgname,
                         instruction_set, vm_safe_mode, debuggable);
+        } else {
+            ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
+            exit(73);
         }
         exit(68);   /* only get here on exec failure */
     } else {
diff --git a/cmds/installd/installd.cpp b/cmds/installd/installd.cpp
index ad2478b..fefb1b7 100644
--- a/cmds/installd/installd.cpp
+++ b/cmds/installd/installd.cpp
@@ -38,10 +38,10 @@
 
 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, outputPath */
-    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]), 0,
-                  atoi(arg[6]), arg[7]);
+    /* apk_path, uid, is_public, pkgname, instruction_set,
+     * dexopt_needed, vm_safe_mode, debuggable, oat_dir */
+    return dexopt(arg[0], atoi(arg[1]), atoi(arg[2]), arg[3], arg[4], atoi(arg[5]),
+                  atoi(arg[6]), atoi(arg[7]), arg[8]);
 }
 
 static int do_mark_boot_complete(char **arg, char reply[REPLY_MAX] __unused)
@@ -152,12 +152,6 @@
                              /* pkgName, seinfo, uid*/
 }
 
-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, 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 */
@@ -179,7 +173,7 @@
 struct cmdinfo cmds[] = {
     { "ping",                 0, do_ping },
     { "install",              4, do_install },
-    { "dexopt",               8, do_dexopt },
+    { "dexopt",               9, do_dexopt },
     { "markbootcomplete",     1, do_mark_boot_complete },
     { "movedex",              3, do_move_dex },
     { "rmdex",                2, do_rm_dex },
@@ -198,7 +192,6 @@
     { "rmuser",               1, do_rm_user },
     { "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},
 };
diff --git a/cmds/installd/installd.h b/cmds/installd/installd.h
index 0ebc0ba..cf5e21f 100644
--- a/cmds/installd/installd.h
+++ b/cmds/installd/installd.h
@@ -83,6 +83,11 @@
 #define PKG_NAME_MAX  128   /* largest allowed package name */
 #define PKG_PATH_MAX  256   /* max size of any path we use */
 
+/* dexopt needed flags matching those in dalvik.system.DexFile */
+#define DEXOPT_DEX2OAT_NEEDED        1
+#define DEXOPT_PATCHOAT_NEEDED       2
+#define DEXOPT_SELF_PATCHOAT_NEEDED  3
+
 /* data structures */
 
 typedef struct {
@@ -221,8 +226,8 @@
              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* outputPath);
+           const char *instruction_set, int dexopt_needed, bool vm_safe_mode,
+           bool debuggable, const char* oat_dir);
 int mark_boot_complete(const char *instruction_set);
 int movefiles();
 int linklib(const char* target, const char* source, int userId);