用c写串口调试程序,相关功能都已经调试完成,正准备做成jni给java层调用,发现在调用tcsetattr接口的时候一直返回-1,错误码5,错误信息I/O error,找原因找了一段时间,还把android推荐的那个串口库也移植了,问题依然存在,后面仔细看了下log,发现是全新问题如下:
01-18 08:58:26.990 2692 2692 I jnidemo: type=1400 audit(0.0:53): avc: denied { read write } for name="ttyS1" dev="tmpfs" ino=8867 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serial_device:s0 tclass=chr_file permissive=1
01-18 08:58:26.990 2692 2692 I jnidemo: type=1400 audit(0.0:54): avc: denied { open } for path="/dev/ttyS1" dev="tmpfs" ino=8867 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serial_device:s0 tclass=chr_file permissive=1
01-18 08:58:26.993 2692 2692 I jnidemo: type=1400 audit(0.0:55): avc: denied { ioctl } for path="/dev/ttyS1" dev="tmpfs" ino=8867 ioctlcmd=5401 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:serial_device:s0 tclass=chr_file permissive=1
发现原来是权限搞得鬼(并非网上所说的NDK版本不对,所以还是要仔细查log)
根据sepolicy权限添加原则(system\sepolicy):
scontext=u:r:untrusted_app:对应的权限文件untrusted_app.te
tcontext=u:object_r:serial_device:操作的相应设备 serial_device
tclass=chr_file:操作的对应类chr_file
最终在untrusted_app.te 增加如下:
allow untrusted_app serial_device:chr_file { read write open ioctl };
编译kernel,烧写,重新运行就ok