AndroidInitProcess分析心得(2)
print?//\system\core\init\init_parser.c
int lookup_keyword(const char *s)
{
switch (*s++) {
case 'c':
if (!strcmp(s, "opy")) return K_copy;
if (!strcmp(s, "apability")) return K_capability;
if (!strcmp(s, "hdir")) return K_chdir;
if (!strcmp(s, "hroot")) return K_chroot;
if (!strcmp(s, "lass")) return K_class;
if (!strcmp(s, "lass_start")) return K_class_start;
if (!strcmp(s, "lass_stop")) return K_class_stop;
if (!strcmp(s, "lass_reset")) return K_class_reset;
//......
}
return K_UNKNOWN;
}
//\system\core\init\init_parser.c
int lookup_keyword(const char *s)
{
switch (*s++) {
case 'c':
if (!strcmp(s, "opy")) return K_copy;
if (!strcmp(s, "apability")) return K_capability;
if (!strcmp(s, "hdir")) return K_chdir;
if (!strcmp(s, "hroot")) return K_chroot;
if (!strcmp(s, "lass")) return K_class;
if (!strcmp(s, "lass_start")) return K_class_start;
if (!strcmp(s, "lass_stop")) return K_class_stop;
if (!strcmp(s, "lass_reset")) return K_class_reset;
//......
}
return K_UNKNOWN;
}
由此段程序代码可以发现此函数会根据存入的字符串先由字符串的第一个字符做分类, 再去比较之后的字符去回传相对应的K_xxx.之后再将keyword传入kw_is作判断,Google在这里的判断设计kw_is会利用macro而不是用一般的函数去实作, 一是因为这个判断动作只有单纯的做比对,另外一点就是会少一次functioncall stack的效能以加快执行速度.
[cpp]
//system\core\init\init_parser.c
#define kw_is(kw, type) (keyword_info[kw].flags & (type))
//system\core\init\init_parser.c
#define kw_is(kw, type) (keyword_info[kw].flags & (type))
这里会发现是用keyword_info数组中的元素所带出来的flags跟type作&比较.
[cpp]
//system\core\init\init_parser.c
#define KEYWORD(symbol, flags, nargs, func) \
[ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
struct {
const char *name;
int (*func)(int nargs, char **args);
unsigned char nargs;
unsigned char flags;
} keyword_info[KEYWORD_COUNT] = {
[ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
#include "keywords.h"
};
//system\core\init\keywords.h
//...
KEYWORD(capability, OPTION, 0, 0)
KEYWORD(chdir, COMMAND, 1, do_chdir)
KEYWORD(chroot, COMMAND, 1, do_chroot)
KEYWORD(class, OPTION, 0, 0)
KEYWORD(class_start, COMMAND, 1, do_class_start)
KEYWORD(class_stop, COMMAND, 1, do_class_stop)
KEYWORD(class_reset, COMMAND, 1, do_class_reset)
KEYWORD(console, OPTION, 0, 0)
//....
//system\core\init\init_parser.c
#define KEYWORD(symbol, flags, nargs, func) \
[ K_##symbol ] = { #symbol, func, nargs + 1, flags, },
struct {
const char *name;
int (*func)(int nargs, char **args);
unsigned char nargs;
unsigned char flags;
} keyword_info[KEYWORD_COUNT] = {
[ K_UNKNOWN ] = { "unknown", 0, 0, 0 },
#include "keywords.h"
};
//system\core\init\keywords.h
//...
KEYWORD(capability, OPTION, 0, 0)
KEYWORD(chdir, COMMAND, 1, do_chdir)
KEYWORD(chroot, COMMAND, 1, do_chroot)
KEYWORD(class, OPTION, 0, 0)
KEYWORD(class_start, COMMAND, 1, do_class_start)
KEYWORD(class_stop, COMMAND, 1, do_class_stop)
KEYWORD(class_reset, COMMAND, 1, do_class_reset)
KEYWORD(console, OPTION, 0, 0)
//....
由上面便可以知道keyword_info是一个Mappingtable. 每一个lookup_keyword 函数所回传的keyword K_xxx对应一个函数do_xxx.
因此便可以再从parse_config函数中的呼叫lookup_keyword继续分析下去.
[cpp]
// system\core\init\init_parser.c
int kw = lookup_keyword(args[0]);
if (kw_is(kw, SECTION)) {
state.parse_line(&state, 0, 0);
parse_new_section(&state, kw, nargs, args);
} else {
state.parse_line(&state, nargs, args);
}
// system\core\init\init_parser.c
int kw = lookup_keyword(args[0]);
if (kw_is(kw, SECTION)) {
state.parse_line(&state, 0, 0);
parse_new_section(&state, kw, nargs, args);
} else {
state.parse_line(&state, nargs, args);
}
由之前针对lookup_keyword和kw_is的分析就可以知道, 这里是要从init.rc中捞出keyword再利用kw_is的判断来做不同parser方式. 由上面的程序代码可以知道, 只有在keyword_infomapping table中有SECTION flag, 才会有新的list. 而在(android4.2)目前的版本中从keywords.h所有的KEYWORD可知道, 有SECTIONflag对应到的字符只有import, on, service. 撇开import字符是用来作类似include的动作之外,on跟service就是用来建立之前所提的actionlist和service list. 接下来分析parse_new_section函数
[cpp]
//system\core\init\init_parser.c
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:
state->context = parse_service(state, nargs, args);
&nb
补充:移动开发 , Android ,