int main(void)
{
int fd;
struct input_event event;
fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0) {
exit(1);
}
while (1) {
if (read(fd, &event, sizeof(struct input_event)) > 0) {
if (event.type == EV_KEY) {
printf("key %d %s\n", event.code,
event.value ? "down" : "up");
}
}
}
}
最终这段代码将被编译生成一个bin文件,在/system/bin/目录下。
那么首先是需要在init.rc里面启动这个服务:
service key /system/bin/key
class core
# key.te
type key, domain;
type key_exec, exec_type, file_type;
init_daemon_domain(key)
/system/bin/key u:object_r:key_exec:s0
-rwxr-xr-x root shell u:object_r:key_exec:s0 key
avc: denied { search } for pid=269 comm="key" name="input" dev="tmpfs" ino=176 scontext=u:r:key:s0 tcontext=u:object_r:input_device:s0 tclass=dir permissive=0
crw-rw---- root input u:object_r:input_device:s0 event0
allow key input_device:dir { search };
avc: denied { read } for pid=270 comm="key" name="event0" dev="tmpfs" ino=2352 scontext=u:r:key:s0 tcontext=u:object_r:input_device:s0 tclass=chr_file permissive=0
allow key input_device:chr_file { open read };
u:r:key:s0 root 272 1 /system/bin/key
sudo apt-get install policycoreutils
adb shell dmesg | audit2allow
echo "avc: denied { read } for pid=270 comm="key" name="event0" dev="tmpfs" ino=2352 scontext=u:r:key:s0 tcontext=u:object_r:input_device:s0 tclass=chr_file permissive=0" | audit2allow
#============= key ==============
allow key input_device:chr_file read;