Android init Process analysis (1)

Source: Internet
Author: User

This article describes how action is triggered in the INIT process

One, what is action

In Android, you use the action to manage and execute commands. The action is a data structure that contains the name of the command set command,action. The Android system executes a set of commands through action.

structAction {/*node in list of all actions*/structListNode alist;/*node in the queue of pending actions*/structListNode qlist;/*node in list of actions for a trigger*/structListNode tlist;unsigned Hash;Const Char*name;structListNode commands;structCommand *Current ;};

All actions are made up of a linked list (action_list) through Alist, which is qlist to form an execution queue (action_queue) and tlist is temporarily unused. All commands inside the action are stored in a linked list with the commands as the header, and the current command is pointed to by a struct command.

struct command{/**/struct  listnode clist; int (*func) (intChar * *args); int Nargs; Char *args[1];};

In the command structure above, the clist is used to link all commands under the action, including the function func, the number of arguments Nargs, and the parameter args.

There are two linked headers to link the action:

Static List_declare (action_list); static List_declare (Action_queue);

The first one is the action-linked header, and the second executes the chain header for the action. The action that is read from the init.rc configuration file and set in the code is first added to the Action_list list, and then the action that needs to be executed is added to the Action_queue based on the start time (init,boot stage, etc.). In the main loop of Init, the commands in Action_queue are executed.

Second, how the action is added

There are two ways to add an action to the Action_list and Action_queue lists, the first configured in Init.rc to parse the init.rc configuration file during the Init process startup, and the other for calling functions in code.

First introduce the first way. When the init process starts, the init.rc (and init.xxxx,rc,xxxx as the device name) configuration file is read. INIT.RC defines a specialized format that is divided into three types: command (action), service, import, such as:

On Early-initstart ueventdservice ueventd/sbin/ueventdclass corecriticalimport init.test.rc

As above, the flag starting with on action starts, Early-init is the name of the action, and the following start Ueventd is the action command, you can have more than one command until you encounter other types of definition keywords (this is the service encountered). Services are marked with service start, service name and service executable, the following class core, and so on, can have multiple properties for uevented service, until other types are encountered. Import imports the other files.

In the init process, the configuration file is first read into memory, and then the int init_parse_config_file (const char *FN) function is called to parse the configuration file, and the start is read into the action struct. The commands that follow it are read into the command struct and linked to the action. The action structure is then linked to the Action_list table header.

The action called Early-init is then added to the Action_queue by calling Action_for_each_trigger ("Early-init", action_add_queue_tail) and other functions.

In addition to parsing the configuration file build action, you can also add actions to action_list and action_queue by calling Queue_builtin_action in the code:

voidQueue_builtin_action (int(*func) (intNargs,Char**args),Char*name) {structAction *Act;structCommand *cmd;act= Calloc (1,sizeof(*Act)); Act->name =Name;list_init (&act->commands); CMD= Calloc (1,sizeof(*cmd)); CMD->func =Func;cmd->args[0] =Name;list_add_tail (&act->commands, &cmd->clist); List_add_tail (&action_list, &act->alist); action_add_queue_tail (act);}voidAction_add_queue_tail (structAction *Act) {List_add_tail (&action_queue, &act->qlist);}

The function requests the memory space of the action and command, and then links to Action_list and action_queue.

Third, execution of action

The Action_list contains a series of actions and is added to the Action_queue queue in a certain order. Next, you need to read the action from the Action_queue and execute the command in turn.

In the main loop of Init, the execution action of the command is done:

 for (;;) {int nr, I, timeout =-1; Execute_one_command (); restart_processes ();..... ...if ( !action_queue_empty () | |  0= poll (UFDs, Fd_count, timeout); if 0 )continue; .......

As above, the Execute_one_command call executes only one command and removes the action from Action_queue if all the commands for the full action are executed. In fact, at the beginning of the Init process, a lot of action was added to the queue, so it was necessary to iterate through the calls to Execute_one_command to perform.

The loop is controlled by a action_queue_empty call, and when Action_queue is not empty, the code sets Timeout = 0, which causes the poll (UFDs, Fd_count, timeout) call to return immediately instead of blocking Way to wait. So once an action is added to the Action_queue queue, the entire for loop is cycled multiple times and executes execute_one_command multiple times until the Action_queue is empty.

Android init Process analysis (1)

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.