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

添加开机自启动 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 查看信息:
在这里插入图片描述

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

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值