blob: 745c34a0478c0056a69f79e729b9433d60995a50 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopian07952722009-05-19 19:08:10 -070011#include <binder/IPCThreadState.h>
12#include <binder/ProcessState.h>
13#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014#include <utils/TextOutput.h>
15#include <utils/Log.h>
16
17#include <SurfaceFlinger.h>
Mathias Agopian1bf79782010-07-14 23:41:37 -070018#include <SensorService.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
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 {
Elliott Hughesd195e5a2011-04-13 15:39:37 -070036public:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 GrimReaper() { }
38
39 virtual void binderDied(const wp<IBinder>& who)
40 {
Steve Block6215d3f2012-01-04 20:05:49 +000041 ALOGI("Grim Reaper killing system_server...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 kill(getpid(), SIGKILL);
43 }
44};
45
46} // namespace android
47
48
49
50extern "C" status_t system_init()
51{
Steve Block6215d3f2012-01-04 20:05:49 +000052 ALOGI("Entered system_init()");
Elliott Hughesd195e5a2011-04-13 15:39:37 -070053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 sp<ProcessState> proc(ProcessState::self());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 sp<IServiceManager> sm = defaultServiceManager();
Steve Block6215d3f2012-01-04 20:05:49 +000057 ALOGI("ServiceManager: %p\n", sm.get());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 sp<GrimReaper> grim = new GrimReaper();
60 sm->asBinder()->linkToDeath(grim, grim.get(), 0);
Elliott Hughesd195e5a2011-04-13 15:39:37 -070061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 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
Mathias Agopianc12b7ba2011-05-26 21:52:39 -070069 property_get("system_init.startsensorservice", propBuf, "1");
70 if (strcmp(propBuf, "1") == 0) {
71 // Start the sensor service
72 SensorService::instantiate();
73 }
Mathias Agopian1bf79782010-07-14 23:41:37 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 // 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.
Steve Block6215d3f2012-01-04 20:05:49 +000081 ALOGI("System server: starting Android runtime.\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 AndroidRuntime* runtime = AndroidRuntime::getRuntime();
83
Steve Block6215d3f2012-01-04 20:05:49 +000084 ALOGI("System server: starting Android services.\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -070085 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
Steve Block6215d3f2012-01-04 20:05:49 +000099 ALOGI("System server: entering thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -0700100 ProcessState::self()->startThreadPool();
101 IPCThreadState::self()->joinThreadPool();
Steve Block6215d3f2012-01-04 20:05:49 +0000102 ALOGI("System server: exiting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -0700103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 return NO_ERROR;
105}