Use the property system on the Android platform)

Source: Internet
Author: User

When using the property system of Android, I encountered some problems. Combined with this experience, I will give a brief introduction to the use of the property system.

1. Introduction to the Property System

Attribute system is an important feature of Android. It runs as a service and manages system configurations and statuses. All these configurations and statuses are attributes. Each attribute is a key-value pair, and its type is a string.

In terms of functions, the properties are very similar to the Windows registry. Many Android applications and libraries depend on this feature directly or indirectly to determine their runtime behavior. For example, the adbd process query Attribute Service has confirmed whether it is currently running in the simulator environment. Another example is Java. Io. file. patheatearator, which returns the value stored in the property service.

Ii. Use of the Property System

To use the property system, you must first include the header file <cutils/properties. h> and add libcutils to the Android. mk file.

The specific example is as follows:

Property_set ("hsf-jpeg. Path", "/data/test.jpg ");
Char propbuf [property_value_max];
Property_get ("hsf-jpeg. Path", propbuf ,"");
Logi ("property_get: % S.", propbuf );
The use of the property system is very simple. here we need to explain that there is only one problem, that is, the property name.

3. Property name of the Property System

The property names used in the property system have certain naming rules.

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.

In addition, there are other prefixes. Before introducing these prefixes, let's take a look at other things.

In the file system/CORE/init/property_service.h, there are two function definitions:

Extern void handle_property_set_fd (int fd );

Extern int property_set (const char * Name, const char * value );

Their implementations are located in system/CORE/init/property_service.c.

In addition, system/CORE/init. C contains the following code:

Int main (INT argc, char ** argv)

{

......

For (;;){

......

If (ufds [0]. revents = Pollin)

Handle_device_fd (device_fd );

If (ufds [1]. revents = Pollin)

Handle_property_set_fd (property_set_fd );

If (ufds [3]. revents = Pollin)

Handle_keychord (keychord_fd );

}

......

}

During the init operation after startup, a loop is executed. When new settings are detected, the setting process is displayed. If authentication fails, related exceptions are prompted, such:

Sys_prop: Permission denied uid: 1013 name: hsf-jpeg. Path

The handle_property_set_fd () function is described as follows:

Void handle_property_set_fd (int fd)

{

Prop_msg MSG;

......

Switch (msg. cmd ){

Case prop_msg_setprop:

MSG. name [PROP_NAME_MAX-1] = 0;

MSG. value [PROP_VALUE_MAX-1] = 0;

 

If (memcmp (msg. Name, "CTL.", 4) = 0 ){

If (check_control_perms (msg. Value, Cr. uid, Cr. GID )){

Handle_control_message (char *) MSG. Name + 4, (char *) msg. value );

} Else {

Error ("sys_prop: Unable to % s service CTL [% s] uid: % d PID: % d/N ",

MSG. Name + 4, MSG. Value, Cr. uid, Cr. PID );

}

} Else {

If (check_perms (msg. Name, Cr. uid, Cr. GID )){

Property_set (char *) MSG. Name, (char *) msg. value );

} Else {

Error ("sys_prop: Permission denied uid: % d name: % s/n ",

Cr. uid, MSG. Name );

}

}

Break;

 

Default:

Break;

}

}

The implementation of check_perms () is as follows:

Static int check_perms (const char * Name, unsigned int uid, int GID)

{

Int I;

If (uid = 0)

Return 1;

 

If (! Strncmp (name, "Ro.", 3 ))

Name + = 3;

 

For (I = 0; property_perms [I]. prefix; I ++ ){

Int TMP;

If (strncmp (property_perms [I]. prefix, name,

Strlen (property_perms [I]. prefix) = 0 ){

If (UID & property_perms [I]. uid = UID) |

(GID & property_perms [I]. gid = GID )){

Return 1;

}

}

}

 

Return 0;

}

With these two features, we can know that the hsf-jpeg. path we set will be compared with the prefix in the property_perms struct. If this parameter is met, the UID will be compared.

The struct property_perms is defined as follows:

Struct {

Const char * prefix;

Unsigned int uid;

Unsigned int GID;

} Property_perms [] = {

{"Net. rmnet0.", aid_radio, 0 },

{"Net. GPRS.", aid_radio, 0 },

{"Net. PPP", aid_radio, 0 },

{"RIL.", aid_radio, 0 },

{"GSM.", aid_radio, 0 },

{"Persist. Radio", aid_radio, 0 },

{"Net. DNS", aid_radio, 0 },

{"Net.", aid_system, 0 },

{"Dev.", aid_system, 0 },

{"Runtime.", aid_system, 0 },

{"HW.", aid_system, 0 },

{"SYS.", aid_system, 0 },

{"Service.", aid_system, 0 },

{"WLAN.", aid_system, 0 },

{"DHCP.", aid_system, 0 },

{"DHCP.", aid_dhcp, 0 },

{"VPN.", aid_system, 0 },

{"VPN.", aid_vpn, 0 },

{"Debug.", aid_shell, 0 },

{"Log.", aid_shell, 0 },

{"Service. ADB. Root", aid_shell, 0 },

{"Persist. SYS.", aid_system, 0 },

{"Persist. Service.", aid_system, 0 },

{"Persist. Security.", aid_system, 0 },

{Null, 0, 0}

};

At this point, we will know that HW. exists in property_perms. Can the UID match? We can see that the UID corresponding to HW is aid_system.

The definition of aid_system is located:

System/CORE/include/private/android_filesystem_config.h

# Define aid_system 1000/* system server */

# Define aid_radio 1001/* telephony subsystem, rIL */

# Define aid_dhcp 1014/* DHCP Client */

# Define aid_vpn 1016/* VPN system */

# Define aid_shell 2000/* ADB and debug shell user */

When we use the watermark name hsf-jpeg. path, the UID of our application must be 1000. Otherwise, an error similar to this will be reported in the handle_property_set_fd () function:

Sys_prop: Permission denied uid: 1013 name: hsf-jpeg. Path

 

4. Make the application have the UID of aid_system

So how can I change the UID of an application to 1000?

Since I can use make to compile the Android system source code environment, the following steps are required:

L add attributes to the manifest node in the androidmanifest. xml file of the application:

Android: shareduserid = "android. uid. system ";

With the shared user ID, multiple APK with the same user ID can be configured to run in the same process. Then, assign the UID of the program to Android. uid. system, that is, to allow the program to run in the system process, so that you have the permission to set the attribute prefixed with HW.

L modify the Android. mk file and add local_certificate: = platform;

It is not enough to add uid. If the APK cannot be installed at this time, the system prompts that the signature is inconsistent because the program wants to run in the system process and has the target system's platform. key, Which is platform. pk8 and platform. x509.pem files. With this setting, the system can get two files: Platform. pk8 and platform. x509.pem. The APK can be put into the system process only after the two keys are signed.

Then use the MM command to compile and the UID of the generated APK will become 1000. Call property_set ("hsf-jpeg. Path", "/data/test.jpg.

In addition, the use of the property_get () function does not have such restrictions.

If you need a deep understanding of the property system, you can refer to the following articles:

Android Property System | Android Property System

Http://blog.csdn.net/jackyu613/archive/2011/01/13/6136620.aspx

Android Property System)

Http://blog.csdn.net/tekkamanitachi/archive/2009/06/18/4280982.aspx

Android Property System | Android Property System

Http://www.bangchui.org/read.php? Tid = 13375

How to modify the system time in Android (the application obtains system permissions)

Http://dev.10086.cn/blog? Uid-49302-action-viewspace-itemid-907

Analysis of systemproperties settings of Android

Http://blog.csdn.net/netpirate/archive/2009/11/11/4799272.aspx

E-mail: wxiaozhe@163.com
QQ: 1226062415
Date: 2011/5/14
Blog: http://blog.csdn.net/wxzking

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.