定义二进制程序
robot_init.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <android/log.h>
#define LOG_TAG "robot_init"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
const char *FLAG = "/data/media/0/robot_xiaoyou/.robot_xiaoyou_unpacked";
const char *SRC = "/system/media/robot_xiaoyou.zip";
const char *DEST = "/data/media/0/";
void* do_unzip(void *arg) {
struct stat st;
if (stat(FLAG, &st) == 0) {
LOGI("Already unpacked, skipping unzip.");
return NULL;
}
if (stat(SRC, &st) != 0) {
LOGE("Source zip not found: %s", SRC);
return NULL;
}
LOGI("Start unzip %s -> %s", SRC, DEST);
char cmd[512];
snprintf(cmd, sizeof(cmd), "/system/bin/unzip -q %s -d %s", SRC, DEST);
int ret = system(cmd);
if (ret != 0) {
LOGE("Unzip failed with code %d", ret);
return NULL;
}
LOGI("Unzip completed successfully.");
snprintf(cmd, sizeof(cmd), "chown -R media_rw:media_rw %s", DEST);
ret = system(cmd);
if (ret != 0) {
LOGE("Chown failed with code %d", ret);
return NULL;
}
LOGI("Permissions set successfully.");
FILE *f = fopen(FLAG, "w");
if (f) {
fclose(f);
LOGI("Flag file created: %s", FLAG);
} else {
LOGE("Failed to create flag file: %s", FLAG);
}
return NULL;
}
int main() {
LOGI("******robot_init started******");
do_unzip(NULL);
LOGI("******robot_init end******");
return 0;
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := robot_init
LOCAL_MODULE_CLASS := EXECUTABLE
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/bin
LOCAL_SRC_FILES := robot_init.c
LOCAL_VENDOR_MODULE := true
LOCAL_CFLAGS := -Wall -O2
LOCAL_LDLIBS := -llog -lc -lz
include $(BUILD_EXECUTABLE)
init.rc 脚本修改
service robot_init /system/bin/robot_init
class main
user root
group root
oneshot
disabled
on post-fs-data
start robot_init
Selinux 规则添加
A133_android_10/android/device/softwinner/common/sepolicy/vendor/robot_init.te
type robot_init, domain;
type robot_init_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(robot_init)
file_contexts
/system/bin/robot_init u:object_r:robot_init_exec:s0