auto import from //branches/cupcake/...@130745
diff --git a/tools/dexpreopt/Config.mk b/tools/dexpreopt/Config.mk
index 58891fa..c6639b2 100644
--- a/tools/dexpreopt/Config.mk
+++ b/tools/dexpreopt/Config.mk
@@ -29,12 +29,20 @@
 # would have different versions.
 intermediates := \
 	$(call intermediates-dir-for,PACKAGING,dexpreopt)
-dexpreopt_initrc := $(LOCAL_PATH)/etc/init.rc
 dexpreopt_system_dir := $(intermediates)/system
 built_afar := $(call intermediates-dir-for,EXECUTABLES,afar)/afar
 built_dowrapper := \
 	$(call intermediates-dir-for,EXECUTABLES,dexopt-wrapper)/dexopt-wrapper
 
+# Generate a stripped-down init.rc based on the real one.
+dexpreopt_initrc := $(intermediates)/etc/init.rc
+geninitrc_script := $(LOCAL_PATH)/geninitrc.awk
+$(dexpreopt_initrc): script := $(geninitrc_script)
+$(dexpreopt_initrc): system/core/rootdir/init.rc $(geninitrc_script)
+	@echo "Dexpreopt init.rc: $@"
+	@mkdir -p $(dir $@)
+	$(hide) awk -f $(script) < $< > $@
+
 BUILT_DEXPREOPT_RAMDISK := $(intermediates)/ramdisk.img
 $(BUILT_DEXPREOPT_RAMDISK): intermediates := $(intermediates)
 $(BUILT_DEXPREOPT_RAMDISK): dexpreopt_root_out := $(intermediates)/root
diff --git a/tools/dexpreopt/dexopt-wrapper/Android.mk b/tools/dexpreopt/dexopt-wrapper/Android.mk
index f206169..e6ca389 100644
--- a/tools/dexpreopt/dexopt-wrapper/Android.mk
+++ b/tools/dexpreopt/dexopt-wrapper/Android.mk
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
 
@@ -25,6 +26,9 @@
 LOCAL_STATIC_LIBRARIES := \
         libdex
 
+LOCAL_SHARED_LIBRARIES := \
+        libcutils
+
 LOCAL_MODULE := dexopt-wrapper
 
 LOCAL_MODULE_TAGS := tests
diff --git a/tools/dexpreopt/dexopt-wrapper/DexOptWrapper.cpp b/tools/dexpreopt/dexopt-wrapper/DexOptWrapper.cpp
index 358f0ca..fde2d08 100644
--- a/tools/dexpreopt/dexopt-wrapper/DexOptWrapper.cpp
+++ b/tools/dexpreopt/dexopt-wrapper/DexOptWrapper.cpp
@@ -14,6 +14,8 @@
 #include <fcntl.h>
 #include <errno.h>
 
+#include "cutils/properties.h"
+
 //using namespace android;
 
 /*
@@ -36,9 +38,13 @@
     static const int kMaxIntLen = 12;   // '-'+10dig+'\0' -OR- 0x+8dig
     char zipNum[kMaxIntLen];
     char odexNum[kMaxIntLen];
+    char dexoptFlags[PROPERTY_VALUE_MAX];
     const char* androidRoot;
     char* execFile;
 
+    /* pull optional configuration tweaks out of properties */
+    property_get("dalvik.vm.dexopt-flags", dexoptFlags, "");
+
     /* find dexopt executable; this exists for simulator compatibility */
     androidRoot = getenv("ANDROID_ROOT");
     if (androidRoot == NULL)
@@ -50,7 +56,7 @@
     sprintf(odexNum, "%d", odexFd);
 
     execl(execFile, execFile, "--zip", zipNum, odexNum, inputFileName,
-        (char*) NULL);
+        dexoptFlags, (char*) NULL);
     fprintf(stderr, "execl(%s) failed: %s\n", kDexOptBin, strerror(errno));
 }
 
diff --git a/tools/dexpreopt/etc/init.rc b/tools/dexpreopt/etc/init.rc
deleted file mode 100644
index e39f1fe..0000000
--- a/tools/dexpreopt/etc/init.rc
+++ /dev/null
@@ -1,167 +0,0 @@
-
-on init
-
-loglevel 3
-
-# setup the global environment
-    export PATH /sbin:/system/sbin:/system/bin:/system/xbin
-    export LD_LIBRARY_PATH /system/lib
-    export ANDROID_BOOTLOGO 1
-    export ANDROID_ROOT /system
-    export ANDROID_ASSETS /system/app
-    export ANDROID_DATA /data
-    export EXTERNAL_STORAGE /sdcard
-    export BOOTCLASSPATH /system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar
-
-# Backward compatibility
-    symlink /system/etc /etc
-
-# create mountpoints and mount tmpfs on sqlite_stmt_journals and debugfs on d
-    mkdir /d
-    mkdir /sdcard 0000 system system
-    mkdir /system
-    mkdir /data 0771 system system
-    mkdir /cache 0770 system cache
-    mkdir /sqlite_stmt_journals 01777 root root
-    mount tmpfs tmpfs /sqlite_stmt_journals
-    mount debugfs debugfs /d
-
-    mount rootfs rootfs / ro remount
-
-    write /proc/sys/kernel/panic_on_oops 1
-    write /proc/sys/kernel/hung_task_timeout_secs 0
-    write /proc/cpu/alignment 4
-    write /proc/sys/kernel/sched_latency_ns 10000000
-    write /proc/sys/kernel/sched_wakeup_granularity_ns 2000000
-
-# mount mtd partitions
-    # Mount /system rw first to give the filesystem a chance to save a checkpoint
-    mount yaffs2 mtd@system /system 
-    #   dexpreopt needs to write to /system
-    ### mount yaffs2 mtd@system /system ro remount
-
-    # We chown/chmod /data again so because mount is run as root + defaults
-    mount yaffs2 mtd@userdata /data
-    chown system system /data
-    chmod 0771 /data
-
-    # Same reason as /data above
-    mount yaffs2 mtd@cache /cache
-    chown system cache /cache
-    chmod 0770 /cache
-
-    # This may have been created by the recovery system with odd permissions
-    chown system system /cache/recovery
-    chmod 0770 /cache/recovery
-
-# create basic filesystem structure
-    mkdir /data/dalvik-cache 0777 root root
-    mkdir /data/misc 01771 system misc
-    mkdir /data/misc/hcid 0770 bluetooth bluetooth
-    mkdir /data/local 0771 shell shell
-    mkdir /data/local/tmp 0771 shell shell
-    mkdir /data/data 0771 system system
-    mkdir /data/app-private 0771 system system
-    mkdir /data/app 0771 system system
-    mkdir /data/property 0700 root root
-
-    # create dalvik-cache and double-check the perms
-    mkdir /data/dalvik-cache 0771 system system
-    chown system system /data/dalvik-cache
-    chmod 0771 /data/dalvik-cache
-
-    # create the lost+found directories, so as to enforce our permissions
-    mkdir /data/lost+found 0770
-    mkdir /cache/lost+found 0770
-
-    # double check the perms, in case lost+found already exists, and set owner
-    chown root root /data/lost+found
-    chmod 0770 /data/lost+found
-    chown root root /cache/lost+found
-    chmod 0770 /cache/lost+found
-
-on boot
-# basic network init
-    ifup lo
-    hostname localhost
-    domainname localdomain
-
-# set RLIMIT_NICE to allow priorities from 19 to -20
-    setrlimit 13 40 40
-
-# Define the oom_adj values for the classes of processes that can be
-# killed by the kernel.  These are used in ActivityManagerService.
-    setprop ro.FOREGROUND_APP_ADJ 0
-    setprop ro.VISIBLE_APP_ADJ 1
-    setprop ro.SECONDARY_SERVER_ADJ 2
-    setprop ro.HIDDEN_APP_MIN_ADJ 7
-    setprop ro.CONTENT_PROVIDER_ADJ 14
-    setprop ro.EMPTY_APP_ADJ 15
-
-# Define the memory thresholds at which the above process classes will
-# be killed.  These numbers are in pages (4k).
-    setprop ro.FOREGROUND_APP_MEM 1536
-    setprop ro.VISIBLE_APP_MEM 2048
-    setprop ro.SECONDARY_SERVER_MEM 4096
-    setprop ro.HIDDEN_APP_MEM 5120
-    setprop ro.CONTENT_PROVIDER_MEM 5632
-    setprop ro.EMPTY_APP_MEM 6144
-
-# Write value must be consistent with the above properties.
-    write /sys/module/lowmemorykiller/parameters/adj 0,1,2,7,14,15
-
-    write /proc/sys/vm/overcommit_memory 1
-    write /sys/module/lowmemorykiller/parameters/minfree 1536,2048,4096,5120,5632,6144
-
-    class_start default
-
-    # Set init its forked children's oom_adj.
-    write /proc/1/oom_adj -16
-
-    # Permissions for System Server and daemons.
-    chown radio system /sys/android_power/state
-    chown radio system /sys/android_power/request_state
-    chown radio system /sys/android_power/acquire_full_wake_lock
-    chown radio system /sys/android_power/acquire_partial_wake_lock
-    chown radio system /sys/android_power/release_wake_lock
-    chown system system /sys/class/timed_output/vibrator/enable
-    chown system system /sys/class/leds/keyboard-backlight/brightness
-    chown system system /sys/class/leds/lcd-backlight/brightness
-    chown system system /sys/class/leds/button-backlight/brightness
-    chown system system /sys/class/leds/red/brightness
-    chown system system /sys/class/leds/green/brightness
-    chown system system /sys/class/leds/blue/brightness
-    chown system system /sys/class/leds/red/device/grpfreq
-    chown system system /sys/class/leds/red/device/grppwm
-    chown system system /sys/class/leds/red/device/blink
-    chown system system /sys/class/leds/red/brightness
-    chown system system /sys/class/leds/green/brightness
-    chown system system /sys/class/leds/blue/brightness
-    chown system system /sys/class/leds/red/device/grpfreq
-    chown system system /sys/class/leds/red/device/grppwm
-    chown system system /sys/class/leds/red/device/blink
-    chown system system /sys/class/timed_output/vibrator/enable
-    chown bluetooth bluetooth /sys/module/board_trout/parameters/bluetooth_power_on
-    chown system system /sys/module/sco/parameters/disable_esco
-    chmod 0660 /sys/module/board_trout/parameters/bluetooth_power_on
-    chown system system /sys/kernel/ipv4/tcp_wmem_min
-    chown system system /sys/kernel/ipv4/tcp_wmem_def
-    chown system system /sys/kernel/ipv4/tcp_wmem_max
-    chown system system /sys/kernel/ipv4/tcp_rmem_min
-    chown system system /sys/kernel/ipv4/tcp_rmem_def
-    chown system system /sys/kernel/ipv4/tcp_rmem_max
-    chown root radio /proc/cmdline
-
-# Define TCP buffer sizes for various networks
-#   ReadMin, ReadInitial, ReadMax, WriteMin, WriteInitial, WriteMax,
-    setprop net.tcp.buffersize.default 4096,87380,110208,4096,16384,110208
-    setprop net.tcp.buffersize.wifi    4095,87380,110208,4096,16384,110208
-    setprop net.tcp.buffersize.umts    4094,87380,110208,4096,16384,110208
-    setprop net.tcp.buffersize.edge    4093,26280,35040,4096,16384,35040
-    setprop net.tcp.buffersize.gprs    4092,8760,11680,4096,8760,11680
-
-
-## Daemon processes to be run by init.
-##
-service console /system/bin/sh
-    console
diff --git a/tools/dexpreopt/geninitrc.awk b/tools/dexpreopt/geninitrc.awk
new file mode 100644
index 0000000..4b67e78
--- /dev/null
+++ b/tools/dexpreopt/geninitrc.awk
@@ -0,0 +1,62 @@
+#
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+BEGIN {
+    fixed_remount = 0;
+    console_state = 0;
+}
+
+/^    mount yaffs2 mtd@system \/system ro remount$/ {
+    fixed_remount = 1;
+    print "    #   dexpreopt needs to write to /system";
+    print "    ### " $0;
+    next;
+}
+
+console_state == 0 && /^service console \/system\/bin\/sh$/ {
+    console_state = 1;
+    print;
+    next;
+}
+
+console_state == 1 && /^    console$/ {
+    console_state = 2;
+    print;
+    exit;
+}
+
+console_state == 1 {
+    # The second line of the console entry should always immediately
+    # follow the first.
+    exit;
+}
+
+{ print }
+
+END {
+    failed = 0;
+    if (fixed_remount != 1) {
+        print "ERROR: no match for remount line" > "/dev/stderr";
+        failed = 1;
+    }
+    if (console_state != 2) {
+        print "ERROR: no match for console lines" > "/dev/stderr";
+        failed = 1;
+    }
+    if (failed == 1) {
+        print ">>>> FAILED <<<<"
+        exit 1;
+    }
+}