Parsing the init. RC file in Android

Source: Internet
Author: User

Tian haili @ csdn

 

This article analyzes how to parse the init. RC file in Android, focuses on Parsing in on action, and summarizes the syntax specification of init. RC from the parsing process.

 

Parsing init. RC is performed in parse_config (): [system/CORE/init/init_parser.c. For details about the phase that occurs in the entire process of init, see Android INIT process Startup Process Analysis.

 

I. parsing process

1. Scan the token in init. RC.

Find the end EOF/Text text/newline, and the spaces '', '\ t', and' \ R' are ignored, and the lines starting with # are ignored;

For text, spaces '', '\ t',' \ R', and '\ n' are the end signs of text.

 

2. Add each text token to The args [] array.

 

3. When a new line ('\ n') is encountered, use ARGs [0] to retrieve matching keywords through lookup_keyword;

1) for section (on and service), call parse_new_section () for parsing:

-For the on section, call parse_action () and set the parsing function parse_line to parse_line_action ()

-For the Service Section, call parse_service () and set the parsing function parse_line to parse_line_service ()

2) Call parse_line ()

That is,

-For the command lines in the on section, call parse_line_action () for parsing;

-For command lines in the Service Section, call parse_line_service () for parsing.

 

II. Key Data Type prototype and key data definition

 

2.1 token Definition

#defineT_EOF 0#defineT_TEXT 1#defineT_NEWLINE 2

 

2.2 keyword Definition

    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(console,     OPTION, 0, 0)    KEYWORD(critical,    OPTION, 0, 0)    KEYWORD(disabled,    OPTION, 0, 0)    KEYWORD(domainname,  COMMAND, 1, do_domainname)    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)    KEYWORD(import,      COMMAND, 1, do_import)    KEYWORD(keycodes,    OPTION, 0, 0)    KEYWORD(mkdir,       COMMAND, 1, do_mkdir)    KEYWORD(mount,       COMMAND, 3, do_mount)    KEYWORD(on,          SECTION, 0, 0)    KEYWORD(oneshot,     OPTION, 0, 0)    KEYWORD(onrestart,   OPTION, 0, 0)    KEYWORD(restart,     COMMAND, 1, do_restart)    KEYWORD(service,     SECTION, 0, 0)    KEYWORD(setenv,      OPTION, 2, 0)    KEYWORD(setkey,      COMMAND, 0, do_setkey)    KEYWORD(setprop,     COMMAND, 2, do_setprop)    KEYWORD(setrlimit,   COMMAND, 3, do_setrlimit)    KEYWORD(socket,      OPTION, 0, 0)    KEYWORD(start,       COMMAND, 1, do_start)    KEYWORD(stop,        COMMAND, 1, do_stop)    KEYWORD(trigger,     COMMAND, 1, do_trigger)    KEYWORD(symlink,     COMMAND, 1, do_symlink)    KEYWORD(sysclktz,    COMMAND, 1, do_sysclktz)    KEYWORD(user,        OPTION, 0, 0)    KEYWORD(wait,        COMMAND, 1, do_wait)    KEYWORD(write,       COMMAND, 2, do_write)    KEYWORD(copy,        COMMAND, 2, do_copy)    KEYWORD(chown,       COMMAND, 2, do_chown)    KEYWORD(chmod,       COMMAND, 2, do_chmod)    KEYWORD(loglevel,    COMMAND, 1, do_loglevel)    KEYWORD(ioprio,      OPTION, 0, 0)

 

2.3 struct action and struct command

struct action {        /* node in list of all actions */    struct listnode alist;        /* node in the queue of pending actions*/    struct listnode qlist;        /* node in list of actions for atrigger */    struct listnode tlist;     unsigned hash;    const char *name;       struct listnode commands;    struct command *current;};

 

struct command{        /* list of commands in an action */    struct listnode clist;     int (*func)(int nargs, char **args);    int nargs;    char *args[1];};

 

2.4 list action_list and action_queue

Action_list

When parsing init. RC, The on action is added through Act-> alist;

Queue_builtin_action () combines the executed functions into a command, creates an action, and hangsAction_list.

Action_queue

Execute action_for_each_trigger () and add it through Act-> qlist;

Queue_builtin_action () combines the executed functions into a command, creates an action, and hangsAction_listAnd appendAction_queue.

 

Iii. Action Parsing

In combination with the init Startup Process and the analysis of init. RC described earlier, we will summarize the analysis of init on the action in init. RC.

 

3.1 action parsing in on Section

When parse_action () is called by parsing a new on section in 1.3.1, struct action * Act is applied. Set:

1) Act-> name is the name of on section (for example, boot/fs /);

2) initialize List Act->Commands;

3) Change Act->AlistAddAction_listEnd of Column

In this way, the action is created and addedAction_list.

 

3.2 parsing command in action in on Section

Call parse_line_action () for the command in the action in the on section ()

1) Search for the keyword, check whether it is command, and check whether the number of parameters is correct

2) request struct command * cmd

-Cmd-> func is obtained from the keyword table;

-Set the number of parameters to cmd-> nargs, and copy the parameters to cmd-> args;

-Add cmd-> clist to act->CommandsEnd of Column

In this way, the command is added to the action.

 

3.3 Add the action in action_list to action_queue

Action_for_each_trigger ()Action_listAnd appendAction_queueTeam end;

Queue_builtin_action () combines the executed functions into a command, creates an action, and hangsAction_listAnd appendAction_queue.

 

3.4 Command Execution

Execute_one_command () in the infinite loop of init: System/CORE/init. c

1) slaveAction_queueRemove structaction * Act and assign it to cur_action;

2) obtain the struct command * From cur_action and assign it to cur_command;

3) execute cur_command-> func (cur_command-> nargs, cur_command-> ARGs)

 

In the preceding steps, 1, 2, and 3 are executed at a time, and 4 are infinite loops. The action is removed from action_queue, and the command is obtained from the action, and then the command is executed.

 

Iv. Summary of init. RC syntax

The init. RC syntax is described in system/CORE/init/readme. I have read this readme file before analyzing the init source code, but I am not quite clear about some concepts. After analyzing the parsing of init. RC, we will try to sort out the init. RC syntax.

1. # The line at the beginning is ignored for comments;

2. '', '\ t', and' \ R' are ignored, so if the attribute contains spaces, the following will not be recognized; there is no syntax requirement for the indentation before command in each action, but it is easy for people to read;

3. '\ n' indicates the line feed. In the init syntax, the start of new Parsing is based on the new line and is scanned and parsed line by line;

4. Concepts: Section/Action/command/trigger

-In init. RC, The on <trigger> or service <Name> <pathname> [<argument>] * line indicates a newSection[See the keyword definition in section 2.2. Only on and service are supported for section.]

-In case of on <trigger>, trigger is the trigger condition and the timing of occurrence. It can be early-init/early-fs/post-fs/early-boot/boot, or property: <name >=< value>, when the value of property <Name> is set to <value>, when device-added-<path>/device-removed-<path> device nodes are added or removed; service-Exited-<Name> when the service exits.

-When the on <trigger> action is executed, that is, the part after the on <trigger> action can contain multiple commands;

-What commands are supported for each line of command? refer to the keyword with the type of command defined in the keyword 2.2.

 

The format is as follows:

on <trigger>    <command>    <command>    <command>

This is a section; all <command> are called actions.

 

Summary

This article analyzes the basic syntax of init. RC, focuses on Parsing on section, service parsing, and property support.

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.