Android init process startup

Source: Internet
Author: User
Tags symlink

1. Android Init. c Execution Process

After the kernel in Android is started, the kernel starts the first user-level process: init, which is a user-level process started by the kernel. After the kernel is started by itself (it has been loaded into the memory, started to run, and initialized to all the device drivers and data structures), it starts a user-level program init, complete the boot process. Init is always the first process.
PS: You can run the: ps aux | grep init command to check that its Pid is 1.
The code of the init process is in system/core/init. c In the android source code directory.
789 int main (int argc, char ** argv)
790 {
# Create directories in some linux root file systems
817 mkdir ("/dev", 0755 );
818 mkdir ("/proc", 0755 );
819 mkdir ("/sys", 0755 );
820
821 mount ("tmpfs", "/dev", "tmpfs", 0, "mode = 0755 ");
822 mkdir ("/dev/pts", 0755 );
823 mkdir ("/dev/socket", 0755 );
824 mount ("devpts", "/dev/pts", "devpts", 0, NULL );
825 mount ("proc", "/proc", "proc", 0, NULL );
826 mount ("sysfs", "/sys", "sysfs", 0, NULL );
# Enable the standard input, standard output, and standard error file descriptor
834 open_devnull_stdio ();
# Read and parse the init. rc file
838 parse_config_file ("/init. rc ");
# Obtain the hardware name
844 get_hardware_name ();
845 snprintf (tmp, sizeof (tmp), "/init. % s. rc", hardware );
# Read and parse hardware-related init script files
846 parse_config_file (tmp );
# Trigger the action named early-init In the init script file and execute its commands, which is actually: on early-init
848 action_for_each_trigger ("early-init", action_add_queue_tail );
849 drain_action_queue ();
# Initialize dynamic device management. The device file is reflected to the kernel when the device file changes.
852 device_fd = device_init ();
# Load the startup animation. If the animation fails to be opened, print "a n d r o I D" on the screen.
872 if (load_565rle_image (INIT_IMAGE_FILE )){
873 fd = open ("/dev/tty0", O_WRONLY );
874 if (fd> = 0 ){
875 const char * msg;
876 msg = "\ n"
877 "\ n"
878 "\ n"
879 "\ n"
880 "\ n"
881 "\ n"
882 "\ n" // console is 40 cols x 30 lines
883 "\ n"
884 "\ n"
885 "\ n"
886 "\ n"
887 "\ n"
888 "\ n"
889 "\ n"
890 // "a n d r o I D ";
891 write (fd, msg, strlen (msg ));
892 close (fd );
893}
894}
895
# Trigger the action named init In the init script file and execute its commands, which is actually: on init
919 action_for_each_trigger ("init", action_add_queue_tail );
920 drain_action_queue ();
# Start the system property service: system property service
927 property_set_fd = start_property_service ();

# Create a socket to process orphan process signals
930 if (socketpair (AF_UNIX, SOCK_STREAM, 0, s) = 0 ){
931 signal_fd = s [0];
932 signal_recv_fd = s [1];
933 fcntl (s [0], F_SETFD, FD_CLOEXEC );
934 fcntl (s [0], F_SETFL, O_NONBLOCK );
935 fcntl (s [1], F_SETFD, FD_CLOEXEC );
936 fcntl (s [1], F_SETFL, O_NONBLOCK );
937}

# Trigger the action named early-boot and boot in the init script file, and execute its commands, in fact: on early-boot and on boot
948 action_for_each_trigger ("early-boot", action_add_queue_tail );
949 action_for_each_trigger ("boot", action_add_queue_tail );
950 drain_action_queue ();
# The trigger command for starting all attribute changes is actually: on property: ro. xx. xx = xx
953 queue_all_property_triggers ();
954 drain_action_queue ();
# Entering the endless loop
987 (;;){

# Start all the declared services in the init script,
# Example: 266 service servicemanager/system/bin/servicemanager
# User system
# Critical
# Onrestart restart zygote
# Onrestart restart media
994 restart_processes ();
# Multi-channel listener device management, sub-process running status, Attribute Service
1012 nr = poll (ufds, fd_count, timeout );
1013 if (nr <= 0)
1014 continue;
1016 if (ufds [2]. revents = POLLIN ){
1017/* we got a SIGCHLD-reap and restart as needed */
1018 read (signal_recv_fd, tmp, sizeof (tmp ));
1019 while (! Wait_for_one_process (0 ))
1020;
1021 continue;
1022}
1023
1024 if (ufds [0]. revents = POLLIN)
1025 handle_device_fd (device_fd );
1026
1027 if (ufds [1]. revents = POLLIN)
1028 handle_property_set_fd (property_set_fd );
1029 if (ufds [3]. revents = POLLIN)
1030 handle_keychord (keychord_fd );
1031}
1032
1033 return 0;
1034}

 

Ii. Android init script language

I have briefly analyzed the operations in init. c and mentioned the detailed parsing of Android Init script language specifications under init. rc and hardware scripts.

The Android initialization language contains four types of declarations:

Actions, Commands, Services, and Options)

  • All of these are in the unit of action, and various marks are separated by spaces.
  • The C-style backslash can be used to insert spaces between marks.
  • Double quotation marks can also be used to prevent strings from being separated by spaces into multiple marks.
  • The backslash at the end of a line is used to fold rows. The comment line starts with a pound sign (#) (it can start with a space ).

Actions and Services declare a new grouping Section. All commands or options belong to the recently declared group. Commands or options located before the first group will be ignored.
Actions and Services have a unique name. In case of duplicate names, the second statement will be ignored as an error.

Actions)
Actions is actually a series of Commands (Commands ). Actions has a trigger, which is used to determine the execution time of the action. When an event that meets the action trigger condition occurs, the action is added to the end of the execution queue unless it is already in the queue.
Each action in the queue is extracted in sequence, and each command in the action is executed in sequence.
The form of Actions is as follows:
On <trigger>
<Command1>
<Command2>
<Command3>
On is followed by a trigger. When a trigger is triggered, command1, command2, and command3 are executed sequentially until the next Action or next Service.
To put it simply, Actions is a STARTUP script defined by Android at startup. when the conditions are met, the script will be executed. Some commands in the script are commands, and different scripts are differentiated by on.

Triggers)
Triggers (trigger) is a string used to match a specific event type, used to make Actions happen.
Boot:
This is the first Triggers triggered after init is executed ). (After/init. conf (start configuration file) is loaded)
<Name >=< value>:
Triggers in this form will be triggered when the attribute <name> is set to the specified <value>.
Device-added-<path>:
Device-removed-<path>:
Triggers is triggered when a device node file is added or deleted.
Service-exited-<name>:
Triggers in this form will be triggered when a specific service exits.
Triggers are usually used together with on.
Example:
On init
Export LD_LIBRARY_PATH/system/lib
Insmod modules/fsr. ko
Symlink/system/etc
Mkdir/sdcard 0000 system
Write/proc/cpu/alignment 4

On boot
...
On property: ro. kernel. qemu = 1
Start adbd
Three actions are declared above: init, boot, and an attribute trigger. When init is triggered, the subsequent commands are executed in sequence until the new action on boot. The trigger of Init is determined by the action_for_each_trigger function in init. c. When the property ro. kernel. qemu is 1, the start adbd command is triggered.

Services)
A service is a program that starts during initialization and can be restarted upon exit. Services are as follows:
Service <name> <pathname> [<argument>] *
<Option>
<Option>
...
Name: Service name
Pathname: The corresponding program location of the current service
Option: the option set by the current service.

Options)
Options is a modifier of Services. They influence when and how Services run.
Critical:
This is a key service for devices. If he exits more than four times in four minutes, the system will restart and enter the recovery mode.

Disabled:
This indicates that the service cannot be automatically started with the trigger. He must be explicitly started by name.

Setenv <name> <value> (set environment variables)
When the process starts, set the environment variable <name> to <value>.

Socket <name> <type> <perm> [<user> [<group>]
Create a socket named/dev/socket/<name> In the Uinx domain and pass its file descriptor to the started process. <Type> it must be "dgram" or "stream ". The default values of User and group are 0.

User <username>
Change the User Name of the service before starting the service. The default value is root. (??? If possible, it should be nobody by default ). Currently, if your process requires Linux capabilities (capability), you cannot use this command. Even if you are the root user, you must request capabilities in the program ). Then drop it to the uid you want.

Group <groupname> [<groupname>] *
Change the group name of the service before starting the service. Except (required) The first group name, the additional group name is usually used to set the supplemental group (via setgroups () for the process ()). The default value is root. (??? If possible, it should be nobody by default ).

Oneshot
Do not restart when the service exits.

Class <name>
Specify a service class. All services of the same category can be started and stopped at the same time. If you do not use the class option to specify a class, the default class service is "default.

Onrestart
When the service is restarted, execute a command (for details below ).

Commands (command)
Exec <path> [<argument>] *
Create and execute a program (<path> ). Before the program is fully executed, init will be blocked. Because it is not a built-in command, try to avoid using exec, which may cause init to become stuck. (??? Do you need a timeout setting ?)
Export <name> <value>
In the global environment variable, set the environment variable <name> to <value>. (This will be inherited by all processes running after this command)
Ifup <interface>
Start Network interface <interface>
Import <filename>
Parse an init configuration file to extend the current configuration.
Hostname <name>
Set the host name.
Chmod <octal-mode> <path>
Change the file access permission.
Chown <owner> <group> <path>
Change the file owner and group.
Class_start <serviceclass>
Start all services not running under the specified service class.
Class_stop <serviceclass>
Stop all running services under the specified service class.
Domainname <name>
Set the domain name.
Insmod <path>
Load the module in <path>.
Mkdir <path> [mode] [owner] [group]
Create a directory <path> to select mode, owner, and group. If not specified, the default permission is 755 and belongs to the root user and root group.
Mount <type> <device> <dir> [<mountoption>] *
Try to mount the specified device in the directory <dir>. <Device> You can specify an mtd block device in the form of mtd @ name. <Mountoption> including "ro", "rw", "remount", "noatime ",...
Setprop <name> <value>
Set the system attribute to the value of <value>.
Setrlimit <resource> <cur> <max>
Set <resource> rlimit (resource limit ).
Start <service>
Start the specified service (if the service is not running ).
Stop <service>
Stop the specified service (if the service is running ).
Symlink <target> <path>
Create a soft connection <target> pointing to <path>.
Sysclktz <mins_west_of_gmt>
Set the system clock benchmark (0 indicates that the clock tick is based on the Greenwich Mean Time (GMT)
Trigger <event>
Trigger an event. Used to sort one action and another action.
Write <path> <string> [<string>] *
Open a file with the path <path> and write one or more strings.

Properties)
Init updates some system attributes to provide monitoring capabilities for ongoing events:
Init. action
This attribute value is the name of the action being executed. If not, it is "".
Init. command
This attribute value is the name of the command being executed. If not, it is "".
Init. svc. <name>
Status of the service named <name> ("stopped", "running", and "restarting ))

 

 

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.