Android 系统如何添加开机自启动 Shell 脚本

2024-06-04 4471阅读

添加开机自启动 Shell 脚本

很多时候,我们想在系统启动的时候干一些“私活”,这个时候,我们就可以添加开机自启动的脚本来完成。下面我们介绍一个简单的示例:

在 device/Jelly/Rice14 目录下添加如下的文件与文件夹:

initscript
├── Android.bp
├── initscript.rc
└── initscript.sh
sepolicy    #部分文件为 seandroid 入门添加的内容
├── device.te      
├── file_contexts
├── hello_se.te
└── initscript.te

initscript.sh 是一个简单的 shell 脚本:

#!/vendor/bin/sh
echo "this is init script"
log -t initscript "this is initscript!" #打 log

需要注意的是 shebang 的内容是 #!/vendor/bin/sh。

initscript.rc 的内容如下:

service initscript /vendor/bin/initscript
    class main
    user root
    group root system
    oneshot
  • class main 指明当前服务时系统的基本服务,保证了系统启动时,会启动这个服务
  • oneshot 表示服务只执行一次

    Android.bp 的内容如下:

    cc_prebuilt_binary {
        name: "initscript",
        srcs: ["initscript.sh"],
        init_rc: ["initscript.rc"], 
        strip: {
            none: true,
        },
        vendor: true
    }
    

    接着是配置 selinux:

    initscript.te 的内容如下:

    type initscript_dt, domain;
    type initscript_dt_exec, exec_type, vendor_file_type, file_type;
    init_daemon_domain(initscript_dt)
    domain_auto_trans(shell, initscript_dt_exec, initscript_dt);
    

    file_contexts 中添加如下内容:

    /vendor/bin/initscript          u:object_r:initscript_dt_exec:s0
    

    最后修改 device/Jelly/Rice14/Rice14.mk:

    PRODUCT_PACKAGES += \
        helloseandroid \
        initscript
    BOARD_SEPOLICY_DIRS += \
        device/Jelly/Rice14/sepolicy
    

    接着编译系统,启动模拟器:

    source build/envsetup.sh
    lunch Rice14-userdebug
    make -j16
    emulator
    

    接着我们查看 log:

    logcat | grep initscript
    04-08 23:34:06.250  1600  1600 W initscript: type=1400 audit(0.0:6): avc: denied { execute_no_trans } for path="/vendor/bin/toybox_vendor" dev="dm-1" ino=205 scontext=u:r:initscript_dt:s0 tcontext=u:object_r:vendor_toolbox_exec:s0 tclass=file permissive=0
    

    错误信息,提示我们缺少权限,按照之前介绍的方法使用 audit2allow 命令,发现并没有生成缺失的权限。那怎么办?看报错信息喽:

    报错信息的意思是:当 initscript_dt 执行安全上下文为 u:object_r:vendor_toolbox_exec:s0 的 /vendor/bin/toybox_vendor 时,缺少 execute_no_trans 权限。

    什么意思呢?

    我们先看下 /vendor/bin/toybox_vendor 文件:

    #切换为 root
    su 
    #带 x 权限,是一个可执行文件
    -rwxr-xr-x 1 root shell 503304 2023-04-08 23:04 /vendor/bin/toybox_vendor
    #不带参数执行一下
    /vendor/bin/toybox_vendor                                             
    acpi base64 basename bc blkid blockdev cal cat chattr chcon chgrp
    chmod chown chroot chrt cksum clear cmp comm cp cpio cut date dd devmem
    df diff dirname dmesg dos2unix du echo egrep env expand expr fallocate
    false fgrep file find flock fmt free freeramdisk fsfreeze fsync getconf
    getenforce getfattr grep groups gunzip gzip head help hostname hwclock
    i2cdetect i2cdump i2cget i2cset iconv id ifconfig inotifyd insmod
    install ionice iorenice iotop kill killall ln load_policy log logname
    losetup ls lsattr lsmod lsof lspci lsusb makedevs md5sum microcom
    mkdir mkfifo mknod mkswap mktemp modinfo modprobe more mount mountpoint
    mv nbd-client nc netcat netstat nice nl nohup nproc nsenter od partprobe
    paste patch pgrep pidof ping ping6 pivot_root pkill pmap printenv
    printf prlimit ps pwd pwdx readlink realpath renice restorecon rev
    rfkill rm rmdir rmmod runcon sed sendevent seq setenforce setfattr
    setprop setsid sha1sum sha224sum sha256sum sha384sum sha512sum sleep
    sort split start stat stop strings stty swapoff swapon sync sysctl
    tac tail tar taskset tee time timeout top touch tr traceroute traceroute6
    true truncate tty tunctl ulimit umount uname uniq unix2dos unlink
    unshare uptime usleep uudecode uuencode uuidgen vconfig vmstat watch
    

    从以上的操作可以看出 toybox_vendor 是一个命令集合,我们常用的 shell 命令均会通过 toybox_vendor 来执行。

    再回到权限那里,我们的脚本调用了 echo log 两个命令,这两个命令会通过执行 toybox_vendor 来实现,当执行 toybox_vendor 时,我们就需要 toybox_vendor 的打开,读取,执行权限,以及配置 domain 转换(A 程序到 B 程序都需要配置域转换)。 domain 转换可以简单配置执行时不转换 execute_no_trans 即可,综上,我们在 initscript.te 中添加如下权限:

    allow initscript_dt vendor_toolbox_exec:file { read open execute execute_no_trans };
    

    接着再次编译系统,启动模拟器:

    source build/envsetup.sh
    lunch Rice14-userdebug
    make -j16
    emulator
    

    进入 adb shell 查看信息:

    Android 系统如何添加开机自启动 Shell 脚本 第1张

    启动时打的 log,以及启动相关的属性值均正常,证明我们添加的脚本执行成功了。


    免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

    目录[+]