blob: bfbc1387071738c159175bf5bd495beb3e97ca09 [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>
18#include <AudioFlinger.h>
19#include <CameraService.h>
Eric Laurenta553c252009-07-17 12:17:14 -070020#include <AudioPolicyService.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include <MediaPlayerService.h>
Mathias Agopian1bf79782010-07-14 23:41:37 -070022#include <SensorService.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
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 {
Elliott Hughesd195e5a2011-04-13 15:39:37 -070040public:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 GrimReaper() { }
42
43 virtual void binderDied(const wp<IBinder>& who)
44 {
Steve Block6215d3f2012-01-04 20:05:49 +000045 ALOGI("Grim Reaper killing system_server...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 kill(getpid(), SIGKILL);
47 }
48};
49
50} // namespace android
51
52
53
54extern "C" status_t system_init()
55{
Steve Block6215d3f2012-01-04 20:05:49 +000056 ALOGI("Entered system_init()");
Elliott Hughesd195e5a2011-04-13 15:39:37 -070057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 sp<ProcessState> proc(ProcessState::self());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 sp<IServiceManager> sm = defaultServiceManager();
Steve Block6215d3f2012-01-04 20:05:49 +000061 ALOGI("ServiceManager: %p\n", sm.get());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 sp<GrimReaper> grim = new GrimReaper();
64 sm->asBinder()->linkToDeath(grim, grim.get(), 0);
Elliott Hughesd195e5a2011-04-13 15:39:37 -070065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 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
Mathias Agopianc12b7ba2011-05-26 21:52:39 -070073 property_get("system_init.startsensorservice", propBuf, "1");
74 if (strcmp(propBuf, "1") == 0) {
75 // Start the sensor service
76 SensorService::instantiate();
77 }
Mathias Agopian1bf79782010-07-14 23:41:37 -070078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 // 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.
Steve Block6215d3f2012-01-04 20:05:49 +000085 ALOGI("System server: starting Android runtime.\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 AndroidRuntime* runtime = AndroidRuntime::getRuntime();
87
Steve Block6215d3f2012-01-04 20:05:49 +000088 ALOGI("System server: starting Android services.\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -070089 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
Steve Block6215d3f2012-01-04 20:05:49 +0000103 ALOGI("System server: entering thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -0700104 ProcessState::self()->startThreadPool();
105 IPCThreadState::self()->joinThreadPool();
Steve Block6215d3f2012-01-04 20:05:49 +0000106 ALOGI("System server: exiting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -0700107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 return NO_ERROR;
109}