1. 안드로이드 시스템 환경
퀄컴 865, 안드로이드10
2. cpp 코드를 컴파일하여 프로세스를 시작하는 구현 방법
1. 벤더 아래에 새 TestInit 모듈을 생성합니다. 디렉터리 구조는 다음과 같습니다.
2. 포함된 standlog.h 파일
#ifndef VCLOG_H
#define VCLOG_H
#include <android/log.h>
#include <stdio.h>
#define LOG_TAG "testinit"
#define filename(x) strrchr(x, '/') ? strrchr(x, '/') + 1 : x
#define LOGI(...) \
do \
{ \
printf(__VA_ARGS__); \
printf("\n"); \
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__); \
} while (0)
#define LOGE(...) \
do \
{ \
printf(__VA_ARGS__); \
printf("\n"); \
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__); \
} while (0)
#define LOGD(...) \
do \
{ \
printf(__VA_ARGS__); \
printf("\n"); \
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__); \
} while (0)
#define LOGW(...) \
do \
{ \
printf(__VA_ARGS__); \
printf("\n"); \
__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__); \
} while (0)
#define LOGV(...) \
do \
{ \
printf(__VA_ARGS__); \
printf("\n"); \
__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__); \
} while (0)
#endif
3. main.cpp 코드는 다음과 같습니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "standlog.h"
#define filename(x) strrchr(x, '/') ? strrchr(x, '/') + 1 : x
using namespace std;
int main()
{
LOGI("%s: %s(%d):%s", filename(__FILE__), __FUNCTION__, __LINE__, "start testinit service");
printf(">>>>>>>>>>>>>>>testinit main");
while (true)
{
sleep(60 * 60);
}
return 0;
}
4. Android.mk 코드는 다음과 같습니다.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := testinit # 模块的名称
LOCAL_CFLAGS += -Wno-error=date-time
LOCAL_SRC_FILES = src/main.cpp \
# $(call all-cpp-files-under, src) \ #使用宏定义引入src文件夹下所有的cpp文件
LOCAL_MODULE_TAGS := optional
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_SHARED_LIBRARIES := \
liblog \
libcutils
LOCAL_CPPFLAGS += -fexceptions
LOCAL_INIT_RC := testinit.rc #设置init rc
include $(BUILD_EXECUTABLE)
5.testinit.rc
service testinit /system/bin/testinit
class main #main类,属于main的服务会开机被运行,且死掉会重启 如果设置了class main 那么需要把进程设置一个死循环,然后就会无限重启,详细细看main.cpp
user root
group root
disabled
on property:sys.boot_completed=1
start testinit
6. 프로젝트의 컴파일된 파일에 모듈을 추가합니다.
공급 업체/qcom/proprietary/prebuilt_HY11/target/product/qssi/prebuilt.mk
7. selinux 구성
(1) device/qcom/sepolicy/qva/private/file_contexts에 다음 코드를 추가합니다.
/system/bin/testinit u:object_r:testinit_exec:s0
(2) device/qcom/sepolicy/qva/private/ 디렉터리에 testinit.te를 추가합니다.
typeattribute testinit coredomain;
init_daemon_domain(testinit)
(3) device/qcom/sepolicy/qva/public/ 디렉토리에 testinit.te를 추가합니다.
type testinit, domain;
type testinit_exec, exec_type, file_type, system_file_type;
8. 프로세스가 존재하는지 확인하기 위해 시스템을 컴파일하고 새로 고칩니다.
프로세스가 존재하지 않는 경우 testinit 바이너리 파일이 /system/bin/ 디렉토리에 존재하는지 확인할 수 있습니다.
ls -l /system/bin/testinit
3. 통합 바이너리 파일 구현
1. 공급업체 아래에 새 TestNative 모듈을 생성합니다. 디렉터리 구조는 다음과 같습니다.
2. Android.mk 코드는 다음과 같습니다.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := testnative
LOCAL_MODULE := testnative
LOCAL_MODULE_CLASS := EXECUTABLES
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_PATH := $(PRODUCT_OUT)/system/bin
LOCAL_INIT_RC := testnative.rc
include $(BUILD_PREBUILT)
3、testnative.rc
service testnative /system/bin/testnative
user root
group root
disabled
on property:sys.boot_completed=1
start testnative
4. 프로젝트의 컴파일된 파일에 모듈을 추가합니다.
공급 업체/qcom/proprietary/prebuilt_HY11/target/product/qssi/prebuilt.mk
5. selinux 구성
(1) device/qcom/sepolicy/qva/private/file_contexts에 다음 코드를 추가합니다.
/system/bin/testnative u:object_r:testnative_exec:s0
(2) device/qcom/sepolicy/qva/private/ 디렉터리에 testnative.te를 추가합니다.
typeattribute testnative coredomain;
init_daemon_domain(testnative)
(3) device/qcom/sepolicy/qva/public/ 디렉토리에 testnative.te를 추가합니다.
type testnative, domain;
type testnative_exec, exec_type, file_type, system_file_type;
6. 프로세스가 존재하는지 확인하기 위해 시스템을 컴파일하고 새로 고칩니다.