在Android源码中添加一个收集开机信息的本地服务
2018-03-29 09:32
393 查看
BootInfo
a. 在vendor/gree/common/目录下创建文件夹BootInfob. 在此文件夹下创建bootinfo.cpp文件,并添加程序代码:
#include <stdio.h> #include <string.h> #include <time.h> #define LOG_NDEBUG 0 #define LOG_TAG "bootinfo" #include <cutils/log.h> float get_uptime(void) { float up_time = 0; FILE* file = fopen("/proc/uptime", "r"); if (!file) { ALOGD("Could not open proc/uptime\n "); return up_time; } if (fscanf(file, "%f %*f", &up_time) != 1) { ALOGD("Could not parse /proc/uptime\n"); } fclose(file); return up_time; } int get_battery_level(void) { int level = 0; FILE* file = fopen("/sys/class/power_supply/battery/capacity", "r"); if (!file) { ALOGD("Could not open proc/uptime \n "); return level; } if (fscanf(file, "%d", &level) != 1) { ALOGD("Could not parse /sys/class/power_supply/battery/capacity\n"); } fclose(file); return level; } int get_up_date(char * date, long up_time) { struct tm *tt; long up_date; time_t t = time(NULL); up_date = t - up_time; tt = localtime(&up_date); sprintf(date,"%4d-%02d-%02d %02d:%02d:%02d", tt->tm_year+1900, tt->tm_mon+1, tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec); return 0; } int main(int argc ,char* argv[]) { int times = 1; int tmp_times = 0; float up_time = 0.0; int battery_level = 0; char line[128] = {0, }; char up_date[50] = {0, }; FILE* file; file = fopen("/data/misc/gree_init/uptime.csv", "a+"); fseek(file, 0, 0); while (!feof(file)) { fgets(line, sizeof(line), file); ALOGD("line = %s", line); } if (strlen(line) > 0) { sscanf(line,"%d, %*f, %*d, %*s", &tmp_times); times += tmp_times; } else { fprintf(file, "times, uptime, battery_capacity, data\n"); } up_time = get_uptime(); get_up_date(up_date, (long)up_time); battery_level = get_battery_level(); fprintf(file,"%d, %f, %d, %s,\n", times, up_time, battery_level, up_date); fclose(file); }
c. 在此目录下创建Android.mk文件,编写配置文件代码:
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := bootinfo.cpp LOCAL_STATIC_LIBRARIES := libcutils liblog LOCAL_MODULE := bootinfo include $(BUILD_EXECUTABLE)
d. 对此模块进行编译:
cd Android_1008M/ source build/envsetup.sh lunch mmm /vendor//common/bootinfo
e. 将服务打包到项目中,打开product.mk文件:
在PRODUCT_PACKAGES+= \后面添加 bootinfo
f. 打开目录
device/ /G0235D/init.target.rc,添加服务:
service bootinfo /system/bin/bootinfo class late_start user system group system oneshot disabled seclabel u:r:gree_init:s0 on property:sys.boot_completed=1 start bootinfo
g. 打开目录kernel/security/Makefile 去掉里面的第一行代码:
EXTRA_CFLAGS += -DCONFIG_CLOSE_SELINUX_KERNEL_LOG
这样会在dmesg 中打印log avc
h. 重新编译整个系统烧入到手机中,根据dmesg | grep avc 中的log信息,在服务中添加进程的安全上下文
seclabel u:r:gree_init:s0
i. 根据avc.log信息在file_context中添加的
device\qcom\sepolicy\common\file_contexts
/system/bin/bootinfo u:object_r:gree_init_exec:s0 /data/misc/gree_init(/.*)? u:object_r:gree_init_data_file:s0
在gree_init.te文件中添加:
Z:\AndroidM_1008\device\qcom\sepolicy\common
allow gree_init gree_init_data_file:file { create rw_file_perms }; allow gree_init gree_init_data_file:dir { search write add_name };
在file.te中添加:
device\qcom\sepolicy\common\file.te
# gree init files type gree_init_data_file, file_type, data_file_type;
在\device\qcom\common\rootdir\etc\init.com.rc文件中添加:
mkdir /data/misc/gree_init 0770 system system
相关文章推荐
- android 源码中添加一个最简单的自启动的本地服务
- android保存崩溃时的错误信息log至本地【源码+jar包+使用说...
- Android手机联系人源码(添加联系人页面一个功能)修改
- Android Studio compile project添加一个本地项目依赖包时,提示程序包不存在
- android项目 之 来电管家(8) ----- 添加开机自启动监听服务
- Android 向系统添加一个联系人信息contact
- Android5.1.1源码 - 在Framework中添加自定义系统服务
- 为Android添加一个开机完成后执行的脚本
- 为了使CallExternalMethod活动工作,我们需要在工作流Runtime中添加一个ExternalDataExchangeService,然后再向ExternalDataExchangeService中添加一个本地服务BugFlowService
- Android 使用Vitamio打造自己的万能播放器(10)―― 本地播放 (缩略图、视频信息、视频扫描服务)
- Android源码添加自定义系统服务
- 如何在Android系统源码中添加一个C项目?
- 为Android添加一个开机完成后执行的脚本
- [实践] Android5.1.1源码 - 为每个应用添加一个Content Provider
- [转载][重要]使用Vitamio打造自己的Android万能播放器(10)—— 本地播放 (缩略图、视频信息、视频扫描服务)
- 【Xamarin开发 Android 系列 12】 创建一个Json读取数据应用-添加定位服务
- 在Android源码中添加厂商的产品信息
- [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
- [实践] Android5.1.1源码 - 在Framework中添加自定义系统服务
- Android应用源码找美食看电影本地服务综合项目