出处:http://hi.baidu.com/chris_zheng
内核启动的最后一步就是启动init进程,代码在init/main.c文件中:
/* This is a non __init function. Force it to be noinline otherwise gcc
* makes it inline to init() and it becomes part of init.text section
*/
static int noinline init_post(void)
{
...
if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
printk(KERN_WARNING "Warning: unable to open an initial console./n");
(void) sys_dup(0);
(void) sys_dup(0);
if (ramdisk_execute_command) {
run_init_process(ramdisk_execute_command);
printk(KERN_WARNING "Failed to execute %s/n",
ramdisk_execute_command);
}
/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* trying to recover a really broken machine.
*/
if (execute_command) {
run_init_process(execute_command);
printk(KERN_WARNING "Failed to execute %s. Attempting "
"defaults.../n", execute_command);
}
run_init_process("/sbin/init");
run_init_process("/etc/init");
run_init_process("/bin/init");
run_init_process("/bin/sh");
panic("No init found. Try passing init= option to kernel.");
}
启动过程;
1.打开标准输入、标准输出、标准错误设备;
2.如果ramdisk_execute_command变量指定了要运行的程序,启动它;
3.如果execute_command变量指定了要运行的程序,启动它;
4.依次执行/sbin/init、/bin/init、/bin/sh。
本文系原创,转载请注明出处:http://hi.baidu.com/chris_zheng