源代码:busybox-1.19.2.tar.bz2(可从http://www.busybox.net/上下载)
Linux内核启动过程的最后一步就是通过do_execve()函数加载执行用户空间的init程序(如BusyBox init、sysvinit等等),它是系统中所有其他进程的父进程(进程ID为1),在系统运行期间以守护进程的形式一直存在,主要用来完成系统的各项配置以及监视其子进程的运行状况。
1、BusyBox init的执行过程
除了基本的命令之外,BusyBox也支持init功能,跟其他init程序一样,BusyBox的init程序也是用来完成系统的各项配置。在busybox-1.19.2中,init的执行过程大致如下:
(1)、在执行parse_inittab()函数时,如果/etc/inittab文件不存在,BusyBox init会使用以下的默认配置:
::sysinit:/etc/init.d/rcS ::askfirst:/bin/sh ::ctrlaltdel:/sbin/reboot ::shutdown:/sbin/swapoff -a ::shutdown:/bin/umount -a -r ::restart:/sbin/init
(2)、在开发板上执行env命令即可查看配置好的环境变量。
/ # env USER=root HOME=/ TERM=vt102 PATH=/sbin:/usr/sbin:/bin:/usr/bin SHELL=/bin/sh PWD=/
2、inittab文件的语法
BusyBox inittab文件的相关说明以及范例都在busybox-1.19.2源码的examples/inittab文件中。它的每一行的格式如下所示:
<id>:<runlevels>:<action>:<process>
<id>:用来指定<process>要使用的控制台,如果使用与init进程一样的控制台,则此项可以省略。
<runlevels>:BusyBox init不支持运行级别,完全忽略此项。
<process>:用来指定要执行的程序,包括此程序的命令行选项。
<action>:用来指定控制<process>执行的方式。
在busybox-1.19.2/init/init.c文件中,BusyBox init所支持的动作类型以宏的形式被定义,并且对每种动作类型的含义都做了简要的说明。
/* Each type of actions can appear many times. They will be * handled in order. RESTART is an exception, only 1st is used. */ /* Start these actions first and wait for completion */ #define SYSINIT 0x01 /* Start these after SYSINIT and wait for completion */ #define WAIT 0x02 /* Start these after WAIT and *dont* wait for completion */ #define ONCE 0x04 /* * NB: while SYSINIT/WAIT/ONCE are being processed, * SIGHUP ("reread /etc/inittab") will be processed only after * each group of actions. If new inittab adds, say, a SYSINIT action, * it will not be run, since init is already "past SYSINIT stage". */ /* Start these after ONCE are started, restart on exit */ #define RESPAWN 0x08 /* Like RESPAWN, but wait for <Enter> to be pressed on tty */ #define ASKFIRST 0x10 /* * Start these on SIGINT, and wait for completion. * Then go back to respawning RESPAWN and ASKFIRST actions. * NB: kernel sends SIGINT to us if Ctrl-Alt-Del was pressed. */ #define CTRLALTDEL 0x20 /* * Start these before killing all processes in preparation for * running RESTART actions or doing low-level halt/reboot/poweroff * (initiated by SIGUSR1/SIGTERM/SIGUSR2). * Wait for completion before proceeding. */ #define SHUTDOWN 0x40 /* * exec() on SIGQUIT. SHUTDOWN actions are started and waited for, * then all processes are killed, then init exec's 1st RESTART action, * replacing itself by it. If no RESTART action specified, * SIGQUIT has no effect. */ #define RESTART 0x80