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