The second day of Android's first century

Source: Internet
Author: User

The next day, Google said that the system would be operating if it was deserted. It Was Linux.

-- Xxx

The land is about to become fertile, and Linux is already running ....

 

I will not talk about Linux much. Here I will only talk about Linux after the kernel has been changed by Google.

 

At the end of the first day, the kernel init has finished its work and handed control to the first user-level process, also called init.

 

If you want to know what the init has done, we can only look at the source code without sticking to the code. Here we only talk about what it has done.

(System/CORE/init. c --> main)

 

1. Clear umask

That is to say, set the default permission. If the value is 0 and umask is 0000, it is equivalent to 0777 in CHMOD. We often use Linux to familiarize ourselves with chmod 777, that is, to grant permissions to a file, all groups and all users can read, write, and run, that is, the loose permission.

 

2. Create and mount some basic Directories

Create a directory and mount the corresponding system:

/DevDevice directory, where all peripheral devices are located, including real devices such as SIM cards and virtual devices such as essential null devices. The Mount relationship is/dev-> tmpfs. tmpfs is a temporary file system, which only occupies memory space.

/ProcThe system information directory contains all information about the current system, such as dynamic information about processes and clocks. The Mount relationship is/proc-> proc

/SysWhat is stored here is the objects mapped by hardware devices on Linux, such as PCI devices. The Mount relationship is/sys-> sysfs.

/Dev/PTSThis is a remote terminal console device, character terminal. If this is available, it is impossible for ADB shell to debug android. The Mount relationship is/dev/PTS-> devpts

/Dev/socketFor Android, socket is a method of Process Communication in Linux. Under/dev/socket, soket resources that have been allocated by the system are basically some local services, for example, if you are interested in ridl, you can view it in ADB shell.

 

3. initialize the null device, redirect the standard input and output, initialize the kmsg system, and parse the init. RC file.

Null is a standard device in Linux, that is, the so-called black hole. As to why it exists, you have to start with the input and output redirection. For example, you can run a program in the Linux console, sometimes a lot of things will be output, which is written to the standard output. We don't want it to be displayed, it is to redirect the output from> null to this black hole device, the system will return a successful write operation to this program. In essence, the system does everything.

Kmsg is a kernel-level Log System in Linux and a kernel message. Like the log system provided by anroid, it is only for the kernel level.

For the init. RC file, only the parsing is performed here, and some commands in it are not executed.

 

4. Obtain kernel command parameters and parse the init. *. RC file of a specific model.

Obtain kernel command parameters, that is, explicitly stated parameters. If grub or Loli is configured, it may be involved.

Each mobile phone hardware platform is different. ADB shell will find two RC files, one of which is the RC configuration file related to a specific platform. For example, my defy is init. mapphone_umts.rc. Why is this? In the middle is the name of the mobile phone hardware platform. You can use CAT/proc/cpuinfo to obtain hardware information. My information is as follows:

# cat cpuinfocat cpuinfoProcessor       : ARMv7 Processor rev 2 (v7l)BogoMIPS        : 299.11Features        : swp half thumb fastmult vfp edsp neon vfpv3CPU implementer : 0x41CPU architecture: 7CPU variant     : 0x3CPU part        : 0xc08CPU revision    : 2Hardware        : mapphone_UMTSRevision        : 0000Serial          : 0000000000000000CPU Tier        : 10

 

The value of hardware is the sub-name of RC.

Init first obtains the attribute value in/porc/cpuinfo, searches for a specific RC file based on the string, and finally parses it according to the configuration content in RC.

 

5. execute commands in the RC file

In the previous step, init has Parsed the two RC files. Here, the corresponding actions are performed based on the specific content in the RC file. The format of the RC file will be analyzed independently later, and the execution method.

 

6. Become the patron saint

Here, init enters an endless loop (;;){}. So what does it protect?

1. porpety service starts and protects the Attribute Service

Android. Like the Registry in Windows, it records various information. It is a sign that the system is successfully running, or a text message. When setting some mobile phone settings, the user is actually dealing with propety service at the underlying layer.

Attribute prefix

Description

Example (shell Operation)

Ro. Read-only attribute Setprop Ro. Media. Capture. maxres 5 m
Maximum camera Pixel

Persist.

Additional storage to the/data/property directory Setprop Persist. SYS. Country CN
Not explained. Note that each attribute is stored as a separate file
Net. Networking, such as GPRS, bluetooth...

Setprop Net. bt. Name CAPF

The Network Name of Bluetooth is CAPF.

The value of net. Change is the name of the attribute that last changed the net. * attribute. For example:

Net. Change =Net. GPRS. Local-IP

CTRL. Start

Control Command

Start the service marked as service by init. RC

SetpropCTL. Start bootanim

Boot dynamic image (second screen boot screen)

After a service is set, the following attributes are returned:

Init. SVC. bootanim = running

CTRL. Stop

Stop the service marked as service by init. RC

SetpropCTL. Stop bootanim

Stop the boot dynamic image (BOOT screen on the second screen)

After a service is set, the following attributes are returned:

Init. SVC. bootanim = stoped

To view and set attributes, you can use the following three methods:

Shell browsing file:

/Default. Prop

/System/build. Prop

/Data/property /*

Java:

System. getproperty ("XXXX ");

System. setproperty ("XXXX ");

C/C ++:

Demo. C:

#include <cutils/properties.h>#include <stdio.h>void print_prop(const char* key,const char* value,void* cookie){printf("key=%s,value=%s/n",key,value);}int main(){property_list(print_prop,NULL);}

 

Android. mk:

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES:= /list_property.cpp /LOCAL_SHARED_LIBRARIES := /libcutils /libutils /LOCAL_MODULE:= list_propinclude $(BUILD_EXECUTABLE)include $(call all-makefiles-under,$(LOCAL_PATH))

 

2. dynamically generate device nodes

In Android Linux, there is no udev. udev is a program that automatically discovers devices based on Kernel hardware messages. In Android, the keyboard and memory card are based on hardware, there are more words on the tablet, such as the mouse and keyboard. Without udev, how does Android achieve hardware hot swapping?

Like udev, the patron saint of init listens to uevent events and does the same thing with udev based on the content of uevent.

Let's take a look at the general process. First, the kernel detects the device status change and sends a uevent message (string). init detects the message and submits it to the corresponding function for processing. This function further processes the message based on the uevent content. In addition to plugging memory cards (Android has a dedicated external storage management mechanism, that is, vold), other (such as data line insertion and mouse insertion) will be processed.

 

3. Listen for keychord events

Keychord is a combination of buttons. From the behavior of the source code, it should take into account the combination of keyboard peripherals. In most cases, it will not be used on mobile phones, but is used on smart devices, that is, Android devices without touch screens and few buttons, such as watches running Android, use different key combinations to represent the input of a standard keyboard.

This stuff is probably not used much.

 

4. Killing botnets

What is a zombie process?

The Linux Process has a special feature. A main process can split (fork) Sub-processes (Android's fertilized egg zygote perfectly carries forward this spirit). In some Linux versions of the desktop version, view the system monitor and take a closer look at the process information. Many processes are structured in a tree. Several processes are displayed after a process, and they are multi-level trees. This is the result of a process with several sub-processes fork.

If the main process is killed, its sub-process may become a zombie process. The so-called zombie is a zombie program that does not work but occupies space. At this time, the patron saint of init is responsible for recycling these innocent souls to release originally scarce memory resources.

 

5. Important services

These services are native services, such as servicemanager and vold. For example, the important zygote, sometimes (not often) feels the same as the sudden restart, which means that zygote crashes and restarts. You must know that the Java World is incubated by zygote.

 

 

Explanation of init. RC

The init. RC Syntax of Android is unique. It can be said that it is a language.

The init. RC syntax is dividedActions, commands, services, and options(Options).

Category

Name

Description

Section On Trigger Condition
Same as above .. Service Resolution Service
Command Chdir Change current working directory
Same as above .. Chroot Change the reference root directory location
.. Class_start Start a service with the class name set
.. Class_stop Stop a service with the class name set
.. Domainname Domain Name
.. Exec Call the program and transfer the process
.. Export Submit variable
.. Hostname Host Name
.. IFUP Activate Nic
.. Insmod Mount Module
.. Import Introduce configuration, such as some RC files under etc, which is similar to the import in Java
.. Mkdir Create directory
.. Mount Mount a File System
.. Setkey From the source code, it should be the keyword abbreviation for setting a command. For example, you can map domainname to DN.
.. Setprop Set an attribute
.. Setrlimit

Set the maximum number of files that can be opened by the current program to the maximum number of files that can be opened by the system.

.. Start Start the service
.. Stop Stop Service
.. Trigger Is it a Custom trigger?
.. Symlink Create a symbolic link
.. Sysclktz

Set Reference time

.. Wait Waiting for the file to be ready? In Linux, this is a process scheduling function.
.. Write Write something to a file or device. It must not be the wirte that transmits the message.
.. Copy Not explained
.. Chown Change owner
.. Chmod Change permissions
.. Loglevel Log output level. If the log output level is lower than this level
.. Restart Restart service
Option Capability Capability, that is, the system controls the permissions of processes.
Same as above .. Class Set Class Name
.. Console Enable Console
.. Critical Is it critical, that is, restart more than 4 times within 4 minutes, and then enter the recovery mode
.. Disabled Not automatically started with Class
.. Group Group owner
.. Keycodes I don't understand .....
.. Oneshot Start only once. Do not restart after unexpected exit
.. Onrestart Restart
.. Setenv Add environment variable
.. Socket Apply for socket Resources
.. User User attribution
.. Ioprio Io scheduling priority

(Many Attributes and command usage are different from those of the same name in Linux.)

Init parses init. RC By section. You can view the keyword definition in keywords. h. What mark does init use to parse init. RC in segments? Combined with the content of init. RC, we can see that the segment tag is marked by on and service. The following is a detailed description.

What should I do?

On is an action.

OnEarly-init

Run the command before init and after loading all RC files. In the miui Rom, init. RC executes start ueventd in Early-init according to keywords. h. Start is a command ).

Here, by the way, ueventd, the underlying (usually the driver) in Android notifies the upper-layer events, using uevent. the Java layer is implemented through the observer mode and the class used isUeventobserver, UseIntentThe native layer usesAndroid_ OS _ueventobserver.cpp, UseUevent. cPassSocketTransfer. Of course, this is the framework and the following layers, which are not frequently used in development. What's more, these classes are not exposed.

OnInit

Run the command before loading the properties files of propety. It is in the init stage before init is changed to propety service.

OnEarly-boot

After the property service is started, it is executed.

OnBoot

Run the command during boot.

OnProperty: XXXXX = x

This parameter is executed when an attribute is set to an expected value.

For init. RC, we can see a lot in combination with the/src/system/CORE/init/* source code and the init. RC file.

The level is limited. Please correct the error. Thank you!

Genesis: Day 1 connection: http://www.cnblogs.com/hangxin1940/archive/2011/10/01/2196964.html

Genesis: the third day connection: http://www.cnblogs.com/hangxin1940/archive/2011/10/22/2221451.html

Original article, reposted, please describe the source:

Http://www.cnblogs.com/hangxin1940/archive/2011/10/01/2196964.html

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.