Attributes are widely used in Android systems to record system settings or information exchange between processes. Properties are globally visible throughout the system. Each process can get/set attributes, and each attribute has a name and value. They are all strings.
During system initialization, Android allocates a shared memory area to store the attributes. These are completed by the "init" daemon, and the source code is located at: device/system/init. The "init" daemon starts an Attribute Service. The property service runs in the "init" daemon. To set properties for each client, you must connect to the property service and then send information to it. Attribute Service will modify and create attributes in the shared memory area. Any client can directly read the property information from the shared memory. This improves read performance.
Client Applications can call the API functions in libcutils to get/set attribute information. The source code of libcutils is located at: device/libs/cutils. The API functions are:
Int property_get (const char * Key, char * value, const char * default_value );
Int property_set (const char * Key, const char * value );
Libcutils calls the _ system_property_xxx function in libc to obtain the attributes in the shared memory. The source code of libc is located at: device/system/bionic.
The property service calls the _ system_property_init function in libc to initialize the shared memory of the property system. When the property service is started, the default properties are loaded from the following files:
/Default. Prop
/System/build. Prop
/System/Default. Prop
/Data/local. Prop
The attribute will be loaded in the above sequence. The loaded attributes overwrite the original values. After these attributes are loaded, the last loaded attributes will be kept in/data/property.
Special attributes
If the attribute name starts with "Ro.", this attribute is regarded as a read-only attribute. Once set, the attribute value cannot be changed.
If the attribute name starts with "Persist.", when this attribute is set, its value will also be written to/data/property.
If the attribute name starts with "net.", when this attribute is set, the "net. Change" attribute is automatically set to add it to the last modified attribute name. (This is clever. The netresolve module uses this attribute to track any changes to the net. * attribute .)
The "Ctrl. Start" and "Ctrl. Stop" attributes are used to start and stop services. Each service must be defined in/init. Rc. When the system is started, the init. RC and the init daemon will parse the init. RC and start the Attribute Service. Once you receive a request to set the "Ctrl. Start" attribute, the property service uses this attribute value as the service name to locate the service and start the service. The startup result of this service will be placed in the "init. SVC. <service name>" attribute. The client application can poll the attribute value to determine the result.
Android toolbox Program
The android toolbox program provides two tools: setprop and getprop to get and set properties. Usage:
Getprop <property name>
Setprop <property name> <property value>
Java
In Java applications, you can use the system. getproperty () and system. setproperty () functions to obtain and set properties.
Action
By default, setting the attribute only causes the "init" daemon to write data to the shared memory. It does not execute any script or binary program. However, you can associate the operations you want to implement with changes to an attribute in init. Rc. For example, the default init. RC contains:
# Adbd on at boot in emulator
On Property: Ro. kernel. qemu = 1
Start adbd
On Property: persist. Service. ADB. Enable = 1
Start adbd
On Property: persist. Service. ADB. Enable = 0
Stop adbd
In this way, if you set persist. Service. ADB. Enable to 1, the "init" daemon knows that you need to take action to enable the adbd service.
The shared memory mentioned in this article is a unique Android sharing method: ashmem
Ashmem is an anonymous shared memory (anonymous shared memory) system. The system adds interfaces so that names of memory blocks can be shared between processes. For example, the system can use the ashmem storage icon to access multiple processes when drawing the user interface. Ashmem is better than traditional Linux shared memory tables. Now, when shared memory blocks are no longer used, it provides kernel with a way to recycle these shared memory blocks. If a program attempts to access a shared memory block released by the kernel, it will receive an error message, re-allocate the memory and reload the data.
In Android, how does one pass propt through the INIT process and add glibc library functions?
ADB Shell
# Printenv
# Getprop get all Java-layer propt
# Setprop wifi. Interface eth0: Set "wifi. Interface" to eth0
Environment Variable android_property_workspace = 9,32768
So FD = 9, size = 32768
The system/init. c => main () process will call
=> Property_init
=> Init_property_area
Void property_init (void)
{
// Ashmem_area-android shared memory area is a way for Android to share content.
// Open the ashmem device and apply for a size of kernel space memory. Do not release it so that all user space processes can share it.
// The kernel driver is located in the Linux/MM/ashmem. c file [Luther. gliethttp].
Init_property_area ();
// # Define prop_path_ramdisk_default "/Default. Prop"
// Read the default. Prop file from ramdisk and release the propt used in all Java environments in the file
// The shared memory is in use.
Load_properties_from_file (prop_path_ramdisk_default );
}
Of course, the INIT program can still call property_set () to set a new propt, for example, property_set ("Ro. Hardware", hardware );
OK. The application for ashmem in the memory area of the kernel space shared by this public user space program has been successfully applied, and all the data we need is filled in, whether it is extracted from ramdisk
The propt directly read from the default. Prop file or the propt set by manually calling property_set () is already in the memory.
Next, init continues to run. register the environment variable android_property_workspace:
=> Service_start
=>
Get_property_workspace (& FD, & sz );
// DUP an idle FD from idle fdt of the init process, for example, idle FD = 9
// Execute DUP once, then the reference count of init will be added with 1, so that [Luther. gliethttp] will not be released without reason.
Sprintf (TMP, "% d, % d", DUP (FD), SZ); // For example, android_property_workspace = 9,32768
Add_environment ("android_property_workspace", TMP); // Add the environment variable "android_property_workspace"
// Actually put it in a static const char * env [32];. When the service_start () function is called, ENV will be passed as a parameter
// Execve (SVC-> ARGs [0], (char **) SVC-> ARGs, (char **) ENV); passed to the service system service process.
Bionic/arch-arm/bionic/crtbegin_dynamic.s
Bionic/arch-arm/bionic/crtbegin_static.s
_ Start:
MoV r0, SP
MoV R1, #0
ADR R2, 0f
ADR R3, 1f
B __ libc_init // glibc library Initialization
0: B Main
1:. Long _ preinit_array __
. Long _ init_array __
. Long _ fini_array __
. Long _ ctor_list __
...
Use ADB pull init. The INIT process is imported from the emulator Simulator for the following disassembly:
Luther @ gliethttp :~ $ Arm-Linux-objdump-Ds init | more
Init: File Format elf32-littlearm
Disassembly of section. Text:
Limit 80a0 <. Text>:
80a0: e1a1_d mov r0, SP
80a4: e3a01000 mov R1, #0; 0x0
80a8: e28f2004 add R2, PC, #4; 0x4
80ac: e28f3004 add R3, PC, #4; 0x4
80b0: ea004ab3 B 0x1ab84 // you can see that this is B _ libc_init.
80b4: ea004ab5 B 0x1ab90 // you can see that this is B Main
80b8: 0001f000 andeq PC, R1, R0
80bc: 0001f008 andeq PC, R1, R8
80c0: 0001f014 andeq PC, R1, R4, LSL R0
80c4: 0001f01c andeq PC, R1, IP, LSL R0
Therefore, arm-Linux-GCC uses the preceding method to link the INIT process [Luther. gliethttp].
Bionic/bionic/libc_init_dynamic.c
Bionic/bionic/libc_init_static.c
==__ Libc_init
Bionic/bionic/libc_init_common.c
==__ Libc_init_common
==__ System_properties_init
==__ System_property_area __
Int _ system_properties_init (void)
{
...
ENV = getenv ("android_property_workspace ");
...
Pa = MMAP (0, SZ, prot_read, map_shared, FD, 0 );
...
_ System_property_area _ = PA;
...
Return 0;
}
All right, after the above glibc library initialization, all applications can compile to the custom property_get library function in the glibc Library
Operation _ system_property_area _, for example, wpa_supplicant user application calls wifi_connect_to_supplicant ()
=> Property_get ("wifi. Interface", iface, "sta"); to obtain the propt [Luther. gliethttp] That matches "wifi. Interface" in ashmem.
This blog is from csdn users.Http://blog.csdn.net/sustzombie/archive/2010/10/23/5960488.aspxAfter reading this article, I believe you have some knowledge about the android property system.