Sunday, April 16, 2017

Init Process

Init is the first process after kernel is started. Process step:
1. Mount basic file system, and initialize log system.
2. Parse /init.rc and /init.%hardware%.rc.
3. Execute early-init action in init.rc.
4. Device specific initialize.
5. Initialize property system, and load android image, then print” ANDROID”.
6. Execute init action in init.rc.
7. Start property service.
8. Execute early-boot and boot actions in init.rc.
9. Execute property action in init.rc.
10. Enter into an indefinite loop to wait for device/property set/child process exit events. For example, if an SD card is plugined, init will receive a device add event, so it can make node for the device. Most of the important process is forked in init, so if any of them crashed, init will receive a SIGCHLD then translate it into a child process exit event, so in the loop init can handle the process exit event and execute the commands defined in *.rc(it will run command onrestart).

Process list
USER     PID   PPID  VSIZE RSS   WCHAN    PC         NAME
root     1     0     548   196   c00b8c14 0000d5cc S /init
root     2     0     0     0     c006bf70 00000000 S kthreadd
root     3     2     0     0     c005cc50 00000000 S ksoftirqd/0
root     4     2     0     0     c007e408 00000000 S watchdog/0
root     5     2     0     0     c0068eec 00000000 S events/0
root     10    2     0     0     c0224f90 00000000 S suspend/0
root     81    2     0     0     c0068eec 00000000 S kblockd/0
root     143   2     0     0     c008f948 00000000 S kswapd0
root     195   2     0     0     c01721f0 00000000 S mtdblockd
root     726   1     772   180   c019dbc4 afe0c1dc S /system/bin/sh
system   727   1     840   188   c022d8a0 afe0c47c S /system/bin/servicemanager
root     729   1     1920  336   ffffffff afe0c1dc S /system/bin/mountd
radio    733   1     12796 648   ffffffff beaab18c S /system/bin/rild
root     734   1     72000 14172 c00b92b0 afe0c5a4 S zygote
root     736   1     1080  216   c00b8c14 bedc021c S /system/bin/dbus-daemon
root     737   1     832   208   c02b6e80 afe0c1dc S /system/bin/installd
root     768   1     720   272   c02265ec afe0c1dc S /system/bin/logcat
system   825   734   574128 28360 ffffffff afe0c47c S system_server
radio    877   734   158260 20040 ffffffff afe0d404 S com.android.phone
system   882   734   144664 24296 ffffffff afe0d404 S android.process.omsservice
app_5    928   734   100888 13336 ffffffff afe0d404 S com.android.inputmethod.borqs
app_18   960   734   104180 15208 ffffffff afe0d404 S com.android.mms
system   1018  734   94732 13792 ffffffff afe0d404 S oms.dm
app_1    1424  734   111904 17024 ffffffff afe0d404 S oms.messaging
root     1684  1     3364  176   ffffffff 0000e8f4 S /sbin/adbd
root     1724  1692  920   356   00000000 afe0c1dc R ps
Zygote: It is the first Dalvik VM. Zygote service does the following tasks step by step:
1. Create JAVA VM
2. Register android native function for JAVA VM.
3. Call the main function in the JAVA class named com.android.internal.os.ZygoteInit whose source is device/java/android/com/android/internal/os/ZygoteInit.java.
  a) Load ZygoteInit class
  b) Register zygote socket
  c) Load preload classes (the default file is device/java/android/preloaded-classes)
  d) Load preload resources
  e) Call Zygote::forkSystemServer (implemented in device/dalvik/vm/InternalNative.c) to fork a new process. In the new process, call the main function in the JAVA class named com.android.server.SystemServer, whose source is in device/java/services/com/android/server.
    i. Load libandroid_servers.so
ii. Call JNI native init1 function implemented in
device/libs/android_servers/com_android_server_SystemServers. It only calls system_init implemented in device/servers/system/library/system_init.cpp.
*If running on simulator, instantiate AudioFlinger, MediaPlayerService and CameraService here.
*Call init2 function in JAVA class named com.android.server.SystemServer, whose source is in device/java/services/com/android/server. This function is very critical for Android because it start all of Android JAVA services.
*If not running on simulator, call IPCThreadState::self()->joinThreadPool() to enter into service dispatcher.

SystemServer::init2 will start a new thread to start all JAVA services as follows:
Core Services:
   1. Starting Power Manager
   2. Creating Activity Manager
   3. Starting Telephony Registry
   4. Starting Package Manager
   5. Set Activity Manager Service as System Process
   6. Starting Context Manager
   7. Starting System Context Providers
   8. Starting Battery Service
   9. Starting Alarm Manager
  10. Starting Sensor Service
  11. Starting Window Manager
  12. Starting Bluetooth Service
  13. Starting Mount Service
Other services
   1. Starting Status Bar Service
   2. Starting Hardware Service
   3. Starting NetStat Service
   4. Starting Connectivity Service
   5. Starting Notification Manager
   6. Starting DeviceStorageMonitor Service
   7. Starting Location Manager
   8. Starting Search Service
   9. Starting Clipboard Service
  10. Starting Checkin Service
  11. Starting Wallpaper Service
  12. Starting Audio Service
  13. Starting HeadsetObserver
  14. Starting AdbSettingsObserver
Finally SystemServer::init2 will call ActivityManagerService.systemReady to launch the first activity by senting Intent.CATEGORY_HOME intent.

There is another way to start system server, which is through a program named system_server whose source is device/servers/system/system_main.cpp. It also calls system_init to start system services. So there is a question: why does Android have two methods to start system services? My guess is that directly start system_server may have synchronous problem with zygote because system_server will call JNI to start SystemServer::init2, while at that time zygote may not start JAVA VM yet. So Android uses another method. After zynote is initialized, fork a new process to start system services.


No comments:

Post a Comment