1. 启动
内核启动应用程序/linuxrc
busybox ini.c
init_main
设置信号处理函数
初始化控制台
parse_inittab解析inittab
1.1. 解析inittab
file = open(INITTAB, “r”); //打开配置文件/etc/inittab
new_init_action
//1 创建一个init_action结构,填充
//2 把结构放入init_action_list链表
默认配置
::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
tty2::askfirst:/bin/sh
tty3::askfirst:/bin/sh
tty4::askfirst:/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init
1.2. 运行
/* First run the sysinit command */
run_actions(SYSINIT);
/* Next run anything that wants to block */
run_actions(WAIT);
/* Next run anything to be run only once */
run_actions(ONCE);
/* Now run the looping stuff for the rest of forever */
while (1) {
/* run the respawn/askfirst stuff */
run_actions(RESPAWN | ASKFIRST);
/* Don't consume all CPU time -- sleep a bit */
sleep(1);
/* Wait for any child process to exit */
wpid = wait(NULL);
while (wpid > 0) {
/* Find out who died and clean up their corpse */
for (a = init_action_list; a; a = a->next) {
if (a->pid == wpid) {
/* Set the pid to 0 so that the process gets
* restarted by run_actions() */
a->pid = 0;
message(L_LOG, "process '%s' (pid %d) exited. "
"Scheduling for restart.",
a->command, wpid);
}
}
/* see if anyone else is waiting to be reaped */
wpid = wait_any_nohang(NULL);
}
}