Android启动流程分析(二) init进程的启动
#############################################
LOCAL_MODULE:= init LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_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_static LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/Android.mk include $(BUILD_EXECUTABLE)我们发现,init的代码其实是被编译成了一个可执行的程序:
include $(BUILD_EXECUTABLE)
LOCAL_MODULE:= init
878 if (!ramdisk_execute_command) 879 ramdisk_execute_command = "/init"; 880 881 if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) { 882 ramdisk_execute_command = NULL; 883 prepare_namespace(); 884 }我们发现,ramdisk_execute_command被初始化为了init
sys_access((const char __user *) ramdisk_execute_command, 0)判断系统中是否存在/init的命令,如果不存在的话,会将ramdisk_execute_command置为NUll
init_post();而这个函数的实现如下:
795/* This is a non __init function. Force it to be noinline otherwise gcc 796 * makes it inline to init() and it becomes part of init.text section 797 */ 798static noinline int init_post(void) 799{ 800 /* need to finish all async __init code before freeing the memory */ 801 async_synchronize_full(); 802 free_initmem(); 803 mark_rodata_ro(); 804 system_state = SYSTEM_RUNNING; 805 numa_default_policy(); 806 807 808 current->signal->flags |= SIGNAL_UNKILLABLE; 809 810 if (ramdisk_execute_command) { 811 run_init_process(ramdisk_execute_command); 812 printk(KERN_WARNING "Failed to execute %s\n", 813 ramdisk_execute_command); 814 } 815 816 /* 817 * We try each of these until one succeeds. 818 * 819 * The Bourne shell can be used instead of init if we are 820 * trying to recover a really broken machine. 821 */ 822 if (execute_command) { 823 run_init_process(execute_command); 824 printk(KERN_WARNING "Failed to execute %s. Attempting " 825 "defaults...\n", execute_command); 826 } 827 run_init_process("/sbin/init"); 828 run_init_process("/etc/init"); 829 run_init_process("/bin/init"); 830 run_init_process("/bin/sh"); 831 832 panic("No init found. Try passing init= option to kernel. " 833 "See Linux Documentation/init.txt for guidance."); 834}可以看到,当ramdisk_execute_command不为NULL的时候,就会去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{ 791 argv_init[0] = init_filename; 792 kernel_execve(init_filename, argv_init, envp_init); 793} 794
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。