标签:
int main(int argc, char **argv) { int fd_count = 0; struct pollfd ufds[4]; char *tmpdev; char* debuggable; char tmp[32]; int property_set_fd_init = 0; int signal_fd_init = 0; int keychord_fd_init = 0; //如果传入的argv[0]参数是ueventd,执行ueventd_main函数 if (!strcmp(basename(argv[0]), "ueventd")) return ueventd_main(argc, argv); /* clear the umask */ //如果是文件,文件权限为666,目录权限是777 umask(0); /* Get the basic filesystem setup we need put * together in the initramdisk on / and then we'll * let the rc file figure out the rest. */ //创建目录并挂载 mkdir("/dev", 0755); mkdir("/proc", 0755); mkdir("/sys", 0755); mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"); mkdir("/dev/pts", 0755); mkdir("/dev/socket", 0755); mount("devpts", "/dev/pts", "devpts", 0, NULL); mount("proc", "/proc", "proc", 0, NULL); mount("sysfs", "/sys", "sysfs", 0, NULL); /* indicate that booting is in progress to background fw loaders, etc */ close(open("/dev/.booting", O_WRONLY | O_CREAT, 0000)); /* We must have some place other than / to create the * device nodes for kmsg and null, otherwise we won't * be able to remount / read-only later on. * Now that tmpfs is mounted on /dev, we can actually * talk to the outside world. */ open_devnull_stdio(); //初始化日志系统 klog_init(); //解析init.rc配置文件(这个是重点分析的) INFO("reading config file\n"); init_parse_config_file("/init.rc"); /* pull the kernel commandline and ramdisk properties file in */ import_kernel_cmdline(0, import_kernel_nv); /* don't expose the raw commandline to nonpriv processes */ chmod("/proc/cmdline", 0440); //读取/proc/cpuinfo得到机器hardware名称 get_hardware_name(hardware, &revision); snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware); //解析tmp 文件,也就是/init.<hardware>.rc文件 init_parse_config_file(tmp); //解析完上面两个rc文件之后得到很多Action。 //这里执行名称为early-init的Action action_for_each_trigger("early-init", action_add_queue_tail); //触发内置的Action。第一个参数是函数指针,第二个参数是action的名称 queue_builtin_action(wait_for_coldboot_done_action, "wait_for_coldboot_done"); queue_builtin_action(property_init_action, "property_init"); queue_builtin_action(keychord_init_action, "keychord_init"); queue_builtin_action(console_init_action, "console_init"); queue_builtin_action(set_init_properties_action, "set_init_properties"); // 执行名称为init的action /* execute all the boot actions to get us started */ action_for_each_trigger("init", action_add_queue_tail); /* skip mounting filesystems in charger mode */ //如果正在充电则执行下面的action if (strcmp(bootmode, "charger") != 0) { action_for_each_trigger("early-fs", action_add_queue_tail); action_for_each_trigger("fs", action_add_queue_tail); action_for_each_trigger("post-fs", action_add_queue_tail); action_for_each_trigger("post-fs-data", action_add_queue_tail); } //触发内置Action queue_builtin_action(property_service_init_action, "property_service_init"); queue_builtin_action(signal_init_action, "signal_init"); queue_builtin_action(check_startup_action, "check_startup"); if (!strcmp(bootmode, "charger")) { action_for_each_trigger("charger", action_add_queue_tail); } else { action_for_each_trigger("early-boot", action_add_queue_tail); action_for_each_trigger("boot", action_add_queue_tail); } /* run all property triggers based on current state of the properties */ queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers"); #if BOOTCHART queue_builtin_action(bootchart_init_action, "bootchart_init"); #endif //执行完上面初始化和触发action的过程之后进入一个死循环,执行Command for(;;) { int nr, i, timeout = -1; execute_one_command(); //如果service异常退出,重启它 restart_processes(); //监听来自property service事件,后面会介绍 if (!property_set_fd_init && get_property_set_fd() > 0) { ufds[fd_count].fd = get_property_set_fd(); ufds[fd_count].events = POLLIN; ufds[fd_count].revents = 0; fd_count++; property_set_fd_init = 1; } //监听来自signal事件 ,signal是用来处理子进程退出时的操作,防止子进程编程僵尸进程 if (!signal_fd_init && get_signal_fd() > 0) { ufds[fd_count].fd = get_signal_fd(); ufds[fd_count].events = POLLIN; ufds[fd_count].revents = 0; fd_count++; signal_fd_init = 1; } //监听来自keychord设备事件 if (!keychord_fd_init && get_keychord_fd() > 0) { ufds[fd_count].fd = get_keychord_fd(); ufds[fd_count].events = POLLIN; ufds[fd_count].revents = 0; fd_count++; keychord_fd_init = 1; } //如果异常终止的service重启,设置等待时间 if (process_needs_restart) { timeout = (process_needs_restart - gettime()) * 1000; if (timeout < 0) timeout = 0; } if (!action_queue_empty() || cur_action) timeout = 0; #if BOOTCHART if (bootchart_count > 0) { if (timeout < 0 || timeout > BOOTCHART_POLLING_MS) timeout = BOOTCHART_POLLING_MS; if (bootchart_step() < 0 || --bootchart_count == 0) { bootchart_finish(); bootchart_count = 0; } } #endif //多路监听设备 nr = poll(ufds, fd_count, timeout); if (nr <= 0) continue; for (i = 0; i < fd_count; i++) { if (ufds[i].revents == POLLIN) { if (ufds[i].fd == get_property_set_fd()) handle_property_set_fd();//处理property service事件 else if (ufds[i].fd == get_keychord_fd()) handle_keychord();//处理keychord事件 else if (ufds[i].fd == get_signal_fd()) handle_signal();//处理signal事件 } } } return 0; }
#on用来声明这是一个Action,early-init是该Action的触发条件,也是它的名称 on early-init #执行命令 start ueventd # create mountpoints #执行命令 mkdir /mnt 0775 root system //省略。。。 #service声明是这是一个Service,servicemanager 是Service名称,/system/bin/servicemanager是程序地址 service servicemanager /system/bin/servicemanager #class、user等都是option class core user system group system critical onrestart restart zygote onrestart restart media service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main socket zygote stream 666 onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netd //....
on property:ro.debuggable=1 start console
exec <path> [ <argument> ]* 执行路径为path的命令,参数是argument export <name> <value> 在全局环境变量中设在环境变量 <name>为<value>。 ifup <interface> 启动网络接口<interface> import <filename> 解析一个init配置文件,扩展当前配置。 hostname <name> 设置主机名。 chmod <octal-mode> <path> 更改文件访问权限。 chown <owner> <group> <path> 更改文件的所有者和组。 class_start <serviceclass> 启动所有指定服务类下的未运行服务。 class_stop <serviceclass> 停止指定服务类下的所有已运行的服务。 domainname <name> 设置域名。 insmod <path> 加载<path>中的模块。 mkdir <path> [mode] [owner] [group] 创建一个目录<path>, mount <type> <device> <dir> [ <mountoption> ]* 试图在目录<dir>挂载指定的设备 setprop <name> <value> 设置系统属性 <name> 为 <value>值. setrlimit <resource> <cur> <max> 设置<resource>的rlimit(资源限制)。 start <service> 启动指定服务(如果此服务还未运行)。 stop <service> 停止指定服务(如果此服务在运行中)。 symlink <target> <path> 创建一个指向<path>的软连接<target>。 sysclktz <mins_west_of_gmt> 设置系统时钟基准(0代表时钟滴答以格林威治平均时(GMT)为准) trigger <event> 触发一个事件。 write <path> <string> [ <string> ]* 打开路径为<path>的一个文件,并写入一个或多个字符串。
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main socket zygote stream 666 onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netd
critical: 说明这是一个对于设备关键的服务。如果一定时间退出多次,系统将会重启并进入recovery(恢复)模式。 disabled:说明这个服务禁用,不会自动启动此服务,但是可以手动启动。 setenv <name> <value> :设置环境变量 在进程启动时将环境变量<name>设置为<value>。 socket <name> <type> <perm> [ <user> [ <group> ] ] 创建一个Uinx域的名为/dev/socket/<name> 的套接字,并传递它的文件描述符给已启动的进程。<type> 必须是 "dgram"或"stream"。User 和 group默认为0。 user <username> 在启动这个服务前改变切换到用户username,此时默认为root。 group <groupname> [ <groupname> ]* 在启动这个服务前改变切换到用户组username,此时默认为root。 oneshot:只启动一次,一旦关闭就不再重启 class <name> 指定一个服务类别。所有同一类的服务可以同时启动和停止。默认为"default"类服务。 onrestart <Command> 当服务重启,执行一个命令
int init_parse_config_file(const char *fn) { char *data; //读取配置文件 data = read_file(fn, 0); if (!data) return -1; //重点是这个函数,解析配置文件 parse_config(fn, data); DUMP(); return 0; }
static void parse_config(const char *fn, char *s) { struct parse_state state;//保存解析状态 char *args[INIT_PARSER_MAXARGS];//存储参数 int nargs;//参数个数 nargs = 0; state.filename = fn;//解析得文件路径 state.line = 0;//当前解析的行号 state.ptr = s;//当前解析的内容 state.nexttoken = 0;//当前解析是那种类型的行,有文件结束T_EOF,新的一行T_NEWLINE,参数T_TEXT state.parse_line = parse_line_no_op;//parse_line_no_op是空操作 for (;;) { switch (next_token(&state)) { case T_EOF://文件结束 state.parse_line(&state, 0, 0); return; case T_NEWLINE://新的一行 state.line++; if (nargs) { int kw = lookup_keyword(args[0]);//是哪一个关键字 if (kw_is(kw, SECTION)) {//如果该关键字是section state.parse_line(&state, 0, 0); parse_new_section(&state, kw, nargs, args);//在这里才真正开始开始解析Section } else { state.parse_line(&state, nargs, args); } nargs = 0; } break; case T_TEXT: if (nargs < INIT_PARSER_MAXARGS) { args[nargs++] = state.text; } break; } } }
void parse_new_section(struct parse_state *state, int kw,int nargs, char **args) { printf("[ %s %s ]\n", args[0],nargs > 1 ? args[1] : ""); switch(kw) { case K_service://如果是Service的Section,开始解析Service state->context = parse_service(state, nargs, args);//保存调用过parse_service的service if (state->context) { state->parse_line = parse_line_service;//parse_line_service 才是真正的解析并填充Service函数 return; } break; case K_on://如果是Action的Section,开始解析Action state->context = parse_action(state, nargs, args);//保存调用过parse_action的action if (state->context) { state->parse_line = parse_line_action;//parse_line_action才是真正的解析并填充Action函数 return; } break; case K_import: if (nargs != 2) { ERROR("single argument needed for import\n"); } else { int ret = init_parse_config_file(args[1]); if (ret) ERROR("could not import file %s\n", args[1]); } } state->parse_line = parse_line_no_op; }
struct service { /* list of all services */ //用于将结构体连接成一个双向链表,init中有一个全局变量service_list,专门保存解析后的service struct listnode slist;//用于将结构体连接成一个双向链表,init中有一个全局变量 const char *name;//名称 const char *classname;//classname,默认是default unsigned flags;//属性标志 pid_t pid;//进程号 time_t time_started; /* time of last start 上次启动时间 */ time_t time_crashed; /* first crash within inspection window 上次异常退出时间 */ int nr_crashed; /* number of times crashed within window 异常退出次数*/ uid_t uid;//用户id gid_t gid;//用户组id gid_t supp_gids[NR_SVC_SUPP_GIDS]; size_t nr_supp_gids; //service使用的socket struct socketinfo *sockets; //service环境变量 struct svcenvinfo *envvars; //service中的onrestart是一个option,但是它后面是一系列的command,可以看做是一个action struct action onrestart; /* Actions to execute on restart. */ /* keycodes for triggering this service via /dev/keychord */ //和keychord有关的 int *keycodes; int nkeycodes; int keychord_id; //io优先级 int ioprio_class; int ioprio_pri; //参数个数 int nargs; /* "MUST BE AT THE END OF THE STRUCT" */ //参数列表 char *args[1]; }
struct listnode { struct listnode * next; struct listnode * prev; };
void list_init(struct listnode *node) { node->next = node; node->prev = node; }
void list_add_tail(struct listnode *head, struct listnode *item) { item->next = head; item->prev = head->prev; head->prev->next = item; head->prev = item; }
static void *parse_service(struct parse_state *state, int nargs, char **args) { struct service *svc;//定义的service结构体,用来保存解析出来的service //异常处理代码 if (nargs < 3) { parse_error(state, "services must have a name and a program\n"); return 0; } if (!valid_name(args[1])) { parse_error(state, "invalid service name '%s'\n", args[1]); return 0; } svc = service_find_by_name(args[1]); if (svc) { parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]); return 0; } nargs -= 2; svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);//为Service分配内存空间 if (!svc) { parse_error(state, "out of memory\n"); return 0; } 为svc结构体填充数据赋值 svc->name = args[1]; svc->classname = "default";//默认为default类比 memcpy(svc->args, args + 2, sizeof(char*) * nargs); svc->args[nargs] = 0; svc->nargs = nargs; svc->onrestart.name = "onrestart"; //Service中的onrestart 是Action类型的链表,初始化该链表 list_init(&svc->onrestart.commands); //将service中的slist加入到service_list 中 list_add_tail(&service_list, &svc->slist); return svc; }
static void parse_line_service(struct parse_state *state, int nargs, char **args) { struct service *svc = state->context;//取出刚才创建的service struct command *cmd; int i, kw, kw_nargs; if (nargs == 0) { return; } svc->ioprio_class = IoSchedClass_NONE;//设置IO优先级 kw = lookup_keyword(args[0]);//配置service中的option关键字 switch (kw) { //...... case K_onrestart://处理onrestart选项 nargs--; args++; kw = lookup_keyword(args[0]); if (!kw_is(kw, COMMAND)) {//如果onrestart 选项后面不是command 提示错误 parse_error(state, "invalid command '%s'\n", args[0]); break; } kw_nargs = kw_nargs(kw); if (nargs < kw_nargs) { parse_error(state, "%s requires %d %s\n", args[0], kw_nargs - 1, kw_nargs > 2 ? "arguments" : "argument"); break; } //service中onrestart option的command序列创建过程 cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs); cmd->func = kw_func(kw); cmd->nargs = nargs; memcpy(cmd->args, args, sizeof(char*) * nargs); list_add_tail(&svc->onrestart.commands, &cmd->clist); break; case K_critical: svc->flags |= SVC_CRITICAL; break; case K_setenv: { /* name value */ struct svcenvinfo *ei; if (nargs < 2) { parse_error(state, "setenv option requires name and value arguments\n"); break; } ei = calloc(1, sizeof(*ei)); if (!ei) { parse_error(state, "out of memory\n"); break; } ei->name = args[1]; ei->value = args[2]; ei->next = svc->envvars; svc->envvars = ei; break; } //如果需要创建socket case K_socket: {/* name type perm [ uid gid ] */ struct socketinfo *si; if (nargs < 4) { parse_error(state, "socket option requires name, type, perm arguments\n"); break; } if (strcmp(args[2],"dgram") && strcmp(args[2],"stream") && strcmp(args[2],"seqpacket")) { parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n"); break; } si = calloc(1, sizeof(*si)); if (!si) { parse_error(state, "out of memory\n"); break; } si->name = args[1]; si->type = args[2]; si->perm = strtoul(args[3], 0, 8); if (nargs > 4) si->uid = decode_uid(args[4]); if (nargs > 5) si->gid = decode_uid(args[5]); si->next = svc->sockets; svc->sockets = si; break; } ... default: parse_error(state, "invalid option '%s'\n", args[0]); } }
标签:
原文地址:http://blog.csdn.net/yujun411522/article/details/46367787