深入剖析Android中init进程实现的C语言源码

2020-01-06 13:38:30丽君

init进程会启动一个属性服务器,而客户端只能通过与属性服务器的交互来设置属性。

启动属性服务器

先来看一下属性服务器的内容,它由property_service_init_action函数启动,源码如下(/system/core/init/init.c&&property_service.c):

 

 
  1. static int property_service_init_action(int nargs, char **args)  { 
  2. start_property_service();  return 0; 
  3. }   
  4. static void load_override_properties()  { 
  5. #ifdef ALLOW_LOCAL_PROP_OVERRIDE  char debuggable[PROP_VALUE_MAX]; 
  6. int ret;   
  7. ret = property_get("ro.debuggable", debuggable);  if (ret && (strcmp(debuggable, "1") == 0)) { 
  8. load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE);  } 
  9. #endif  } 
  10.   static void load_properties(char *data) 
  11. {  char *key, *value, *eol, *sol, *tmp; 
  12.   sol = data; 
  13. while ((eol = strchr(sol, 'n'))) {  key = sol; 
  14. // 赋值下一行的指针给sol  *eol ++ = 0; 
  15. sol = eol;   
  16. value = strchr(key, '=');  if (value == 0) continue; 
  17. *value++ = 0;   
  18. while (isspace(*key)) key ++;  if (*key == '#') continue; 
  19. tmp = value - 2;  while ((tmp > key) && isspace(*tmp)) *tmp-- = 0; 
  20.   while (isspace(*value)) value ++; 
  21. tmp = eol - 2;  while ((tmp > value) && isspace(*tmp)) *tmp-- = 0; 
  22.   property_set(key, value); 
  23. }  } 
  24.   int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid) 
  25. {  struct sockaddr_un addr; 
  26. int fd, ret;  char *secon; 
  27.   fd = socket(PF_UNIX, type, 0);