安卓系统开机运行shell脚本

在安卓系统上很多业务需求是通过shell脚本实现的,开机自启动一般做法是创建安卓service服务,然后通过该服务调用执行shell脚本。详细步骤:

1、编辑shell脚本

如下shell脚本功能为:循环查询系统下是否有厂商ID为0x1A86的USB转串口设备匹配到了CDC-ACM驱动上,若是则解绑USB设备和CDC-ACM驱动的绑定,并重新绑定到厂商的CH343SER串口驱动上。

#! /bin/sh

usbpath=""
usbnode=""
usbdevpath='/sys/bus/usb/devices/'
usbdriverpath='/sys/bus/usb/drivers/'

while [ true ]
do
	for file in /sys/bus/usb/drivers/cdc_acm/*
	do
		if [ -d "$file" ]
		then 
			usbpath=${file##*/}
			usbpath=${usbpath%:*}
			idVendor=$usbdevpath$usbpath'/idVendor'

			if [ ! -f "$idVendor" ]
			then
				continue
			fi

			if [[ $(cat $idVendor) == "1a86" ]]
			then
				usbnode=${file##*/}
				echo $usbnode > /sys/bus/usb/drivers/cdc_acm/unbind
				echo $usbnode > /sys/bus/usb/drivers/usb_ch343/bind
			fi
		fi
	done
	sleep 1
done

2、修改device.mk文件

在该文件中增加,实现将脚本文件编译时拷贝到系统。shell脚本文件的系统路径:/vendor/bin/ch343check.sh

PRODUCT_COPY_FILES += \
    $(LOCAL_PATH)/ch343check.sh:$(TARGET_COPY_OUT_VENDOR)/bin/ch343check.sh \

3、修改init.xxx.rc文件

在系统启动rc文件中新增service服务,如下所示:

#Add shell scripts for ch343 service
service ch343check /vendor/bin/sh /vendor/bin/ch343check.sh
    class main
    user root
    group root

service声明格式:service [服务名称] [执行的shell命令]

注:部分平台上必须使用"sh + shell"脚本名称的方式声明,否则可能不工作。"class main" 声明方式可实现开机自动执行,并不需要在 on property:sys.boot_completed=1 后面添加 "start ch343check"

4、查看并设置selinux权限

查看运行此服务所需要的selinux权限,可通过“start 服务名”,查看logcat确定权限。

adb shell "dmesg | grep avc" > avc_log.txt

如下所示:

[ 232.117640 ] type=1400 audit(1682072440.187:512):avc: denied { write } for comm="sh" name="unbind" dev="sysfs" ino=38155 scontext=u:r:vendor_qti_init_shell:s0 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0

其中 permissive=0 说明缺乏某项权限。

根据如上权限问题,修改qcom下selinux的sepolicy目录下的,file_contents和init_shell.te文件

file_contents文件新增:
/(vendor|system/vendor)/bin/ch343check\.sh u:object_r:vendor_qti_init_shell_exec:s0

根据te文件规则 allow scontext tcontext : tclass permission 在te文件后面增加对应的权限。

init_shell.te文件新增:
allow vendor_qti_init_shell sysfs:file { write }; 

注:可以选择创建新的te文件(系统常规会遍历文件夹下的所有te文件),也可以在原有的te文件中新增内容。

编译到系统后,查看文件或进程是否有此新增权限,可使用 “ls -Z filepath”“ps -ef -Z” 命令。

/vendor/bin/ch343check.sh u:object_r:vendor_qti_init_shell_exec:s0
# ps -ef -Z | grep ch343
u:r:vendor_qti_init_shell:s0   root  1250 1 0 14:02:55 ? 00:00:00 sh /vendor/bin/ch343check.sh

5、解决neverallow冲突

当修改te文件后进行系统编译时,可能会遇到安卓系统编译问题。原因是新增的allow规则与全局的neverallow有冲突,举例:

[2023-04-22T11:40:58.529Z] neverallow check failed at out/soong/.intermediates/system/sepolicy/recovery_sepolicy.cil/android_common/recovery_sepolicy.cil:11224 from system/sepolicy/public/domain.te:507
[2023-04-22T11:40:58.529Z]   (neverallow domain vendor_file_type (file (write create setattr relabelfrom append unlink link rename)))
[2023-04-22T11:40:58.529Z]     <root>
[2023-04-22T11:40:58.529Z]     allow at out/soong/.intermediates/system/sepolicy/recovery_sepolicy.cil/android_common/recovery_sepolicy.cil:40813
[2023-04-22T11:40:58.529Z]       (allow shell vendor_file (file (read write getattr execute open execute_no_trans)))
[2023-04-22T11:40:58.529Z]
[2023-04-22T11:40:58.529Z] Failed to generate binary
[2023-04-22T11:40:58.529Z] Failed to build policydb

此问题,直接修改报错文件 domain.te,然后在出错行号的 neverallow 定义中使用 "-xxx" 来排除对此权限的not allow。

修改前:
neverallow { domain } vendor_file_type (file (write create setattr relabelfrom append unlink link rename))

修改后:
neverallow { domain -shell } vendor_file_type (file (write create setattr relabelfrom append unlink link rename))

除此之外,domain.te 会与系统的其他 apixx/domain.te 文件进行比对,此内容必须完全匹配。建议直接复制替换即可。

至此,安卓系统通过服务实现开机自动运行shell脚本完成。

猜你喜欢

转载自blog.csdn.net/WCH_TechGroup/article/details/130337224
今日推荐