写给应用开发的 Android Framework 教程是一个系列教程,目前已更新以下内容:

  • Android Framework 学习道路指南

  • 体系开发根底

    • Ubuntu 运用快速入门
    • Make 构建东西入门
    • 理解 Unicode UTF-8 UTF-16 UTF-32
    • Linux Shell 脚本编程入门——核心根底语法
    • SEAndroid 运用极速上手
    • 理解 C++ 的 Memory Order
  • AOSP 上手指南

    • AOSP 极速上手
    • 体系开发东西推荐
    • 增加 Product
    • 增加 C/C++、Java 可履行程序
    • 增加 C/C++、Java 库
    • 增加装备文件与删去已有模块
    • 体系 App 源码增加
    • 运用 Android Studio 开发体系 App
    • 增加开机自发动 Shell 脚本
  • 学穿 Binder 系列

    • Binder 根本原理
    • Binder 程序示例之 C 言语篇
    • Binder 服务注册进程情形剖析之C言语篇
    • Binder 服务获取与运用进程情形剖析之C言语篇
    • Binder C++ 程序示例
    • Binder C++ 程序剖析之主要类解析
    • Binder 服务注册进程情形剖析之 C++ 篇
  • HAL 与硬件服务

    • Kernel 下载与编译
    • Linux 驱动开发入门

许多时候,咱们想在体系发动的时候干一些“私活”,这个时候,咱们就能够增加开机自发动的脚本来完结。下面咱们介绍一个简略的示例:

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 Framework 教程——玩转 AOSP 之添加开机自启动 Shell 脚本

发动时打的 log,以及发动相关的属性值均正常,证明咱们增加的脚本履行成功了。

关于

假如你对 Android Framework 感兴趣,能够持续重视:

  • 平台个人技术博客
  • 我的个人公众号

写给应用开发的 Android Framework 教程——玩转 AOSP 之添加开机自启动 Shell 脚本

  • 我的个人微信(能够加我微信进 Android Framework 沟通群)

写给应用开发的 Android Framework 教程——玩转 AOSP 之添加开机自启动 Shell 脚本