1. Introduction to the System Startup Process
 
In Linux, each process has 4 GB space, and the kernel divides the 4 GB space 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 ".
 
After the Linux kernel is started,Init 1(Process 1) will actFirst user spaceAndroid 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), such: "Service vold/system/bin/vold" is a serviceAnd "socket vold stream 0660 root Mount" is the option used with the service;Action is composed of a series of commands,For example, "On
Init mkdir/System "is the action for creating the System Folder during system initialization;
 
(3) trigger the action to be executed:Action needs to be called in triggers (triggers condition)This phase triggers the action to be executed and puts the action to be executed into the action Queue according to the trigger conditions;
 
(4) execute the command in the Action queue: Execute the action and service triggered in the previous stage. AndIn this process, two very important processes are derived: zygote and service manager;
 
(5) Cyclic processing events: the INIT process enters an infinite loop to handle device insertion/removal, service attribute status changes, and signal events.
 
2. source code analysis results
 
Analyze the following source code files in the android-2.3.3_r1 version:
 
(1) Init. C: the path is system/CORE/init. C.
 
(2) init_parser.c: the path is system/CORE/init/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 (Note: The sequential execution relationships between the following functions are represented by ">"; the invocation relationships between functions are represented by "à)
 
2.1 Phase 1 (start preparation)
 
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 ("Boot", action_add_queue_tail); à action_add_queue_tail (class_start default) à 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/init_parser.c | Read the content of the init. RC file to the memory data zone. | 
 
   
   | Parse_config | System/CORE/init/init_parser.c | Identify section (Service and action series) and text in the init. RC file | 
 
   
   | Parse_new_section | System/CORE/init/init_parser.c | Recognize section categories | 
 
   
   | Parse_service | System/CORE/init/init_parser.c | Analyze the first line of Service Section | 
 
   
   | Parse_line_service | System/CORE/init/init_parser.c | Analyze the option options of Service Section | 
 
   
   | Parse_action | System/CORE/init/init_parser.c | Analyze the first line of the Action Section | 
 
   
   | Parse_line_action | System/CORE/init/init_parser.c | Analyze each line of independent commands in the Action Section | 
 
   
   | Action_for_each_trigger | System/CORE/init/init_parser.c | Trigger the execution of an action | 
 
   
   | Action_add_queue_tail | System/CORE/init/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/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/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 |