1. Introduction to the System Startup Process
After the Linux kernel is started, init 1 (process 1) will serve as the first user space (the Linux virtual memory size is 232 (on a 32-bit x86 machine ), the kernel divides the space of 4G bytes into two parts. The maximum 1 GB is used by the kernel, which is called "kernel space ". The lower 3G bytes are used by each process, which is called "user space ".) Android system. The startup process can be divided into the following five stages, for example:
(1) Start preparation: this stage includes creating the basic directory of the file system, enabling the basic input and output devices, and initializing the log function;
(2) parsing the init. RC file: This phase parses the init. RC Script file, mainly for service and action parsing. The Service is composed of a command and a series of additional services (options). For example, "service vold/system/bin/vold" is a service, "socket vold stream 0660 root Mount" is the option used with the service; action is composed of a series of commands, such: "On init mkdir/System" is the action for creating the System Folder during system initialization;
(3) trigger the action to be executed: The action needs to be called in triggers (triggers condition). In this stage, the action to be executed is triggered, add the action to be executed to the action Queue according to the trigger condition;
(4) execute the command in the Action queue: Execute the action and service triggered in the previous stage. In this process, two very important processes, zygote and Service Manager, are derived;
(5) Cyclic processing events: the INIT process enters an infinite loop to handle device insertion/removal, service attribute status changes, and signal events.
Ii. source code analysis results
Analyze the following source code files in Android-2.1:
(1) Init. C: the path is system/CORE/init. C.
(2) parser. C: Path: System/CORE/init/parser. c
(3) builtins. C: the path is system/CORE/init/builtins. C.
(4) property_service.c: the path is system/CORE/init/property_service.c.
(5) keycords. C: the path is system/CORE/init/keycords. C.
(6) signal_handler.c: Path: System/CORE/init/signal_handler.c
The source code files and functions corresponding to the system startup process are summarized as follows:
2.1 Phase 1 (preparation) system/CORE/init. c
The specific function execution process is as follows:
Mkdir-> Mount-> open_devnull_stdio-> log_init |
2.2 Phase 2 (parsing the init. RC file)
The specific function call process is as follows:
Init_parse_config_file-> parse_config-> parse_new_section-> parse_service (or parse_action)-> Parse_line_service (or parce_line_action) |
2.3 Stage 3 (trigger the action to be executed)
The specific call process is as follows:
Action_for_each_trigger ("early-init", action_add_queue_tail);-> action_add_queue_tail (class_start default)-> drain_action_queue ();-> action_remove_queue_head-> Do_class_start |
2.4 Stage 4 (execute commands in the Action Queue)
The specific call process is as follows:
Execute_one_command-> action_remove_queue_head-> do_class_start-> service_for_each_class-> Service_start_if_not_disabled-> service_start |
2.5 Stage 5 (Cyclic processing)
The specific cyclic processing process is as follows:
- for (; ;) {
- poll > handle_property_set_fd > handle_keychord > handle_signal
- }
2.6 Main Functions
Function Name |
File |
Function Overview |
Main |
System/CORE/init. c |
The entry function of process init 1. This module mainly analyzes the init. RC configuration file, executes basic actions and starts necessary native services, and then enters an infinite loop to process events from property and signal. |
Mkdir |
System/CORE/init. c |
Create a basic directory for the File System |
Mount |
System/CORE/init. c |
Mount a File System |
Open_devnull_stdio |
System/CORE/init. c |
Enable basic input and output devices |
Log_init |
System/CORE/init. c |
Initialize the log feature |
Init_parse_config_file |
System/CORE/init/parser. c |
Read the content of the init. RC file to the memory data zone. |
Parse_config |
System/CORE/init/parser. c |
Identify section (Service and action series) and text in the init. RC file |
Parse_new_section |
System/CORE/init/parser. c |
Recognize section categories |
Parse_service |
System/CORE/init/parser. c |
Analyze the first line of Service Section |
Parse_line_service |
System/CORE/init/parser. c |
Analyze the option options of Service Section |
Parse_action |
System/CORE/init/parser. c |
Analyze the first line of the Action Section |
Parse_line_action |
System/CORE/init/parser. c |
Analyze each line of independent commands in the Action Section |
Action_for_each_trigger |
System/CORE/init/parser. c |
Trigger the execution of an action |
Action_add_queue_tail |
System/CORE/init/parser. c |
Add an action from action_list to action_queue |
Execute_one_command |
System/CORE/init. c |
Execute a command for the current action |
Action_remove_queue_head |
System/CORE/init/parser. c |
Remove the header node (Action) from the action_queue linked list) |
Do_class_start |
System/CORE/init/builtins. c |
The entry function corresponding to class_start default, which is mainly used to start native service. |
Service_for_each_class |
System/CORE/init/parser. c |
Traverse all nodes on the service_list linked list |
Service_start_if_not_disabled |
System/CORE/init/builtins. c |
Determine whether the flag of the service is disabled. If not, call the relevant function to start the service. |
Service_start |
System/CORE/init. c |
Start the main entry function of the service, set the relevant data structure of the Service, call fork to create a new operation, and then call execve to execute the new service |
Fork |
Lib function (ulibc) |
Process Creation Function |
Execve |
Lib function (ulibc) |
Call to execute the new service |
Poll |
Lib function (ulibc) |
Queries whether the property_set_fd, signal_fd, and keychord_fd file handles have service requests. |
Handle_property_set_fd |
System/CORE/init/property_service.c |
Process System property service requests, such as service, WLAN, and DHCP. |
Handle_keychord |
System/CORE/init/keycords. c |
Process keychord registered on the service structure, usually starting the service |
Handle_signal |
System/CORE/init/signal_handler.c |
Processing sigchld Signal |