android 属性系统流程

用途
android 属性系统,在程序用途讲,个人理解有有两个作用,1,设置属性触发相应动作,2,在各层中作为条件判断用途
框架
android 属性系统,分为设置属性和获得属性,此两类又用两种方法来让客户端操作
即,设置属性,用本地socket来进行设置;获得属性,用共享内存形势来取得,如共享地址用__system_property_area__来定义。
下面来讲具体代码流程:
现讲set属性---socket
服务器端:
   ../system/core/init/init.c
   在android系统启动时,执行init进程时,即建立服务器端.
   如下:
     init main()
                .............
     queue_builtin_action(property_init_action, "property_init");//分配共享内存供get属性用
       ................
               queue_builtin_action(property_service_init_action, "property_service_init");//建立socket
     ....................
               for(;;)//init进程死循环一个作用就是在poll一个客户端设置属性。
               {
         ....................
                    handle_property_set_fd();
        .........................
               }


     handle_property_set_fd()处理过程
       accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)
       r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
       property_set((char*) msg.name, (char*) msg.value);
          真正设置属性值,如成功成功,会调用property_changed(name, value); 

                            -->queue_property_triggers(name, value);

                                                                                                          看看在属性表有没有触发action.有则执行

简单分析服务器端完成,接着就客户端,我们就从framework开始分析
客户端:
   ..../frameworks/base/core/java/android/os/SystemProperties.java
          
   public class SystemProperties
        {
              ......................... 
             //各种get 作用一样,都是取得属性值,无非加些判断返回正假值。以下用获得value
              public static String get(String key) {
                if (key.length() > PROP_NAME_MAX) {
                    throw new IllegalArgumentException("key.length > " + PROP_NAME_MAX);
                }
                return native_get(key);//调用本地函数
            }


      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);//调用本地函数
         }
                  ...........
}
   .../frameworks/base/core/jni/android_os_SystemProperties.cpp
通过JNI会调用本地函数,如下
static JNINativeMethod method_table[] = {
     { "native_get", "(Ljava/lang/String;)Ljava/lang/String;",
       (void*) SystemProperties_getS },
     { "native_get", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
       (void*) SystemProperties_getSS },
     { "native_get_int", "(Ljava/lang/String;I)I",
       (void*) SystemProperties_get_int },
     { "native_get_long", "(Ljava/lang/String;J)J",
       (void*) SystemProperties_get_long },
     { "native_get_boolean", "(Ljava/lang/String;Z)Z",
       (void*) SystemProperties_get_boolean },
     { "native_set", "(Ljava/lang/String;Ljava/lang/String;)V",
       (void*) SystemProperties_set },
 };


现在以set为例:
static void SystemProperties_set(JNIEnv *env, jobject clazz,
155                                       jstring keyJ, jstring valJ)
156 {
157     int err;
158     const char* key;
159     const char* val;
160 
161     if (keyJ == NULL) {
162         jniThrowNullPointerException(env, "key must not be null.");
163         return ;
164     }
165     key = env->GetStringUTFChars(keyJ, NULL);
166 
167     if (valJ == NULL) {
168         val = "";      
169     } else {
170         val = env->GetStringUTFChars(valJ, NULL);
171     }
172 
173     err = property_set(key, val);        //正真设置属性值
174 
175     env->ReleaseStringUTFChars(keyJ, key);
176 
177     if (valJ != NULL) {
178         env->ReleaseStringUTFChars(valJ, val);
179     }
180 
181     if (err < 0) {
182         jniThrowException(env, "java/lang/RuntimeException",
183                           "failed to set system property");
184     }
185 }




上面property_set  在 .../system/core/include/cutils/properties.h 定义声明函数,不过找不到原函数??
看了头文件内的注释它是这样解释的, 
24 /* System properties are *small* name value pairs managed by the
 25 ** property service.  If your data doesn't fit in the provided
 26 ** space it is not appropriate for a system property.
 27 **
 28 ** WARNING: system/bionic/include/sys/system_properties.h also defines
 29 **          these, but with different names.  (TODO: fix that)

 30 */
所以,调用property_set 会调用在
     .../bionic/libc/include/sys/system_properties.h
                         int __system_property_get(const char *name, char *value);
                         int __system_property_set(const char *key, const char *value);
               实现在
     .../bionic/libc/bionic/system_properties.c
 218 int __system_property_set(const char *key, const char *value)
219 {
220     int err;
221     int tries = 0;
222     int update_seen = 0;
223     prop_msg msg;
224 
225     if(key == 0) return -1;
226     if(value == 0) value = "";
227     if(strlen(key) >= PROP_NAME_MAX) return -1;
228     if(strlen(value) >= PROP_VALUE_MAX) return -1;
229 
230     memset(&msg, 0, sizeof msg);
231     msg.cmd = PROP_MSG_SETPROP;
232     strlcpy(msg.name, key, sizeof msg.name);
233     strlcpy(msg.value, value, sizeof msg.value);
234 
235     err = send_prop_msg(&msg);      //将要发送
236     if(err < 0) {
237         return err;
238     }
239 
240     return 0;
241 }


158 static int send_prop_msg(prop_msg *msg)
159 {
160     struct pollfd pollfds[1];
161     struct sockaddr_un addr;
162     socklen_t alen;
163     size_t namelen;
164     int s;
165     int r;
166     int result = -1;
167 
168     s = socket(AF_LOCAL, SOCK_STREAM, 0);       //建立客户端socket
169     if(s < 0) {
170         return result;
171     }
172 
173     memset(&addr, 0, sizeof(addr));
174     namelen = strlen(property_service_socket);
175     strlcpy(addr.sun_path, property_service_socket, sizeof addr.sun_path);
176     addr.sun_family = AF_LOCAL;
177     alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
178 
179     if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen) < 0)) {   // 连接服务端
180         close(s);
181         return result;
182     }
183 
184     r = TEMP_FAILURE_RETRY(send(s, msg, sizeof(prop_msg), 0));   //发送请求,即设置属性值
185 
186     if(r == sizeof(prop_msg)) {
187         // We successfully wrote to the property server but now we
188         // wait for the property server to finish its work.  It
189         // acknowledges its completion by closing the socket so we
190         // poll here (on nothing), waiting for the socket to close.
191         // If you 'adb shell setprop foo bar' you'll see the POLLHUP
192         // once the socket closes.  Out of paranoia we cap our poll
193         // at 250 ms.
194         pollfds[0].fd = s;
195         pollfds[0].events = 0;
196         r = TEMP_FAILURE_RETRY(poll(pollfds, 1, 250 ));
197         if (r == 1 && (pollfds[0].revents & POLLHUP) != 0) {
198             result = 0;
199         } else {
200             // Ignore the timeout and treat it like a success anyway.
201             // The init process is single-threaded and its property
202             // service is sometimes slow to respond (perhaps it's off
203             // starting a child process or something) and thus this
204             // times out and the caller thinks it failed, even though
205             // it's still getting around to it.  So we fake it here,
206             // mostly for ctl.* properties, but we do try and wait 250
207             // ms so callers who do read-after-write can reliably see
208             // what they've written.  Most of the time.
209             // TODO: fix the system properties design.
210             result = 0;
211         }
212     }
213 
214     close(s);
215     return result;
216 }


简述get过程 
.../bionic/libc/bionic/system_properties.c


145 int __system_property_get(const char *name, char *value)
146 {
147     const prop_info *pi = __system_property_find(name);//获得所要读的属性信息结构体
148 
149     if(pi != 0) {
150         return __system_property_read(pi, 0, value);    //读属性值,并返回
151     } else {
152         value[0] = 0;
153         return 0;
154     }
155 }




103 const prop_info *__system_property_find(const char *name)
104 {
105     prop_area *pa = __system_property_area__;     //共享内存头地址
106     unsigned count = pa->count;
107     unsigned *toc = pa->toc;
108     unsigned len = strlen(name);
109     prop_info *pi;
110 
111     while(count--) {   //从共享内存中找到相应属性结构体
112         unsigned entry = *toc++;
113         if(TOC_NAME_LEN(entry) != len) continue;
114 
115         pi = TOC_TO_INFO(pa, entry);
116         if(memcmp(name, pi->name, len)) continue;
117 
118         return pi;
119     }
120 
121     return 0;
122 }


124 int __system_property_read(const prop_info *pi, char *name, char *value)
125 {
126     unsigned serial, len;
127 
128     for(;;) {
129         serial = pi->serial;
130         while(SERIAL_DIRTY(serial)) {
131             __futex_wait((volatile void *)&pi->serial, serial, 0);
132             serial = pi->serial;
133         }
134         len = SERIAL_VALUE_LEN(serial);
135         memcpy(value, pi->value, len + 1);
136         if(serial == pi->serial) {
137             if(name != 0) {
138                 strcpy(name, pi->name);
139             }
140             return len;
141         }
142     }
143 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值