android 5.1 动态hosts实现

来源:互联网 发布:java反射机制的作用 编辑:程序博客网 时间:2024/06/14 04:19

公司自己的rom ,根据公司需求,需要更新hosts文件;仅供参考

android 版本为5.1 ,在没有root的前提下,system分区为只读;

hosts文件在系统的路径为 :/system/etc/hosts

         在源码的路径为: /system/core/rootdir/etc/hosts ,如果想在hosts中预制ip,可以在此位置添加;

但若想更新hosts,需要对system分区有读写权限;

1,创建sh脚本;名为 remntsys.sh

    功能:修改system分区为可读写,将获取的hosts文件拷贝到/system/etc/hosts下,修改hosts权限,修改system分区为只读。

mount -o remount,rw /dev/block/platform/mtk-msdc.0/by-name/system /system

cp /sdcard/hosts /system/etc/hosts

chmod 644 /system/etc/hosts

mount -o remount,ro /dev/block/platform/mtk-msdc.0/by-name/system /system

源码:/system/core/include/private/android_filesystem_config.h 中,添加sh脚本的执行权限,添加系统下/system/etc和/system/etc/hosts的读写权限;

android_dis[] ={}中,加 {00777,AID_ROOT,AID_ROOT,0,"system/etc"}

android_files[] = {}中,加 {00555,AID_ROOT, AID_ROOT, 0 , "system/etc/remntsys.sh"} ,

{00555, AID_ROOT, AID_ROOT, 0, "system/etc/hosts"}

2,配置该脚本;

源码:/device/discovery/m853p/device.mk , 其中discovery 是由方案商提供,在该文件中配置脚本在编译后系统的路径;

PRODUCT_COPY_FILES += device/discovery/m853p/remntsys.sh: system/etc/remntsys.sh

源码:/system/core/rootdir/init.rc , 此文件为系统启动初始化文件,最先加载。配置sh脚本启动服务

service remount-sys /system/etc/remntsys.sh

class main

disable

oneshot

3,加载脚本

系统代码调用该服务,

import android.os.SystemProperties;

SystemProperties.set("ctl.start","remount-sys");  // remount-sys 为init.rc 中自定义的服务名称


        在pc终端:adb shell情况下,执行 setprop ctl.start remount-sys,使用dmesg查看执行日志,进行调试(重要,下面所需的权限都是通过此方式添加)

**添加权限**

源码:/external/sepolicyinit.te , 添加文件的执行权限,具体权限参照dmesg日志信息

allow init system_file:file write;

allow init system_file :file append;

源码:/external/sepolicy/system_app.te 添加文件执行权限,具体参照dmesg日志

allow system_app powerctl_prop:property_service set;

allow system_app ctl_default_prop:property_service set;

例: 一下为截取dmesg日志;

denied { setattr } for pid=3179 comm="chmod" name="hosts" dev="mmcblk0p20" ino=655 scontext=u:r:init:s0 tcontext=u:object_r:system_file:s0 tclass=file permissive=0 

具体修改语法参照相应te文件

scontext中包含的init是te文件名,对应的init.te

allow init system_file:filesetattr   

大括号内的代表缺少的权限


源码:/external/sepolicy/domain.te  , 注释2行代码,此代码限制了init.te 中添加的文件权限;

         #only recovery should be doing write to /system 下面的2行

源码:/device/mediatek/common/sepolicy/property.te ,注释一行代码,

#never allow {domain -inti -system_server -recovery } ctl_default_prop:property_service set;


4,调用;我的方案是在系统app中执行,例如:packageInstall.apk ,在mainfest中,添加shareUserId=“android.uid.system"

通过SystemProperties.set("ctl.start","remount-sys");调用sh脚本。


0 0