Android init process startup process analysis

Tian Haili@CSDN

2013-3-16

 

This article analyzes the execution process of the init process in Android, only analyzes the flow of init process startup, and the specific details will be analyzed in detail in each topic in the future. Although this article is the basis of the following topics, readers may not have a deep understanding at first glance. When reading the following topics, you can have a clearer understanding of the overall process of this article.

 

The Init process starts from the main() function in /system/core/init/init.c

 

1.      mkdir && mount

 

2.      import_kernel_cmdline

    Import the following command line parameters from the kernel through /proc/cmdline, and these parameters will be set to each attribute value:

      androidboot.console      // 设置console
      androidboot.mode         // 设置属性ro.factorytest
      androidboot.serialno     // 设置属性ro.serialno
      androidboot.baseband     // 设置属性ro.baseband
      androidboot.carrier      // 设置属性ro.carrier
      androidboot.bootloader   // 设置属性ro.bootloader
      androidboot.hardware     // 设置属性ro.hardware
      androidboot.bsp          // 根据这个设置与否,选择不同的init.rc版本
Note: The setting properties mentioned above have not been set, and the property mechanism has not yet worked.

 

3.      init_parse_config_file()

    Analyze init.rc or init_bsp.rc (see whether the parameter "androidboot.bsp" imported in step 2 is set)

    For the analysis of Init.rc, see "Analysis of the init.rc file in Android "

 

4.      get_hardware_name()

    Obtain the "Hardware" field information from /proc/cpuinfo and write it into <hw>; write the "Reversion" field information into <reversion>

 

5.      init_parse_config_file()

    Analyze init.<hw>.rc or init_bsp.<hw>.rc (see whether the parameter "androidboot.bsp" imported in step 2 is set)

    For the analysis of Init.rc, see "Analysis of the init.rc file in Android "

 

6.      action_for_each_trigger("early-init",action_add_queue_tail);

    Perform the action_add_queue_tail operation on the action in the early-initsection parsed from init???.rc, that is, add act- >qlist to the end of the action_queue column

Note : It is not actually executed at this time, just hanging at the end of the queue.

 

7. Add some initialization operations to the action_queue list

    queue_builtin_action(wait_for_coldboot_done_action,"wait_for_coldboot_done");
    queue_builtin_action(property_init_action,"property_init");
    queue_builtin_action(keychord_init_action,"keychord_init");
    queue_builtin_action(console_init_action,"console_init");
    queue_builtin_action(set_init_properties_action,"set_init_properties");
queue_builtin_action(int (*func)(int nargs,char **args), char *name) is an action formed by name, which is hung on the action_list ; a command formed by func and name, hung on the commands of the action. Then join the end of the action_queue team.

 

8. For actions in other sections , add them to the action_queue list

    Some other initial operations are also added to the action_queue list

        /* execute all the boot actions to getus started */
    action_for_each_trigger("init",action_add_queue_tail);
    action_for_each_trigger("early-fs", action_add_queue_tail);
    action_for_each_trigger("fs",action_add_queue_tail);
    action_for_each_trigger("post-fs",action_add_queue_tail);

    queue_builtin_action(property_service_init_action,"property_service_init");
    queue_builtin_action(signal_init_action,"signal_init");
    queue_builtin_action(check_startup_action,"check_startup");
 
    /* execute all the boot actions to get usstarted */
    action_for_each_trigger("early-boot", action_add_queue_tail);
    action_for_each_trigger("boot",action_add_queue_tail);
 
        /* run all property triggers based oncurrent state of the properties */
    queue_builtin_action(queue_property_triggers_action,"queue_propety_triggers");
 

9. Enter the infinite loop for(;;)

   9.1 execute_one_command():[system/core/init/init.c]

      1) Remove structaction *act from action_queue and assign it to cur_action;

      2) Obtain struct command * from cur_action and assign it to cur_command;

      3) 执行cur_command->func(cur_command->nargs, cur_command->args)

     Note 1: The above is the first time it is executed. If there is a command in the action, there is no need to 1, and 2 is to directly remove a command from the action.

     Note 2: This is the actual execution of the command. The previous action_for_each_trigger() and queue_builtin_action() are just added to the action_queue queue, and here are taken out of the queue in order and executed.

     Therefore, the order of adding the action_queue to the queue also determines the order of execution. The section where the actions are located in Init???.rc determines the order of their execution: early-init -> init -> early-fs -> fs -> post-fs -> early-boot -> boot.

 

   9.2 restart_processes():system/core/init/init.c

      For the service with the SVC_RESTARTING flag, execute restart_service_if_needed()

 

  9.3 Use poll to wait for several events: property event/signal event when the child process ends/keychord

 

   9.4 Dealing with waiting events

      For the property_set event, call handle_property_set_fd() to process;

      For keychord events, call handle_keychord() to process;

      For signal events, call handle_signal() to process;  

 

Summary :

This article analyzes the startup process of the init process in Android, from which we can know that the main work done by init includes the analysis of init.rc , the realization of the property mechanism, and the realization of service support . The details are discussed in the following topics.

 


Guess you like

Origin blog.csdn.net/thl789/article/details/8681049