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

    • 前序
    • 环境
    • c++程序
    • init.rc启动该程序
    • 启动时出现问题1
    • 启动时出现问题2

前序

今天工作时接到有个小需求,在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 systemoneshot...on initstart 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 systemoneshotseclabel 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 initstart noahled

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

on fsstart 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};

更多相关文章

  1. 20172323 2017-2018-2《程序设计与数据结构》第十一周学习总结
  2. Android多文件断点续传(二)——实现数据库储存下载信息
  3. Android中,在C++层使用TinyXML解析XML文件
  4. Android清单文件详解(四) ---- backupAgent的用法
  5. 程序的组件模式

随机推荐

  1. listview常用属性记录
  2. Android项目Android Studio目录结构
  3. 【Android 系统开发】 编译 Android文件
  4. registerActivityLifecycleCallbacks 的
  5. Android(java):database disk image is m
  6. Android AudioRecord介绍与Android 6.0后
  7. Android(安卓)实时视频采集
  8. android之Uri的使用简介\Uri与path相互
  9. android异步更新UI
  10. Android——消息机制