Parsing and sharing of init. rc files in Android

Source: Internet
Author: User
Tags symlink

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/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

[Cpp]
# DefineT_EOF 0
# DefineT_TEXT 1
# DefineT_NEWLINE 2

# DefineT_EOF 0
# DefineT_TEXT 1
# DefineT_NEWLINE 2
2.2 keyword Definition

[Cpp]
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)

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

[Cpp]

Copy codeThe Code is as follows: 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 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;
};

[Cpp]

View plaincopyprint?

Copy codeThe Code is as follows: struct command
{
/* List of commands in an action */
Struct listnode clist;

Int (* func) (int nargs, char ** args );
Int nargs;
Char * args [1];
};

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 hangs it on action_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, hangs it on action_list, and appends it to the end of action_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) add act-> alist to the end of the action_list column.

In this way, action is created and added to action_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 the end of the act-> commands 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 (): append the matched action in the action_list queue to the end of action_queue;

Queue_builtin_action () combines the executed functions into a command, creates an action, hangs it on action_list, and appends it to the end of action_queue.

3.4 Command Execution

Execute_one_command () in the infinite loop of Init: system/core/init. c

1) Remove structaction * act from action_queue 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

-Init. in rc, The on <trigger> or service <name> <pathname> [<argument>] * line indicates the start of a new section. [See the keyword definition in section 2.2, only on and service] are supported when the data type is 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:

[Cpp]

On <trigger>
<Command>
<Command>
<Command>

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.

Related Article

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.