Android10开机自动启动应用的权限配置

在android8.0之前的系统,native层编译的 二进制xxx.bin文件可以直接放到init.rc中当成一个service,放到on init标签中用start xxx 来运行,只要内核设备树中设置selinux权限为SELINUX_PERMISSIVE模式(调试模式),都可以正常运行,不会报错误。但是在android8.1之后,android的系统分区结构发生变化,我们native层编译的二进制xxx.bin文件可能编译到system/bin/xxx.bin,也有可能放到平台到vendor/bin/xxx.bin下,针对这两种情况,需要添加不同的selinux属性权限才可以正常运行。

1、native层编译的二进制xxx.bin文件编译到vendor/bin/xxx.bin的情况

service cameraTest /vendor/bin/cameraTest
   class hal
   priority -20
   user graphics
   group automotive_evs
   disabled
   oneshot

on init
    start cameraTest

以上添加之后,在系统开机启动时,init进程会打印

init: Could not start service 'cameraTest' as part of class 'hal': File /vendor/bin/cameraTest(labeled "u:object_r:system_file:s0") has incorrect label or no domain transition from u:r:init:s0 to another SELinux domain defined. Have you configured your service correctly?https://source.android.com/security/selinux/device-policy#label_new_services_and_address_denials

说明缺少selinux权限。需要添加selinux权限,方法如下:

1.1) 在android/system/sepolicy/vendor/file_contexts文件最下面加上一句

/(vendor|system/vendor)/bin/hw/cameraTest     u:object_r:cameraTest_exec:s0

1.2)在system/sepolicy/vendor/目录下新增一个文件cameraTest.te

type cameraTest, domain;
type cameraTest_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(cameraTest)

1.3)在system/sepolicy/prebuilts/api/29.0/private/file_contexts里,增加如下一条:

/(vendor|system/vendor)/bin/cameraTest                 u:object_r:cameraTest_exec:s0

1.4 在system/sepolicy/prebuilts/api/29.0/private下新增一个文件cameraTest.te

type cameraTest, domain;
type cameraTest_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(cameraTest)

以上4个步骤是针对编译到vendor/bin/cameraTest的情况,添加完以上4步之后系统全编译下载测试,系统运行之后看init进程是否还在报错误,如果没有了可以使用 ps -Af  |grep cameraTest进行查看是否运行成功。

2、针对编译到system/bin/cameraTest的情况添加selinux权限

2.1)在system/sepolicy/prebuilts/api/29.0/private/file_contexts里新增如下一行代码

/system/bin/cameraTest	--	u:object_r:cameraTest_exec:s0

2.2 在sepolicy/prebuilts/api/29.0/private/目录下新增文件cameraTest.te 内容如下:

type cameraTest, coredomain;
type cameraTest_exec, exec_type, file_type;
init_daemon_domain(cameraTest)

2.3 在system/sepolicy/private/file_contexts文件里新增如下一行代码:

/system/bin/cameraTest	--	u:object_r:cameraTest_exec:s0

2.4 在system/sepolicy/private/目录下新增文件cameraTest.te 文件内容为:

type cameraTest, coredomain;
type cameraTest_exec, exec_type, file_type;
init_daemon_domain(cameraTest)

完成后在android目录执行make installclean,然后进行全编译。下载系统运行之后看init进程是否还在报错误,如果没有了可以使用 ps -Af  |grep cameraTest进行查看是否运行成功。

以上的sepolicy/prebuilts/api/29.0/中的29.0指的是android SDK版本api级别号

android10(API级别29) Android 9(API 级别 28) Android 8.1(API 级别 27) Android 8.0(API 级别 26) Android 7.1(API 级别 25) Android 7.0(API 级别 24) Android 6.0(API 级别 23)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值