Android Property System

Source: Internet
Author: User
Tags access properties unix domain socket
Property System is an important feature on Android. it runs as a service and manages System deployments and status. all these deployments and status are properties. A property is a key/value pair, both of which are of string type.
From the sense of function, it's very similar to Windows registry. using Android applications and libraries directly or indirectly relies on this feature to determine their runtime behavior. for example, adbd process queries property service to check if it's running in emulator. another example is the java. io. file. pathseparator returns the value stored in property service.

How Property System Works
the High Level Architecture of property system is shown as following.
in the figure, there are three processes, a group of persistent Property Files and a shared memory block. the shared memory block is the container of all property records. only the property service process can write to the shared memory block. it'll load property records from persistent the save them in the shared memory.
the consumer process loads the shared memory in its own virtual space and access properties directly. the setter process also loads the shared memory in its virtual space, but it can't write to the memory directly. when the setter tries to add or update a property, it sends the property to property service via Unix domain socket. the property service will write the property to shared memory on behalf on Setter process, as well as to the persistent file.

property service runs inside INIT process. the INIT process first creates a shared memory region and stores a FD to the region. then INIT process maps the region into its virtual space with MMAP with map_shared flag, as a result, any updates to this area can be seen by all processes. this FD and region size are saved in a environment variable named "android_property_workspace ". any other processes like consumer and setter will use this environment variable to get the FD and size, so that they can MMAP this region into its own virtual space. the layout of the shared memory is shown below.
after that, INIT process will load properties from following files:
/default. prop
/system/build. prop
/system/default. prop
/data/local. prop

The next step is start property service. in this step, a Unix domain socket server is created. this socket's pathname is "/dev/socket/property_service" which is well known to other client processes.
Finally, INIT process callpoll to wait for connect event on the socket.

On the consumer side, when it initializes libc (Bionic/libc/bionic/libc_common.c _ libc_init_common Function). It will retrieve the FD and size from environment variable, and map the shared memory into its own space (Bionic/libc/bionic/system_properties.c _ system_properties_init Function). After that, libcutils can read property just as normal memory for the consumer.

Currently, properties can't be removed. That's to say, once a property has been added, it can't be removed, neither can its key be changed.

How to Get/Set Properties

There are three main means to get/set properies on Android.

1. native code
When writing native applications, property_get and property_set APIs can be used to get/set properties. to use them, we need to include cutils/properties. h and link against libcutils.

2. Java code
Android also provides system. getproperty and system. setproperty functions in Java library, our Java application can use them to get/set properties. But it's importantNoteThat although these Java APIs are semantically equal to native version, Java version store data in a totally different place. actually, a hashtable is employed by Dalvik VM to store properties. so, Java properties are separated, it can't get or set native properties, and neither vice versa.

3. shell script
Android provides getprop and setprop command line tool to retrieve and update properties. they can be used in shell script. they are implemented on top of libcutils.

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.