在Android源码中添加一个收集开机信息的本地服务
程序员文章站
2022-04-28 13:09:45
bootinfo
a. 在vendor/gree/common/目录下创建文件夹bootinfo
b. 在此文件夹下创建bootinfo.cpp文件,并添加程序代码:
#include
#i...
bootinfo
a. 在vendor/gree/common/目录下创建文件夹bootinfo
b. 在此文件夹下创建bootinfo.cpp文件,并添加程序代码:
#include #include #include #define log_ndebug 0 #define log_tag "bootinfo" #include 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 a
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