在Android源码中添加一个收集开机信息的本地服务

统计开机次数、开机时间、开机电量

BootInfo

a. 在vendor/gree/common/目录下创建文件夹BootInfo
b. 在此文件夹下创建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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值