INIT process analysis for Android startup

Source: Internet
Author: User

 

The first application running after Linux kernel is init,

Init is the next application in Linux. Its source code is in system/CORE/init. Main is the entry of the application. From the main () function, you can know the main functions of init.
Main ()

(1) install the sigchld signal. (If the parent process does not wait for the child process to end, the child process will become a zombie process, occupying system resources. Therefore, the sigchld signal must be processed to recycle the resources of the zombie process to avoid unnecessary waste of resources.
Act. sa_handler = sigchld_handler;

Act. sa_flags = sa_nocldstop;

Act. sa_mask = 0;

Act. sa_restorer = NULL;

Sigaction (sigchld, & act, 0 );

(2) create necessary folders for rootfs and mount appropriate partitions.
Mkdir ("/dev", 0755 );

Mkdir ("/proc", 0755 );

Mkdir ("/sys", 0755 );

Mount ("tmpfs", "/dev", "tmpfs", 0, "mode = 0755 ");

Mkdir ("/dev/PTS", 0755 );

Mkdir ("/dev/socket", 0755 );

Mount ("devpts", "/dev/PTS", "devpts", 0, null );

Mount ("proc", "/proc", "proc", 0, null );

Mount ("sysfs", "/sys", "sysfs", 0, null );

(3) create/dev/null and/dev/kmsg nodes.

Open_devnull_stdio ();

Log_init ();

(4) Parse/init. RC and add all service and operation information to the linked list.
Parse_config_file ("/init. RC ");

(5) extract information kernel startup parameters from/proc/cmdline and save them to global variables.
/* Pull the kernel CommandLine and ramdisk properties file in */

Qemu_init ();

Import_kernel_cmdline (0 );
(6) obtain the hardware information and version number from the global variables obtained in the previous step. If not, extract the information from/proc/cpuinfo and save the information to the global variables.
Get_hardware_name ();
(7) Select/init. % hardware %. RC Based on the hardware information, parse and add the service and operation information to the linked list.
Snprintf (TMP, sizeof (TMP), "/init. % S. RC", hardware );

Parse_config_file (TMP );

(8) execute the command triggered by "early-init" in the linked list.
Action_for_each_trigger ("early-init", action_add_queue_tail );

Drain_action_queue ();
(9) traverse the/sys folder, which is a device addition event generated by the kernel (to automatically generate a device node ).

Device_init ();

{Int FD;

FD = open_uevent_socket ();

If (FD <0) Return-1;

Fcntl (FD, f_setfd, fd_cloexec );

Fcntl (FD, f_setfl, o_nonblock );

Coldboot (FD, "/sys/class ");

Coldboot (FD, "/sys/block ");

Coldboot (FD, "/sys/devices ");

Return FD;

}

(10) initialize the property system and import the initialization property file.
Property_init ();
(11) Obtain Ro. debuggable from the property system. If the value is 1, The keychord is initialized.

Debuggable = property_get ("Ro. debuggable ");

If (debuggable &&! Strcmp (debuggable, "1 "))

Keychord_fd = open_keychord ();

(12) Open the console. If the console is not specified in cmdline, open the saved/dev/console.
If (console [0]) {

Snprintf (TMP, sizeof (TMP), "/dev/% s", console );

Lele_name = strdup (TMP );

}

FD = open (lele_name, o_rdwr );

If (FD> = 0)

Have_console = 1;

Close (FD );
(14) Read/initlogo. RLE (A 565 RLE Compressed bitmap logo). If it succeeds, the logo is displayed in/dev/graphics/fb0. If the logo fails, set/dev/tty0 to text mode, open/dev/tty0, and output the "android" string.
If (load_565rle_image (/initlogo. RLE )){

FD = open ("/dev/tty0", o_wronly );

If (FD> = 0 ){

Const char * MSG;

MSG = "/N" "/N"

A n d r o I d ";

Write (FD, MSG, strlen (MSG ));

Close (FD );

}

}
(15) determine the parameters in the response line and set the parameters in the attribute system:
If (qemu [0])

Import_kernel_cmdline (1 );

If (! Strcmp (bootmode, "Factory "))

Property_set ("Ro. factorytest", "1 ");

Else if (! Strcmp (bootmode, "factory2 "))

Property_set ("Ro. factorytest", "2 ");

Else

Property_set ("Ro. factorytest", "0 ");

Property_set ("Ro. serialno", serialno [0]? Serialno :"");

Property_set ("Ro. bootmode", bootmode [0]? Bootmode: "unknown ");

Property_set ("Ro. baseband", baseband [0]? Baseband: "unknown ");

Property_set ("Ro. Carrier", carrier [0]? Carrier: "unknown ");

Property_set ("Ro. bootloader", bootloader [0]? Bootloader: "unknown ");

Property_set ("Ro. Hardware", hardware );

Snprintf (TMP, prop_value_max, "% d", revision );

Property_set ("Ro. Revision", TMP );
(16) execute all the actions marked as init.
/* Execute all the init actions to get us started */

Action_for_each_trigger ("init", action_add_queue_tail );

Drain_action_queue ();
(17) Start property service

/* Read any property files on system or data and

* Fire up the property service. This must happen

* After the Ro. Foo properties are set above so

* That/data/local. Prop cannot interfere with them.

*/

Property_set_fd = start_property_service ();
(18) create a signal mechanism for sigchld handler.
/* Create a signalling mechanic for the sigchld handler */

If (socketpair (af_unix, sock_stream, 0, S) = 0 ){

Signal_fd = s [0];

Signal_recv_fd = s [1];

Fcntl (s [0], f_setfd, fd_cloexec );

Fcntl (s [0], f_setfl, o_nonblock );

Fcntl (s [1], f_setfd, fd_cloexec );

Fcntl (s [1], f_setfl, o_nonblock );

}

(19) execute all the actions marked as early-boot and boot.
/* Execute all the boot actions to get us started */

Action_for_each_trigger ("early-Boot", action_add_queue_tail );

Action_for_each_trigger ("Boot", action_add_queue_tail );

Drain_action_queue ();

(20) execute all actions marked as property based on the current property status
/* Run all property triggers based on current state of the properties */

Queue_all_property_triggers ();

Drain_action_queue ();
(21) register a polling event:
-Device_fd
-Property_set_fd
-Signal_recv_fd
-If keychord exists, add keychord_fd.
Ufds [0]. FD = device_fd;

Ufds [0]. Events = Pollin;

Ufds [1]. FD = property_set_fd;

Ufds [1]. Events = Pollin;

Ufds [2]. FD = signal_recv_fd;

Ufds [2]. Events = Pollin;

Fd_count = 3;

If (keychord_fd> 0 ){

Ufds [3]. FD = keychord_fd;

Ufds [3]. Events = Pollin;

Fd_count ++;

}

(22) enter the infinite loop to ensure that the INIT process will never exit.

In this loop body, the poll mode Pollin is used to monitor events such as device/property-set/chid-process-exit,

For example, when an SD card is inserted, device_fd in init can receive the card insertion event to create nodes.

In addition, all other important processes are init sub-processes. Once these sub-processes encounter exceptions, the signal_recv_fd of init will receive sigchld and the system will be able to perform subsequent processing.

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.