Android-init process (1)

Source: Internet
Author: User

Android-init process (1)

The init process is the first process pid started by android. 1. It mainly does the following:

* Parse the configuration file

* Perform the early_init init early_boot boot operation based on the configuration file.

* Set Property Services


This section describes how to parse the init. rc file and run zygote.

1. parse the init. rc configuration file

/** Init. c */

In the main function, execute the following function:

init_parse_config_file("/init.rc");
/** Init_parse.c */
int init_parse_config_file(const char *fn){    char *data;    data = read_file(fn, 0);    if (!data) return -1;    parse_config(fn, data);    DUMP();    return 0;}static void parse_config(const char *fn, char *s);

The parse_config function mainly finds the node of a SECTION and starts parsing if (kw_is (kw, SECTION) {state. parse_line (& state, 0, 0); parse_new_section (& state, kw, nargs, args );}

The kw_is function executes the keywords file and identifies the key _ # symbol

/** Keywords. h */

1> define enumeration. The enumerated value is k_class k_mkdir.
2> define the struct array keyword_info, change the KEYOWRD macro to the struct, and use the macro definition to find out the file for parsing the init. rc with symbol and func.
If the symbol of the keyword section is chdir, find the declaration such as int do_chdir (int nargs, char ** args) to view the corresponding definition.
>>> Keywords: OPTION, COMMAND, and SECTION
/** Init. rc */
On init # indicates that the SECTION on is named init. The basic step is early-init early-boot.
# Start from a section and end before the start of the next section.

Export ANDROID_ROOT/system # indicates that export is a COMMAND

* For example




2. Analyze zygote

/** Init. rc configuration file */

In this file, zygote is defined as a sub-process of init that requires 4 operations and has a socket of 660. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPjxicj4KPC9wPgo8cHJlIGNsYXNzPQ = "brush: java; "> service zygote/system/bin/app_process-Xzygote/system/bin -- zygote -- start-system-server class main socket zygote stream 660 root system onrestart write/sys/android_power/request_state wake onrestart write/sys/power/state on onrestart restart media onrestart restart netd
--------------- Service definition information ------------------
-Maintain a two-way linked list listnode for all services generated
-The name of service zygote is init. svc. zygote.
-ClassName: default "default"
-Attribute flag pid uid gid io priority parameter (Quantity)
-Time_started time_crashed nr_crashed Total number of deaths at last startup
-Socketinfo (socket environment variable information) svcenvinfo (environment variable information required by the process)
-Struct action onrestart; Save the COMMAND information after OPTION
------------------------------------------------

--------------- Onrestart definition information ------------------

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 a trigger */struct listnode tlist; unsigned hash; const char * name; struct listnode commands; struct command * current; // Save the COMMOND action in restart };

------------------------------------------------


/** Init_parse.c */
# Mainly execute parse_service and parse_line_service
// Create the main framework of the service struct and add it to the service_list bidirectional linked list.
// Socketinfo is a one-way linked list zygote with only one 660 TCP scoket
// Onrestart points to a commonds linked list zygote through commond. As shown above in init. rc, four commond

static void *parse_service(struct parse_state *state, int nargs, char **args){struct service *svc;svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);    if (!svc) {        parse_error(state, "out of memory\n");        return 0;    }    svc->name = args[1];    svc->classname = "default";    memcpy(svc->args, args + 2, sizeof(char*) * nargs);    svc->args[nargs] = 0;    svc->nargs = nargs;    svc->onrestart.name = "onrestart";    list_init(&svc->onrestart.commands);    list_add_tail(&service_list, &svc->slist);    return svc;}

Static void parse_line_service (struct parse_state * state, int nargs, char ** args) {struct service * svc = state-> context; nargs --; args ++; kw = lookup_keyword (args [0]); // create the Commond struct cmd = malloc (sizeof (* cmd) + sizeof (char *) * nargs ); cmd-> func = kw_func (kw); cmd-> nargs = nargs; memcpy (cmd-> args, args, sizeof (char *) * nargs ); // Add list_add_tail (& svc-> onrestart. commands, & cmd-> clist );}

* ********* Init starts zygote **********
1) init. c: main>
// Add the boot commond to the execution queueBecause zygote is included in the boot action
Action_for_each_trigger ("boot", action_add_queue_tail );
// Execute the commond in the queue and uniformly execute the commond of all sections.
Queue_builtin_action (queue_property_triggers_action, "queue_property_triggers ");
2) Bultins. c: do_class_start>
// Because class_start is a COMMOND, It will be executed here. zygote is a "default" className
// Execute the service_start_if_not_disabled function.

// If the flag of the service is explicitly set to disable, the service must be executed separately.
Service_for_each_class (args [1],Service_start_if_not_disabled);
3) Bultins. c ::Service_start_if_not_disabled>
If (! (Svc-> flags & SVC_DISABLED )){
Service_start (svc, NULL); // if no flags has been created before, this indicates that the service has not been enabled to execute service_start.
}
4) init. c: service_start>
> Identify the file Process/System/bin/processWhether the file exists. Generally, the service has its own process.
Rc = security_compute_create (mycon, fcon, string_to_security_class ("process"), & scon );
> Start the init sub-process
Pid = fork ();
> Add process environment variable information
> Add the socket environment variable information and create a socket
> Set uid gid and start the main function of the/system/bin/app_process file.

> Set the service startup time pid.


* ********* Init restart zygote **********
> Bind an operation RESPONSE event
1) init. c: main>
Queue_builtin_action (signal_init_action, "signal_init ");
2) init. c: signal_init_action>
Signal_init ();
3) signal_handler: signal_init>
Struct sigaction act; // bind two methods: sigchld_handler handle_signal. Its struct also includes a sa_flags flag.
> After zygote's death, the parent process init calls sigchld_handler.
1) signal_handler: sigchld_handler>
Write (signal_fd, & s, 1); // write data to signal_fdSignal_fd is one of the two sockets in socketpair.
2) init. c: main>
Nr = poll (ufds, fd_count, timeout );
If (nr <= 0)
Continue;
For (I = 0; I <fd_count; I ++ ){
If (ufds [I]. revents = POLLIN ){
Else if (ufds [I]. fd = get_signal_fd ())
Handle_signal ();
}
}
3) signal_handler: handle_signal>
Read (signal_recv_fd, tmp, sizeof (tmp ));
While (! Wait_for_one_process (0 ));
4) signal_handler: wait_for_one_process>
// Kill all child processes of zygote
// Clear the scoket to be rebuilt
// Add commond (4 in zygote) of all onrestart to the action struct list in svc
// Change the svc status to restarting
5) init. c: main>
// Poll and execute main in the next loop
Execute_one_command (); // execute all COMMOND

Restart_processes (); // change the flag ID


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.