init.rc中的service 问题

本文探讨了在Android环境中,非AID_ROOT、AID_SYSTEM用户进程中调用ctl.start和ctl.stop时遇到权限问题的原因,并提供了解决方案。通过在控制权限列表control_perms中添加所需的用户权限,可以允许特定用户应用启动指定的服务。例如,通过添加{ service_xx

通过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


  1.  * White list of UID that are allowed to start/stop services. 
  2.  * Currently there are no user apps that require. 
  3.  */  
  4. struct {  
  5.     const char *service;  
  6.     unsigned int uid;  
  7.     unsigned int gid;  
  8. } control_perms[] = {  
  9.     { "dumpstate",AID_SHELL, AID_LOG },  
  10.      {NULL, 0, 0 }  
  11. };  
  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. static int check_control_perms(const char *name, int uid, int gid) {  
  20.     int i;  
  21.     if (uid == AID_SYSTEM || uid == AID_ROOT)  
  22.         return 1;  
  23.   
  24.     /* Search the ACL */  
  25.     for (i = 0; control_perms[i].service; i++) {  
  26.         if (strcmp(control_perms[i].service, name) == 0) {  
  27.             if ((uid && control_perms[i].uid == uid) ||  
  28.                 (gid && control_perms[i].gid == gid)) {  
  29.                 return 1;  
  30.             }  
  31.         }  
  32.     }  
  33.     return 0;  
  34. }  

 

只有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 }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值