Move non-Java commands over from frameworks/base

Change-Id: I0571813c1cfcf66abd36eb9f178fc49b618e88a6
Signed-off-by: Mike Lockwood <lockwood@google.com>
diff --git a/cmds/system_server/library/Android.mk b/cmds/system_server/library/Android.mk
new file mode 100644
index 0000000..c42424c
--- /dev/null
+++ b/cmds/system_server/library/Android.mk
@@ -0,0 +1,26 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	system_init.cpp
+
+base = $(LOCAL_PATH)/../../..
+native = $(LOCAL_PATH)/../../../../native
+
+LOCAL_C_INCLUDES := \
+	$(native)/services/sensorservice \
+	$(native)/services/surfaceflinger \
+	$(JNI_H_INCLUDE)
+
+LOCAL_SHARED_LIBRARIES := \
+	libandroid_runtime \
+	libsensorservice \
+	libsurfaceflinger \
+    libinput \
+	libutils \
+	libbinder \
+	libcutils
+
+LOCAL_MODULE:= libsystem_server
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/cmds/system_server/library/system_init.cpp b/cmds/system_server/library/system_init.cpp
new file mode 100644
index 0000000..745c34a
--- /dev/null
+++ b/cmds/system_server/library/system_init.cpp
@@ -0,0 +1,105 @@
+/*
+ * System server main initialization.
+ *
+ * The system server is responsible for becoming the Binder
+ * context manager, supplying the root ServiceManager object
+ * through which other services can be found.
+ */
+
+#define LOG_TAG "sysproc"
+
+#include <binder/IPCThreadState.h>
+#include <binder/ProcessState.h>
+#include <binder/IServiceManager.h>
+#include <utils/TextOutput.h>
+#include <utils/Log.h>
+
+#include <SurfaceFlinger.h>
+#include <SensorService.h>
+
+#include <android_runtime/AndroidRuntime.h>
+
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <cutils/properties.h>
+
+using namespace android;
+
+namespace android {
+/**
+ * This class is used to kill this process when the runtime dies.
+ */
+class GrimReaper : public IBinder::DeathRecipient {
+public:
+    GrimReaper() { }
+
+    virtual void binderDied(const wp<IBinder>& who)
+    {
+        ALOGI("Grim Reaper killing system_server...");
+        kill(getpid(), SIGKILL);
+    }
+};
+
+} // namespace android
+
+
+
+extern "C" status_t system_init()
+{
+    ALOGI("Entered system_init()");
+
+    sp<ProcessState> proc(ProcessState::self());
+
+    sp<IServiceManager> sm = defaultServiceManager();
+    ALOGI("ServiceManager: %p\n", sm.get());
+
+    sp<GrimReaper> grim = new GrimReaper();
+    sm->asBinder()->linkToDeath(grim, grim.get(), 0);
+
+    char propBuf[PROPERTY_VALUE_MAX];
+    property_get("system_init.startsurfaceflinger", propBuf, "1");
+    if (strcmp(propBuf, "1") == 0) {
+        // Start the SurfaceFlinger
+        SurfaceFlinger::instantiate();
+    }
+
+    property_get("system_init.startsensorservice", propBuf, "1");
+    if (strcmp(propBuf, "1") == 0) {
+        // Start the sensor service
+        SensorService::instantiate();
+    }
+
+    // And now start the Android runtime.  We have to do this bit
+    // of nastiness because the Android runtime initialization requires
+    // some of the core system services to already be started.
+    // All other servers should just start the Android runtime at
+    // the beginning of their processes's main(), before calling
+    // the init function.
+    ALOGI("System server: starting Android runtime.\n");
+    AndroidRuntime* runtime = AndroidRuntime::getRuntime();
+
+    ALOGI("System server: starting Android services.\n");
+    JNIEnv* env = runtime->getJNIEnv();
+    if (env == NULL) {
+        return UNKNOWN_ERROR;
+    }
+    jclass clazz = env->FindClass("com/android/server/SystemServer");
+    if (clazz == NULL) {
+        return UNKNOWN_ERROR;
+    }
+    jmethodID methodId = env->GetStaticMethodID(clazz, "init2", "()V");
+    if (methodId == NULL) {
+        return UNKNOWN_ERROR;
+    }
+    env->CallStaticVoidMethod(clazz, methodId);
+
+    ALOGI("System server: entering thread pool.\n");
+    ProcessState::self()->startThreadPool();
+    IPCThreadState::self()->joinThreadPool();
+    ALOGI("System server: exiting thread pool.\n");
+
+    return NO_ERROR;
+}