【Android Framework】开机执行自定义脚本之Init.rc文件的妙用与如何编写开机脚本?

Init.rc妙用及语法说明
参考:system\core\init\readme.txt


案例1
当开机启动完毕,写mpp2的寄存器,使其设置为PWM模式。

#add by eliot shao 2016.11.03 for mmp2-pwm function
on property:sys.boot_completed=1
    write /sys/kernel/debug/spmi/spmi-0/address 0xa140
    write /sys/kernel/debug/spmi/spmi-0/data 0x1a

案例2
创建一个native service,在系统启动完毕后执行某脚本,完成一个简单的任务。 步骤: ①在system/core/rootdir/init.rc中加入:

on property:sys.boot_completed=1
start service_set_bkl
service service_set_bkl /system/bin/setBKL.sh
    user root
    group root
    disabled
    oneshot
② 在device/qcom/msm8909/msm8909.mk中加入:

#add by eiot shao 2016.11.11 for bug 4612
PRODUCT_COPY_FILES += device/qcom/msm8909/setBKL.sh:system/bin/setBKL.sh
将device/qcom/msm8909/setBKL.sh脚本拷贝到system/bin/setBKL.sh

③ 在device/qcom/slm753/创建需要执行的脚本setBKL.sh setBKL.sh脚本内容如下:

#!/system/bin/sh
busybox echo "eliot shao !!!native service for backlight setting!!for bug 4612"
#busybox echo 255 >/sys/class/leds/lcd-backlight/brightness
#sleep 2
#you can add some register setting such as:
busybox cat /sys/class/leds/lcd-backlight/brightness > /sys/class/leds/lcd-backlight/brightness
busybox echo "eliot shao !!!native service for backlight setting!"

Init语言包含了四个大类的声明
Actions, Commands, Services, and Options.

Actions and Services implicitly declare a new section. All commands or options belong to the section most recently declared. Commands or options before the first section are ignored.

Actions和Services隐性的声明了一个新的段,所有的commands和options都属于这些最近声明的段。commands和options放在段之前会被忽略。

Actions take the form of:

on
   
   
   
Services take the form of:

service [ ]*
   

[ ]* 挂载设备到某个目录

13、setenforce 0|1 设置SELinux system-wide的状态

14、setprop 设置system property

15、start 启动service

16、stop 停止service

17、wait [ ] 等待某个文件的出现

18、write 向文件写入字符串

 Eg: write /proc/cpu/alignment 4

**selinux问题

新增service遇到selinux问题,可参考如下方法添加规则;
例如:service名为meig_camera_service

1、新建meig_camera_service.te,后续service相关权限都可以在这里添加:
      先加入下面内容:
type meig_camera_service, domain, domain_deprecated; # 定义 domain
type meig_camera_service_exec, exec_type, file_type; # 定义 可执行文件类型
init_daemon_domain(meig_camera_service) # 赋权 domain

2、service.te 中添加服务类型定义:
type meig_camera_service_service, service_manager_type;

3、service_contexts中添加 服务 映射规则:
xxx_camera_service u:object_r:xxx_camera_service_service:s0

4、file_contexts中添加 可执行文件 映射规则:
/system/bin/xxx_camera_service u:object_r:xxx_camera_service_exec:s0

你可能感兴趣的:(Linux)