Android startup process Analysis (ii) INIT process start-up

Source: Internet
Author: User

#############################################

This article is extremely ice original, reproduced please indicate the source#############################################Analysis of the Init process, first to analyze how the init process started, init source is located (system/core/init), we first look at the init process android.mk below is from system/core/ The init module takes out a section of code:
local_module:= initlocal_force_static_executable: = Truelocal_module_path: = $ (target_root_out) LOCAL_UNSTRIPPED_ PATH: = $ (target_root_out_unstripped) Local_static_libraries: =         libfs_mgr         liblogwrap         libcutils         Liblog         libc         libselinux         libmincrypt         libext4_utils_staticlocal_additional_dependencies + = $ (LOCAL_ PATH)/android.mkinclude $ (build_executable)
we found that the INIT code was actually compiled into an executable program:
Include $ (build_executable)
The name of the program is called:
local_module:= Init
So how did Init's executable application get started? This is the kernel with the Android AP side of the link.
Let's take a look at kernel code: in the main function of kernel's code:init/main.c, we see the following:
878if (!ramdisk_execute_command) 879ramdisk_execute_command = "/init"; 880881if (sys_access (const char __user *) Ramdisk_execute_command, 0)! = 0) {882ramdisk_execute_command = Null;883prepare_namespace (); 884}
We found that Ramdisk_execute_command was initialized to init.
Determine if the/init command exists in the system and, if it does not exist, Ramdisk_execute_command nulland will continue to look for, check whether there is no mounted file system, INITRD is in this function is mounted.
And for us Android, generally normal circumstances, we will go to perform
Init_post ();
And this function is implemented as follows:
795/* This is a non __init function. Force it-be noinline otherwise gcc796 * makes it-inline to init () and it becomes part of Init.text section797 */798stat IC noinline int init_post (void) 799{800/* need to finish all async __init code before freeing the memory */801async_synchro Nize_full (); 802free_initmem (); 803mark_rodata_ro (); 804system_state = System_running;805numa_default_policy (); 806807808current->signal->flags |= signal_unkillable;809810if (ramdisk_execute_command) {811run_init_process (Ramdisk_execute_command); 812PRINTK (kern_warning "Failed to execute%s\n", 813ramdisk_execute_command); 814}815816/* 817 * We try each of these until one succeeds.818 *819 * The Bourne shell can be used instead of init if We are820 * tryin G to recover a really broken machine.821 */822if (Execute_command) {823run_init_process (Execute_command); 824PRINTK (KERN  _warning "Failed to execute%s. Attempting "825" defaults...\n ", Execute_command) 826}827run_init_process ("/sbin/init "); 828run_init_process ("/Etc/init "), 829run_init_process ("/bin/init "), 830run_init_process ("/bin/sh "), 831832panic (" No init found. Try passing init= option to kernel. "833" See Linux Documentation/init.txt for guidance. "); 834}
As you can see, when Ramdisk_execute_command is not NULL, it goes to run run_init_process.
<pre name= "code" class= "CPP" style= "FONT-SIZE:13.63636302948PX; line-height:25.9943180084229px; " >789static void run_init_process (const char *init_filename) 790{791argv_init[0] = Init_filename;792kernel_execve ( Init_filename, Argv_init, envp_init); 793}794
See here, we see clearly. This will execute the init command compiled by our AP, which will go into the/system/core/init directory.
The link from kernel to init has been completed.

Android startup process Analysis (ii) INIT process start-up

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.