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

您所在的位置:网站首页 安卓开机自动运行网页 Android 系统如何添加开机自启动 Shell 脚本

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

2024-07-17 16:12:35| 来源: 网络整理| 查看: 265

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



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭