系统文件节点操作,
有个手电筒功能,其实就是开关一个灯,之前驱动做成闪光灯,这个上层可以通过camera的接口去操作灯,但是带了一个问题就是操作灯以后就不能操作摄像头了,于是就修改为节点操作,节点操作需要注意2个问题,一个是节点文件权限,还有就是selinux权限
节点权限无非就是init.rc文件里chmod给权限,selinux权限就需要看log需要给什么权限了,当然还有一种更简单暴力的方法就是直接关闭selinux了,这个网上搜下很多了,不作介绍了,下面看下在.te文件里给权限的操作了
今天在系统app中操作节点/sys/bus/platform/drivers/mtk-kpd/kpd_torch_open_state时,发现打不开灯,查看log avc 发现下面的log,
avc: denied { open } for path="/sys/bus/platform/drivers/mtk-kpd/kpd_torch_open_state" dev="sysfs" ino=18379 scontext=u:r:system_app:s0 tcontext=u:object_r:sysfs_keypad_file:s0 tclass=file permissive=1
scontext 指的需要赋予权限的主体,tcontext 指受体, 即scontext需要tcontext open权限
我们在.te文件里添加。这里还添加了read, write search等权限,
allow system_app sysfs_keypad_file:file { open read write getattr };
allow system_app sysfs_keypad_file:dir { write add_name remove_name search };
中间调试的时候,刷机第一次是不会有avc的log出来,也没有操作成功,灯没亮,需要setenforce 0 和 1 一下,也就是关闭下selinux,然后打开才会有avc log出来,
这样就可以点亮了,
操作节点的时候,可以直接读取节点里的值,然后判断下次是要打开还是关闭,就不需要加个变量保存去判断是打开还是关闭,贴下操作代码吧
private void torchLedHanlde(){
byte[] buffer=fileRead("/sys/bus/platform/drivers/mtk-kpd/kpd_torch_open_state");
if(buffer!=null){
Log.e("songjian","torchLedHanlde buffer="+buffer[0]);
if(buffer[0]=='0')
savePackageFile("yes".getBytes(),false,"aaaa");
else if(buffer[0]=='1')
savePackageFile("no".getBytes(),false,"aaaa");
}
}
private byte[] fileRead(String filePath)
{
byte[] buffer=null;
try {
FileInputStream fis;
File file=new File(filePath);
fis = new FileInputStream(file);
buffer = new byte[1024];
fis.read(buffer);
//String fileContent = EncodingUtils.getString(buffer, "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer;
}
private void savePackageFile(byte[] byteArray,boolean isSeek, String fileName) {
try {
File file = new File("/sys/bus/platform/drivers/mtk-kpd/kpd_torch_open_state");
FileOutputStream fileOutputStream = new FileOutputStream(file,isSeek);
fileOutputStream.write(byteArray);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("songjian","error="+e);
}
}