根據項目需求,在init.rc添加一個服務,開機自動啟動這個服務:
service eGTouchD /system/bin/eGTouchD class main user root group root oneshot
開機后通過ps -Z查看進程,發現/system/bin/eGTouchD這個服務並沒有跑起來。
查看kernel的開機log信息,發現有如下提示
init: Service eGTouchD does not have a SELinux domain defined.
說明該服務沒有啟動起來,是被selinux服務給禁用掉了。
為了解決這個問題,需要自己添加一些允許的規則,讓改服務可以正常啟用:
1.修改seplicy/file_contexts文件,添加以下內容:
/system/bin/eGTouchD u:object_r:eGTouchD_exec:s0
2.在system/sepolicy下新加eGTouchD.te文件,內容如下:
# File types must be defined for file_contexts. type eGTouchD, domain; type eGTouchD_exec, exec_type, file_type; init_daemon_domain(eGTouchD) allow eGTouchD rootfs:lnk_file { getattr }; allow eGTouchD shell_exec:file { execute read open execute_no_trans getattr }; allow eGTouchD system_data_file:dir { read open write remove_name add_name }; allow eGTouchD toolbox_exec:file { getattr execute read open execute_no_trans }; allow eGTouchD system_data_file:file { getattr open read write create unlink }; allow eGTouchD proc:file { read open getattr }; allow eGTouchD sysfs:dir { read open }; allow eGTouchD sysfs:file { read open getattr }; allow eGTouchD sysfs:lnk_file { getattr }; allow eGTouchD system_file:file { execute_no_trans }; allow eGTouchD device:chr_file { read write open ioctl }; allow eGTouchD uhid_device:chr_file { read write open ioctl }; allow eGTouchD system_data_file:fifo_file { create setattr getattr read write open }; allow eGTouchD input_device:dir { read open }; allow eGTouchD input_device:chr_file { getattr setattr }; allow eGTouchD eGTouchD:capability { dac_override fsetid };
說明,以上的內容都是根據開機的log提示的一些缺省的權限一個一個加上去的,例如以下log:
avc: denied { write } for pid=2047 comm="sh" name="/" dev="dm-1" ino=3 scontext=u:r:sudaemon:s0 tcontext=u:object_r:system_data_file:s0 tclass=dir permissive=1
則需要添加規則語句為:
allow sudaemon system_data_file:dir { write };
添加完這些,編譯的時候可能會報錯,這可能是由於自己寫的xxx.te與domain.te等有一些
neverallow
的規則沖突了,解決辦法就是domain.te
的規則加入例外:
neverallow { domain -init -ueventd -eGTouchD} device:chr_file { open read write };
neverallow {
domain
-system_server
-system_app
-init
-eGTouchD #wmc
-installd # for relabelfrom and unlink, check for this in explicit neverallow
}system_data_file:file no_w_file_perms;
# do not grant anything greater than r_file_perms and relabelfrom unlink
# to installd
neverallow installd system_data_file:file ~{ r_file_perms relabelfrom unlink };
具體要根據報錯來添加。
這樣就可以正常編譯通過了,重新燒錄系統,開機查看ps -Z,可以看到服務已經正常跑起來了: