Android Startup Process Analysis (2) Startup Of The init process, androidinit

Source: Internet
Author: User

Android Startup Process Analysis (2) Startup Of The init process, androidinit

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

This article is original to extreme cold ice, for reprint, please specify the source #################################### ######## analyze the init process, first, we need to analyze how the init process starts. The init source code is located at (system/core/init). Let's first look at the android of the init process. mk is a piece of code from the system/core/init module:
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 is actually compiled into an executable program:
include $(BUILD_EXECUTABLE)
The program name is:
LOCAL_MODULE:= init
So how is the init executable application started? This is the connection between the kernel and the android ap.
Let's take a look at the kernel code: in the main function of kernel code: init/main. c, we see the following code:
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 is initialized to init
sys_access((const char __user *) ramdisk_execute_command, 0) 
Determine whether the/init command exists in the system. If it does not exist, the ramdisk_execute_command will be set to NUll and will continue searching to check whether there is any mounted file system, initrd is mounted in this function.
For android, we normally Execute
init_post();
The implementation of this function is as follows:
795/* This is a non __init function. Force it to be noinline otherwise gcc796 * makes it inline to init() and it becomes part of init.text section797 */798static noinline int init_post(void)799{800/* need to finish all async __init code before freeing the memory */801async_synchronize_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 * trying 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 will 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
We can see it here. Here we will execute the init command compiled by the ap, that is, it will enter the/system/core/init directory. 

Now the link from kernel to init is complete.

Related Article

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.