Intro Android Kernel

- 3 mins read

Introduction to Android Kernel

Overview of Android System Initialization and the Role of Zygote

After the kernel loads, Android begins its initialization process by parsing the init.rc file and starting the necessary native services. One of the early processes started is the execution of the app_process binary. This process eventually calls AndroidRuntime.callMain() with parameters that initialize the Zygote process, which in turn is critical for forking the System Server.

1. Startup via init.rc and app_process

2. Processing Command-Line Arguments in ZygoteInit

3. Starting the Zygote Server

4. Forking the System Server Process

If startSystemServer is true, the forkSystemServer method is invoked:

  • Capabilities Setup: Calculates the capabilities required for the System Server (e.g., networking, system time management).

  • Command Construction: Constructs a command-line argument array for the System Server.

  • Forking: The Zygote forks a new process for the System Server using Zygote.forkSystemServer().

  • If the forked process (pid == 0):

    • The Zygote server socket is closed.
    • handleSystemServerProcess is executed:
      • Sets the UID, GID, and process group for the System Server.
      • Prepares the environment (e.g., classpath, permissions).
      • Passes control to the SystemServer class by invoking zygoteInit.
  • In the parent process (Zygote):

Here is a graph diagram of Zygote’s execution flow:

graph TD A[ZygoteInit.main] --> B[Parse Command-line Arguments] B --> C[Check 'start-system-server' Parameter] C -->|No Lazy Preload| D[Preload Resources and Classes] D --> E[Initialize ZygoteServer] C -->|Lazy Preload| E E -->| 'start-system-server' is True | F[Fork System Server] E -->| 'start-system-server' is False | G[Parent Process
Run Command Socket Loop] F -->|Child Process pid == 0| H[Child Process
Handle System Server] H --> I[Setup Environment and Invoke SystemServer] F -->|Parent Process pid != 0| G