Android 在init.rc启动一个c++程序

前序

今天工作时接到有个小需求,在android层启动时,需要让LED灯发光。

环境

android 7.0 (PLATFORM_VERSION := 7.0)

c++程序

功能:对proc节点进行写操作,1为亮,0为灭
部分代码 :

if (fd >= 0) {
		ret = write(fd, pid_string, length);
		if (ret <= 0) {
			ALOGD("noahled,Fail to write 1 to %s %x\n", PROCFS_NOAH_GREEN_LED,
					ret);
		} else {
			property_set("noah.fw.led.green.state", "1");
			ALOGD("noahled,Success to write 1 to %s\n", PROCFS_NOAH_GREEN_LED);
		}
		close(fd);
	} else {
		ALOGD("noahled,PROCFS_NOAH_GREEN_LED is not exist");
	}

然后在源码全编时加入该mk的编译

init.rc启动该程序

service noahled /system/vendor/bin/noahled
    class main
    user system
    group system
	oneshot
...
on init
	start	 noahled
...

启动时出现问题1

发现灯没亮,抓了串口日志

init: Service noahled does not have a SELinux domain defined.

解决办法:
1、在system/sepolicy/file_contexts后面增加

/(system\/vendor|vendor)/bin/noahled         u:object_r:noahled_exec:s0

2、在system/seplicy/目录下,增加noahled.te文件

type noahled_exec , exec_type, file_type;
type noahled ,domain, domain_deprecated;

init_daemon_domain(noahled)

3、修改init.rc

service noahled /system/vendor/bin/noahled
    class main
    user system
    group system
	oneshot
	seclabel u:r:noahled:s0

启动时出现问题2

发现灯还是没亮,抓到路口日志显示:

init: cannot find ‘system/vendor/bin/noahled’ (No such file or directory), disabling ‘noahled’

立刻adb shell 进去查看system/vendor/bin,查看文件noahled,并且确认有这个文件存在,但是,串口日志怎么报不存在这个文件呢,奇怪咯,是不是在init.rc写的这个路径有问题,然后,我就改成各种路径尝试了一下,发现还是报这个错误,最后我从init.rc启动noahled时发现,

on init
	start	 noahled

我在磁盘还没挂载时,就启动,肯定是报这个文件不存在,所以改动下

on fs
	start	 noahled

后来启动调试,以出现几个avc报错,

1、noahc_avc: denied { write } for pid=181 comm=“noahled” name=“property_service” dev=“tmpfs” ino=8813 scontext=u:r:noahled:s0 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0

2、noahc_avc: denied { write } for pid=245 comm=“noahled” name=“green_led” dev=“proc” ino=4026533125 scontext=u:r:noahled:s0 tcontext=u:object_r:proc:s0 tclass=file permissive=0

3、noahc_avc: denied { connectto } for pid=183 comm=“noahled” path="/dev/socket/property_service" scontext=u:r:noahled:s0 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=0

修改方法:将之前新加的文件system/sepolicy/noahled.te进行修改

type noahled_exec , exec_type, file_type;
type noahled ,domain, domain_deprecated;
init_daemon_domain(noahled)

allow noahled proc:file {write};
allow noahled property_socket:sock_file {write};
allow noahled init:unix_stream_socket {connectto};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值