Android Init Process Analysis init script parsing and processing

Source: Internet
Author: User
Tags unix domain socket
<span id="Label3"></p>(lazy people think of me recently and csdn for a long time.) This Android Init has been lying in my draft box for almost 5 years. Change it slightly to send it out.) RC file format<p><p><span style="color:#000000">RC files are files that are common in Linux that run during the boot-loading Phase. RC is the abbreviation for run COMMANDS. basically, you can understand some of the column commands that run during the startup Phase. This startup script file is also run when the Android init process starts, init.rc. Init.rc the wording of a bit complex, specific to/system/core/init under the Readme file. The basic composition of the script consists of four categories:</span></p></p> <ul> <ul> <li><span style="color:#000000">Commands: command</span></li> <li><span style="color:#000000">Action: actions</span></li> <li><span style="color:#000000">Services: Service</span></li> <li><span style="color:#000000">options: Option</span> <span style="color:#000000">  </span></li> </ul> </ul><p><p><span style="color:#000000">The syntax for the language contains the following conventions:</span></p></p> <ul> <ul> <li><span style="color:#000000">All types of statements are row-based (line-oriented), and one statement consists of several tokens,token separated by a space character.<p></p><p>Suppose a token includes a space character, it needs to be escaped by a c-style backslash (' \ '). Or use a double-lead to bring the whole token together.</p><p>The backslash can also be at the end of a line today. Indicates that the contents of the next line still belong to the current Statement.</p></span></li> <li><span style="color:#000000">The line starting with ' # ' is the gaze line (staring at the first non-empty character that can only be a line, this must be noted).<p></p><p></p></span></li> <li><span style="color:#000000">The actions (ACTIONS) and services statements implicitly indicate the beginning of a new paragraph (section).<p></p><p>All instructions (commands) and options are vested in a recent paragraph Above. The instructions (commands) and options before the first paragraph are invalid.</p><p></p></span></li> <li><span style="color:#000000">The Action (actions) and service (services) have unique Names. Assume that there is a duplicate Name. Then the definition that appears will be ignored as an error "right now or ignore, not overwrite!"<p></p><p>】。</p></span></li> </ul> </ul><p><p>In fact. Strictly speaking, there should be only two categories of action and service:</p></p> <ul> <ul> <li>Action is made up of a bunch of command under a specific trigger</li> <li>Service is made up of a bunch of daemon services described in the options Description.<br></li> </ul> </ul><p><p><span style="color:#000000"><strong>Actions (action)</strong></span></p></p><p><p>An action is a sequence of named commands (commands). Each action (action) defines a trigger condition (trigger). Used to indicate when to run this Action.</p></p><p><p>In the actual processing. When an event that matches an Action's trigger Occurs. The command under this action will be added to the end of a queue that is about to be run (unless it is already in the queue).<br>Each command in the queue is checked out and Run.</p></p><p><p></p></p><p><p>The form of an action definition such as the following:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code">On <trigger> <command> <command> <command></pre></pre><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>Service (services)</strong></p></p><p><p>Services are some of the programs that the initialization program needs to start, and it is possible for the initialization program to restart them after the programs have Exited. Services take the form of a service definition such as the following:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code">Service <name> <pathname> [<argument>]* <option> <option> ...</pre></pre><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><strong>Option (options)</strong></p></p><p><p>Options affect the timing and method of controlling the initialization program's execution of the Service. Possible options such as the following table (note that the options are only part of the service):</p></p><p><p><span>Description</span></p></p><p align="left" class="MsoNormal"><p align="left" class="MsoNormal"><span lang="EN-US">Disabled<br>When the service starts its class, it does not initiate it on its own, but only when it is clearly indicated that it will start when the service is started (usually each service will belong to a class, described by Classkeyword Description)<br><br>Sockets <name> <type> <perm> [<user> [<group>]]<br>Create a UNIX domain socket named/dev/socket/<name> and pass its FD to the launched Process. Valid<type> values Includedgram andstream.user andgroup default to 0.<br><br>User <username><br>Change the user of the process before exec starts the Service. The default user is root<br><br>Group <groupname> [<groupname>]*<br>Cycle exec before starting the Service. Change the group for this service process. The default group is Root. Add a service to a specific group to get some specific permissions.<br><br>Capability [<capability>]+<br>Set Linux capability before exec ' ing this service<br><br>OneShot<br>The service runs only once. For Non-daemon Programs. It is necessary to use Onshot Mode.<br><br>Class <name><br>Set the class name of the service, all services must be set one, assuming not set. Defaults to the default class. A service in the same class. Can be started uniformly by command Class_start.</span></p></p><p><p></p></p><p align="left" class="MsoNormal"><p align="left" class="MsoNormal"><span lang="EN-US"><br></span></p></p><p align="left" class="MsoNormal"><p align="left" class="MsoNormal"><strong>Trigger (<span lang="EN-US">Triggers</span>)</strong><span lang="EN-US"></span></p></p><p align="left" class="MsoNormal"><p align="left" class="MsoNormal"><span>a trigger is a string that is used to match a specific event. These events trigger</span><span lang="EN-US"></span><span>The operation of the action to which the trigger belongs. </span><span lang="EN-US"></span></p></p><p><p><span lang="EN-US"><br></span><strong>Directive (<span lang="EN-US">Commands</span>)</strong><span lang="EN-US"></span></p></p><p><p>Which directives are Supported. Each Android version number is Different. For details, please refer to the Readme or the best code under/system/core/init.</p></p><p><p><span lang="EN-US"><br></span></p></p><p align="left" class="MsoNormal"><p align="left" class="MsoNormal"><strong>Attributes (<span lang="EN-US"></span>properties)</strong><span lang="EN-US"></span></p></p><p align="left" class="MsoNormal"><p align="left" class="MsoNormal"><span>Initialization Program (</span> <span lang="EN-US">Init</span> <span>) can change the properties of some systems as Needed. </span>the three words in the readme:<br>Init.action<br>Equal to the name of the action currently being executed or "" if None.</p></p><p><p>Init.command<br>Equal to the command being executed or "" if None.<br><br>Init.svc.<name><br>State of a named service ("stopped", "running", or "restarting").</p></p><p><p><span lang="EN-US"><span>The actual init process sees only the update init.svc.<name>, and each service creates ONE. Ability to pass Getprop | grep init.svc See the status of each service, when the state is switched on, with a time to Update.<br></span></span></p></p><span lang="EN-US"><span lang="EN-US"><span></span></span></span><p class="MsoNormal"><p class="MsoNormal"></p></p><pre class="cpp" name="code"><pre class="cpp" name="code">void Notify_service_state (const char *name, const char *state) { char pname[prop_name_max]; int len = strlen (name); If (len + ten) > prop_name_max) return; snprintf (pname, sizeof (pname), "init.svc.%s", name); Property_set (pname, state);}</pre></pre><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><span lang="EN-US"><span>The other two is meaningless, the actual also useless, may originally design has, but finally also useless (comman/action all is the boot time runs, and the duration is very short, not much use).<p><p></p></p><p><p>Init's Parser</p></p><p><p>Init's parser is simpler than its own active machine, the code is not complicated, the main parser is three parts:</p></p> <ol> <ol> <li>Service: the descriptive narrative and attributes of each service, parser good, put in the service_list, so that later through the actions of the command to Start.<p></p><p></p></li> <li>Actions:parser each action, on xxx and subsequent command. is an Action. This parser good after. It is also placed in the action_list in order to start in init.</li> <li>Import:init.rc can support the same as include in c, including other init.rc. This support is relatively simple, not equivalent to inserting the import code in the import Location. But after the Lord Init.rc all parser Good. Then parser the RC files that are included in Turn.<p></p><p><br></p></li> </ol> </ol><p><p><br></p></p>Init.rc run of script commands<p><p>After parsing the script, the actions will be pushed into the action_queue in chronological order, and then run one after the Other.</p></p><p><p><br>Actions follow the chronological order of the trigger and run in Turn. The order of the default trigger is as Follows:</p></p><pre class="cpp" name="code"><pre class="cpp" name="code">Action_for_each_trigger ("early-init", action_add_queue_tail); Action_for_each_trigger ("init", action_add_queue_tail); 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); </pre></pre><p><p>So. Each command requires special attention to handle the Dependencies. For example, in the Init phase. It is impossible to access the data Partition. Also pay attention to the Other. In the same phase, the import RC file is run later (according to the parser order of the import file).</p></p><p><p><br><br>The start time of the Service. Note that it is also run with the command in Action. For example, on the boot stage:<br></p></p><pre class="html" name="code"><pre class="html" name="code">On boot ... Class_start Core</pre></pre><p><p><br>Such When running to this command at the boot Stage. All services that belong to the core will be launched one by One.</p></p><p><p></p></p>  <p><p>Android Init Process Analysis init script parsing and processing</p></p></span></span></span>
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.