1. 程式人生 > >設定ctl.start 屬性啟動init.rc 中service以及許可權問題

設定ctl.start 屬性啟動init.rc 中service以及許可權問題

通過property_set("ctl.start", service_xx);

來啟動init.rc中的service是一個很方便方法來呼叫某個可執行程式或某個指令碼程式

service service_xx  /system/bin/xx

    disabled
    oneshot

但在非AID_ROOT、AID_SYSTEM 使用者的程序中呼叫ctl.start ctl.stop會碰到許可權問題:

system/core/init/property_service.c

[cpp] view plain copy print?
  1. /* 
  2.  * White list of UID that are allowed to start/stop services.
     
  3.  * Currently there are no user apps that require. 
  4.  */
  5. struct {  
  6.     constchar *service;  
  7.     unsigned int uid;  
  8.     unsigned int gid;  
  9. } control_perms[] = {  
  10.     { "dumpstate",AID_SHELL, AID_LOG },  
  11.      {NULL, 0, 0 }  
  12. };  
  13. /* 
  14.  * Checks permissions for starting/stoping system services. 
  15.  * AID_SYSTEM and AID_ROOT are always allowed.
     
  16.  * 
  17.  * Returns 1 if uid allowed, 0 otherwise. 
  18.  */
  19. staticint check_control_perms(constchar *name, int uid, int gid) {  
  20.     int i;  
  21.     if (uid == AID_SYSTEM || uid == AID_ROOT)  
  22.         return 1;  
  23.     /* Search the ACL */
  24.     for (i = 0; control_perms[i].service; i++) {  
  25.         if (strcmp(control_perms[i].service, name) == 0) {  
  26.             if ((uid && control_perms[i].uid == uid) ||  
  27.                 (gid && control_perms[i].gid == gid)) {  
  28.                 return 1;  
  29.             }  
  30.         }  
  31.     }  
  32.     return 0;  
  33. }  
save_snippets.png

只有uid == AID_SYSTEM || uid == AID_ROOT

或符合 control_perms[] = {
    { "dumpstate",AID_SHELL, AID_LOG },
     {NULL, 0, 0 }
}; 的uid程序才有許可權star/stop services

因此,如果我們碰到了許可權問題,根據log提示,在/system/core/include/private/android_filesystem_config.h

中查到程序定義,新增到control_perms[]列表

比如,uid ==AID_WIFI的某個程式需要許可權啟動service_xx

control_perms[] = {
    { "dumpstate",AID_SHELL, AID_LOG },

+  { "service_xx ",AID_WIFI, AID_WIFI},
     {NULL, 0, 0 }
};