android system server 启动流程

start app_process from init
init.rc


service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server

socket zygote stream 666
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media
onrestart restart netd




frameworks/base/cmds/app_process/app_main.cpp::main
if (0 == strcmp("--zygote", arg)) {
bool startSystemServer = (i < argc) ?
strcmp(argv[i], "--start-system-server") == 0 : false;
setArgv0(argv0, "zygote");
set_process_name("zygote");
runtime.start("com.android.internal.os.ZygoteInit",
startSystemServer);
} else {




frameworks/base/core/jni/AndroidRuntime.cpp::start
void AndroidRuntime::start(const char* className, const bool startSystemServer)
{
LOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
className != NULL ? className : "(unknown)");


...
JNIでcallStatic
com.android.internal.os.ZygoteInit#main




frameworks/base/core/java/com/android/internal/os/ZygoteInit.java#main
if (argv[1].equals("true")) {
startSystemServer();
} else if (!argv[1].equals("false")) {
throw new RuntimeException(argv[0] + USAGE_STRING);
}


frameworks/base/core/java/com/android/internal/os/ZygoteInit.java#startSystemServer


String args[] = {
"--setuid=1000",
"--setgid=1000",
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,3001,3002,3003",
"--capabilities=130104352,130104352",
"--runtime-init",
"--nice-name=system_server",
"com.android.server.SystemServer",
};
...
try {
parsedArgs = new ZygoteConnection.Arguments(args);


...
/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids, debugFlags, null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);




framesorks/base/services/java/com/android/server/SystemServer.java
SystemServer#main
public static void main(String[] args) {


...


System.loadLibrary("android_servers");
init1(args); // <-- native method
}


frameworks/base/services/jni/com_android_server_SystemServer.cpp
static void android_server_SystemServer_init1(JNIEnv* env, jobject clazz)
{
system_init();
}




frameworks/base/cmds/system_server/library/system_init.cpp
system_init
Start services written in C++
- SurfaceFlinger
- SensorService
- AudioFlinger
- MediaPlayerService
- CameraService
- AudioPolicyService
callStatic by JNI com/android/server/SystemServer#init2


framesorks/base/services/java/com/android/server/SystemServer.java
public static final void init2() {
Slog.i(TAG, "Entered the Android system server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}


ServerThread#run
Start services written in Java
- Entropy Service
- Power Manager
- Activity Manager
- Telephony Registry
- Package Manager
- Account Manager
- Content Manger
- System Content Providers
- Battery Service
- Lights Service
- Vibrator Service
- Alarm Manager
- Init Watchdog
- Window Manager
- Bluetooth Service
- Device Policy
- Status Bar
- Clipboard Service
- Input Method Service
- NetStat Service
- NetworkMnagement Service
- Connectivity Service
- Throttle Service
- Accessibility Manager
- Mount Service
- Notification Manager
- Device Storage Monitor
- Location Manager
- Search Service
- DropBox Service
- Wallpaper Service
- Audio Service
- Headset Observer
- Dock Observer
- USB Observer
- UI Mode Manager Service
- Backup Service
- AppWidget Service
- Recognition Service
- DiskStats Service


These services create threads as needs. All those threads are running in the System Server process.
In the normal Linux system, these services are independent process.

你可能感兴趣的:(android)