The SystemServer process is mainly used to create system services. AMS, WMS, and PMS are created by it.
Start the SystemServer process in the startSystemServer method of ZygoteInit. Because the SystemServer process copies the address space of the Zygote process, it also gets the Socket created by the Zygote process. But the Socket is not useful to SystemServer, so the Socket is turned off. Finally will call handleSystemServerProcess method to start the SystemServer process.
In handleSystemServerProcess approach, will create PathClassLoader first, and then call ZygoteInit ZygoteInit method. The nativeZygoteInit method is called in the zygoteInit method, which calls the Native method to start the Binder thread pool. This allows SystemServer processes to communicate with other processes using Binder. It then enters the SystemServer main method.
Start a Binder thread pool
The JNI file corresponding to the nativeZygoteInit method is the com_android_internal_os_ZygoteInit_nativeZygoteInit function of AndroidRuntime. CPP. The function finally returns to the onZygoteInit method of AndroidRuntime in app_main. CPP, which calls startThreadPool to start the Binder thread pool.
SystemServer main method
After calling the nativeZygote method, RuntimeInit’s applicationInit method is called to enter SystemServer’s main method.
Then invokes the RuntimeInit invokeStaticMain method, in this method will get SystemServer class through reflection, reflection of the className for com. Android. Server SystemServer. Then find the SystemServer main method. The main method found is then passed into the MethodAndArgsCaller exception and thrown. The code that catches the MethodAndArgsCaller exception is in zygoteinit. Java’s main method, which calls SystemServer’s main method
The reason for calling SystemServer’s main method by throwing an exception is that it clears all stack frames required by the setup process and makes SystemServer’s main method look like the entry method of the SystemServer process.
After Zygote starts SystemServer, the SystemServer process has done a lot of preparatory work before the main method is called, which makes the main method look like an entry method to SystemServer. So the main method looks like the entry method to SystemServer.
When you catch the MethodAndArgsCaller exception, call back the run method of that exception. MethodAndArgsCaller is Zygote’s static inner class and implements the Runnable method, which calls SystemServer’s main method in the Run method.
The main method calls only one line of code:
new SystemServer().run();
Copy the code
The looper.prepareMainLooper () method is called in the run method, which should be the system’s MainLooper. The libandroid_Servers. so dynamic library is loaded, the system Context is created, the SystemServiceManager is created, and the system Context is passed in. Add the SystemServiceManager to the LocalServices collection after it is created.
Next, call the startBootstrapServices method, Start ActivityManagerService, PowerManagerService, and PackageManagerService with SystemServiceManager.
The startCoreServices method is then called to start DropBoxManagerService, BateryService, UsageStatsService, and WebViewUpdateService.
Finally, start CameraService, AleramManagerService, and VrManagerService in startOtherServices.
The parent class of these services is SystemService.
It can be seen that the system divides services into three types, namely boot services, core services and other services. Other services are non-critical and do not need to be started immediately. There are more than 100 of these system services.
The startup logic for these services is similar. Take PowerManagerService as an example:
mPowerManagerService = mStstemServiceManager.startService(PowerManagerService.class)
Copy the code
The SystemServiceManager startService method starts PowerManagerService. Method adds PowerManagerService to mServices, which is an ArrayList that stores type SystemService, completing the registration of PowerManagerService. PowerManagerService is then started by calling the onStart function of PowerManagerService.
In addition to mSystemServiceManager’s startService function to start a system service, you can also call the main method of the system service to start the service. The addService method of the ServiceManager is called in the main method to register with the ServiceManager.
The ServiceManager manages various services in the system and uses the Binder communication mechanism in the C/S architecture of user systems. If the Client needs to use a Service, the ServiceManager queries the Service information and establishes a communication channel with the Server process where the Service is located. In this way, the Client can use the Service.
SystemServer process summary:
- Start a Binder thread pool
- Create a SystemServiceManager to create, start, and manage system services.
- Start various services