添加开机自启动 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,以及启动相关的属性值均正常,证明我们添加的脚本执行成功了。

下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/20516,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?