umask(0);
//初始化内核log,位于节点/dev/kmsg【见小节1.2】
klog_init();
//设置输出的log级别
klog_set_level(KLOG_NOTICE_LEVEL);
//创建一块共享的内存空间,用于属性服务【见小节5.1】
property_init();
//初始化epoll功能
epoll_fd = epoll_create1(EPOLL_CLOEXEC);
//初始化子进程退出的信号处理函数,并调用epoll_ctl设置signal fd可读的回调函数【见小节2.1】
signal_handler_init();
//加载default.prop文件
property_load_boot_defaults();
//启动属性服务器,此处会调用epoll_ctl设置property fd可读的回调函数【见小节5.2】
start_property_service();
//解析init.rc文件
init_parse_config_file("/init.rc");
//执行rc文件中触发器为on early-init的语句
action_for_each_trigger("early-init", action_add_queue_tail);
//等冷插拔设备初始化完成
queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done");
queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
//设备组合键的初始化操作,此处会调用epoll_ctl设置keychord fd可读的回调函数
queue_builtin_action(keychord_init_action, "keychord_init");
// 屏幕上显示Android静态Logo 【见小节1.3】
queue_builtin_action(console_init_action, "console_init");
//执行rc文件中触发器为on init的语句
action_for_each_trigger("init", action_add_queue_tail);
queue_builtin_action(mix_hwrng_into_linux_rng_action, "mix_hwrng_into_linux_rng");
char bootmode[PROP_VALUE_MAX];
//当处于充电模式,则charger加入执行队列;否则late-init加入队列。
if (property_get("ro.bootmode", bootmode) > 0 && strcmp(bootmode, "charger") == 0)
{
action_for_each_trigger("charger", action_add_queue_tail);
} else {
action_for_each_trigger("late-init", action_add_queue_tail);
}
//触发器为属性是否设置
queue_builtin_action(queue_property_triggers_action, "queue_property_triggers");
while (true) {
if (!waiting_for_exec) {
execute_one_command();
//根据需要重启服务【见小节1.4】
restart_processes();
}
int timeout = -1;
if (process_needs_restart) {
timeout = (process_needs_restart - gettime()) * 1000;
if (timeout < 0)
timeout = 0;
}
if (!action_queue_empty() || cur_action) {
timeout = 0;
}
epoll_event ev;
//循环等待事件发生
int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, timeout));
if (nr == -1) {
ERROR("epoll_wait failed: %s\n", strerror(errno));
} else if (nr == 1) {
((void (*)()) ev.data.ptr)();
}
}
return 0;
}
init进程执行完成后进入循环等待epoll\_wait的状态。
##### 1.2 log系统
此时android的log系统还没有启动,采用kernel的log系统,打开的设备节点/dev/kmsg, 那么可通过`cat /dev/kmsg`来获取内核log。
接下来,设置log的输出级别为KLOG\_NOTICE\_LEVEL(5),当log级别小于5时则会输出到kernel log, 默认值为3.
#define KLOG_ERROR_LEVEL 3
#define KLOG_WARNING_LEVEL 4
#define KLOG_NOTICE_LEVEL 5
#define KLOG_INFO_LEVEL 6
#define KLOG_DEBUG_LEVEL 7
#define KLOG_DEFAULT_LEVEL 3 //默认为3
##### 1.3 console\_init\_action
[-> init.cpp]
static int console_init_action(int nargs, char **args) {
char console[PROP_VALUE_MAX];
if (property_get(“ro.boot.console”, console) > 0) {
snprintf(console_name, sizeof(console_name), “/dev/%s”, console);
}
int fd = open(console_name, O_RDWR | O_CLOEXEC);
if (fd >= 0)
have_console = 1;
close(fd);
fd = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
if (fd >= 0) {
const char *msg;
msg = "\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n" // console is 40 cols x 30 lines
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
" A N D R O I D ";
write(fd, msg, strlen(msg));