写给应用开发的 Android Framework 教程——玩转 AOSP 之添加开机自启动 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,以及启动相关的属性值均正常,证明我们添加的脚本执行成功了。

猜你喜欢

转载自juejin.im/post/7219712310586474553