Android开机启动shell脚本(Android 8.0测试OK)

Android 下做开机启动shell脚本的大致流程如下:

目录

  1. 写shell脚本
  2. 为脚本写te文件
  3. 在init.rc中启动脚本
  4. 添加Selinux权限

写shell脚本

比如新建一个init.test.sh,内容如下:

#!/system/bin/sh
###注意这里的开头一定要写正确,android一般是/system/bin/sh 或/system/xbin/sh, linux下的是/bin/sh。写错了是执行不了的,不要以为有#没有影响。###


##这里建议测试时直接设置一个属性,不建议新建文件啥的,避免引起权限等的干扰##
setprop test.prop 111

建议写完后,先push到手机里面手动执行下,看是否正常运行。

 

为脚本写te文件

新建test_service.te 内容如下:

type test_service, coredomain;

type test_service_exec, exec_type, vendor_file_type, file_type;

#permissive test_service;

init_daemon_domain(test_service);

#allow shell test_service_exec:file { read open getattr execute };

我所做的是MTK平台,添加在了device/mediatek/sepolicy/basic/non_plat

 

接着在device/mediatek/sepolicy/basic/non_plat/file_contexts 下添加如下代码:

/(system\/vendor|vendor)/bin/ init.test.sh         u:object_r:test_service_exec:s0

 

注意:这一步即使你关闭了selinux还是要加的,不然开机启动不了。倘若你手机能接串口(不是adb),那调试起来将会相当爽。

 

在init.rc中启动脚本

service test_service /system/bin/ init.test.sh

    class main

    user root

    group root

    oneshot

seclabel u:object_r:test_service_exec:s0

这里一般芯片厂商一般都有专门的一个init.XXX.rc 或XXX.sh 用于客户启动自定义的脚本,不建议直接加在init.rc里面。

 

添加Selinux权限

这步根据你的实际报错来进行添加,可另行百度。

 

Ps:本文是对 https://blog.csdn.net/u012975242/article/details/83274693 理解与实际使用的优化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章