利用fuser查看文件使用情况

本文介绍如何在Android中创建一个root权限下的fuser服务,用于检查文件是否被其他程序操作。通过修改init.rc文件和编写fuser.sh脚本,结合应用层与服务层的property通信机制,实现对指定文件的操作状态监测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android中有时候我们需要通过应用查看一个文件是否有被其他文件操作,在linux命令行下可以通过fuser命令查看,如果上层的android应用直接调用该程序则由于受权限影响无法查看(fuser会通过/proc文件系统来搜集相关的操作文件的信息),即fuser只有在root 权限下才能充分发挥查看占用的问题。要达到这个目的,可以将fuer变为一个运行于root权限下的按需调用的服务,通过该服务间接实现查看文件的目的。服务和应用之间的通讯可以通过property来进行。具体如下:
修改init.rc文件,加入fuser service
service fuser /system/bin/fuser.sh
    user root
    oneshot
    disabled
增加fuser.sh运行脚本如下:
#!/system/bin/busybox sh
#app will set boot.CHK_FILE to the file(absolute path) he wants to check,then trigger this service
#the result will be incuded in the proprity ,0 means occupied, 1 means file not exist or some error occuor.
/system/bin/setprop "boot.LOOK_PROG_RES" ""
/system/bin/setprop "boot.LOOK_PROG_STAT" "beginning"
PRG=`/system/bin/getprop "boot.CHK_FILE"`
/system/bin/busybox fuser $PRG
CHK_RESULT=$?
echo check prg=$PRG,result=$CHK_RESULT
/system/bin/setprop "boot.LOOK_PROG_RES" "$CHK_RESULT"
/system/bin/setprop "boot.LOOK_PROG_STAT" "end"
在应用代码中嵌入如下代码:
SystemProperties.set("boot.CHK_FILE", "/sdcard/music/zh.mp3");
       SystemProperties.set("boot.LOOK_PROG_STAT","");
        SystemService.start("fuser");
        try{Thread.sleep(1000);}catch(Exception e){}
while(SystemProperties.get("boot.LOOK_PROG_STAT").compareTo("end")!=0)
{
      Log.v("boot.LOOK_PROG_STAT="+SystemProperties.get("boot.LOOK_PROG_STAT"));
       try{Thread.sleep(100);}catch(Exception e){}
}
Log.v("====get check result:"+SystemProperties.get("boot.LOOK_PROG_RES"));
即可实现是否检查/sdcard/music/zh.mp3是否有被其他程序操作。
注意应用代码的manifest中需要加入system权限:
android:sharedUserId="android.uid.system"
此外还需调整修改property_service.c,加入新的bootsystem的权限。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值