Android Start-Up Process Analysis (vii) INIT.RC parsing

Source: Internet
Author: User

#############################################

This article is extremely ice original, reproduced please indicate the source

#############################################

Init.rc's parsing process is the most complex and difficult part of the Android launcher that I think of.

Although its content is very small, but it contains a lot of processing, then we will slowly analyze.

After the previous analysis, we know that after reading the init.rc file, the data is saved to the array, passed to the Parse_config function.

Let's take a look at the processing of the Parse_config function:

static void Parse_config (const char *FN, char *s)//For init.rc only, FN points to the init.rc of this file, and S points to the array of data {struct parse_state   State    Parse_state structure struct ListNode import_list;    struct ListNode *node;    Char *args[init_parser_maxargs];    int Nargs;    Nargs = 0; State.filename = fn;   The init.rc file State.line = 0 is identified as parsing;   Set the initialized line to 0 state.ptr = s;  PTR points to the first element of s state.nexttoken = 0;  Set Nexttoken to 0 state.parse_line = Parse_line_no_op; The parsing is set to Parse_line_no_op, that is, no processing is required.    It is important to note that Parse_line is a function pointer.   List_init (&import_list); Initialize the list of previous import State.priv = &import_list;    The Priv points to the linked list of the initialized thick import. for (;;) {///Start parsing file switch (Next_token (&state)) {///Next_token function is parsed for the state->ptr pointer, reading the contents of the data array backwards, if read to "\ n "," 0 "words, return t_eof and T_newline, if read out is a word, then the content is stored in the args array, the contents of the case backward case t_eof://If the file reads the end of the time, State.parse_ Line (&state, 0, 0); If the file is empty, then the function executed is parse_line_no_op, if not empty, then the parse_line_action is executed.or service, and in these two functions, if Nargs is 0, it will be returned.  Goto Parser_done;   Go to parser do case t_newline:state.line++;                If "\ n" is encountered, State.line will be 1 rows if (Nargs) {///if Nargs has a value, this line needs to be parsed. int kw = Lookup_keyword (Args[0]);                    Use the first keyword in this line, Args[0], to get to kw if (kw_is (kw, section)) {//If this kw is a section, it will return true, if not, it will return false. State.parse_line (&state, 0, 0);                \ \ Clears the current parse line and opens a new section parse_new_section (&state, KW, Nargs, args); } else {state.parse_line (&state, Nargs, args);//If it is not a section, pass Nargs and args as arguments to parse_line corresponding function Go} nargs = 0;            After a row has been executed, the Nargs is set to 0 because new content needs to be read into args.        } break; Case T_text:if (Nargs < Init_parser_maxargs) {args[nargs++] = State.text; \ \ Every token is taken out,        It will be put into an array of args, and Nargs will automatically +1} break;  }  }parser_done: \ \ At the end of the file, will go to parse_done    List_for_each (node, &import_list) {//This will go through all the Import_ List node          struct Import *import = node_to_item (node, struct import, list); Remove these import nodes          int ret;         info ("Importing '%s '", import- >filename);         ret = Init_parse_config_file (import->filename); Continue parsing these files         &NBSP;IF (ret)              error ("could not Import file '%s ' from '%s ' \ n ',                   import->filename, fn );   }}
After reading the above analysis, we will have a strong interest in the following two functions, namely
Parse_new_section,kw_is
And the state.parse_line of a function pointer.

Then let's look at the kw_is function first:

The function prototypes are as follows:

#define KW_IS (kw, type) (Keyword_info[kw].flags & (Type))

And what is Keyword_info?

static 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"};
We see that this is actually a struct, but when initializing the struct, the rest of the value is read from the keywords.h, in addition to the first value being placed in the K_unknown.

Then let's see what's in Keywords.h.

#define KEYWORD (symbol, flags, Nargs, func) k_# #symbol, enum {k_unknown, #endif 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 (critical, option, 0, 0) KEYWORD (disabled, option, 0, 0) keywo  RD (domainname, command, 1, do_domainname) KEYWORD (Enable, command, 1, do_enable) KEYWORD (exec, command,    1, do_exec) KEYWORD (export, COMMAND, 2, Do_export) KEYWORD (group, OPTION, 0, 0) KEYWORD (hostname, command, 1, do_hostname) KEYWORD (ifup, command, 1, do_ifup) KEYWORD (insmod, command, 1, do_insmod) K Eyword (Import, section, 1, 0) KEYWORD (keycodes, OPTION, 0, 0) KEYWORD (mkdir, COMMand, 1, Do_mkdir) KEYWORD (mount_all, command, 1, do_mount_all) KEYWORD (Mount, command, 3, Do_mount) keyw ORD (on, section, 0, 0) KEYWORD (oneshot, option, 0, 0) KEYWORD (onrestart, option, 0, 0) KEYWORD (p Owerctl, command, 1, do_powerctl) KEYWORD (restart, command, 1, Do_restart)

See here, we can look at the section that is interested in what the section is, you can see that Import,on,service is the three only section.

This corresponds to the syntax in front of the init.rc, these three are the main keywords.

If parsing to a new file starts with these three keywords, the function will be executed after clearing the current function.

Parse_new_section


Then let's go to see Parse_new_section's implementation:

static 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: \ \ If it starts with a service State->context = Parse_service (state, Nargs, args); \ \ Perform the initialization of the parse service, and later parse if (state->context) {state->parse_line = Parse_line_service;//        The function pointer of Parse_line is set to Parse_line_service, which is called when the function return is called.    } break;  Case K_ON: \ \ If it starts with on state->context = Parse_action (state, Nargs, args); Perform initialization preparation for parse_action, followed by analysis if (state->context) {state->parse_line = parse_line_action;//P            The function pointer of Arse_line is set to Parse_line_action, which is executed when it is called.        Return    } break; Case K_import://If it starts with import parse_import (state, Nargs, args);    Import this file break; } state->parse_line = Parse_line_no_op; If it weren't for these three keywords, we wouldn't do it.Processing. }
As you can see from the above, the most important task in addition to the initialization of the parse Action,service is to initialize the pointer to the function.

This allows the function pointer to be invoked unless the keyword is executed to the next section.

In the next two articles, we will analyze how to parse the action and service two key sections

Android Start-Up Process Analysis (vii) INIT.RC parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.