Android系统中prop详解

文章出处:http://blog.csdn.net/shift_wwx

Android 中很多时候会用到prop的操作,这样的话在系统操作的时候可能会更方便一点。这篇文章就是关于prop的详细使用,讲解的可能还是不全,但是还是希望给大家带来一点帮助。

 

主要分两部分:

1. SystemProperties 类

2. 终端prop命令

 

首先来看一下SystemProperties类的source code。

 

package android.os;

import java.util.ArrayList;

import android.util.Log;


/**
 * Gives access to the system properties store.  The system properties
 * store contains a list of string key-value pairs.
 *
 * {@hide}
 */
public class SystemProperties
{
    public static final int PROP_NAME_MAX = 31;
    public static final int PROP_VALUE_MAX = 91;

    private static final ArrayList<Runnable> sChangeCallbacks = new ArrayList<Runnable>();

    private static native String native_get(String key);
    private static native String native_get(String key, String def);
    private static native int native_get_int(String key, int def);
    private static native long native_get_long(String key, long def);
    private static native boolean native_get_boolean(String key, boolean def);
    private static native void native_set(String key, String def);
    private static native void native_add_change_callback();

    /**
     * Get the value for the given key.
     * @return an empty string if the key isn't found
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static String get(String key) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get(key);
    }

    /**
     * Get the value for the given key.
     * @return if the key isn't found, return def if it isn't null, or an empty string otherwise
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static String get(String key, String def) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get(key, def);
    }

    /**
     * Get the value for the given key, and return as an integer.
     * @param key the key to lookup
     * @param def a default value to return
     * @return the key parsed as an integer, or def if the key isn't found or
     *         cannot be parsed
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static int getInt(String key, int def) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get_int(key, def);
    }

    /**
     * Get the value for the given key, and return as a long.
     * @param key the key to lookup
     * @param def a default value to return
     * @return the key parsed as a long, or def if the key isn't found or
     *         cannot be parsed
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static long getLong(String key, long def) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get_long(key, def);
    }

    /**
     * Get the value for the given key, returned as a boolean.
     * Values 'n', 'no', '0', 'false' or 'off' are considered false.
     * Values 'y', 'yes', '1', 'true' or 'on' are considered true.
     * (case sensitive).
     * If the key does not exist, or has any other value, then the default
     * result is returned.
     * @param key the key to lookup
     * @param def a default value to return
     * @return the key parsed as a boolean, or def if the key isn't found or is
     *         not able to be parsed as a boolean.
     * @throws IllegalArgumentException if the key exceeds 32 characters
     */
    public static boolean getBoolean(String key, boolean def) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        return native_get_boolean(key, def);
    }

    /**
     * Set the value for the given key.
     * @throws IllegalArgumentException if the key exceeds 32 characters
     * @throws IllegalArgumentException if the value exceeds 92 characters
     */
    public static void set(String key, String val) {
        if (key.length() > PROP_NAME_MAX) {
            throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
        }
        if (val != null && val.length() > PROP_VALUE_MAX) {
            throw new IllegalArgumentException("val.length > " +
                PROP_VALUE_MAX);
        }
        native_set(key, val);
    }

    public static void addChangeCallback(Runnable callback) {
        synchronized (sChangeCallbacks) {
            if (sChangeCallbacks.size() == 0) {
                native_add_change_callback();
            }
            sChangeCallbacks.add(callback);
        }
    }

    static void callChangeCallbacks() {
        synchronized (sChangeCallbacks) {
            //Log.i("foo", "Calling " + sChangeCallbacks.size() + " change callbacks!");
            if (sChangeCallbacks.size() == 0) {
                return;
            }
            ArrayList<Runnable> callbacks = new ArrayList<Runnable>(sChangeCallbacks);
            for (int i=0; i<callbacks.size(); i++) {
                callbacks.get(i).run();
            }
        }
    }
}

因此,要用到SystemProperties类的时候首先用import android.os.SystemProperties,其他的接口很明了。

至于native的接口,当然是通过jni调用的底层的呀,详细可以查看android_os_SystemProperties.cpp就知道了,例如prop set就调用了jni的static void SystemProperties_set函数,最终通过err = property_set(key, val);调用底层的接口函数,这个接口从哪里调用的,需要注意头文件了:

#include "cutils/properties.h"
找到这个文件:

@/system/core/include/cutils/properties.h

/* property_set: returns 0 on success, < 0 on failure
*/
int property_set(const char *key, const char *value);
实现的地方@/system/core/libcutils/properties.c

int property_set(const char *key, const char *value)
{
    return __system_property_set(key, value);
}
接口函数__system_property_set

@/bionic/libc/bionic/system_properties.c

int __system_property_set(const char *key, const char *value)
{
    int err;
    prop_msg msg;

    if(key == 0) return -1;
    if(value == 0) value = "";
    if(strlen(key) >= PROP_NAME_MAX) return -1;
    if(strlen(value) >= PROP_VALUE_MAX) return -1;

    memset(&msg, 0, sizeof msg);
    msg.cmd = PROP_MSG_SETPROP;
    strlcpy(msg.name, key, sizeof msg.name);
    strlcpy(msg.value, value, sizeof msg.value);

    err = send_prop_msg(&msg);
    if(err < 0) {
        return err;
    }

    return 0;
}
发了一个msg出来了,接受这个msg是在init.c的main中,详细请看一下 《Android 的init过程详解》
        for (i = 0; i < fd_count; i++) {
            if (ufds[i].revents == POLLIN) {
                if (ufds[i].fd == get_property_set_fd())
                    handle_property_set_fd();
                else if (ufds[i].fd == get_keychord_fd())
                    handle_keychord();
                else if (ufds[i].fd == get_signal_fd())
                    handle_signal();
            }
        }
 @/system/core/init/property_service.c
void handle_property_set_fd()
{
    prop_msg msg;
    int s;
    int r;
    int res;
    struct ucred cr;
    struct sockaddr_un addr;
    socklen_t addr_size = sizeof(addr);
    socklen_t cr_size = sizeof(cr);
    char * source_ctx = NULL;

    if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) {
        return;
    }

    /* Check socket options here */
    if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) {
        close(s);
        ERROR("Unable to receive socket options\n");
        return;
    }

    r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
    if(r != sizeof(prop_msg)) {
        ERROR("sys_prop: mis-match msg size received: %d expected: %d errno: %d\n",
              r, sizeof(prop_msg), errno);
        close(s);
        return;
    }

    switch(msg.cmd) {
    case PROP_MSG_SETPROP:
        msg.name[PROP_NAME_MAX-1] = 0;
        msg.value[PROP_VALUE_MAX-1] = 0;

        if (!is_legal_property_name(msg.name, strlen(msg.name))) {
            ERROR("sys_prop: illegal property name. Got: \"%s\"\n", msg.name);
            close(s);
            return;
        }

        getpeercon(s, &source_ctx);

        if(memcmp(msg.name,"ctl.",4) == 0) {
            // Keep the old close-socket-early behavior when handling
            // ctl.* properties.
            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 {
            if (check_perms(msg.name, cr.uid, cr.gid, source_ctx)) {
                property_set((char*) msg.name, (char*) msg.value);
            } else {
                ERROR("sys_prop: permission denied uid:%d  name:%s\n",
                      cr.uid, msg.name);
            }

            // Note: bionic's property client code assumes that the
            // property server will not close the socket until *AFTER*
            // the property is written to memory.
            close(s);
        }
        freecon(source_ctx);
        break;

    default:
        close(s);
        break;
    }
}
注意case PROP_MSG_SETPROP,这里就是接受的地方了。最终会调用property_set:
int property_set(const char *name, const char *value)
{
    prop_info *pi;
    int ret;

    size_t namelen = strlen(name);
    size_t valuelen = strlen(value);

    if (!is_legal_property_name(name, namelen)) return -1;
    if (valuelen >= PROP_VALUE_MAX) return -1;

    pi = (prop_info*) __system_property_find(name);

    if(pi != 0) {
        /* ro.* properties may NEVER be modified once set */
        if(!strncmp(name, "ro.", 3)) return -1;

        __system_property_update(pi, value, valuelen);
    } else {
        ret = __system_property_add(name, namelen, value, valuelen);
        if (ret < 0) {
            ERROR("Failed to set '%s'='%s'\n", name, value);
            return ret;
        }
    	if(strcmp(name, "ro.ubootenv.varible.prefix") == 0) {
    		int vlen = (valuelen < 30) ? valuelen : 30;
    		memcpy(uboot_var_prefix, value, vlen);
    		uboot_var_prefix[vlen] = '.';
    	}
    }
    /* If name starts with "net." treat as a DNS property. */
    if (strncmp("net.", name, strlen("net.")) == 0)  {
        if (strcmp("net.change", name) == 0) {
            return 0;
        }
       /*
        * The 'net.change' property is a special property used track when any
        * 'net.*' property name is updated. It is _ONLY_ updated here. Its value
        * contains the last updated 'net.*' property.
        */
        property_set("net.change", name);
    } else if (persistent_properties_loaded &&
            strncmp("persist.", name, strlen("persist.")) == 0) {
        /*
         * Don't write properties to disk until after we have read all default properties
         * to prevent them from being overwritten by default values.
         */
        write_persistent_property(name, value);
    } else if (strcmp("selinux.reload_policy", name) == 0 &&
               strcmp("1", value) == 0) {
        selinux_reload_policy();
    }
    property_changed(name, value);
    return 0;
}
code中刚开始会判断name的长度和value的长度:
    if (!is_legal_property_name(name, namelen)) return -1;
    if (valuelen >= PROP_VALUE_MAX) return -1;
这个长度的定义是在/bionic/libc/includes/sys/system _properties.h
#define PROP_NAME_MAX   32
#define PROP_VALUE_MAX  92
syste/core/init/init.c中刚开始会做property_init();具体可以看一下 《Android 的init过程详解》
map_prop_area_rw会定义:
    pa_size = PA_SIZE;
    pa_data_size = pa_size - sizeof(prop_area);
    compat_mode = false;
大小大概是128*1024,这样就可以算出来prop最多个数是128*1024/(32+92),我记得以前公司一个专家跟我说过,4.0以前都是250??这里大概有1000多了。

到这里就基本介绍了从setprop到处理prop的过程,可是有一点奇怪,sendmsg是通过什么连接的呢?
还是从init.c入手:
queue_builtin_action(property_service_init_action, "property_service_init");
在property_service_init_action的时候
@/syste/core/init/property_service.c
void start_property_service(void)
{
    int fd;

    load_properties_from_file(PROP_PATH_SYSTEM_BUILD);
    load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
    load_override_properties();
    /* Read persistent properties after all default values have been loaded. */
    load_persistent_properties();

    fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0);
    if(fd < 0) return;
    fcntl(fd, F_SETFD, FD_CLOEXEC);
    fcntl(fd, F_SETFL, O_NONBLOCK);

    listen(fd, 8);
    property_set_fd = fd;
}
创建了个socket,然后对property_set_fd做了监控,这就跟之前说的handle_property_set_fd()连起来了。
总结一下过程:
1、init 起来的时候会做property_init,对prop存储空间做了限定
2、创建了一个socket,然后对这个fd做了监听,init里面会有个for一直在listen知道handle_property_set_fd触发
3、应用通过SystemProperties这个类做prop的相关动作,例如setprop触发了handle_property_set_fd

到这里就可以知道android 上层通过SystemProperties类调用setprop的整个过程。

 

接下来,看一下终端命令setprop、getprop、watchprops。

在init.c文件main函数中会调用start_property_service(),而它分别调用load_properties_from_file函数读取PROP_PATH_SYSTEM_BUILD(/system/build.prop)、PROP_PATH_SYSTEM_DEFAULT(/system/default.prop)和PROP_PATH_LOCAL_OVERRIDE(/data/local.prop)存放系统属性的文件并设置到系统属性。

编译时由build/tool/buildinfo.sh文件写到文件build.prop,修改系统默认属性一般是改build/tool/buildinfo.sh文件

 

在android系统中,有一些初始化的配置文件,例如:

/init.rc

/default.prop

/system/build.prop

文件里面里面配置了开机设置的系统属性值,

这些属性值,可以通过getprop获取,setprop设置,

它的格式如下:

getprop [key]

获取指定key的配置值,如果不带参数,只是getprop则是显示系统所有的配置值。

[dalvik.vm.heapsize]: [24m]

[curlockscreen]: [1]

[ro.sf.hwrotation]: [0]

[ro.config.notification_sound]: [OnTheHunt.ogg]

[ro.config.alarm_alert]: [Alarm_Classic.ogg]

setprop [key] [value]
设置指定key的属性值,
watchprops
监听系统属性的变化,如果期间系统的属性发生变化则把变化的值显示出来
/system # watchprops
1307501833 sys.settings_system_version = '37'
1307501836 sys.settings_system_version = '38'
1307501862 persist.sys.timezone = 'Asia/Hong_Kong'
 
其实这三个命令都是toolbox的子命令,如果有兴趣的可以看在android源码中看到其对应的源码:system/core/toolbox/

toolbox命令主要完成扩展命令的执行,每一个扩展命令对应一个name_main函数,如ls命令,对应ls_main函数。同时,每一个扩展命令都由一个system/core/toolbox/目录下面的.c文件实现。toolbox.c会根据这个目录下面的.c文件生成tools.h头文件,并在system/core/toolbox/Android.mk文件中为每个命令生成指向toolbox的连接。toolbox的实现结构使它扩展一个命令很容易。

假设现在我们自己想手工添加一个shell命令mycommand,只要在system/core/toolbox/目录下面新建一个mycommand.c文件,并在里面实现一个mycommand_main函数,然后在system/core/toolbox/Android.mk中添加mycommand.c即可。Android.mk会自动把它编译进toolbox程序,并在编译生成的Android系统/system/bin目录下为这个命令生成一个指向toolbox的连接。

以setprop这个命令为例:在system/core/toolbox目录下会有个setprop.c这个文件, source code如下:

#include <stdio.h>

#include <cutils/properties.h>

int setprop_main(int argc, char *argv[])
{
    if(argc != 3) {
        fprintf(stderr,"usage: setprop <key> <value>\n");
        return 1;
    }

    if(property_set(argv[1], argv[2])){
        fprintf(stderr,"could not set property\n");
        return 1;
    }

    return 0;
}

 不难分析出来,setprop这个命令是需要3个参数,一个命令本身,一个是prop name,一个是prop value,如果不是三个就会报错usage: setprop <key> <value>,在Android.mk中将这个命令import进去就ok了,最终编译会在system/bin下面生成对应的文件。

而如果参数正确的话就会调用property_set这个函数,传入的值分别是prop name 和prop value。

同样的,根据code可以看出会调用system/core/libcutils/properties.c下面的接口,与之前说的是一样的。

最后还是会通过init.c对prop 的fd做轮询。


至于prop的存储,从bionic/libc/include/sys/_system_properties.h就可以看出来:

#define PROP_PATH_RAMDISK_DEFAULT  "/default.prop"
#define PROP_PATH_SYSTEM_BUILD     "/system/build.prop"
#define PROP_PATH_SYSTEM_DEFAULT   "/system/default.prop"
#define PROP_PATH_LOCAL_OVERRIDE   "/data/local.prop"

而通过上面code中write_persistent_property函数就可以知道一般persist.xx等prop是存储在/data/property下,具体code如下:

static void write_persistent_property(const char *name, const char *value)
{
    const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp";
    char path[PATH_MAX];
    int fd, length;

    snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name);

    fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600);
    if (fd < 0) {
        ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno);
        return;
    }
    write(fd, value, strlen(value));
    close(fd);

    if (rename(tempPath, path)) {
        unlink(tempPath);
        ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path);
    }
}

过程感觉还是不够详细,后期碰到再补充吧。




你可能感兴趣的:(android,prop,getprop,SetProp)