ITPub博客

首页 > > > Android7.1 在init.rc 添加shell服务

Android7.1 在init.rc 添加shell服务

作者:dapan 时间:2018-11-08 17:47:20 0 删除 编辑

我有一个需求就是在Android系统开机的时候把一个配置文件放到Android系统的data目录中。

我使用的方法就是在 init.rc 中添加一个服务,这个服务就是执行一个将文件拷贝到指定目录的脚本,下面是添加服务的方法.


1、写一个shell脚本

/system/bin/factoryboot.sh                                                 <
#!/system/bin/sh
echo "factoryboot start." >> /cache/recovery/last_factory_log
if [ ! -f "/cache/recovery/last_factory_bit" ];then
        echo "last_factory_bit is not exist" >> /cache/recovery/last_factory_log
else
        factory_bit=`head -1 /cache/recovery/last_factory_bit`
        echo "factory_bit" >> /cache/recovery/last_factory_log
        echo $factory_bit >> /cache/recovery/last_factory_log
fi
echo "factoryboot end." >> /cache/recovery/last_factory_log

注意!!因为echo的日志不会显示在dmesg里面,这样重定向到文件会更好查看日志

2、之后在 init.rc 文件的最后加上

service factoryboot /system/bin/factoryboot.sh
    class main
    user root
    group root
    oneshot

注意!!服务的名字不要有下划线‘_’,没完全理解,请不要另类

上面这一段是添加了一个服务。

然后,还需要在init.rc里添加这么一段代码:

on property:sys.boot_completed=1
    start factoryboot

作用是启动前面添加的服务。

注意:init.rc 文件的执行是有特定的顺序的,添加这些代码的时候,最好都放在和原来 init.rc 里面格式类似的地方


3、源码拷贝文件

你可以把你需要执行的工具或脚本直接放在/out/…/system/bin的目录下,也可以放在你的源码中

PRODUCT_COPY_FILES += \
    device/rockchip/rk3288/factoryboot.sh:/system/bin/factoryboot.sh \

这些做完之后,编译,烧写,在Android启动的时候,就会自动执行你添加的服务了。


4、添加selinux权限

diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts
index 8ac09f9..12e904c 100755
--- a/sepolicy/file_contexts
+++ b/sepolicy/file_contexts
@@ -66,7 +66,7 @@
 #for evergrande_start service
 /system/bin/evergrande_start.sh   u:object_r:evergrande_start_exec:s0
 #for factory check data file system
+/system/bin/factoryboot.sh          u:object_r:factoryboot_exec:s0
 
 #hdmi
 /sys/devices/virtual/display/HDMI(/.*)? -- u:object_r:sysfs_hdmi:s0
weiqifa@ubuntu:~/repoRk3399_yan4_dev/device/rockchip/common/sepolicy$ cat factoryboot.te 
type factoryboot, domain;
type factoryboot_exec, exec_type, file_type;
permissive factoryboot;

init_daemon_domain(factoryboot)

allow factoryboot shell_exec:file        { getattr read open execute execute_no_trans};
allow factoryboot system_file:file       { execute_no_trans };
allow factoryboot rootfs:lnk_file        { getattr };
allow factoryboot zygote_exec:file       { getattr read open execute execute_no_trans };
allow factoryboot servicemanager:binder            { call transfer };
allow factoryboot sysfs:file                       { read write open };
allow factoryboot toolbox_exec:file                { getattr execute read open execute_no_trans };
allow factoryboot serial_device:chr_file       { read write ioctl getattr };
allow factoryboot cache_recovery_file:dir      { search write add_name };
allow factoryboot cache_recovery_file:file     { create write open };
allow factoryboot cache_file:dir     { search };
allow factoryboot cache_recovery_file:file     { getattr read write ioctl};
#allow factoryboot cache_recovery_file:file     { getattr };

注意!!在factoryboot.te里面有一个permissive factoryboot; 这个用来说明如果权限有问题也执行脚本,这个权限非常有用。

原文作者:;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2020-08-07

  • 博文量
    4504
  • 访问量
    87098