Android-init process (2)

Source: Internet
Author: User

Android-init process (2)
The init Attribute Service is like the windows registry. An application can perform attribute operations on its application information.

The following describes the principles of the property service:


Key Attribute methods:
* Void property_init (void)> Analysis
Void property_init (void) {init_property_area ();} static int init_property_area (void) {prop_area * pa; // create shared memory if (init_workspace (& pa_workspace, PA_SIZE )) return-1;/** set other information such as space size * // the system automatically calls the _ libc_prenit function, _ system_property_area _ = pa ;}

// Notify other processes
Void _ libc_preinit (void) {__ extract (elfdata);} void _ extract (uintptr_t * elfdata) {__ system_properties_init ();} int _ system_properties_init (void) {// retrieve the init environment variable to obtain the file descriptor env = getenv ("ANDROID_PROPERTY_WORKSPACE"); fd = atoi (env ); // map the memory created by init to the local process space // so that other processes can share the memory. // here, the client can only perform read Operations pa = mmap (0, sz, PROT_READ, MAP_SHARED, fd, 0 );}

* Void start_property_service (void)> Analysis

1) load the property file to the property space to create scoket at the same time

* Void start_property_service (void) {// load the attribute file objects (PROP_PATH_SYSTEM_BUILD); loads (PROP_PATH_SYSTEM_DEFAULT); load_override_properties (); // load the persistent file load_persistent_properties (); // create socketfd = create_socket (PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);} init. c: int main () {nr = poll (ufds, fd_count, timeout); if (ufds [I]. revents = POLLIN) {if (ufds [I]. fd = get_property_set_fd () handle_property_set_fd ();}}}

2) The main function waits for other processes to call scoket and process and modify attributes.
Void handle_property_set_fd () {// receives scoket if (s = accept (property_set_fd, (struct sockaddr *) & addr, & addr_size) <0) {return ;} // retrieve the process permission attribute if (getsockopt (s, SOL_SOCKET, SO_PEERCRED, & cr, & cr_size) <0) {close (s); return ;} // receive data r = TEMP_FAILURE_RETRY (recv (s, & msg, sizeof (msg), 0); if (memcmp (msg. name, "ctl. ", 4) = 0) {// ctl. * control the message // such as setprop ctl. start bootanim close (s); if (check_control_perms (Msg. value, cr. uid, cr. gid, source_ctx) {handle_control_message (char *) msg. name + 4, (char *) msg. value);} else {ERROR ("sys_prop: Unable to % s service ctl [% s] uid: % d gid: % d pid: % d \ n", msg. name + 4, msg. value, cr. uid, cr. gid, cr. pid) ;}} else {// check the permission if (check_perms (msg. name, cr. uid, cr. gid, source_ctx) {// set the property property_set (char *) msg. name, (char *) msg. value);} else {ERROR ("sys_prop: pe Rmission denied uid: % d name: % s \ n ", cr. uid, msg. name) ;}} int property_set (const char * name, const char * value) {prop_info * pi = (prop_info *) _ system_property_find (name); if (pi! = 0) {/* ro. * properties read-only attribute cannot be set */if (! Strncmp (name, "ro. ", 3) return-1; // update the property update_prop_info (pi, value, valuelen );} else {// Add up to 247 attributes} // process persistent attributes/net. */..................... // modify the property_changed (name, value );}

3) create a scoket request on the client
// Android provides System. getProperty to obtain the property service. The following uses android2.3 as an example.
/** SystemProperties. java */

private static native String native_get(String key);    public static String get(String key) {        if (key.length() > PROP_NAME_MAX) {            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);        }        return native_get(key);    }

/** The jni call part uses the dynamically registered android_ OS _SystemProperties.cpp */
Static JNINativeMethod method_table [] = {"native_get", "(Ljava/lang/String;) Ljava/lang/String;", (void *) systemProperties_getS },....... // omit some code} static jstring SystemProperties_getS (JNIEnv * env, jobject clazz, jstring keyJ) {return SystemProperties_getSS (env, clazz, keyJ, NULL );} static jstring SystemProperties_getSS (JNIEnv * env, jobject clazz, jstring keyJ, jstring defJ) {int len; const char * key; char buf [PROPERTY_VALUE_MAX]; jstring rvJ = NULL ;............. // some codes are omitted: key = env-> GetStringUTFChars (keyJ, NULL); len = property_get (key, buf ,""); // construct the obtained attribute rvJ = env-> NewStringUTF (buf );.............}

/** Properties. c * // view property_get to obtain the property service source code.
Int property_get (const char * key, char * value, const char * default_value) {char sendBuf [1 + PROPERTY_KEY_MAX]; char recvBuf [1 + PROPERTY_VALUE_MAX]; int len =-1 ;............... // execute the init function to obtain the property pthread_once (& gInitOnce, init );............... return len;} static void init (void ){................ gPropFd = connectToServer (SYSTEM_PROPERTY_PIPE_NAME );................} static int connectToServer (const char * fileName) {int sock =-1; int cc; struct sockaddr_un addr ;................. /* connect to socket; fails if file doesn't exist * // use TCP to connect the server to define the address and protocol family AF_UNIXstrcpy (addr. sun_path, fileName); // max 108 bytesaddr. sun_family = AF_UNIX; cc = connect (sock, (struct sockaddr *) & addr, SUN_LEN (& addr); if (cc <0) {close (sock ); return-1;} return sock ;}


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.