mirror of
https://gitee.com/openharmony/startup_init
synced 2024-12-03 00:16:47 +00:00
commit
e517eb1de1
@ -36,7 +36,7 @@ int DoReboot(const char *cmdContent)
|
||||
}
|
||||
char value[MAX_REBOOT_VAUE_SIZE];
|
||||
if (cmdContent == NULL || strlen(cmdContent) == 0) {
|
||||
if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, MAX_REBOOT_NAME_SIZE - 1, "%s", "reboot") < 0) {
|
||||
if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, strlen("reboot") + 1, "%s", "reboot") < 0) {
|
||||
INIT_LOGE("DoReboot api error, MAX_REBOOT_NAME_SIZE is not enough");
|
||||
return -1;
|
||||
}
|
||||
|
@ -34,9 +34,9 @@
|
||||
#define MAX_SOCKET_ENV_PREFIX_LEN 64
|
||||
#define MAX_SOCKET_DIR_LEN 128
|
||||
|
||||
static int GetControlFromEnv(char *path)
|
||||
static int GetControlFromEnv(char *path, int length)
|
||||
{
|
||||
if (path == NULL) {
|
||||
if (path == NULL || length <= 0) {
|
||||
return -1;
|
||||
}
|
||||
INIT_LOGI("GetControlFromEnv path is %s ", path);
|
||||
@ -68,7 +68,7 @@ int GetControlSocket(const char *name)
|
||||
return -1;
|
||||
}
|
||||
INIT_LOGI("GetControlSocket path is %s ", path);
|
||||
int fd = GetControlFromEnv(path);
|
||||
int fd = GetControlFromEnv(path, MAX_SOCKET_ENV_PREFIX_LEN);
|
||||
if (fd < 0) {
|
||||
INIT_LOGE("GetControlFromEnv fail ");
|
||||
return -1;
|
||||
|
@ -37,6 +37,7 @@ static void OnReceiveAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t*
|
||||
{
|
||||
// 这里需要按实际回复大小申请内存,不需要大内存
|
||||
buf->base = (char *)malloc(sizeof(ResponseMsg));
|
||||
PARAM_CHECK(buf->base != NULL, return, "OnReceiveAlloc malloc failed");
|
||||
buf->len = sizeof(ResponseMsg);
|
||||
PARAM_LOGD("OnReceiveAlloc handle %p %zu", handle, suggestedSize);
|
||||
}
|
||||
@ -92,6 +93,7 @@ static int StartRequest(int cmd, RequestNode *request)
|
||||
request->result = -1;
|
||||
request->msg.type = cmd;
|
||||
request->loop = uv_loop_new();
|
||||
PARAM_CHECK(request->loop != NULL, return -1, "StartRequest uv_loop_new failed");
|
||||
uv_pipe_init(request->loop, &request->handle, 1);
|
||||
uv_pipe_connect(&request->connect, &request->handle, PIPE_NAME, OnConnection);
|
||||
uv_run(request->loop, UV_RUN_DEFAULT);
|
||||
|
@ -22,6 +22,10 @@
|
||||
|
||||
static void ProcessParam(ParamHandle handle, void* cookie)
|
||||
{
|
||||
if (cookie == NULL) {
|
||||
printf("ProcessParam cookie is NULL\n");
|
||||
return;
|
||||
}
|
||||
SystemGetParameterName(handle, (char*)cookie, BUFFER_SIZE);
|
||||
u_int32_t size = BUFFER_SIZE;
|
||||
SystemGetParameterValue(handle, ((char*)cookie) + BUFFER_SIZE, &size);
|
||||
|
@ -103,7 +103,7 @@ typedef int (*CMD_EXECUTE) (TriggerNode *trigger, const char *cmdName, const cha
|
||||
|
||||
TriggerNode *GetTriggerByName(TriggerWorkSpace *workSpace, const char *triggerName, u_int32_t *triggerIndex);
|
||||
int ExecuteTrigger(TriggerWorkSpace *workSpace, TriggerNode *trigger, CMD_EXECUTE cmdExecuter);
|
||||
int CheckTrigger(TriggerWorkSpace *workSpace,
|
||||
int CheckTrigger(const TriggerWorkSpace *workSpace,
|
||||
int type, void *content, u_int32_t contentSize, PARAM_CHECK_DONE triggerExecuter);
|
||||
int CheckParamTrigger(TriggerWorkSpace *workSpace,
|
||||
const char *content, u_int32_t contentSize, PARAM_CHECK_DONE triggerExecuter);
|
||||
@ -123,4 +123,4 @@ TriggerWorkSpace *GetTriggerWorkSpace();
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#endif // STARTUP_TRIGER_MANAGER_H
|
||||
#endif // STARTUP_TRIGER_MANAGER_H
|
||||
|
@ -66,10 +66,13 @@ int InitParamWorkSpace(ParamWorkSpace *workSpace, int onlyRead, const char *cont
|
||||
workSpace->paramSpace.compareTrieNode = CompareTrieDataNode;
|
||||
workSpace->paramSpace.allocTrieNode = AllocateTrieDataNode;
|
||||
int ret = InitWorkSpace(PARAM_STORAGE_PATH, &workSpace->paramSpace, onlyRead);
|
||||
PARAM_CHECK(ret == 0, return ret, "InitWorkSpace failed.");
|
||||
|
||||
workSpace->paramLabelSpace.compareTrieNode = CompareTrieNode; // 必须先设置
|
||||
workSpace->paramLabelSpace.allocTrieNode = AllocateTrieNode;
|
||||
ret |= InitWorkSpace(PARAM_INFO_PATH, &workSpace->paramLabelSpace, onlyRead);
|
||||
ret = InitWorkSpace(PARAM_INFO_PATH, &workSpace->paramLabelSpace, onlyRead);
|
||||
PARAM_CHECK(ret == 0, return ret, "InitWorkSpace failed.");
|
||||
|
||||
atomic_store_explicit(&workSpace->flags, WORKSPACE_FLAGS_INIT, memory_order_release);
|
||||
return ret;
|
||||
}
|
||||
@ -310,7 +313,7 @@ int CheckControlParamPerms(ParamWorkSpace *workSpace,
|
||||
// their value is the name of the service to apply that action to. Permissions for these
|
||||
// actions are based on the service, so we must create a fake name of ctl.<service> to
|
||||
// check permissions.
|
||||
int n = snprintf_s(legacyName, size, size, "ctl.%s", value);
|
||||
int n = snprintf_s(legacyName, size, strlen("ctl.") + strlen(value) + 1, "ctl.%s", value);
|
||||
PARAM_CHECK(n > 0, free(legacyName); return PARAM_CODE_INVALID_PARAM, "Failed to snprintf value");
|
||||
legacyName[n] = '\0';
|
||||
|
||||
@ -494,10 +497,12 @@ int BuildParamContent(char *content, u_int32_t contentSize, const char *name, co
|
||||
|
||||
int offset = 0;
|
||||
int ret = memcpy_s(content + offset, contentSize - offset, name, nameLen);
|
||||
PARAM_CHECK(ret == 0, return -1, "Failed to copy porperty");
|
||||
offset += nameLen;
|
||||
ret |= memcpy_s(content + offset, contentSize - offset, "=", 1);
|
||||
ret = memcpy_s(content + offset, contentSize - offset, "=", 1);
|
||||
PARAM_CHECK(ret == 0, return -1, "Failed to copy porperty");
|
||||
offset += 1;
|
||||
ret |= memcpy_s(content + offset, contentSize - offset, value, valueLen);
|
||||
ret = memcpy_s(content + offset, contentSize - offset, value, valueLen);
|
||||
offset += valueLen;
|
||||
content[offset] = '\0';
|
||||
PARAM_CHECK(ret == 0, return -1, "Failed to copy porperty");
|
||||
|
@ -471,7 +471,8 @@ u_int32_t AddData(WorkSpace *workSpace, const char *key, u_int32_t keyLen, const
|
||||
atomic_init(&node->serial, ATOMIC_VAR_INIT(0));
|
||||
atomic_init(&node->dataLength, ATOMIC_VAR_INIT(dataLength));
|
||||
int ret = memcpy_s(node->data, keyLen, key, keyLen);
|
||||
ret |= memcpy_s(node->data + keyLen + 1, valueLen, value, valueLen);
|
||||
PARAM_CHECK(ret == 0, return 0, "Failed to copy key");
|
||||
ret = memcpy_s(node->data + keyLen + 1, valueLen, value, valueLen);
|
||||
PARAM_CHECK(ret == 0, return 0, "Failed to copy key");
|
||||
node->data[keyLen] = '=';
|
||||
node->data[keyLen + 1 + valueLen] = '\0';
|
||||
|
@ -53,10 +53,11 @@ static int ProcessParamTraversal(WorkSpace *workSpace, TrieNode *node, void *coo
|
||||
}
|
||||
PersistContext *persistContext = (PersistContext *)cookie;
|
||||
int ret = GetDataName(entry, persistContext->buffer, MAX_BUFF);
|
||||
PARAM_CHECK(ret == 0, return ret, "GetDataName failed");
|
||||
if (strncmp(persistContext->buffer, "persist.", strlen("persist.")) != 0) {
|
||||
return 0;
|
||||
}
|
||||
ret |= GetDataValue(entry, persistContext->buffer + MAX_BUFF, MAX_BUFF);
|
||||
ret = GetDataValue(entry, persistContext->buffer + MAX_BUFF, MAX_BUFF);
|
||||
if (ret == 0) { // 只支持新建
|
||||
//PARAM_LOGI("Insert new persist param from normal param %s %s",
|
||||
// persistContext->buffer, persistContext->buffer + MAX_BUFF);
|
||||
@ -78,7 +79,8 @@ static int ProcessPersistPropertTraversal(WorkSpace *workSpace, TrieNode *node,
|
||||
}
|
||||
PersistContext *persistContext = (PersistContext *)cookie;
|
||||
int ret = GetDataName(entry, persistContext->buffer, MAX_BUFF);
|
||||
ret |= GetDataValue(entry, persistContext->buffer + MAX_BUFF, MAX_BUFF);
|
||||
PARAM_CHECK(ret == 0, return ret, "GetDataName failed");
|
||||
ret = GetDataValue(entry, persistContext->buffer + MAX_BUFF, MAX_BUFF);
|
||||
if (ret == 0) {
|
||||
//PARAM_LOGI("update normal param %s %s from persist param %u",
|
||||
// persistContext->buffer, persistContext->buffer + MAX_BUFF, current->dataIndex);
|
||||
|
@ -45,11 +45,12 @@ int LoadDefaultParams(const char *fileName)
|
||||
if ((flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) {
|
||||
return PARAM_CODE_NOT_INIT;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
PARAM_CHECK(fp != NULL, return -1, "Open file %s fail", fileName);
|
||||
char buff[BUFFER_SIZE];
|
||||
SubStringInfo *info = malloc(sizeof(SubStringInfo) * (SUBSTR_INFO_LABEL + 1));
|
||||
PARAM_CHECK(info != NULL, return -1, "malloc failed");
|
||||
|
||||
while(fgets(buff, BUFFER_SIZE, fp) != NULL) {
|
||||
int subStrNumber = GetSubStringInfo(buff, strlen(buff), '=', info, SUBSTR_INFO_LABEL + 1);
|
||||
if (subStrNumber <= SUBSTR_INFO_LABEL) {
|
||||
@ -85,6 +86,7 @@ int LoadParamInfos(const char *fileName)
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
PARAM_CHECK(fp != NULL, return -1, "Open file %s fail", fileName);
|
||||
SubStringInfo *info = malloc(sizeof(SubStringInfo) * SUBSTR_INFO_MAX);
|
||||
PARAM_CHECK(info != NULL, return -1, "Load parameter malloc failed.");
|
||||
char buff[BUFFER_SIZE];
|
||||
int infoCount = 0;
|
||||
while(fgets(buff, BUFFER_SIZE, fp) != NULL) {
|
||||
@ -140,7 +142,7 @@ static void OnWriteResponse(uv_write_t *req, int status)
|
||||
free(node);
|
||||
}
|
||||
|
||||
static void SendResponse(uv_stream_t *handle, RequestType type, int result, void *content, int size)
|
||||
static void SendResponse(uv_stream_t *handle, RequestType type, int result, const void *content, int size)
|
||||
{
|
||||
int ret = 0;
|
||||
// 申请整块内存,用于回复数据和写请求
|
||||
@ -158,11 +160,13 @@ static void SendResponse(uv_stream_t *handle, RequestType type, int result, void
|
||||
PARAM_CHECK(ret >= 0, return, "Failed to uv_write2 ret %s", uv_strerror(ret));
|
||||
}
|
||||
|
||||
static void OnReceiveRequest(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
|
||||
static void OnReceiveRequest(uv_stream_t *handle, ssize_t nread, uv_buf_t *buf)
|
||||
{
|
||||
if (nread <= 0 || buf == NULL || buf->base == NULL) {
|
||||
uv_close((uv_handle_t*)handle, OnClose);
|
||||
free(buf->base);
|
||||
if (buf != NULL && buf->base != NULL) {
|
||||
free(buf->base);
|
||||
}
|
||||
return;
|
||||
}
|
||||
int freeHandle = 1;
|
||||
@ -179,6 +183,7 @@ static void OnReceiveRequest(uv_stream_t *handle, ssize_t nread, const uv_buf_t
|
||||
break;
|
||||
}
|
||||
free(buf->base);
|
||||
buf->base = NULL;
|
||||
uv_close((uv_handle_t*)handle, OnClose);
|
||||
}
|
||||
|
||||
@ -227,7 +232,6 @@ int StartParamService()
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to chmod %s, err %d. ", PIPE_NAME, errno);
|
||||
ret = uv_listen((uv_stream_t*)&pipeServer, SOMAXCONN, OnConnection);
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to uv_listen %d %s", ret, uv_err_name(ret));
|
||||
|
||||
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
||||
PARAM_LOGI("Start service exit.");
|
||||
return 0;
|
||||
|
@ -80,7 +80,7 @@ static int CalculatorPopChar(LogicCalculator *calculator, char *data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CalculatorPush(LogicCalculator *calculator, void *data)
|
||||
static int CalculatorPush(LogicCalculator *calculator, const void *data)
|
||||
{
|
||||
PARAM_CHECK(calculator != NULL, return -1, "Invalid param");
|
||||
PARAM_CHECK(calculator->endIndex < calculator->dataNumber, return -1, "More data for calculator support");
|
||||
@ -93,7 +93,7 @@ static int CalculatorPush(LogicCalculator *calculator, void *data)
|
||||
|
||||
static int CalculatorPop(LogicCalculator *calculator, void *data)
|
||||
{
|
||||
PARAM_CHECK(calculator != NULL || data == NULL, return -1, "Invalid param");
|
||||
PARAM_CHECK(calculator != NULL && data != NULL, return -1, "Invalid param");
|
||||
PARAM_CHECK(calculator->endIndex < calculator->dataNumber, return -1, "More data for calculator support");
|
||||
if (calculator->endIndex == 0) {
|
||||
return -1;
|
||||
@ -212,8 +212,8 @@ int ComputeCondition(LogicCalculator *calculator, const char *condition)
|
||||
if (condition[currIndex] == '|' || condition[currIndex] == '&') {
|
||||
noneOper = 0;
|
||||
int ret = CalculatorPop(calculator, (void*)&data2);
|
||||
ret |= CalculatorPop(calculator, (void*)&data1);
|
||||
PARAM_CHECK(ret == 0, return -1, "Failed to pop data");
|
||||
int ret1 = CalculatorPop(calculator, (void*)&data1);
|
||||
PARAM_CHECK((ret == 0 && ret1 == 0), return -1, "Failed to pop data");
|
||||
|
||||
ret = ComputeSubCondition(calculator, &data1, condition);
|
||||
data1.flags = 0;
|
||||
@ -254,7 +254,7 @@ int ComputeCondition(LogicCalculator *calculator, const char *condition)
|
||||
|
||||
int ConvertInfixToPrefix(const char *condition, char *prefix, u_int32_t prefixLen)
|
||||
{
|
||||
char e;
|
||||
char e = 0;
|
||||
int ret = 0;
|
||||
u_int32_t curr = 0;
|
||||
u_int32_t prefixIndex = 0;
|
||||
@ -309,4 +309,4 @@ char *GetMatchedSubCondition(const char *condition, const char *input, int lengt
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
@ -238,7 +238,7 @@ int ParseTrigger(TriggerWorkSpace *workSpace, cJSON *triggerItem)
|
||||
|
||||
for (int i = 0; i < cmdLinesCnt; ++i) {
|
||||
char *cmdLineStr = cJSON_GetStringValue(cJSON_GetArrayItem(cmdItems, i));
|
||||
PARAM_CHECK(cmdLinesCnt > 0, continue, "Command is null");
|
||||
PARAM_CHECK(cmdLineStr != NULL, continue, "Command is null");
|
||||
|
||||
size_t cmdLineLen = strlen(cmdLineStr);
|
||||
const char *matchCmd = GetMatchCmd(cmdLineStr);
|
||||
@ -351,13 +351,13 @@ static int CheckTrigger_(TriggerWorkSpace *workSpace,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CheckTrigger(TriggerWorkSpace *workSpace,
|
||||
int CheckTrigger(const TriggerWorkSpace *workSpace,
|
||||
int type, void *content, u_int32_t contentSize, PARAM_CHECK_DONE triggerExecuter)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && content != NULL && triggerExecuter != NULL,
|
||||
return -1, "Failed arg for trigger");
|
||||
|
||||
LogicCalculator calculator;
|
||||
LogicCalculator calculator = {};
|
||||
calculator.triggerExecuter = triggerExecuter;
|
||||
return CheckTrigger_(workSpace, &calculator, type, (char *)content, contentSize);
|
||||
}
|
||||
@ -367,7 +367,7 @@ int CheckParamTrigger(TriggerWorkSpace *workSpace,
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && content != NULL && triggerExecuter != NULL,
|
||||
return -1, "Failed arg for param trigger");
|
||||
LogicCalculator calculator;
|
||||
LogicCalculator calculator = {};
|
||||
CalculatorInit(&calculator, 100, sizeof(LogicData), 1);
|
||||
|
||||
// 先解析content
|
||||
@ -387,7 +387,7 @@ int CheckAndExecuteTrigger(TriggerWorkSpace *workSpace, const char *content, PAR
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && content != NULL && triggerExecuter != NULL,
|
||||
return -1, "Failed arg for param trigger");
|
||||
LogicCalculator calculator;
|
||||
LogicCalculator calculator = {};
|
||||
CalculatorInit(&calculator, 100, sizeof(LogicData), 1);
|
||||
|
||||
int ret = memcpy_s(calculator.triggerContent, sizeof(calculator.triggerContent), content, strlen(content));
|
||||
|
@ -123,6 +123,9 @@ static const char *GetCmdInfo(const char *content, u_int32_t contentSize, char *
|
||||
|
||||
static void SendTriggerEvent(TriggerDataEvent *event)
|
||||
{
|
||||
if (event == NULL) {
|
||||
return;
|
||||
}
|
||||
int ctrlSize = strlen(SYS_POWER_CTRL);
|
||||
if (strncmp(event->content, SYS_POWER_CTRL, ctrlSize) == 0) {
|
||||
char *cmdParam = NULL;
|
||||
@ -169,7 +172,7 @@ void PostTrigger(EventType type, const char *content, u_int32_t contentLen)
|
||||
event->type = type;
|
||||
event->request.data = (char*)event + sizeof(uv_work_t);
|
||||
event->contentSize = contentLen;
|
||||
memcpy_s(event->content, contentLen, content, contentLen);
|
||||
PARAM_CHECK(memcpy_s(event->content, contentLen, content, contentLen) == 0, return, "Failed to copy content");
|
||||
event->content[contentLen] = '\0';
|
||||
SendTriggerEvent(event);
|
||||
PARAM_LOGD("PostTrigger %d success", type);
|
||||
@ -209,4 +212,4 @@ void DoTriggerExec(const char *content)
|
||||
TriggerWorkSpace *GetTriggerWorkSpace()
|
||||
{
|
||||
return &g_triggerWorkSpace;
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@
|
||||
#include "securec.h"
|
||||
|
||||
#ifndef OHOS_LITE
|
||||
static int ExtractCfgFile(char **cfgFile, char *content)
|
||||
static int ExtractCfgFile(char **cfgFile, const char *content)
|
||||
{
|
||||
if ((!cfgFile) || (!content)) {
|
||||
return -1;
|
||||
|
@ -118,8 +118,8 @@ static void UVSignalHandler(uv_signal_t* handle, int signum)
|
||||
void SignalInitModule()
|
||||
{
|
||||
int ret = uv_signal_init(uv_default_loop(), &g_sigchldHandler);
|
||||
ret |= uv_signal_init(uv_default_loop(), &g_sigtermHandler);
|
||||
if (ret != 0) {
|
||||
int ret1 = uv_signal_init(uv_default_loop(), &g_sigtermHandler);
|
||||
if (ret != 0 && ret1 != 0) {
|
||||
INIT_LOGW("initialize signal handler failed");
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user