Android 修改init.rc 添加开机自启服务

说明

硬件:ROC-RK3399-PC-Pro
固件:ROC-RK3399-PC-Pro_Industry7.1.2_HDMI
目录: firefly-rk3399-Industry 为工程根目录,所有编译、烧录命令均在改目录下执行
官方教程:https://wiki.t-firefly.com/zh_CN/ROC-RK3399-PC-Pro/
个人专栏:firefly ROC-RK3399-PC-Pro开发板专栏

开机自启服务:ademo
完成任务:写入一条日志

前提:这里我们假定我们已经成功将可执行文件编译到系统中,具体编译方式请参考博文
android 7.1 修改源码添加可执行文件到system/bin目录
可执行文件名称为 ademo位于/system/bin目录下,可执行文件源码 ademo.c 如下:

#include <stdio.h>  // fopen(),fclose(), fprintf(), printf()
#include <errno.h> // errno
#include <string.h> // strerror()

int main()
{
    FILE *log_fp = NULL;
    // 在on sys.boot_completed=1 时操作sdcard失败,建议操作/data/local/tmp/
    log_fp = fopen("/data/local/tmp/ademo_log", "w+");
    if (log_fp != NULL)
    {
        fprintf(log_fp, "open /data/local/tmp/ademo_log success\n");
        fprintf(log_fp, "----------------log by ademo\n");
        fclose(log_fp);
    }
    else
    {
        int errNum = errno; // 防止 printf 异常
        printf("open /data/local/tmp/ademo_log failed, errno:%d, error:%s\n", errNum, strerror(errNum));
    }
    return 0;
}

1. 在 init.rc 中添加自己的服务

init.rc 位置:firefly-rk3399-Industry/system/core/rootdir/init.rc

1.1 定义 service

service ademo /system/bin/ademo
    user root
    group root
    seclabel u:r:ademo:s0
    class main    
    oneshot

1.2 设置启动时机

设置启动时机为开机自启

on property:sys.boot_completed=1
    start ademo

2. 设置SELinux策略(sepolicy)

文件路径:firefly-rk3399-Industry/device/rockchip/common/sepolicy

2.1 修改file_contexts

修改firefly-rk3399-Industry/device/rockchip/common/sepolicy/file_contexts文件,增加如下行:

#ademo
# 定义 ademo 的文件类型(type)为 ademo_exec,(新的文件类型)
/system/bin/ademo          u:object_r:ademo_exec:s0

2.2 新建ademo.te

同级目录下 ademo.te 文件,添加内容如下


type ademo, domain;

# 将 ademo_exec 关联到 exec_type 和 file_type
type ademo_exec, exec_type, file_type;

# permissive ademo 说明如果权限有问题也执行脚本,这个权限非常有用,但也比较危险
permissive ademo;
# 进行进程 domain 切换
init_daemon_domain(ademo)

allow ademo shell_exec:file        { getattr read open execute execute_no_trans};
allow ademo system_file:file       { execute_no_trans };
allow ademo rootfs:lnk_file        { getattr };
allow ademo zygote_exec:file       { getattr read open execute execute_no_trans };
allow ademo servicemanager:binder            { call transfer };
allow ademo sysfs:file                       { read write open };
allow ademo toolbox_exec:file                { getattr execute read open execute_no_trans };
allow ademo serial_device:chr_file       { read write ioctl getattr };
allow ademo cache_recovery_file:dir      { search write add_name };
allow ademo cache_recovery_file:file     { create write open };
allow ademo cache_file:dir     { search write add_name create};
# 运行 ademo 进程对type为cache_recovery_file的 file 进行getattr read write ioctl操作
allow ademo cache_recovery_file:file     { getattr read write ioctl};


有关SeLinux,这里有一篇比较好的博文 Linux安全机制之Selinux

3. 编译运行

3.1 编译

此过程分三步,具体参考firefly ROC-RK3399-PC-Pro android7.1 编译打包详解

  • 整体编译:./FFTools/make.sh -d rk3399-roc-pc-plus -j8 -l rk3399_roc_pc_plus-userdebug
  • 打包固件:./FFTools/mkupdate/mkupdate.sh -l rk3399_roc_pc_plus-userdebug
  • 升级固件:sudo upgrade_tool uf the_path_of_image_file

3.2 运行效果

开机后查看日志文件,如果有 ademo 打印的日志,则说明设置开机自启成功。
在这里插入图片描述

4. 其他

4.1 注意事项

  • 操作目录最好是 /data/local/tmp,我尝试过操作 sdcard 和 /storage/self/primary,均 access 失败,提示无此文件或目录,也许是在 boot_completed=1 时,sdcard 还没有被mount上。

4.2 参考文献

  1. android selinux开发入门
  2. Linux安全机制之Selinux
  3. android selinux权限配置
  • 1
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值