1/*
2 * System server main initialization.
3 *
4 * The system server is responsible for becoming the Binder
5 * context manager, supplying the root ServiceManager object
6 * through which other services can be found.
7 */
8
9#define LOG_TAG "sysproc"
10
11#include <binder/IPCThreadState.h>
12#include <binder/ProcessState.h>
13#include <binder/IServiceManager.h>
14#include <utils/TextOutput.h>
15#include <utils/Log.h>
16
17#include <SurfaceFlinger.h>
18#include <SensorService.h>
19
20#include <android_runtime/AndroidRuntime.h>
21
22#include <signal.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <unistd.h>
26#include <sys/time.h>
27#include <cutils/properties.h>
28
29using namespace android;
30
31namespace android {
32/**
33 * This class is used to kill this process when the runtime dies.
34 */
35class GrimReaper : public IBinder::DeathRecipient {
36public:
37    GrimReaper() { }
38
39    virtual void binderDied(const wp<IBinder>& who)
40    {
41        ALOGI("Grim Reaper killing system_server...");
42        kill(getpid(), SIGKILL);
43    }
44};
45
46} // namespace android
47
48
49
50extern "C" status_t system_init()
51{
52    ALOGI("Entered system_init()");
53
54    sp<ProcessState> proc(ProcessState::self());
55
56    sp<IServiceManager> sm = defaultServiceManager();
57    ALOGI("ServiceManager: %p\n", sm.get());
58
59    sp<GrimReaper> grim = new GrimReaper();
60    sm->asBinder()->linkToDeath(grim, grim.get(), 0);
61
62    char propBuf[PROPERTY_VALUE_MAX];
63    property_get("system_init.startsurfaceflinger", propBuf, "1");
64    if (strcmp(propBuf, "1") == 0) {
65        // Start the SurfaceFlinger
66        SurfaceFlinger::instantiate();
67    }
68
69    property_get("system_init.startsensorservice", propBuf, "1");
70    if (strcmp(propBuf, "1") == 0) {
71        // Start the sensor service
72        SensorService::instantiate();
73    }
74
75    // And now start the Android runtime.  We have to do this bit
76    // of nastiness because the Android runtime initialization requires
77    // some of the core system services to already be started.
78    // All other servers should just start the Android runtime at
79    // the beginning of their processes's main(), before calling
80    // the init function.
81    ALOGI("System server: starting Android runtime.\n");
82    AndroidRuntime* runtime = AndroidRuntime::getRuntime();
83
84    ALOGI("System server: starting Android services.\n");
85    JNIEnv* env = runtime->getJNIEnv();
86    if (env == NULL) {
87        return UNKNOWN_ERROR;
88    }
89    jclass clazz = env->FindClass("com/android/server/SystemServer");
90    if (clazz == NULL) {
91        return UNKNOWN_ERROR;
92    }
93    jmethodID methodId = env->GetStaticMethodID(clazz, "init2", "()V");
94    if (methodId == NULL) {
95        return UNKNOWN_ERROR;
96    }
97    env->CallStaticVoidMethod(clazz, methodId);
98
99    ALOGI("System server: entering thread pool.\n");
100    ProcessState::self()->startThreadPool();
101    IPCThreadState::self()->joinThreadPool();
102    ALOGI("System server: exiting thread pool.\n");
103
104    return NO_ERROR;
105}
106