mirror of
https://github.com/openharmony/startup_init_lite.git
synced 2026-07-21 04:25:27 -04:00
@@ -51,7 +51,7 @@ void StopParamService(void);
|
||||
* 加载默认的参数值
|
||||
*
|
||||
*/
|
||||
int LoadDefaultParams(const char *fileName, int mode);
|
||||
int LoadDefaultParams(const char *fileName, unsigned int mode);
|
||||
|
||||
/**
|
||||
* Init 接口
|
||||
|
||||
@@ -31,7 +31,8 @@ static void GetUserIdByName(FILE *fp, uid_t *uid, const char *name, uint32_t nam
|
||||
(void)fseek(fp, 0, SEEK_SET);
|
||||
struct passwd *data = NULL;
|
||||
while ((data = fgetpwent(fp)) != NULL) {
|
||||
if (strlen(data->pw_name) == nameLen && strncmp(data->pw_name, name, nameLen) == 0) {
|
||||
if ((data->pw_name != NULL) && (strlen(data->pw_name) == nameLen) &&
|
||||
(strncmp(data->pw_name, name, nameLen) == 0)) {
|
||||
*uid = data->pw_uid;
|
||||
return;
|
||||
}
|
||||
@@ -44,7 +45,8 @@ static void GetGroupIdByName(FILE *fp, gid_t *gid, const char *name, uint32_t na
|
||||
(void)fseek(fp, 0, SEEK_SET);
|
||||
struct group *data = NULL;
|
||||
while ((data = fgetgrent(fp)) != NULL) {
|
||||
if (strlen(data->gr_name) == nameLen && strncmp(data->gr_name, name, nameLen) == 0) {
|
||||
if ((data->gr_name != NULL) && (strlen(data->gr_name) == nameLen) &&
|
||||
(strncmp(data->gr_name, name, nameLen) == 0)) {
|
||||
*gid = data->gr_gid;
|
||||
break;
|
||||
}
|
||||
@@ -54,6 +56,9 @@ static void GetGroupIdByName(FILE *fp, gid_t *gid, const char *name, uint32_t na
|
||||
// user:group:r|w
|
||||
static int GetParamDacData(FILE *fpForGroup, FILE *fpForUser, ParamDacData *dacData, const char *value)
|
||||
{
|
||||
if (dacData == NULL) {
|
||||
return -1;
|
||||
}
|
||||
char *groupName = strstr(value, ":");
|
||||
if (groupName == NULL) {
|
||||
return -1;
|
||||
@@ -98,7 +103,7 @@ static int EncodeSecurityLabel(const ParamSecurityLabel *srcLabel, char *buffer,
|
||||
return memcpy_s(buffer, *bufferSize, srcLabel, sizeof(ParamSecurityLabel));
|
||||
}
|
||||
|
||||
static int DecodeSecurityLabel(ParamSecurityLabel **srcLabel, char *buffer, uint32_t bufferSize)
|
||||
static int DecodeSecurityLabel(ParamSecurityLabel **srcLabel, const char *buffer, uint32_t bufferSize)
|
||||
{
|
||||
PARAM_CHECK(bufferSize >= sizeof(ParamSecurityLabel), return -1, "Invalid buffersize %u", bufferSize);
|
||||
PARAM_CHECK(srcLabel != NULL && buffer != NULL, return -1, "Invalid param");
|
||||
@@ -108,17 +113,16 @@ static int DecodeSecurityLabel(ParamSecurityLabel **srcLabel, char *buffer, uint
|
||||
|
||||
static int LoadParamLabels(const char *fileName, SecurityLabelFunc label, void *context)
|
||||
{
|
||||
uint32_t infoCount = 0;
|
||||
ParamAuditData auditData = {0};
|
||||
FILE *fpForGroup = fopen(GROUP_FILE_PATH, "r");
|
||||
FILE *fpForUser = fopen(USER_FILE_PATH, "r");
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
SubStringInfo *info = malloc(sizeof(SubStringInfo) * (SUBSTR_INFO_DAC + 1));
|
||||
PARAM_CHECK(fpForGroup != NULL && fpForUser != NULL && fp != NULL && info != NULL,
|
||||
goto exit, "Can not open file for load param labels");
|
||||
|
||||
uint32_t infoCount = 0;
|
||||
char buff[PARAM_BUFFER_SIZE];
|
||||
ParamAuditData auditData = {};
|
||||
while (fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
|
||||
char *buff = (char *)calloc(1, PARAM_BUFFER_SIZE);
|
||||
SubStringInfo *info = calloc(1, sizeof(SubStringInfo) * (SUBSTR_INFO_DAC + 1));
|
||||
while (fp != NULL && fpForGroup != NULL && fpForUser != NULL &&
|
||||
info != NULL && buff != NULL && fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
|
||||
buff[PARAM_BUFFER_SIZE - 1] = '\0';
|
||||
int subStrNumber = GetSubStringInfo(buff, strlen(buff), ' ', info, SUBSTR_INFO_DAC + 1);
|
||||
if (subStrNumber <= SUBSTR_INFO_DAC) {
|
||||
continue;
|
||||
@@ -134,19 +138,21 @@ static int LoadParamLabels(const char *fileName, SecurityLabelFunc label, void *
|
||||
infoCount++;
|
||||
}
|
||||
PARAM_LOGI("Load parameter label total %u success %s", infoCount, fileName);
|
||||
exit:
|
||||
if (fp) {
|
||||
if (fp != NULL) {
|
||||
(void)fclose(fp);
|
||||
}
|
||||
if (info) {
|
||||
if (info != NULL) {
|
||||
free(info);
|
||||
}
|
||||
if (fpForGroup) {
|
||||
if (fpForGroup != NULL) {
|
||||
(void)fclose(fpForGroup);
|
||||
}
|
||||
if (fpForUser) {
|
||||
if (fpForUser != NULL) {
|
||||
(void)fclose(fpForUser);
|
||||
}
|
||||
if (buff != NULL) {
|
||||
free(buff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -174,7 +180,7 @@ static int CheckFilePermission(const ParamSecurityLabel *localLabel, const char
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, int mode)
|
||||
static int CheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, uint32_t mode)
|
||||
{
|
||||
int ret = DAC_RESULT_FORBIDED;
|
||||
PARAM_CHECK(srcLabel != NULL && auditData != NULL && auditData->name != NULL, return ret, "Invalid param");
|
||||
@@ -184,7 +190,7 @@ static int CheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamA
|
||||
* DAC group 实现的label的定义
|
||||
* user:group:read|write|watch
|
||||
*/
|
||||
uint32_t localMode = 0;
|
||||
uint32_t localMode;
|
||||
if (srcLabel->cred.uid == auditData->dacData.uid) {
|
||||
localMode = mode & (DAC_READ | DAC_WRITE | DAC_WATCH);
|
||||
} else if (srcLabel->cred.gid == auditData->dacData.gid) {
|
||||
@@ -196,8 +202,8 @@ static int CheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamA
|
||||
ret = DAC_RESULT_PERMISSION;
|
||||
}
|
||||
PARAM_LOGD("Src label %d %d ", srcLabel->cred.gid, srcLabel->cred.uid);
|
||||
PARAM_LOGD("auditData label %d %d mode %o lable %s",
|
||||
auditData->dacData.gid, auditData->dacData.uid, auditData->dacData.mode, auditData->label);
|
||||
PARAM_LOGD("auditData label %d %d mode %o",
|
||||
auditData->dacData.gid, auditData->dacData.uid, auditData->dacData.mode);
|
||||
PARAM_LOGD("%s check %o localMode %o ret %d", auditData->name, mode, localMode, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ static LibuvBaseTask *CreateLibuvTask(uint32_t size, uint32_t flags, uint16_t us
|
||||
{
|
||||
PARAM_CHECK(size <= RECV_BUFFER_MAX, return NULL, "Invaid size %u", size);
|
||||
PARAM_CHECK(userDataSize <= RECV_BUFFER_MAX, return NULL, "Invaid user size %u", userDataSize);
|
||||
LibuvBaseTask *worker = (LibuvBaseTask *)malloc(size + userDataSize);
|
||||
LibuvBaseTask *worker = (LibuvBaseTask *)calloc(1, size + userDataSize);
|
||||
PARAM_CHECK(worker != NULL, return NULL, "Failed to create param woker");
|
||||
worker->worker.flags = flags;
|
||||
worker->userDataSize = userDataSize;
|
||||
@@ -141,7 +141,7 @@ static int InitPipeSocket(uv_pipe_t *pipeServer)
|
||||
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));
|
||||
PARAM_CHECK(chmod(PIPE_NAME, S_IRWXU | S_IRWXG | S_IRWXO) == 0,
|
||||
return -1, "Open file %s error %s", PIPE_NAME, strerror(errno));
|
||||
return -1, "Open file %s error %d", PIPE_NAME, errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -247,11 +247,12 @@ int ParamEventTaskCreate(ParamTaskPtr *stream, EventProcess eventProcess, EventP
|
||||
int ParamEventSend(ParamTaskPtr stream, uint64_t eventId, const char *content, uint32_t size)
|
||||
{
|
||||
PARAM_CHECK(stream != NULL, return -1, "Invalid stream");
|
||||
PARAM_CHECK(size <= RECV_BUFFER_MAX, return -1, "Invalid stream");
|
||||
PARAM_CHECK((stream->flags & WORKER_TYPE_EVENT) == WORKER_TYPE_EVENT, return -1, "Invalid stream type");
|
||||
int ret = PARAM_CODE_INVALID_PARAM;
|
||||
if (stream->flags & WORKER_TYPE_ASYNC) {
|
||||
LibuvEventTask *worker = (LibuvEventTask *)stream;
|
||||
LibuvAsyncEvent *event = (LibuvAsyncEvent *)malloc(sizeof(LibuvAsyncEvent) + size + 1);
|
||||
LibuvAsyncEvent *event = (LibuvAsyncEvent *)calloc(1, sizeof(LibuvAsyncEvent) + size + 1);
|
||||
PARAM_CHECK(event != NULL, return -1, "Failed to alloc event");
|
||||
event->eventId = eventId;
|
||||
event->contentSize = size + 1;
|
||||
@@ -285,9 +286,6 @@ int ParamTaskClose(ParamTaskPtr stream)
|
||||
} else if (stream->flags & WORKER_TYPE_MSG) {
|
||||
LibuvStreamTask *worker = (LibuvStreamTask *)stream;
|
||||
uv_close((uv_handle_t *)(&worker->stream.pipe), OnClientClose);
|
||||
} else if (stream->flags & WORKER_TYPE_EVENT) {
|
||||
LibuvAsyncEvent *event = (LibuvAsyncEvent *)stream;
|
||||
uv_close((uv_handle_t *)&event->async, OnAsyncCloseCallback);
|
||||
} else {
|
||||
free(stream);
|
||||
}
|
||||
@@ -339,6 +337,9 @@ static void SignalHandler(uv_signal_t *handle, int signum)
|
||||
|
||||
int ParamServiceStart(ProcessPidDelete pidDelete)
|
||||
{
|
||||
if (uv_default_loop() == NULL) {
|
||||
return -1;
|
||||
}
|
||||
libuv.pidDeleteProcess = pidDelete;
|
||||
uv_signal_t sigchldHandler;
|
||||
int ret = uv_signal_init(uv_default_loop(), &sigchldHandler);
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
*/
|
||||
#ifndef BASE_STARTUP_PARAM_LIBUVADP_H
|
||||
#define BASE_STARTUP_PARAM_LIBUVADP_H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
@@ -38,9 +38,10 @@ static int LoadPersistParam(PersistParamGetPtr persistParamGet, void *context)
|
||||
}
|
||||
PARAM_CHECK(fp != NULL, return -1, "No valid persist parameter file %s", PARAM_PERSIST_SAVE_PATH);
|
||||
|
||||
char *buff = (char *)malloc(PARAM_BUFFER_SIZE);
|
||||
SubStringInfo *info = malloc(sizeof(SubStringInfo) * (SUBSTR_INFO_VALUE + 1));
|
||||
char *buff = (char *)calloc(1, PARAM_BUFFER_SIZE);
|
||||
SubStringInfo *info = calloc(1, sizeof(SubStringInfo) * (SUBSTR_INFO_VALUE + 1));
|
||||
while (info != NULL && buff != NULL && fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
|
||||
buff[PARAM_BUFFER_SIZE - 1] = '\0';
|
||||
int subStrNumber = GetSubStringInfo(buff, strlen(buff), '=', info, SUBSTR_INFO_VALUE + 1);
|
||||
if (subStrNumber <= SUBSTR_INFO_VALUE) {
|
||||
continue;
|
||||
@@ -48,8 +49,12 @@ static int LoadPersistParam(PersistParamGetPtr persistParamGet, void *context)
|
||||
int ret = persistParamGet(info[0].value, info[1].value, context);
|
||||
PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buff);
|
||||
}
|
||||
free(info);
|
||||
free(buff);
|
||||
if (info) {
|
||||
free(info);
|
||||
}
|
||||
if (buff) {
|
||||
free(buff);
|
||||
}
|
||||
(void)fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
@@ -57,7 +62,7 @@ static int LoadPersistParam(PersistParamGetPtr persistParamGet, void *context)
|
||||
static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
|
||||
{
|
||||
FILE *fp = fopen(PARAM_PERSIST_SAVE_TMP_PATH, "w");
|
||||
PARAM_CHECK(fp != NULL, return -1, "Open file %s fail error %s", PARAM_PERSIST_SAVE_TMP_PATH, strerror(errno));
|
||||
PARAM_CHECK(fp != NULL, return -1, "Open file %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
|
||||
*handle = (PERSIST_SAVE_HANDLE)fp;
|
||||
return 0;
|
||||
}
|
||||
@@ -77,12 +82,12 @@ static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
|
||||
(void)fclose(fp);
|
||||
unlink(PARAM_PERSIST_SAVE_PATH);
|
||||
int ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
|
||||
PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %s",
|
||||
PARAM_PERSIST_SAVE_TMP_PATH, strerror(errno));
|
||||
PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
|
||||
}
|
||||
|
||||
int RegisterPersistParamOps(PersistParamOps *ops)
|
||||
{
|
||||
PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
|
||||
ops->save = NULL;
|
||||
ops->load = LoadPersistParam;
|
||||
ops->batchSaveBegin = BatchSavePersistParamBegin;
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
|
||||
#define LABEL "PARAM_SELINUX"
|
||||
#define SELINUX_LABEL_LEN 128
|
||||
|
||||
typedef struct SELinuxSecurityLabel {
|
||||
ParamSecurityLabel securityLabel;
|
||||
char label[SELINUX_LABEL_LEN];
|
||||
@@ -56,7 +55,7 @@ static int EncodeSecurityLabel(const ParamSecurityLabel *srcLabel, char *buffer,
|
||||
return memcpy_s(buffer, *bufferSize, srcLabel, sizeof(SELinuxSecurityLabel));
|
||||
}
|
||||
|
||||
static int DecodeSecurityLabel(ParamSecurityLabel **srcLabel, char *buffer, uint32_t bufferSize)
|
||||
static int DecodeSecurityLabel(ParamSecurityLabel **srcLabel, const char *buffer, uint32_t bufferSize)
|
||||
{
|
||||
PARAM_CHECK(bufferSize >= sizeof(SELinuxSecurityLabel), return -1, "Invalid buffersize %u", bufferSize);
|
||||
PARAM_CHECK(srcLabel != NULL && buffer != NULL, return -1, "Invalid param");
|
||||
@@ -66,28 +65,32 @@ static int DecodeSecurityLabel(ParamSecurityLabel **srcLabel, char *buffer, uint
|
||||
|
||||
static int LoadParamLabels(const char *fileName, SecurityLabelFunc label, void *context)
|
||||
{
|
||||
int ret = 0;
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
PARAM_CHECK(fp != NULL, return -1, "Open file %s fail", fileName);
|
||||
SubStringInfo *info = malloc(sizeof(SubStringInfo) * (SUBSTR_INFO_DAC + 1));
|
||||
PARAM_CHECK(info != NULL, (void)fclose(fp);
|
||||
return -1, "Failed to malloc for %s", fileName);
|
||||
char buff[PARAM_BUFFER_SIZE];
|
||||
|
||||
SubStringInfo *info = calloc(1, sizeof(SubStringInfo) * (SUBSTR_INFO_DAC + 1));
|
||||
char *buff = (char *)calloc(1, PARAM_BUFFER_SIZE);
|
||||
int infoCount = 0;
|
||||
ParamAuditData auditData = {};
|
||||
while (fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
|
||||
ParamAuditData auditData = {0};
|
||||
while (info != NULL && buff != NULL && fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
|
||||
buff[PARAM_BUFFER_SIZE - 1] = '\0';
|
||||
int subStrNumber = GetSubStringInfo(buff, strlen(buff), ' ', info, SUBSTR_INFO_DAC + 1);
|
||||
if (subStrNumber <= SUBSTR_INFO_DAC) {
|
||||
continue;
|
||||
}
|
||||
auditData.name = info[SUBSTR_INFO_NAME].value;
|
||||
auditData.label = info[SUBSTR_INFO_LABEL].value;
|
||||
ret = label(&auditData, context);
|
||||
int ret = label(&auditData, context);
|
||||
PARAM_CHECK(ret == 0, continue, "Failed to write param info %d %s", ret, buff);
|
||||
infoCount++;
|
||||
}
|
||||
if (buff) {
|
||||
free(buff);
|
||||
}
|
||||
if (info) {
|
||||
free(info);
|
||||
}
|
||||
(void)fclose(fp);
|
||||
free(info);
|
||||
PARAM_LOGI("Load parameter info %d success %s", infoCount, fileName);
|
||||
return 0;
|
||||
}
|
||||
@@ -101,7 +104,7 @@ static int ProcessParamFile(const char *fileName, void *context)
|
||||
static int GetParamSecurityLabel(SecurityLabelFunc label, const char *path, void *context)
|
||||
{
|
||||
PARAM_CHECK(label != NULL, return -1, "Invalid param");
|
||||
int ret = 0;
|
||||
int ret;
|
||||
struct stat st;
|
||||
LabelFuncContext cxt = { label, context };
|
||||
if ((stat(path, &st) == 0) && !S_ISDIR(st.st_mode)) {
|
||||
@@ -119,7 +122,7 @@ static int CheckFilePermission(const ParamSecurityLabel *localLabel, const char
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, int mode)
|
||||
static int CheckParamPermission(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, uint32_t mode)
|
||||
{
|
||||
PARAM_LOGI("CheckParamPermission ");
|
||||
PARAM_CHECK(srcLabel != NULL && auditData != NULL && auditData->name != NULL, return -1, "Invalid param");
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
@@ -26,6 +25,7 @@
|
||||
#include "param_message.h"
|
||||
|
||||
#define INVALID_SOCKET (-1)
|
||||
#define INIT_PROCESS_PID 1
|
||||
#define LABEL "Client"
|
||||
static const uint32_t RECV_BUFFER_MAX = 5 * 1024;
|
||||
|
||||
@@ -37,6 +37,10 @@ __attribute__((destructor)) static void ClientDeinit(void);
|
||||
|
||||
static int InitParamClient(void)
|
||||
{
|
||||
if (getpid() == INIT_PROCESS_PID) {
|
||||
PARAM_LOGI("Init process, do not init client");
|
||||
return 0;
|
||||
}
|
||||
if (PARAM_TEST_FLAG(g_clientSpace.paramSpace.flags, WORKSPACE_FLAGS_INIT)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -62,7 +66,7 @@ static ParamSecurityOps *GetClientParamSecurityOps(void)
|
||||
return &g_clientSpace.paramSpace.paramSecurityOps;
|
||||
}
|
||||
|
||||
static int FillLabelContent(ParamMessage *request, uint32_t *start, uint32_t length)
|
||||
static int FillLabelContent(const ParamMessage *request, uint32_t *start, uint32_t length)
|
||||
{
|
||||
uint32_t bufferSize = request->msgSize - sizeof(ParamMessage);
|
||||
uint32_t offset = *start;
|
||||
@@ -159,7 +163,7 @@ int SystemSetParameter(const char *name, const char *value)
|
||||
PARAM_CHECK(ret == 0, return -1, "Failed to get label length");
|
||||
}
|
||||
msgSize += sizeof(ParamMsgContent) + labelLen;
|
||||
msgSize = msgSize < RECV_BUFFER_MAX ? RECV_BUFFER_MAX : msgSize;
|
||||
msgSize = (msgSize < RECV_BUFFER_MAX) ? RECV_BUFFER_MAX : msgSize;
|
||||
|
||||
ParamMessage *request = (ParamMessage *)CreateParamMessage(MSG_SET_PARAM, name, msgSize);
|
||||
PARAM_CHECK(request != NULL, return -1, "Failed to malloc for connect");
|
||||
@@ -195,7 +199,7 @@ int SystemWaitParameter(const char *name, const char *value, int32_t timeout)
|
||||
timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
|
||||
}
|
||||
uint32_t msgSize = sizeof(ParamMessage) + sizeof(ParamMsgContent) + sizeof(ParamMsgContent) + sizeof(uint32_t);
|
||||
msgSize = msgSize < RECV_BUFFER_MAX ? RECV_BUFFER_MAX : msgSize;
|
||||
msgSize = (msgSize < RECV_BUFFER_MAX) ? RECV_BUFFER_MAX : msgSize;
|
||||
uint32_t offset = 0;
|
||||
ParamMessage *request = NULL;
|
||||
if (value != NULL) {
|
||||
|
||||
@@ -51,12 +51,11 @@ static void ShowParam(ParamHandle handle, void *cookie)
|
||||
static void ExeuteCmdParamGet(int argc, char *argv[], int start)
|
||||
{
|
||||
uint32_t size = PARAM_CONST_VALUE_LEN_MAX + PARAM_NAME_LEN_MAX + 1 + 1;
|
||||
char *buffer = (char *)malloc(size);
|
||||
char *buffer = (char *)calloc(1, size);
|
||||
if (buffer == NULL) {
|
||||
printf("Get parameterfail\n");
|
||||
return;
|
||||
}
|
||||
memset_s(buffer, size, 0, size);
|
||||
if (argc == start) {
|
||||
SystemTraversalParameter(ShowParam, (void *)buffer);
|
||||
} else {
|
||||
@@ -117,11 +116,18 @@ static void ExeuteCmdParamRead(int argc, char *argv[], int start)
|
||||
|
||||
static void HandleParamChange(const char *key, const char *value, void *context)
|
||||
{
|
||||
if (key == NULL || value == NULL) {
|
||||
return;
|
||||
}
|
||||
UNUSED(context);
|
||||
printf("Receive parameter change %s %s \n", key, value);
|
||||
}
|
||||
|
||||
static void ExeuteCmdParamWatch(int argc, char *argv[], int start)
|
||||
{
|
||||
if (argc <= start) {
|
||||
return;
|
||||
}
|
||||
int ret = SystemWatchParameter(argv[start], HandleParamChange, NULL);
|
||||
if (ret != 0) {
|
||||
return;
|
||||
|
||||
@@ -54,24 +54,25 @@ typedef struct {
|
||||
int InitParamWorkSpace(ParamWorkSpace *workSpace, int onlyRead);
|
||||
void CloseParamWorkSpace(ParamWorkSpace *workSpace);
|
||||
|
||||
int ReadParamWithCheck(ParamWorkSpace *workSpace, const char *name, int op, ParamHandle *handle);
|
||||
int ReadParamValue(ParamWorkSpace *workSpace, ParamHandle handle, char *value, uint32_t *len);
|
||||
int ReadParamName(ParamWorkSpace *workSpace, ParamHandle handle, char *name, uint32_t len);
|
||||
int ReadParamCommitId(ParamWorkSpace *workSpace, ParamHandle handle, uint32_t *commitId);
|
||||
int ReadParamWithCheck(const ParamWorkSpace *workSpace, const char *name, uint32_t op, ParamHandle *handle);
|
||||
int ReadParamValue(const ParamWorkSpace *workSpace, ParamHandle handle, char *value, uint32_t *len);
|
||||
int ReadParamName(const ParamWorkSpace *workSpace, ParamHandle handle, char *name, uint32_t len);
|
||||
int ReadParamCommitId(const ParamWorkSpace *workSpace, ParamHandle handle, uint32_t *commitId);
|
||||
|
||||
int CheckParamName(const char *name, int paramInfo);
|
||||
int CheckParamPermission(ParamWorkSpace *workSpace, const ParamSecurityLabel *srcLabel, const char *name, int mode);
|
||||
int CheckParamPermission(const ParamWorkSpace *workSpace,
|
||||
const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
|
||||
|
||||
typedef void (*TraversalParamPtr)(ParamHandle handle, void *context);
|
||||
typedef struct {
|
||||
TraversalParamPtr traversalParamPtr;
|
||||
void *context;
|
||||
} ParamTraversalContext;
|
||||
int TraversalParam(ParamWorkSpace *workSpace, TraversalParamPtr walkFunc, void *cookie);
|
||||
int TraversalParam(const ParamWorkSpace *workSpace, TraversalParamPtr walkFunc, void *cookie);
|
||||
|
||||
ParamWorkSpace *GetParamWorkSpace(void);
|
||||
ParamWorkSpace *GetClientParamWorkSpace(void);
|
||||
void DumpParameters(ParamWorkSpace *workSpace, int verbose);
|
||||
void DumpParameters(const ParamWorkSpace *workSpace, int verbose);
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#ifndef BASE_STARTUP_PARAM_MESSAGE_H
|
||||
#define BASE_STARTUP_PARAM_MESSAGE_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -88,7 +87,7 @@ typedef struct {
|
||||
|
||||
struct ParamTask_;
|
||||
typedef struct ParamTask_ *ParamTaskPtr;
|
||||
typedef int (*IncomingConnect)(const ParamTaskPtr stream, int flags);
|
||||
typedef int (*IncomingConnect)(const ParamTaskPtr stream, uint32_t flags);
|
||||
typedef int (*RecvMessage)(const ParamTaskPtr stream, const ParamMessage *msg);
|
||||
typedef void (*TimerProcess)(const ParamTaskPtr stream, void *context);
|
||||
typedef void (*EventProcess)(uint64_t eventId, const char *context, uint32_t size);
|
||||
@@ -96,11 +95,11 @@ typedef void (*TaskClose)(const ParamTaskPtr stream);
|
||||
typedef void (*ProcessPidDelete)(pid_t pid);
|
||||
|
||||
typedef struct ParamTask_ {
|
||||
int32_t flags;
|
||||
uint32_t flags;
|
||||
} ParamTask;
|
||||
|
||||
typedef struct {
|
||||
int flags;
|
||||
uint32_t flags;
|
||||
char *server;
|
||||
IncomingConnect incomingConnect;
|
||||
RecvMessage recvMessage;
|
||||
@@ -123,7 +122,7 @@ int ParamTimerStart(ParamTaskPtr timer, uint64_t timeout, uint64_t repeat);
|
||||
|
||||
void *ParamGetTaskUserData(ParamTaskPtr stream);
|
||||
|
||||
int FillParamMsgContent(ParamMessage *request, uint32_t *start, int type, const char *value, uint32_t length);
|
||||
int FillParamMsgContent(const ParamMessage *request, uint32_t *start, int type, const char *value, uint32_t length);
|
||||
ParamMsgContent *GetNextContent(const ParamMessage *reqest, uint32_t *offset);
|
||||
ParamMessage *CreateParamMessage(int type, const char *name, uint32_t msgSize);
|
||||
|
||||
|
||||
@@ -16,9 +16,6 @@
|
||||
|
||||
#ifndef BASE_STARTUP_PARAM_REQUEST_H
|
||||
#define BASE_STARTUP_PARAM_REQUEST_H
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include "param_manager.h"
|
||||
#include "sys_param.h"
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -77,9 +77,9 @@ typedef struct {
|
||||
int (*securityInitLabel)(ParamSecurityLabel **label, int isInit);
|
||||
int (*securityGetLabel)(SecurityLabelFunc label, const char *path, void *context);
|
||||
int (*securityCheckFilePermission)(const ParamSecurityLabel *label, const char *fileName, int flags);
|
||||
int (*securityCheckParamPermission)(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, int mode);
|
||||
int (*securityCheckParamPermission)(const ParamSecurityLabel *srcLabel, const ParamAuditData *auditData, uint32_t mode);
|
||||
int (*securityEncodeLabel)(const ParamSecurityLabel *srcLabel, char *buffer, uint32_t *bufferSize);
|
||||
int (*securityDecodeLabel)(ParamSecurityLabel **srcLabel, char *buffer, uint32_t bufferSize);
|
||||
int (*securityDecodeLabel)(ParamSecurityLabel **srcLabel, const char *buffer, uint32_t bufferSize);
|
||||
int (*securityFreeLabel)(ParamSecurityLabel *srcLabel);
|
||||
} ParamSecurityOps;
|
||||
|
||||
|
||||
@@ -33,9 +33,9 @@ struct CmdLineEntry {
|
||||
int set;
|
||||
};
|
||||
|
||||
int WriteParam(WorkSpace *workSpace, const char *name, const char *value, uint32_t *dataIndex, int onlyAdd);
|
||||
int WriteParam(const WorkSpace *workSpace, const char *name, const char *value, uint32_t *dataIndex, int onlyAdd);
|
||||
|
||||
int InitPersistParamWorkSpace(ParamWorkSpace *workSpace);
|
||||
int InitPersistParamWorkSpace(const ParamWorkSpace *workSpace);
|
||||
void ClosePersistParamWorkSpace(void);
|
||||
int LoadPersistParam(ParamWorkSpace *workSpace);
|
||||
int WritePersistParam(ParamWorkSpace *workSpace, const char *name, const char *value);
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
#include "init_log.h"
|
||||
#include "param_security.h"
|
||||
#include "securec.h"
|
||||
#include "sys_param.h"
|
||||
@@ -100,21 +99,21 @@ struct WorkSpace_;
|
||||
typedef struct WorkSpace_ {
|
||||
char fileName[FILENAME_LEN_MAX + 1];
|
||||
uint32_t (*allocTrieNode)(struct WorkSpace_ *workSpace, const char *key, uint32_t keyLen);
|
||||
int (*compareTrieNode)(ParamTrieNode *node, const char *key2, uint32_t key2Len);
|
||||
int (*compareTrieNode)(const ParamTrieNode *node, const char *key2, uint32_t key2Len);
|
||||
ParamTrieHeader *area;
|
||||
} WorkSpace;
|
||||
|
||||
int InitWorkSpace(const char *fileName, WorkSpace *workSpace, int onlyRead);
|
||||
void CloseWorkSpace(WorkSpace *workSpace);
|
||||
|
||||
ParamTrieNode *GetTrieNode(WorkSpace *workSpace, uint32_t offset);
|
||||
ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset);
|
||||
void SaveIndex(uint32_t *index, uint32_t offset);
|
||||
|
||||
ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen);
|
||||
ParamTrieNode *FindTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel);
|
||||
ParamTrieNode *FindTrieNode(const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel);
|
||||
|
||||
typedef int (*TraversalTrieNodePtr)(WorkSpace *workSpace, ParamTrieNode *node, void *cookie);
|
||||
int TraversalTrieNode(WorkSpace *workSpace, ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, void *cookie);
|
||||
typedef int (*TraversalTrieNodePtr)(const WorkSpace *workSpace, const ParamTrieNode *node, void *cookie);
|
||||
int TraversalTrieNode(const WorkSpace *workSpace, const ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, void *cookie);
|
||||
|
||||
uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData);
|
||||
uint32_t AddParamNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, const char *value, uint32_t valueLen);
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
#ifndef STARTUP_TRIGER_CHECKER_H
|
||||
#define STARTUP_TRIGER_CHECKER_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -16,10 +16,8 @@
|
||||
#ifndef STARTUP_TRIGER_MANAGER_H
|
||||
#define STARTUP_TRIGER_MANAGER_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "init_log.h"
|
||||
#include "list.h"
|
||||
#include "param_message.h"
|
||||
#include "param_utils.h"
|
||||
@@ -35,6 +33,7 @@ extern "C" {
|
||||
#define TRIGGER_CMD "trigger "
|
||||
#define TRIGGER_ARR_NAME_IN_JSON "jobs"
|
||||
#define CMDS_ARR_NAME_IN_JSON "cmds"
|
||||
#define TRIGGER_MAX_CMD 4096
|
||||
|
||||
#define TRIGGER_EXECUTE_QUEUE 64
|
||||
#define MAX_CONDITION_NUMBER 64
|
||||
@@ -114,13 +113,14 @@ typedef struct {
|
||||
} ParamWatcher;
|
||||
|
||||
typedef struct TriggerExtData_ {
|
||||
int (*excuteCmd)(struct TriggerExtData_ *trigger, int cmd, const char *content);
|
||||
int (*excuteCmd)(const struct TriggerExtData_ *trigger, int cmd, const char *content);
|
||||
uint32_t watcherId;
|
||||
ParamWatcher *watcher;
|
||||
} TriggerExtData;
|
||||
|
||||
typedef struct TriggerWorkSpace {
|
||||
void (*cmdExec)(TriggerNode *trigger, CommandNode *cmd, const char *content, uint32_t size);
|
||||
void (*cmdExec)(const TriggerNode *trigger,
|
||||
const CommandNode *cmd, const char *content, uint32_t size);
|
||||
ParamTaskPtr eventHandle;
|
||||
char buffer[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX];
|
||||
TriggerExecuteQueue executeQueue;
|
||||
@@ -136,30 +136,29 @@ typedef int (*TRIGGER_MATCH)(TriggerWorkSpace *workSpace, LogicCalculator *calcu
|
||||
TriggerNode *trigger, const char *content, uint32_t contentSize);
|
||||
int CheckTrigger(TriggerWorkSpace *workSpace, int type,
|
||||
const char *content, uint32_t contentSize, PARAM_CHECK_DONE triggerExecuter);
|
||||
int MarkTriggerToParam(TriggerWorkSpace *workSpace, TriggerHeader *triggerHead, const char *name);
|
||||
int MarkTriggerToParam(const TriggerWorkSpace *workSpace, const TriggerHeader *triggerHead, const char *name);
|
||||
int CheckAndMarkTrigger(int type, const char *name);
|
||||
|
||||
TriggerNode *ExecuteQueuePop(TriggerWorkSpace *workSpace);
|
||||
int ExecuteQueuePush(TriggerWorkSpace *workSpace, TriggerNode *trigger);
|
||||
int ExecuteQueueSize(TriggerWorkSpace *workSpace);
|
||||
int ExecuteQueuePush(TriggerWorkSpace *workSpace, const TriggerNode *trigger);
|
||||
|
||||
TriggerNode *AddTrigger(TriggerHeader *triggerHead, const char *name, const char *condition, uint16_t extDataSize);
|
||||
TriggerNode *GetTriggerByName(TriggerWorkSpace *workSpace, const char *triggerName);
|
||||
TriggerNode *GetTriggerByName(const TriggerWorkSpace *workSpace, const char *triggerName);
|
||||
void FreeTrigger(TriggerNode *trigger);
|
||||
void ClearTrigger(TriggerHeader *head);
|
||||
|
||||
int AddCommand(TriggerNode *trigger, uint32_t cmdIndex, const char *content);
|
||||
CommandNode *GetNextCmdNode(TriggerNode *trigger, CommandNode *curr);
|
||||
CommandNode *GetNextCmdNode(const TriggerNode *trigger, const CommandNode *curr);
|
||||
|
||||
void DumpTrigger(TriggerWorkSpace *workSpace);
|
||||
void DumpTrigger(const TriggerWorkSpace *workSpace);
|
||||
void PostParamTrigger(int type, const char *name, const char *value);
|
||||
|
||||
ParamWatcher *GetParamWatcher(const ParamTaskPtr worker);
|
||||
ParamWatcher *GetNextParamWatcher(TriggerWorkSpace *workSpace, ParamWatcher *curr);
|
||||
ParamWatcher *GetNextParamWatcher(const TriggerWorkSpace *workSpace, const ParamWatcher *curr);
|
||||
TriggerNode *AddWatcherTrigger(ParamWatcher *watcher,
|
||||
int triggerType, const char *name, const char *condition, const TriggerExtData *extData);
|
||||
void DelWatcherTrigger(ParamWatcher *watcher, uint32_t watcherId);
|
||||
void ClearWatcherTrigger(ParamWatcher *watcher);
|
||||
void DelWatcherTrigger(const ParamWatcher *watcher, uint32_t watcherId);
|
||||
void ClearWatcherTrigger(const ParamWatcher *watcher);
|
||||
|
||||
TriggerWorkSpace *GetTriggerWorkSpace(void);
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
#include "param_manager.h"
|
||||
#include <ctype.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#define LABEL "Manager"
|
||||
#if !defined PARAM_SUPPORT_SELINUX && !defined PARAM_SUPPORT_DAC
|
||||
@@ -25,9 +24,8 @@ static ParamSecurityLabel g_defaultSecurityLabel;
|
||||
static int GetParamSecurityOps(ParamWorkSpace *workSpace, int isInit)
|
||||
{
|
||||
UNUSED(isInit);
|
||||
int ret = 0;
|
||||
#if (defined PARAM_SUPPORT_SELINUX || defined PARAM_SUPPORT_DAC)
|
||||
ret = RegisterSecurityOps(&workSpace->paramSecurityOps, isInit);
|
||||
int ret = RegisterSecurityOps(&workSpace->paramSecurityOps, isInit);
|
||||
PARAM_CHECK(workSpace->paramSecurityOps.securityInitLabel != NULL, return -1, "Invalid securityInitLabel");
|
||||
ret = workSpace->paramSecurityOps.securityInitLabel(&workSpace->securityLabel, isInit);
|
||||
PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Failed to init security");
|
||||
@@ -35,7 +33,7 @@ static int GetParamSecurityOps(ParamWorkSpace *workSpace, int isInit)
|
||||
workSpace->securityLabel = &g_defaultSecurityLabel;
|
||||
workSpace->securityLabel->flags |= LABEL_ALL_PERMISSION;
|
||||
#endif
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int InitParamWorkSpace(ParamWorkSpace *workSpace, int onlyRead)
|
||||
@@ -91,7 +89,7 @@ static uint32_t ReadCommitId(ParamNode *entry)
|
||||
return commitId & PARAM_FLAGS_COMMITID;
|
||||
}
|
||||
|
||||
int ReadParamCommitId(ParamWorkSpace *workSpace, ParamHandle handle, uint32_t *commitId)
|
||||
int ReadParamCommitId(const ParamWorkSpace *workSpace, ParamHandle handle, uint32_t *commitId)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && commitId != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(&workSpace->paramSpace, handle);
|
||||
@@ -102,11 +100,11 @@ int ReadParamCommitId(ParamWorkSpace *workSpace, ParamHandle handle, uint32_t *c
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ReadParamWithCheck(ParamWorkSpace *workSpace, const char *name, int op, ParamHandle *handle)
|
||||
int ReadParamWithCheck(const ParamWorkSpace *workSpace, const char *name, uint32_t op, ParamHandle *handle)
|
||||
{
|
||||
PARAM_CHECK(handle != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param handle");
|
||||
PARAM_CHECK(workSpace != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param name");
|
||||
*handle = 0;
|
||||
*handle = -1;
|
||||
int ret = CheckParamPermission(workSpace, workSpace->securityLabel, name, op);
|
||||
PARAM_CHECK(ret == 0, return ret, "Forbid to access parameter %s", name);
|
||||
|
||||
@@ -118,7 +116,7 @@ int ReadParamWithCheck(ParamWorkSpace *workSpace, const char *name, int op, Para
|
||||
return PARAM_CODE_NOT_FOUND;
|
||||
}
|
||||
|
||||
int ReadParamValue(ParamWorkSpace *workSpace, ParamHandle handle, char *value, uint32_t *length)
|
||||
int ReadParamValue(const ParamWorkSpace *workSpace, ParamHandle handle, char *value, uint32_t *length)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && length != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(&workSpace->paramSpace, handle);
|
||||
@@ -141,7 +139,7 @@ int ReadParamValue(ParamWorkSpace *workSpace, ParamHandle handle, char *value, u
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ReadParamName(ParamWorkSpace *workSpace, ParamHandle handle, char *name, uint32_t length)
|
||||
int ReadParamName(const ParamWorkSpace *workSpace, ParamHandle handle, char *name, uint32_t length)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(&workSpace->paramSpace, handle);
|
||||
@@ -157,6 +155,7 @@ int ReadParamName(ParamWorkSpace *workSpace, ParamHandle handle, char *name, uin
|
||||
|
||||
int CheckParamName(const char *name, int info)
|
||||
{
|
||||
PARAM_CHECK(name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
size_t nameLen = strlen(name);
|
||||
if (nameLen >= PARAM_NAME_LEN_MAX) {
|
||||
return PARAM_CODE_INVALID_NAME;
|
||||
@@ -190,7 +189,7 @@ int CheckParamName(const char *name, int info)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ProcessParamTraversal(WorkSpace *workSpace, ParamTrieNode *node, void *cookie)
|
||||
static int ProcessParamTraversal(const WorkSpace *workSpace, const ParamTrieNode *node, void *cookie)
|
||||
{
|
||||
UNUSED(workSpace);
|
||||
ParamTraversalContext *context = (ParamTraversalContext *)cookie;
|
||||
@@ -205,17 +204,20 @@ static int ProcessParamTraversal(WorkSpace *workSpace, ParamTrieNode *node, void
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TraversalParam(ParamWorkSpace *workSpace, TraversalParamPtr walkFunc, void *cookie)
|
||||
int TraversalParam(const ParamWorkSpace *workSpace, TraversalParamPtr walkFunc, void *cookie)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && walkFunc != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
ParamTraversalContext context = {
|
||||
walkFunc, cookie
|
||||
};
|
||||
return TraversalTrieNode(&workSpace->paramSpace, NULL, ProcessParamTraversal, &context);
|
||||
}
|
||||
|
||||
int CheckParamPermission(ParamWorkSpace *workSpace,
|
||||
const ParamSecurityLabel *srcLabel, const char *name, int mode)
|
||||
int CheckParamPermission(const ParamWorkSpace *workSpace,
|
||||
const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->securityLabel != NULL,
|
||||
return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
if (LABEL_IS_ALL_PERMITTED(workSpace->securityLabel)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -237,7 +239,7 @@ int CheckParamPermission(ParamWorkSpace *workSpace,
|
||||
return workSpace->paramSecurityOps.securityCheckParamPermission(srcLabel, &auditData, mode);
|
||||
}
|
||||
|
||||
static int DumpTrieDataNodeTraversal(WorkSpace *workSpace, ParamTrieNode *node, void *cookie)
|
||||
static int DumpTrieDataNodeTraversal(const WorkSpace *workSpace, const ParamTrieNode *node, void *cookie)
|
||||
{
|
||||
int verbose = *(int *)cookie;
|
||||
ParamTrieNode *current = (ParamTrieNode *)node;
|
||||
@@ -245,28 +247,28 @@ static int DumpTrieDataNodeTraversal(WorkSpace *workSpace, ParamTrieNode *node,
|
||||
return 0;
|
||||
}
|
||||
if (verbose) {
|
||||
printf(" Trie node info [%5u,%5u,%5u] data [%5u,%5u] length:%-5d %s \n",
|
||||
printf("\tTrie node info [%u,%u,%u] data: %u label: %u key length:%d \n\t key: %s \n",
|
||||
current->left, current->right, current->child,
|
||||
current->dataIndex, current->labelIndex, current->length, current->key);
|
||||
}
|
||||
if (current->dataIndex != 0) {
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, current->dataIndex);
|
||||
if (entry != NULL) {
|
||||
printf("\tparameter length [%5d,%5d] %s \n",
|
||||
printf("\tparameter length info [%d, %d] \n\t param: %s \n",
|
||||
entry->keyLength, entry->valueLength, (entry != NULL) ? entry->data : "null");
|
||||
}
|
||||
}
|
||||
if (current->labelIndex != 0) {
|
||||
if (current->labelIndex != 0 && verbose) {
|
||||
ParamSecruityNode *label = (ParamSecruityNode *)GetTrieNode(workSpace, current->labelIndex);
|
||||
if (label != NULL) {
|
||||
printf("\tparameter label dac %d %d %o %s \n",
|
||||
printf("\tparameter label dac %d %d %o \n\t label: %s \n",
|
||||
label->uid, label->gid, label->mode, (label->length > 0) ? label->data : "null");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void DumpWorkSpace(ParamWorkSpace *workSpace, int verbose)
|
||||
static void DumpWorkSpace(const ParamWorkSpace *workSpace, int verbose)
|
||||
{
|
||||
printf("workSpace information \n");
|
||||
printf(" map file: %s \n", workSpace->paramSpace.fileName);
|
||||
@@ -280,8 +282,9 @@ static void DumpWorkSpace(ParamWorkSpace *workSpace, int verbose)
|
||||
TraversalTrieNode(&workSpace->paramSpace, NULL, DumpTrieDataNodeTraversal, (void *)&verbose);
|
||||
}
|
||||
|
||||
void DumpParameters(ParamWorkSpace *workSpace, int verbose)
|
||||
void DumpParameters(const ParamWorkSpace *workSpace, int verbose)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->securityLabel != NULL, return, "Invalid param");
|
||||
printf("Dump all paramters begin ...\n");
|
||||
DumpWorkSpace(workSpace, verbose);
|
||||
if (verbose) {
|
||||
|
||||
@@ -35,11 +35,11 @@ int ConntectServer(int fd, const char *servername)
|
||||
PARAM_CHECK(ret > EOK, return -1, "Failed to sprintf_s server address");
|
||||
int len = offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path);
|
||||
ret = connect(fd, (struct sockaddr *)&addr, len);
|
||||
PARAM_CHECK(ret != -1, return -1, "Failed to connect server %s %s", servername, strerror(errno));
|
||||
PARAM_CHECK(ret != -1, return -1, "Failed to connect server %s %d", servername, errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int FillParamMsgContent(ParamMessage *request, uint32_t *start, int type, const char *value, uint32_t length)
|
||||
int FillParamMsgContent(const ParamMessage *request, uint32_t *start, int type, const char *value, uint32_t length)
|
||||
{
|
||||
PARAM_CHECK(request != NULL && start != NULL, return -1, "Invalid param");
|
||||
PARAM_CHECK(value != NULL && length > 0, return -1, "Invalid value");
|
||||
@@ -60,10 +60,11 @@ int FillParamMsgContent(ParamMessage *request, uint32_t *start, int type, const
|
||||
|
||||
ParamMessage *CreateParamMessage(int type, const char *name, uint32_t msgSize)
|
||||
{
|
||||
PARAM_CHECK(name != NULL, return NULL, "Invalid name");
|
||||
if (msgSize < sizeof(ParamMessage)) {
|
||||
msgSize = sizeof(ParamMessage);
|
||||
}
|
||||
ParamMessage *msg = (ParamMessage *)malloc(msgSize);
|
||||
ParamMessage *msg = (ParamMessage *)calloc(1, msgSize);
|
||||
PARAM_CHECK(msg != NULL, return NULL, "Failed to malloc message");
|
||||
msg->type = type;
|
||||
msg->id.msgId = 0;
|
||||
@@ -76,6 +77,8 @@ ParamMessage *CreateParamMessage(int type, const char *name, uint32_t msgSize)
|
||||
|
||||
ParamMsgContent *GetNextContent(const ParamMessage *reqest, uint32_t *offset)
|
||||
{
|
||||
PARAM_CHECK(reqest != NULL, return NULL, "Invalid reqest");
|
||||
PARAM_CHECK(offset != NULL, return NULL, "Invalid offset");
|
||||
ParamMessage *msg = (ParamMessage *)reqest;
|
||||
if ((msg == NULL) || ((*offset + sizeof(ParamMessage) + sizeof(ParamMsgContent)) >= msg->msgSize)) {
|
||||
return NULL;
|
||||
|
||||
@@ -15,11 +15,8 @@
|
||||
|
||||
#include "param_trie.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -35,6 +32,7 @@
|
||||
static int InitWorkSpace_(WorkSpace *workSpace, int mode, int prot, uint32_t spaceSize, int readOnly)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
|
||||
PARAM_CHECK(spaceSize <= PARAM_WORKSPACE_MAX, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
|
||||
PARAM_CHECK(workSpace->allocTrieNode != NULL,
|
||||
return PARAM_CODE_INVALID_PARAM, "Invalid allocTrieNode %s", workSpace->fileName);
|
||||
PARAM_CHECK(workSpace->compareTrieNode != NULL,
|
||||
@@ -44,14 +42,14 @@ static int InitWorkSpace_(WorkSpace *workSpace, int mode, int prot, uint32_t spa
|
||||
|
||||
int fd = open(workSpace->fileName, mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
|
||||
PARAM_CHECK(fd >= 0, return PARAM_CODE_INVALID_NAME,
|
||||
"Open file %s mode %x fail error %s", workSpace->fileName, mode, strerror(errno));
|
||||
"Open file %s mode %x fail error %d", workSpace->fileName, mode, errno);
|
||||
|
||||
if (!readOnly) {
|
||||
ftruncate(fd, spaceSize);
|
||||
}
|
||||
void *areaAddr = (void *)mmap(NULL, spaceSize, prot, MAP_SHARED, fd, 0);
|
||||
PARAM_CHECK(areaAddr != MAP_FAILED, close(fd); return PARAM_CODE_ERROR_MAP_FILE,
|
||||
"Failed to map memory error %s", strerror(errno));
|
||||
PARAM_CHECK(areaAddr != MAP_FAILED && areaAddr != NULL, close(fd);
|
||||
return PARAM_CODE_ERROR_MAP_FILE, "Failed to map memory error %d", errno);
|
||||
close(fd);
|
||||
|
||||
if (!readOnly) {
|
||||
@@ -93,7 +91,7 @@ static uint32_t AllocateParamTrieNode(WorkSpace *workSpace, const char *key, uin
|
||||
return offset;
|
||||
}
|
||||
|
||||
static int CompareParamTrieNode(ParamTrieNode *node, const char *key, uint32_t keyLen)
|
||||
static int CompareParamTrieNode(const ParamTrieNode *node, const char *key, uint32_t keyLen)
|
||||
{
|
||||
if (node->length > keyLen) {
|
||||
return -1;
|
||||
@@ -112,9 +110,9 @@ int InitWorkSpace(const char *fileName, WorkSpace *workSpace, int onlyRead)
|
||||
}
|
||||
workSpace->compareTrieNode = CompareParamTrieNode;
|
||||
workSpace->allocTrieNode = AllocateParamTrieNode;
|
||||
int ret = memcpy_s(workSpace->fileName, FILENAME_LEN_MAX, fileName, strlen(fileName));
|
||||
PARAM_CHECK(ret == EOK, return PARAM_CODE_INVALID_NAME, "Copy file %s fail ", fileName);
|
||||
int openMode = 0;
|
||||
int ret = strcpy_s(workSpace->fileName, sizeof(workSpace->fileName), fileName);
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to copy file name %s", fileName);
|
||||
int openMode;
|
||||
int prot = PROT_READ;
|
||||
if (onlyRead) {
|
||||
openMode = O_RDONLY;
|
||||
@@ -134,8 +132,9 @@ void CloseWorkSpace(WorkSpace *workSpace)
|
||||
workSpace->area = NULL;
|
||||
}
|
||||
|
||||
static ParamTrieNode *GetTrieRoot(WorkSpace *workSpace)
|
||||
static ParamTrieNode *GetTrieRoot(const WorkSpace *workSpace)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return NULL, "The workspace is null");
|
||||
return (ParamTrieNode *)(workSpace->area->data + workSpace->area->firstNode);
|
||||
}
|
||||
|
||||
@@ -151,7 +150,7 @@ static void GetNextKey(const char **remainingKey, char **subKey, uint32_t *subKe
|
||||
|
||||
static ParamTrieNode *AddToSubTrie(WorkSpace *workSpace, ParamTrieNode *current, const char *key, uint32_t keyLen)
|
||||
{
|
||||
if (current == NULL) {
|
||||
if (current == NULL || workSpace == NULL || key == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ParamTrieNode *subTrie = NULL;
|
||||
@@ -182,7 +181,7 @@ static ParamTrieNode *AddToSubTrie(WorkSpace *workSpace, ParamTrieNode *current,
|
||||
ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen)
|
||||
{
|
||||
PARAM_CHECK(key != NULL && keyLen > 0, return NULL, "Invalid param ");
|
||||
PARAM_CHECK(workSpace != NULL, return NULL, "Invalid param %s", key);
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid workSpace %s", key);
|
||||
PARAM_CHECK(workSpace->allocTrieNode != NULL, return NULL, "Invalid param %s", key);
|
||||
PARAM_CHECK(workSpace->compareTrieNode != NULL, return NULL, "Invalid param %s", key);
|
||||
const char *remainingKey = key;
|
||||
@@ -215,7 +214,7 @@ ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLe
|
||||
return current;
|
||||
}
|
||||
|
||||
static ParamTrieNode *FindSubTrie(WorkSpace *workSpace,
|
||||
static ParamTrieNode *FindSubTrie(const WorkSpace *workSpace,
|
||||
ParamTrieNode *current, const char *key, uint32_t keyLen, uint32_t *matchLabel)
|
||||
{
|
||||
if (current == NULL) {
|
||||
@@ -244,10 +243,10 @@ static ParamTrieNode *FindSubTrie(WorkSpace *workSpace,
|
||||
return FindSubTrie(workSpace, subTrie, key, keyLen, matchLabel);
|
||||
}
|
||||
|
||||
ParamTrieNode *FindTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel)
|
||||
ParamTrieNode *FindTrieNode(const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel)
|
||||
{
|
||||
PARAM_CHECK(key != NULL && keyLen > 0, return NULL, "Invalid key ");
|
||||
PARAM_CHECK(workSpace != NULL, return NULL, "Invalid workSpace %s", key);
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid workSpace %s", key);
|
||||
PARAM_CHECK(workSpace->allocTrieNode != NULL, return NULL, "Invalid alloc function %s", key);
|
||||
PARAM_CHECK(workSpace->compareTrieNode != NULL, return NULL, "Invalid compare function %s", key);
|
||||
const char *remainingKey = key;
|
||||
@@ -286,8 +285,8 @@ ParamTrieNode *FindTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyL
|
||||
return current;
|
||||
}
|
||||
|
||||
static int TraversalSubTrieNode(WorkSpace *workSpace,
|
||||
ParamTrieNode *current, TraversalTrieNodePtr walkFunc, void *cookie)
|
||||
static int TraversalSubTrieNode(const WorkSpace *workSpace,
|
||||
const ParamTrieNode *current, TraversalTrieNodePtr walkFunc, void *cookie)
|
||||
{
|
||||
if (current == NULL) {
|
||||
return 0;
|
||||
@@ -299,11 +298,11 @@ static int TraversalSubTrieNode(WorkSpace *workSpace,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TraversalTrieNode(WorkSpace *workSpace, ParamTrieNode *root, TraversalTrieNodePtr walkFunc, void *cookie)
|
||||
int TraversalTrieNode(const WorkSpace *workSpace, const ParamTrieNode *root, TraversalTrieNodePtr walkFunc, void *cookie)
|
||||
{
|
||||
PARAM_CHECK(walkFunc != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
ParamTrieNode *current = root;
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid workSpace");
|
||||
ParamTrieNode *current = (ParamTrieNode *)root;
|
||||
if (root == NULL) {
|
||||
current = GetTrieRoot(workSpace);
|
||||
}
|
||||
@@ -321,9 +320,9 @@ int TraversalTrieNode(WorkSpace *workSpace, ParamTrieNode *root, TraversalTrieNo
|
||||
|
||||
uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return 0, "Invalid param");
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid param");
|
||||
PARAM_CHECK(auditData != NULL && auditData->name != NULL, return 0, "Invalid auditData");
|
||||
uint32_t labelLen = ((auditData->label == NULL) || (strlen(auditData->label) == 0)) ? 0 : strlen(auditData->label);
|
||||
const uint32_t labelLen = (auditData->label == NULL) ? 0 : strlen(auditData->label);
|
||||
uint32_t realLen = sizeof(ParamSecruityNode) + PARAM_ALIGN(labelLen + 1);
|
||||
PARAM_CHECK((workSpace->area->currOffset + realLen) < workSpace->area->dataSize, return 0,
|
||||
"Failed to allocate currOffset %u, dataSize %u datalen %u",
|
||||
@@ -348,7 +347,7 @@ uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditD
|
||||
|
||||
uint32_t AddParamNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, const char *value, uint32_t valueLen)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return 0, "Invalid param");
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid param");
|
||||
PARAM_CHECK(key != NULL && value != NULL, return 0, "Invalid param");
|
||||
|
||||
uint32_t realLen = sizeof(ParamNode) + 1 + 1;
|
||||
@@ -375,8 +374,9 @@ uint32_t AddParamNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, co
|
||||
return offset;
|
||||
}
|
||||
|
||||
ParamTrieNode *GetTrieNode(WorkSpace *workSpace, uint32_t offset)
|
||||
ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return NULL, "Invalid param");
|
||||
if (offset == 0 || offset > workSpace->area->dataSize) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -385,5 +385,6 @@ ParamTrieNode *GetTrieNode(WorkSpace *workSpace, uint32_t offset)
|
||||
|
||||
void SaveIndex(uint32_t *index, uint32_t offset)
|
||||
{
|
||||
PARAM_CHECK(index != NULL, return, "Invalid index");
|
||||
*index = offset;
|
||||
}
|
||||
|
||||
@@ -14,13 +14,12 @@
|
||||
*/
|
||||
|
||||
#include "param_utils.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
@@ -43,6 +42,9 @@ void CheckAndCreateDir(const char *fileName)
|
||||
int ReadFileInDir(const char *dirPath, const char *includeExt,
|
||||
int (*processFile)(const char *fileName, void *context), void *context)
|
||||
{
|
||||
if (dirPath == NULL || processFile == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
DIR *pDir = opendir(dirPath);
|
||||
PARAM_CHECK(pDir != NULL, return -1, "Read dir :%s failed.%d", dirPath, errno);
|
||||
char *fileName = malloc(PARAM_BUFFER_SIZE);
|
||||
@@ -84,7 +86,7 @@ char *ReadFileData(const char *fileName)
|
||||
char *buffer = NULL;
|
||||
int fd = -1;
|
||||
do {
|
||||
fd = open(fileName, O_RDONLY);
|
||||
fd = open(fileName, O_RDONLY); // 阶段早,不能使用realpath
|
||||
PARAM_CHECK(fd >= 0, break, "Failed to read file %s", fileName);
|
||||
|
||||
buffer = (char *)malloc(MAX_DATA_BUFFER);
|
||||
@@ -112,6 +114,7 @@ static void TrimString(char *string, uint32_t currLen)
|
||||
|
||||
int GetSubStringInfo(const char *buff, uint32_t buffLen, char delimiter, SubStringInfo *info, int subStrNumber)
|
||||
{
|
||||
PARAM_CHECK(buff == NULL && info == NULL, return 0, "Invalid buff");
|
||||
size_t i = 0;
|
||||
// 去掉开始的空格
|
||||
for (; i < strlen(buff); i++) {
|
||||
|
||||
@@ -39,7 +39,7 @@ static int AddPersistParam(const char *name, const char *value, void *context)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int SavePersistParam(WorkSpace *workSpace, ParamTrieNode *node, void *cookie)
|
||||
static int SavePersistParam(const WorkSpace *workSpace, const ParamTrieNode *node, void *cookie)
|
||||
{
|
||||
ParamTrieNode *current = (ParamTrieNode *)node;
|
||||
if (current == NULL || current->dataIndex == 0) {
|
||||
@@ -49,7 +49,6 @@ static int SavePersistParam(WorkSpace *workSpace, ParamTrieNode *node, void *coo
|
||||
if (entry == NULL) {
|
||||
return 0;
|
||||
}
|
||||
PARAM_LOGD("SavePersistParam %s", entry->data);
|
||||
if (strncmp(entry->data, PARAM_CONST_PREFIX, strlen(PARAM_CONST_PREFIX)) != 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -62,7 +61,7 @@ static int SavePersistParam(WorkSpace *workSpace, ParamTrieNode *node, void *coo
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int BatchSavePersistParam(WorkSpace *workSpace)
|
||||
static int BatchSavePersistParam(const WorkSpace *workSpace)
|
||||
{
|
||||
PARAM_LOGI("BatchSavePersistParam");
|
||||
if (g_persistWorkSpace.persistParamOps.batchSaveBegin == NULL ||
|
||||
@@ -84,8 +83,9 @@ static int BatchSavePersistParam(WorkSpace *workSpace)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int InitPersistParamWorkSpace(ParamWorkSpace *workSpace)
|
||||
int InitPersistParamWorkSpace(const ParamWorkSpace *workSpace)
|
||||
{
|
||||
UNUSED(workSpace);
|
||||
if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_INIT)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -107,20 +107,17 @@ void ClosePersistParamWorkSpace(void)
|
||||
|
||||
int LoadPersistParam(ParamWorkSpace *workSpace)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
|
||||
int ret = InitPersistParamWorkSpace(workSpace);
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to init persist param");
|
||||
if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED)) {
|
||||
PARAM_LOGE("Persist param has been loaded");
|
||||
return 0;
|
||||
}
|
||||
ret = -1;
|
||||
if (g_persistWorkSpace.persistParamOps.load != NULL) {
|
||||
ret = g_persistWorkSpace.persistParamOps.load(AddPersistParam, &workSpace->paramSpace);
|
||||
}
|
||||
if (ret == 0) {
|
||||
int ret = g_persistWorkSpace.persistParamOps.load(AddPersistParam, &workSpace->paramSpace);
|
||||
PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED);
|
||||
} else {
|
||||
PARAM_LOGE("Failed to load persist param ");
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to load persist param");
|
||||
}
|
||||
// 刷新新增的常量到persist
|
||||
BatchSavePersistParam(&workSpace->paramSpace);
|
||||
@@ -140,6 +137,7 @@ static void TimerCallbackForSave(ParamTaskPtr timer, void *context)
|
||||
|
||||
int WritePersistParam(ParamWorkSpace *workSpace, const char *name, const char *value)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
|
||||
PARAM_CHECK(value != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
|
||||
if (strncmp(name, PARAM_CONST_PREFIX, strlen(PARAM_CONST_PREFIX)) != 0) {
|
||||
return 0;
|
||||
|
||||
@@ -14,11 +14,10 @@
|
||||
*/
|
||||
|
||||
#include "param_service.h"
|
||||
#include <ctype.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/msg.h>
|
||||
@@ -58,7 +57,7 @@ static int AddParam(WorkSpace *workSpace, const char *name, const char *value, u
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int UpdateParam(WorkSpace *workSpace, uint32_t *dataIndex, const char *name, const char *value)
|
||||
static int UpdateParam(const WorkSpace *workSpace, uint32_t *dataIndex, const char *name, const char *value)
|
||||
{
|
||||
ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, *dataIndex);
|
||||
PARAM_CHECK(entry != NULL, return PARAM_CODE_REACHED_MAX, "Failed to update param value %s %u", name, *dataIndex);
|
||||
@@ -79,7 +78,7 @@ static int UpdateParam(WorkSpace *workSpace, uint32_t *dataIndex, const char *na
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CheckParamValue(WorkSpace *workSpace, const ParamTrieNode *node, const char *name, const char *value)
|
||||
static int CheckParamValue(const WorkSpace *workSpace, const ParamTrieNode *node, const char *name, const char *value)
|
||||
{
|
||||
if (IS_READY_ONLY(name)) {
|
||||
PARAM_CHECK(strlen(value) < PARAM_CONST_VALUE_LEN_MAX,
|
||||
@@ -96,7 +95,7 @@ static int CheckParamValue(WorkSpace *workSpace, const ParamTrieNode *node, cons
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WriteParam(WorkSpace *workSpace, const char *name, const char *value, uint32_t *dataIndex, int onlyAdd)
|
||||
int WriteParam(const WorkSpace *workSpace, const char *name, const char *value, uint32_t *dataIndex, int onlyAdd)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && dataIndex != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
|
||||
PARAM_CHECK(value != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid name or value");
|
||||
@@ -110,7 +109,7 @@ int WriteParam(WorkSpace *workSpace, const char *name, const char *value, uint32
|
||||
}
|
||||
return UpdateParam(workSpace, &node->dataIndex, name, value);
|
||||
}
|
||||
return AddParam(workSpace, name, value, dataIndex);
|
||||
return AddParam((WorkSpace *)workSpace, name, value, dataIndex);
|
||||
}
|
||||
|
||||
PARAM_STATIC int AddSecurityLabel(const ParamAuditData *auditData, void *context)
|
||||
@@ -162,7 +161,8 @@ static char *GetServiceCtrlName(const char *name, const char *value)
|
||||
key = (char *)malloc(keySize + 1);
|
||||
PARAM_CHECK(key != NULL, return NULL, "Failed to alloc memory for %s", name);
|
||||
int ret = sprintf_s(key, keySize, "%s%s", OHOS_SERVICE_CTRL_PREFIX, powerCtrlArg[i][1]);
|
||||
PARAM_CHECK(ret > EOK, return NULL, "Failed to format key for %s", name);
|
||||
PARAM_CHECK(ret > EOK, free(key);
|
||||
return NULL, "Failed to format key for %s", name);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -173,7 +173,8 @@ static char *GetServiceCtrlName(const char *name, const char *value)
|
||||
key = (char *)malloc(keySize + 1);
|
||||
PARAM_CHECK(key != NULL, return NULL, "Failed to alloc memory for %s", name);
|
||||
int ret = sprintf_s(key, keySize, "%s%s", OHOS_SERVICE_CTRL_PREFIX, value);
|
||||
PARAM_CHECK(ret > EOK, return NULL, "Failed to format key for %s", name);
|
||||
PARAM_CHECK(ret > EOK, free(key);
|
||||
return NULL, "Failed to format key for %s", name);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -187,7 +188,7 @@ static void CheckAndSendTrigger(ParamWorkSpace *workSpace, uint32_t dataIndex, c
|
||||
PARAM_CHECK(entry != NULL, return, "Failed to get data %s ", name);
|
||||
uint32_t trigger = 1;
|
||||
if ((atomic_load_explicit(&entry->commitId, memory_order_relaxed) & PARAM_FLAGS_TRIGGED) != PARAM_FLAGS_TRIGGED) {
|
||||
trigger = CheckAndMarkTrigger(TRIGGER_PARAM, name) != 0 ? 1 : 0;
|
||||
trigger = (CheckAndMarkTrigger(TRIGGER_PARAM, name) != 0) ? 1 : 0;
|
||||
}
|
||||
if (trigger) {
|
||||
atomic_store_explicit(&entry->commitId,
|
||||
@@ -198,7 +199,7 @@ static void CheckAndSendTrigger(ParamWorkSpace *workSpace, uint32_t dataIndex, c
|
||||
|
||||
int wait = 1;
|
||||
if ((atomic_load_explicit(&entry->commitId, memory_order_relaxed) & PARAM_FLAGS_WAITED) != PARAM_FLAGS_WAITED) {
|
||||
wait = CheckAndMarkTrigger(TRIGGER_PARAM_WAIT, name) != 0 ? 1 : 0;
|
||||
wait = (CheckAndMarkTrigger(TRIGGER_PARAM_WAIT, name) != 0) ? 1 : 0;
|
||||
}
|
||||
if (wait) {
|
||||
atomic_store_explicit(&entry->commitId,
|
||||
@@ -218,12 +219,12 @@ static int SystemSetParam(const char *name, const char *value, const ParamSecuri
|
||||
char *key = GetServiceCtrlName(name, value);
|
||||
if (srcLabel != NULL) {
|
||||
ret = CheckParamPermission(&g_paramWorkSpace, srcLabel, (key == NULL) ? name : key, DAC_WRITE);
|
||||
PARAM_CHECK(ret == 0, return ret, "Forbit to set parameter %s", name);
|
||||
}
|
||||
if (key != NULL) {
|
||||
serviceCtrl = 1;
|
||||
free(key);
|
||||
}
|
||||
PARAM_CHECK(ret == 0, return ret, "Forbit to set parameter %s", name);
|
||||
uint32_t dataIndex = 0;
|
||||
ret = WriteParam(&g_paramWorkSpace.paramSpace, name, value, &dataIndex, 0);
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to set param %d name %s %s", ret, name, value);
|
||||
@@ -249,7 +250,7 @@ static int SendResponseMsg(ParamTaskPtr worker, const ParamMessage *msg, int res
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int SendWatcherNotifyMessage(TriggerExtData *extData, int cmd, const char *content)
|
||||
static int SendWatcherNotifyMessage(const TriggerExtData *extData, int cmd, const char *content)
|
||||
{
|
||||
UNUSED(cmd);
|
||||
PARAM_CHECK(content != NULL, return -1, "Invalid content");
|
||||
@@ -259,7 +260,7 @@ static int SendWatcherNotifyMessage(TriggerExtData *extData, int cmd, const char
|
||||
PARAM_CHECK(msg != NULL, return -1, "Failed to create msg ");
|
||||
|
||||
uint32_t offset = 0;
|
||||
int ret = 0;
|
||||
int ret;
|
||||
char *tmp = strstr(content, "=");
|
||||
if (tmp != NULL) {
|
||||
ret = strncpy_s(msg->key, sizeof(msg->key) - 1, content, tmp - content);
|
||||
@@ -287,7 +288,7 @@ static int HandleParamSet(const ParamTaskPtr worker, const ParamMessage *msg)
|
||||
uint32_t offset = 0;
|
||||
ParamMsgContent *valueContent = GetNextContent(msg, &offset);
|
||||
PARAM_CHECK(valueContent != NULL, return -1, "Invalid msg for %s", msg->key);
|
||||
int ret = 0;
|
||||
int ret;
|
||||
ParamMsgContent *lableContent = GetNextContent(msg, &offset);
|
||||
ParamSecurityLabel *srcLabel = NULL;
|
||||
if (lableContent != NULL && lableContent->contentSize != 0) {
|
||||
@@ -306,7 +307,7 @@ static int HandleParamSet(const ParamTaskPtr worker, const ParamMessage *msg)
|
||||
return SendResponseMsg(worker, msg, ret);
|
||||
}
|
||||
|
||||
static ParamNode *CheckMatchParamWait(ParamWorkSpace *worksapce, const char *name, const char *value)
|
||||
static ParamNode *CheckMatchParamWait(const ParamWorkSpace *worksapce, const char *name, const char *value)
|
||||
{
|
||||
uint32_t nameLength = strlen(name);
|
||||
ParamTrieNode *node = FindTrieNode(&worksapce->paramSpace, name, nameLength, NULL);
|
||||
@@ -332,13 +333,14 @@ static ParamNode *CheckMatchParamWait(ParamWorkSpace *worksapce, const char *nam
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int HandleParamWaitAdd(ParamWorkSpace *worksapce, const ParamTaskPtr worker, const ParamMessage *msg)
|
||||
static int HandleParamWaitAdd(const ParamWorkSpace *worksapce, const ParamTaskPtr worker, const ParamMessage *msg)
|
||||
{
|
||||
PARAM_CHECK(msg != NULL, return -1, "Invalid message");
|
||||
uint32_t offset = 0;
|
||||
uint32_t timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
|
||||
ParamMsgContent *valueContent = GetNextContent(msg, &offset);
|
||||
PARAM_CHECK(valueContent != NULL, return -1, "Invalid msg");
|
||||
PARAM_CHECK(valueContent->contentSize <= PARAM_CONST_VALUE_LEN_MAX, return -1, "Invalid msg");
|
||||
ParamMsgContent *timeoutContent = GetNextContent(msg, &offset);
|
||||
if (timeoutContent != NULL) {
|
||||
timeout = *((uint32_t *)(timeoutContent->content));
|
||||
@@ -361,7 +363,7 @@ static int HandleParamWaitAdd(ParamWorkSpace *worksapce, const ParamTaskPtr work
|
||||
}
|
||||
|
||||
uint32_t buffSize = strlen(msg->key) + valueContent->contentSize + 1 + 1;
|
||||
char *condition = malloc(buffSize);
|
||||
char *condition = calloc(1, buffSize);
|
||||
PARAM_CHECK(condition != NULL, return -1, "Failed to create condition for %s", msg->key);
|
||||
int ret = sprintf_s(condition, buffSize - 1, "%s=%s", msg->key, valueContent->content);
|
||||
PARAM_CHECK(ret > EOK, free(condition);
|
||||
@@ -407,7 +409,7 @@ PARAM_STATIC int ProcessMessage(const ParamTaskPtr worker, const ParamMessage *m
|
||||
{
|
||||
PARAM_CHECK(msg != NULL, return -1, "Invalid msg");
|
||||
PARAM_CHECK(worker != NULL, return -1, "Invalid worker");
|
||||
int ret = 0;
|
||||
int ret = PARAM_CODE_INVALID_PARAM;
|
||||
switch (msg->type) {
|
||||
case MSG_SET_PARAM:
|
||||
ret = HandleParamSet(worker, msg);
|
||||
@@ -428,17 +430,18 @@ PARAM_STATIC int ProcessMessage(const ParamTaskPtr worker, const ParamMessage *m
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int LoadDefaultParam_(const char *fileName, int mode, const char *exclude[], uint32_t count)
|
||||
static int LoadDefaultParam_(const char *fileName, uint32_t mode, const char *exclude[], uint32_t count)
|
||||
{
|
||||
uint32_t paramNum = 0;
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
PARAM_CHECK(fp != NULL, return -1, "Open file %s fail", fileName);
|
||||
char *buff = malloc(sizeof(SubStringInfo) * (SUBSTR_INFO_VALUE + 1) + PARAM_BUFFER_SIZE);
|
||||
char *buff = calloc(1, sizeof(SubStringInfo) * (SUBSTR_INFO_VALUE + 1) + PARAM_BUFFER_SIZE);
|
||||
PARAM_CHECK(buff != NULL, (void)fclose(fp);
|
||||
return -1, "Failed to alloc memory for load %s", fileName);
|
||||
|
||||
SubStringInfo *info = (SubStringInfo *)(buff + PARAM_BUFFER_SIZE);
|
||||
while (fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
|
||||
buff[PARAM_BUFFER_SIZE - 1] = '\0';
|
||||
int subStrNumber = GetSubStringInfo(buff, strlen(buff), '=', info, SUBSTR_INFO_VALUE + 1);
|
||||
if (subStrNumber <= SUBSTR_INFO_VALUE) {
|
||||
continue;
|
||||
@@ -465,7 +468,7 @@ static int LoadDefaultParam_(const char *fileName, int mode, const char *exclude
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int OnIncomingConnect(const ParamTaskPtr server, int flags)
|
||||
static int OnIncomingConnect(const ParamTaskPtr server, uint32_t flags)
|
||||
{
|
||||
PARAM_LOGD("OnIncomingConnect %p", server);
|
||||
ParamStreamInfo info = {};
|
||||
@@ -479,6 +482,7 @@ static int OnIncomingConnect(const ParamTaskPtr server, int flags)
|
||||
PARAM_CHECK(ret == 0, return -1, "Failed to create client");
|
||||
|
||||
ParamWatcher *watcher = (ParamWatcher *)ParamGetTaskUserData(client);
|
||||
PARAM_CHECK(watcher != NULL, return -1, "Failed to get watcher");
|
||||
ListInit(&watcher->node);
|
||||
PARAM_TRIGGER_HEAD_INIT(watcher->triggerHead);
|
||||
ListAddTail(&GetTriggerWorkSpace()->waitList, &watcher->node);
|
||||
@@ -490,6 +494,7 @@ static int OnIncomingConnect(const ParamTaskPtr server, int flags)
|
||||
static void TimerCallback(ParamTaskPtr timer, void *context)
|
||||
{
|
||||
UNUSED(context);
|
||||
PARAM_CHECK(GetTriggerWorkSpace() != NULL, return, "Invalid wrokspace");
|
||||
ParamWatcher *watcher = GetNextParamWatcher(GetTriggerWorkSpace(), NULL);
|
||||
while (watcher != NULL) {
|
||||
ParamWatcher *next = GetNextParamWatcher(GetTriggerWorkSpace(), watcher);
|
||||
@@ -534,11 +539,11 @@ int LoadPersistParams(void)
|
||||
static int ProcessParamFile(const char *fileName, void *context)
|
||||
{
|
||||
static const char *exclude[] = {"ctl.", "selinux.restorecon_recursive"};
|
||||
int mode = *(int *)context;
|
||||
uint32_t mode = *(int *)context;
|
||||
return LoadDefaultParam_(fileName, mode, exclude, sizeof(exclude) / sizeof(exclude[0]));
|
||||
}
|
||||
|
||||
int LoadDefaultParams(const char *fileName, int mode)
|
||||
int LoadDefaultParams(const char *fileName, uint32_t mode)
|
||||
{
|
||||
PARAM_CHECK(fileName != NULL, return -1, "Invalid fielname for load");
|
||||
if (!PARAM_TEST_FLAG(g_paramWorkSpace.flags, WORKSPACE_FLAGS_INIT)) {
|
||||
@@ -614,6 +619,7 @@ void StopParamService(void)
|
||||
PARAM_LOGI("StopParamService.");
|
||||
CloseParamWorkSpace(&g_paramWorkSpace);
|
||||
CloseTriggerWorkSpace();
|
||||
ParamTaskClose(g_paramWorkSpace.serverTask);
|
||||
g_paramWorkSpace.serverTask = NULL;
|
||||
ParamServiceStop();
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
*/
|
||||
|
||||
#include "trigger_checker.h"
|
||||
#include <ctype.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include "init_param.h"
|
||||
#include "trigger_manager.h"
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
int CalculatorInit(LogicCalculator *calculator, int dataNumber, int dataUnit, int needCondition)
|
||||
{
|
||||
PARAM_CHECK(calculator != NULL, return -1, "Invalid param");
|
||||
PARAM_CHECK(dataUnit <= (int)sizeof(LogicData), return -1, "Invalid param");
|
||||
PARAM_CHECK(dataNumber <= MAX_CALC_PARAM, return -1, "Invalid param");
|
||||
int dataSize = dataUnit * dataNumber;
|
||||
if (needCondition) {
|
||||
dataSize += MAX_DATA_BUFFER_MAX;
|
||||
@@ -86,7 +88,7 @@ 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");
|
||||
char *tmpData = (calculator->data + calculator->dataUnit * calculator->endIndex);
|
||||
char *tmpData = (char *)calculator->data + calculator->dataUnit * calculator->endIndex;
|
||||
int ret = memcpy_s(tmpData, calculator->dataUnit, data, calculator->dataUnit);
|
||||
PARAM_CHECK(ret == EOK, return -1, "Failed to copy logic data");
|
||||
calculator->endIndex++;
|
||||
@@ -100,7 +102,7 @@ static int CalculatorPop(LogicCalculator *calculator, void *data)
|
||||
if (calculator->endIndex == 0) {
|
||||
return -1;
|
||||
}
|
||||
char *tmpData = calculator->data + calculator->dataUnit * (calculator->endIndex - 1);
|
||||
char *tmpData = (char *)calculator->data + calculator->dataUnit * (calculator->endIndex - 1);
|
||||
int ret = memcpy_s(data, calculator->dataUnit, tmpData, calculator->dataUnit);
|
||||
PARAM_CHECK(ret == EOK, return -1, "Failed to copy logic data");
|
||||
calculator->endIndex--;
|
||||
@@ -126,7 +128,6 @@ static int PrefixAdd(char *prefix, uint32_t *prefixIndex, uint32_t prefixLen, ch
|
||||
|
||||
static int HandleOperationOr(LogicCalculator *calculator, char *prefix, uint32_t *prefixIndex, uint32_t prefixLen)
|
||||
{
|
||||
int ret = 0;
|
||||
char e;
|
||||
prefix[(*prefixIndex)++] = ' ';
|
||||
if (CalculatorLength(calculator) == 0) {
|
||||
@@ -137,7 +138,7 @@ static int HandleOperationOr(LogicCalculator *calculator, char *prefix, uint32_t
|
||||
if (e == '(') {
|
||||
CalculatorPushChar(calculator, e);
|
||||
} else {
|
||||
ret = PrefixAdd(prefix, prefixIndex, prefixLen, e);
|
||||
int ret = PrefixAdd(prefix, prefixIndex, prefixLen, e);
|
||||
PARAM_CHECK(ret == 0, return -1, "Invalid prefix");
|
||||
}
|
||||
} while (CalculatorLength(calculator) > 0 && e != '(');
|
||||
@@ -161,7 +162,7 @@ static int CompareValue(const char *condition, const char *value)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ComputeSubCondition(LogicCalculator *calculator, LogicData *data, const char *condition)
|
||||
static int ComputeSubCondition(const LogicCalculator *calculator, LogicData *data, const char *condition)
|
||||
{
|
||||
if (!LOGIC_DATA_TEST_FLAG(data, LOGIC_DATA_FLAGS_ORIGINAL)) {
|
||||
return LOGIC_DATA_TEST_FLAG(data, LOGIC_DATA_FLAGS_TRUE);
|
||||
@@ -215,6 +216,7 @@ int GetValueFromContent(const char *content, uint32_t contentSize, uint32_t star
|
||||
|
||||
int ComputeCondition(LogicCalculator *calculator, const char *condition)
|
||||
{
|
||||
PARAM_CHECK(calculator != NULL && condition != NULL, return -1, "Invalid calculator");
|
||||
uint32_t currIndex = 0;
|
||||
uint32_t start = 0;
|
||||
int noneOper = 1;
|
||||
@@ -266,8 +268,9 @@ int ComputeCondition(LogicCalculator *calculator, const char *condition)
|
||||
|
||||
int ConvertInfixToPrefix(const char *condition, char *prefix, uint32_t prefixLen)
|
||||
{
|
||||
PARAM_CHECK(condition != NULL, return -1, "Invalid condition");
|
||||
char e = 0;
|
||||
int ret = 0;
|
||||
int ret;
|
||||
uint32_t curr = 0;
|
||||
uint32_t prefixIndex = 0;
|
||||
LogicCalculator calculator;
|
||||
@@ -319,8 +322,10 @@ int ConvertInfixToPrefix(const char *condition, char *prefix, uint32_t prefixLen
|
||||
|
||||
int CheckMatchSubCondition(const char *condition, const char *input, int length)
|
||||
{
|
||||
PARAM_CHECK(condition != NULL, return 0, "Invalid condition");
|
||||
PARAM_CHECK(input != NULL, return 0, "Invalid input");
|
||||
char *tmp = strstr(condition, input);
|
||||
if ((tmp != NULL) && (strlen(tmp) > length) && (tmp[length] == '=')) {
|
||||
if ((tmp != NULL) && ((int)strlen(tmp) > length) && (tmp[length] == '=')) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -15,11 +15,8 @@
|
||||
|
||||
#include "trigger_manager.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -35,10 +32,10 @@ int AddCommand(TriggerNode *trigger, uint32_t cmdKeyIndex, const char *content)
|
||||
{
|
||||
PARAM_CHECK(trigger != NULL, return -1, "trigger is null");
|
||||
uint32_t size = sizeof(CommandNode);
|
||||
size += (content == NULL || strlen(content) == 0) ? 1 : strlen(content) + 1;
|
||||
size += (content == NULL) ? 1 : strlen(content) + 1;
|
||||
size = PARAM_ALIGN(size);
|
||||
|
||||
CommandNode *node = (CommandNode *)malloc(size);
|
||||
CommandNode *node = (CommandNode *)calloc(1, size);
|
||||
PARAM_CHECK(node != NULL, return -1, "Failed to alloc memory for command");
|
||||
node->cmdKeyIndex = cmdKeyIndex;
|
||||
node->next = NULL;
|
||||
@@ -46,13 +43,15 @@ int AddCommand(TriggerNode *trigger, uint32_t cmdKeyIndex, const char *content)
|
||||
if (content != NULL && strlen(content) != 0) {
|
||||
int ret = memcpy_s(node->content, size, content, strlen(content));
|
||||
node->content[strlen(content)] = '\0';
|
||||
PARAM_CHECK(ret == EOK, return 0, "Failed to copy command");
|
||||
PARAM_CHECK(ret == EOK, free(node);
|
||||
return 0, "Failed to copy command");
|
||||
}
|
||||
// 插入队列
|
||||
if (trigger->firstCmd == NULL) {
|
||||
trigger->firstCmd = node;
|
||||
trigger->lastCmd = node;
|
||||
} else {
|
||||
PARAM_CHECK(trigger->lastCmd != NULL, return 0, "Invalid last cmd");
|
||||
trigger->lastCmd->next = node;
|
||||
trigger->lastCmd = node;
|
||||
}
|
||||
@@ -60,8 +59,9 @@ int AddCommand(TriggerNode *trigger, uint32_t cmdKeyIndex, const char *content)
|
||||
return 0;
|
||||
}
|
||||
|
||||
CommandNode *GetNextCmdNode(TriggerNode *trigger, CommandNode *curr)
|
||||
CommandNode *GetNextCmdNode(const TriggerNode *trigger, const CommandNode *curr)
|
||||
{
|
||||
PARAM_CHECK(trigger != NULL, return NULL, "trigger is null");
|
||||
if (curr == NULL) {
|
||||
return trigger->firstCmd;
|
||||
}
|
||||
@@ -79,7 +79,7 @@ TriggerNode *AddTrigger(TriggerHeader *triggerHead, const char *name, const char
|
||||
conditionLen = PARAM_ALIGN(strlen(condition) + 1) + CONDITION_EXTEND_LEN;
|
||||
}
|
||||
|
||||
TriggerNode *node = (TriggerNode *)malloc(triggerNodeLen + conditionLen + extDataSize);
|
||||
TriggerNode *node = (TriggerNode *)calloc(1, triggerNodeLen + conditionLen + extDataSize);
|
||||
PARAM_CHECK(node != NULL, return NULL, "Failed to alloc memory for trigger");
|
||||
int ret = memcpy_s(node->name, triggerNodeLen - sizeof(TriggerNode), name, nameLen);
|
||||
PARAM_CHECK(ret == EOK, free(node);
|
||||
@@ -107,6 +107,7 @@ TriggerNode *AddTrigger(TriggerHeader *triggerHead, const char *name, const char
|
||||
|
||||
void ClearTrigger(TriggerHeader *head)
|
||||
{
|
||||
PARAM_CHECK(head != NULL, return, "head is null");
|
||||
ListNode *node = head->triggerList.next;
|
||||
while (node != &head->triggerList) {
|
||||
ListRemove(node);
|
||||
@@ -120,7 +121,7 @@ void ClearTrigger(TriggerHeader *head)
|
||||
|
||||
void FreeTrigger(TriggerNode *trigger)
|
||||
{
|
||||
PARAM_CHECK(trigger != NULL, return, "trigger is null");
|
||||
PARAM_CHECK(trigger != NULL && trigger->triggerHead != NULL, return, "trigger is null");
|
||||
PARAM_LOGD("FreeTrigger %s", trigger->name);
|
||||
TriggerHeader *triggerHead = trigger->triggerHead;
|
||||
CommandNode *cmd = trigger->firstCmd;
|
||||
@@ -150,8 +151,9 @@ void FreeTrigger(TriggerNode *trigger)
|
||||
free(trigger);
|
||||
}
|
||||
|
||||
static TriggerNode *GetNextTrigger(TriggerHeader *triggerHead, TriggerNode *curr)
|
||||
static TriggerNode *GetNextTrigger(const TriggerHeader *triggerHead, const TriggerNode *curr)
|
||||
{
|
||||
PARAM_CHECK(triggerHead != NULL, return NULL, "Invalid triggerHead");
|
||||
ListNode *node = NULL;
|
||||
if (curr != NULL) {
|
||||
node = curr->node.next;
|
||||
@@ -164,12 +166,13 @@ static TriggerNode *GetNextTrigger(TriggerHeader *triggerHead, TriggerNode *curr
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *GetTriggerCondition(TriggerWorkSpace *workSpace, TriggerNode *trigger)
|
||||
static const char *GetTriggerCondition(const TriggerWorkSpace *workSpace, const TriggerNode *trigger)
|
||||
{
|
||||
return trigger->condition == NULL ? "" : trigger->condition;
|
||||
UNUSED(workSpace);
|
||||
return (trigger == NULL || trigger->condition == NULL) ? "" : trigger->condition;
|
||||
}
|
||||
|
||||
TriggerNode *GetTriggerByName(TriggerWorkSpace *workSpace, const char *triggerName)
|
||||
TriggerNode *GetTriggerByName(const TriggerWorkSpace *workSpace, const char *triggerName)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && triggerName != NULL, return NULL, "Invalid param");
|
||||
for (size_t i = 0; i < sizeof(workSpace->triggerHead) / sizeof(workSpace->triggerHead[0]); i++) {
|
||||
@@ -184,16 +187,17 @@ TriggerNode *GetTriggerByName(TriggerWorkSpace *workSpace, const char *triggerNa
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ExecuteQueuePush(TriggerWorkSpace *workSpace, TriggerNode *trigger)
|
||||
int ExecuteQueuePush(TriggerWorkSpace *workSpace, const TriggerNode *trigger)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return -1, "Invalid area");
|
||||
PARAM_CHECK(workSpace != NULL, return -1, "Invalid workSpace");
|
||||
uint32_t index = workSpace->executeQueue.endIndex++ % workSpace->executeQueue.queueCount;
|
||||
workSpace->executeQueue.executeQueue[index] = trigger;
|
||||
workSpace->executeQueue.executeQueue[index] = (TriggerNode *)trigger;
|
||||
return 0;
|
||||
}
|
||||
|
||||
TriggerNode *ExecuteQueuePop(TriggerWorkSpace *workSpace)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return NULL, "Invalid workSpace");
|
||||
TriggerNode *trigger = NULL;
|
||||
do {
|
||||
if (workSpace->executeQueue.endIndex <= workSpace->executeQueue.startIndex) {
|
||||
@@ -207,16 +211,15 @@ TriggerNode *ExecuteQueuePop(TriggerWorkSpace *workSpace)
|
||||
return trigger;
|
||||
}
|
||||
|
||||
int ExecuteQueueSize(TriggerWorkSpace *workSpace)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return 0, "Invalid param");
|
||||
return workSpace->executeQueue.endIndex - workSpace->executeQueue.startIndex;
|
||||
}
|
||||
|
||||
static int CheckBootTriggerMatch(TriggerWorkSpace *workSpace, LogicCalculator *calculator,
|
||||
TriggerNode *trigger, const char *content, uint32_t contentSize)
|
||||
{
|
||||
if (strncmp(trigger->name, (char *)content, contentSize) == 0) {
|
||||
UNUSED(workSpace);
|
||||
UNUSED(calculator);
|
||||
UNUSED(content);
|
||||
UNUSED(contentSize);
|
||||
PARAM_CHECK(trigger != NULL, return -1, "Invalid trigger");
|
||||
if (strncmp(trigger->name, content, contentSize) == 0) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -225,7 +228,12 @@ static int CheckBootTriggerMatch(TriggerWorkSpace *workSpace, LogicCalculator *c
|
||||
static int CheckWatcherTriggerMatch(TriggerWorkSpace *workSpace, LogicCalculator *calculator,
|
||||
TriggerNode *trigger, const char *content, uint32_t contentSize)
|
||||
{
|
||||
if (strncmp(trigger->name, (char *)content, strlen(trigger->name)) == 0) {
|
||||
UNUSED(workSpace);
|
||||
UNUSED(calculator);
|
||||
UNUSED(content);
|
||||
UNUSED(contentSize);
|
||||
PARAM_CHECK(trigger != NULL, return -1, "Invalid trigger");
|
||||
if (strncmp(trigger->name, content, strlen(trigger->name)) == 0) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
@@ -314,7 +322,7 @@ int CheckTrigger(TriggerWorkSpace *workSpace, int type,
|
||||
PARAM_CHECK(workSpace != NULL && content != NULL && triggerExecuter != NULL,
|
||||
return -1, "Failed arg for trigger");
|
||||
PARAM_LOGD("CheckTrigger type: %d content: %s ", type, content);
|
||||
int ret = 0;
|
||||
int ret;
|
||||
LogicCalculator calculator;
|
||||
CalculatorInit(&calculator, MAX_CONDITION_NUMBER, sizeof(LogicData), 1);
|
||||
calculator.triggerExecuter = triggerExecuter;
|
||||
@@ -345,11 +353,15 @@ int CheckTrigger(TriggerWorkSpace *workSpace, int type,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MarkTriggerToParam(TriggerWorkSpace *workSpace, TriggerHeader *triggerHead, const char *name)
|
||||
int MarkTriggerToParam(const TriggerWorkSpace *workSpace, const TriggerHeader *triggerHead, const char *name)
|
||||
{
|
||||
int ret = 0;
|
||||
TriggerNode *trigger = GetNextTrigger(triggerHead, NULL);
|
||||
while (trigger != NULL) {
|
||||
if (GetTriggerCondition(workSpace, trigger) == NULL) {
|
||||
trigger = GetNextTrigger(triggerHead, trigger);
|
||||
continue;
|
||||
}
|
||||
const char *tmp = strstr(GetTriggerCondition(workSpace, trigger), name);
|
||||
if (tmp != NULL && strncmp(tmp + strlen(name), "=", 1) == 0) {
|
||||
TRIGGER_SET_FLAG(trigger, TRIGGER_FLAGS_RELATED);
|
||||
@@ -361,8 +373,9 @@ int MarkTriggerToParam(TriggerWorkSpace *workSpace, TriggerHeader *triggerHead,
|
||||
}
|
||||
|
||||
|
||||
ParamWatcher *GetNextParamWatcher(TriggerWorkSpace *workSpace, ParamWatcher *curr)
|
||||
ParamWatcher *GetNextParamWatcher(const TriggerWorkSpace *workSpace, const ParamWatcher *curr)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return NULL, "Invalid workSpace");
|
||||
ListNode *node = NULL;
|
||||
if (curr != NULL) {
|
||||
node = curr->node.next;
|
||||
@@ -400,7 +413,7 @@ TriggerNode *AddWatcherTrigger(ParamWatcher *watcher,
|
||||
return trigger;
|
||||
}
|
||||
|
||||
void DelWatcherTrigger(ParamWatcher *watcher, uint32_t watcherId)
|
||||
void DelWatcherTrigger(const ParamWatcher *watcher, uint32_t watcherId)
|
||||
{
|
||||
PARAM_CHECK(watcher != NULL, return, "Failed to add watcher ");
|
||||
TriggerNode *trigger = GetNextTrigger(&watcher->triggerHead, NULL);
|
||||
@@ -416,7 +429,7 @@ void DelWatcherTrigger(ParamWatcher *watcher, uint32_t watcherId)
|
||||
}
|
||||
}
|
||||
|
||||
void ClearWatcherTrigger(ParamWatcher *watcher)
|
||||
void ClearWatcherTrigger(const ParamWatcher *watcher)
|
||||
{
|
||||
PARAM_CHECK(watcher != NULL, return, "Invalid watcher ");
|
||||
TriggerNode *trigger = GetNextTrigger(&watcher->triggerHead, NULL);
|
||||
@@ -428,42 +441,45 @@ void ClearWatcherTrigger(ParamWatcher *watcher)
|
||||
}
|
||||
}
|
||||
|
||||
#define DUMP_DEBUG PARAM_LOGD
|
||||
static void DumpTriggerQueue(TriggerWorkSpace *workSpace, int index)
|
||||
static void DumpTriggerQueue(const TriggerWorkSpace *workSpace, int index)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL, return, "Invalid workSpace ");
|
||||
TriggerNode *trigger = GetNextTrigger(&workSpace->triggerHead[index], NULL);
|
||||
while (trigger != NULL) {
|
||||
DUMP_DEBUG("trigger 0x%08x", trigger->flags);
|
||||
DUMP_DEBUG("trigger name %s ", trigger->name);
|
||||
DUMP_DEBUG("trigger condition %s ", trigger->condition);
|
||||
printf("trigger 0x%08x \n", trigger->flags);
|
||||
printf("trigger name %s \n", trigger->name);
|
||||
if (trigger->condition != NULL) {
|
||||
printf("trigger condition %s \n", trigger->condition);
|
||||
}
|
||||
|
||||
CommandNode *cmd = GetNextCmdNode(trigger, NULL);
|
||||
while (cmd != NULL) {
|
||||
DUMP_DEBUG("\t command name %s", GetCmdKey(cmd->cmdKeyIndex));
|
||||
DUMP_DEBUG("\t command args %s", cmd->content);
|
||||
printf("\t command name %s \n", GetCmdKey(cmd->cmdKeyIndex));
|
||||
printf("\t command args %s \n", cmd->content);
|
||||
cmd = GetNextCmdNode(trigger, cmd);
|
||||
}
|
||||
trigger = GetNextTrigger(&workSpace->triggerHead[index], trigger);
|
||||
}
|
||||
}
|
||||
|
||||
void DumpTrigger(TriggerWorkSpace *workSpace)
|
||||
void DumpTrigger(const TriggerWorkSpace *workSpace)
|
||||
{
|
||||
DUMP_DEBUG("Ready to dump all trigger memory");
|
||||
DUMP_DEBUG("workspace queue BOOT info:");
|
||||
PARAM_CHECK(workSpace != NULL, return, "Invalid workSpace ");
|
||||
printf("Ready to dump all trigger memory \n");
|
||||
printf("workspace queue BOOT info:\n");
|
||||
DumpTriggerQueue(workSpace, TRIGGER_BOOT);
|
||||
DUMP_DEBUG("workspace queue parameter info:");
|
||||
printf("workspace queue parameter info:\n");
|
||||
DumpTriggerQueue(workSpace, TRIGGER_PARAM);
|
||||
DUMP_DEBUG("workspace queue other info:");
|
||||
printf("workspace queue other info:\n");
|
||||
DumpTriggerQueue(workSpace, TRIGGER_UNKNOW);
|
||||
|
||||
DUMP_DEBUG("workspace queue execute info:");
|
||||
DUMP_DEBUG("queue info count: %u start: %u end: %u",
|
||||
printf("workspace queue execute info:\n");
|
||||
printf("queue info count: %u start: %u end: %u\n",
|
||||
workSpace->executeQueue.queueCount, workSpace->executeQueue.startIndex, workSpace->executeQueue.endIndex);
|
||||
for (uint32_t index = workSpace->executeQueue.startIndex; index < workSpace->executeQueue.endIndex; index++) {
|
||||
TriggerNode *trigger = workSpace->executeQueue.executeQueue[index % workSpace->executeQueue.queueCount];
|
||||
if (trigger != 0) {
|
||||
DUMP_DEBUG("queue node trigger name: %s ", trigger->name);
|
||||
printf("queue node trigger name: %s \n", trigger->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <pthread.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "init_cmds.h"
|
||||
@@ -37,7 +37,7 @@ void DoTriggerExec(const char *triggerName)
|
||||
}
|
||||
}
|
||||
|
||||
static void DoCmdExec(TriggerNode *trigger, CommandNode *cmd, const char *content, uint32_t size)
|
||||
static void DoCmdExec(const TriggerNode *trigger, const CommandNode *cmd, const char *content, uint32_t size)
|
||||
{
|
||||
if (cmd->cmdKeyIndex == CMD_INDEX_FOR_PARA_WAIT || cmd->cmdKeyIndex == CMD_INDEX_FOR_PARA_WATCH) {
|
||||
TriggerExtData *extData = TRIGGER_GET_EXT_DATA(trigger, TriggerExtData);
|
||||
@@ -59,7 +59,7 @@ static void DoCmdExec(TriggerNode *trigger, CommandNode *cmd, const char *conten
|
||||
#endif
|
||||
}
|
||||
|
||||
static int ExecuteTrigger(TriggerWorkSpace *workSpace, TriggerNode *trigger)
|
||||
static int ExecuteTrigger(const TriggerWorkSpace *workSpace, const TriggerNode *trigger)
|
||||
{
|
||||
PARAM_CHECK(workSpace != NULL && trigger != NULL, return -1, "Invalid trigger");
|
||||
PARAM_CHECK(workSpace->cmdExec != NULL, return -1, "Invalid cmdExec");
|
||||
@@ -152,7 +152,7 @@ static void ProcessBeforeEvent(uint64_t eventId, const char *content, uint32_t s
|
||||
break;
|
||||
}
|
||||
default:
|
||||
PARAM_LOGI("CheckTriggers: %lu", eventId);
|
||||
PARAM_LOGI("CheckTriggers: %llu", eventId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -205,6 +205,8 @@ void PostParamTrigger(int type, const char *name, const char *value)
|
||||
{
|
||||
PARAM_CHECK(name != NULL && value != NULL, return, "Invalid param");
|
||||
uint32_t bufferSize = strlen(name) + strlen(value) + 1 + 1 + 1;
|
||||
PARAM_CHECK(bufferSize < (PARAM_CONST_VALUE_LEN_MAX + PARAM_NAME_LEN_MAX + 1 + 1 + 1),
|
||||
return, "bufferSize is longest %d", bufferSize);
|
||||
char *buffer = (char *)malloc(bufferSize);
|
||||
PARAM_CHECK(buffer != NULL, return, "Failed to alloc memory for param %s", name);
|
||||
int ret = sprintf_s(buffer, bufferSize - 1, "%s=%s", name, value);
|
||||
@@ -237,7 +239,7 @@ static int GetTriggerType(const char *type)
|
||||
return TRIGGER_UNKNOW;
|
||||
}
|
||||
|
||||
static int ParseTrigger_(TriggerWorkSpace *workSpace, cJSON *triggerItem)
|
||||
static int ParseTrigger_(const TriggerWorkSpace *workSpace, const cJSON *triggerItem)
|
||||
{
|
||||
PARAM_CHECK(triggerItem != NULL, return -1, "Invalid file");
|
||||
PARAM_CHECK(workSpace != NULL, return -1, "Failed to create trigger list");
|
||||
@@ -246,17 +248,17 @@ static int ParseTrigger_(TriggerWorkSpace *workSpace, cJSON *triggerItem)
|
||||
char *condition = cJSON_GetStringValue(cJSON_GetObjectItem(triggerItem, "condition"));
|
||||
int type = GetTriggerType(name);
|
||||
PARAM_CHECK(type < TRIGGER_MAX, return -1, "Failed to get trigger index");
|
||||
|
||||
TriggerHeader *header = (TriggerHeader *)&workSpace->triggerHead[type];
|
||||
TriggerNode *trigger = GetTriggerByName(workSpace, name);
|
||||
if (trigger == NULL) {
|
||||
trigger = AddTrigger(&workSpace->triggerHead[type], name, condition, 0);
|
||||
trigger = AddTrigger(header, name, condition, 0);
|
||||
PARAM_CHECK(trigger != NULL, return -1, "Failed to create trigger %s", name);
|
||||
}
|
||||
if (type == TRIGGER_BOOT) { // 设置trigger立刻删除,如果是boot
|
||||
TRIGGER_SET_FLAG(trigger, TRIGGER_FLAGS_ONCE);
|
||||
TRIGGER_SET_FLAG(trigger, TRIGGER_FLAGS_SUBTRIGGER);
|
||||
}
|
||||
PARAM_LOGD("ParseTrigger %s type %d count %d", name, type, workSpace->triggerHead[type].triggerCount);
|
||||
PARAM_LOGD("ParseTrigger %s type %d count %d", name, type, header->triggerCount);
|
||||
|
||||
// 添加命令行
|
||||
cJSON *cmdItems = cJSON_GetObjectItem(triggerItem, CMDS_ARR_NAME_IN_JSON);
|
||||
@@ -264,11 +266,11 @@ static int ParseTrigger_(TriggerWorkSpace *workSpace, cJSON *triggerItem)
|
||||
int cmdLinesCnt = cJSON_GetArraySize(cmdItems);
|
||||
PARAM_CHECK(cmdLinesCnt > 0, return -1, "Command array size must positive %s", name);
|
||||
|
||||
int ret = 0;
|
||||
int ret;
|
||||
uint32_t cmdKeyIndex = 0;
|
||||
for (int i = 0; i < cmdLinesCnt; ++i) {
|
||||
for (int i = 0; (i < cmdLinesCnt) && (i < TRIGGER_MAX_CMD); ++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, &cmdKeyIndex);
|
||||
@@ -293,7 +295,7 @@ int ParseTriggerConfig(const cJSON *fileRoot)
|
||||
int size = cJSON_GetArraySize(triggers);
|
||||
PARAM_CHECK(size > 0, return -1, "Trigger array size must positive");
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int i = 0; i < size && i < TRIGGER_MAX_CMD; ++i) {
|
||||
cJSON *item = cJSON_GetArrayItem(triggers, i);
|
||||
ParseTrigger_(&g_triggerWorkSpace, item);
|
||||
}
|
||||
@@ -337,7 +339,9 @@ void CloseTriggerWorkSpace(void)
|
||||
ClearTrigger(&g_triggerWorkSpace.triggerHead[i]);
|
||||
}
|
||||
free(g_triggerWorkSpace.executeQueue.executeQueue);
|
||||
g_triggerWorkSpace.executeQueue.executeQueue = NULL;
|
||||
ParamTaskClose(g_triggerWorkSpace.eventHandle);
|
||||
g_triggerWorkSpace.eventHandle = NULL;
|
||||
}
|
||||
|
||||
int CheckAndMarkTrigger(int type, const char *name)
|
||||
|
||||
@@ -55,9 +55,9 @@ private:
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t watcherId_{ 0 };
|
||||
std::string keyPrefix_{};
|
||||
ParameterChangePtr callback_{ nullptr };
|
||||
uint32_t watcherId_ { 0 };
|
||||
std::string keyPrefix_ {};
|
||||
ParameterChangePtr callback_ { nullptr };
|
||||
void *context_ { nullptr };
|
||||
};
|
||||
|
||||
@@ -77,8 +77,8 @@ private:
|
||||
void ResetService(const wptr<IRemoteObject> &remote);
|
||||
sptr<IWatcherManager> GetService();
|
||||
std::mutex lock_;
|
||||
sptr<IWatcherManager> watcherManager_{};
|
||||
sptr<IRemoteObject::DeathRecipient> deathRecipient_{};
|
||||
sptr<IWatcherManager> watcherManager_ {};
|
||||
sptr<IRemoteObject::DeathRecipient> deathRecipient_ {};
|
||||
|
||||
std::mutex mutex_;
|
||||
std::map<std::string, ParamWatcherKitPtr> watchers_;
|
||||
|
||||
@@ -29,7 +29,6 @@ namespace init_param {
|
||||
REGISTER_SYSTEM_ABILITY_BY_ID(WatcherManager, PARAM_WATCHER_DISTRIBUTED_SERVICE_ID, true)
|
||||
|
||||
const static int32_t INVALID_SOCKET = -1;
|
||||
const static int32_t SLEEP_TIME = 2000;
|
||||
uint32_t WatcherManager::AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher)
|
||||
{
|
||||
WATCHER_CHECK(watcher != nullptr, return 0, "Invalid remove watcher for %s", keyPrefix.c_str());
|
||||
@@ -94,9 +93,10 @@ int WatcherManager::SendMessage(WatcherGroupPtr group, int type)
|
||||
return 0;
|
||||
}
|
||||
|
||||
void WatcherManager::ProcessWatcherMessage(const char *buffer, uint32_t dataSize)
|
||||
void WatcherManager::ProcessWatcherMessage(const std::vector<char> &buffer, uint32_t dataSize)
|
||||
{
|
||||
ParamMessage *msg = (ParamMessage *)buffer;
|
||||
ParamMessage *msg = (ParamMessage *)buffer.data();
|
||||
PARAM_CHECK(msg != NULL, return, "Invalid msg");
|
||||
uint32_t offset = 0;
|
||||
if (msg->type != MSG_NOTIFY_PARAM) {
|
||||
return;
|
||||
@@ -194,8 +194,7 @@ void WatcherManager::SendLocalChange(const std::string &keyPrefix, ParamWatcherP
|
||||
std::vector<char> buffer(PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX);
|
||||
struct Context context = {buffer.data(), watcher, keyPrefix};
|
||||
// walk watcher
|
||||
SystemTraversalParameter(
|
||||
[](ParamHandle handle, void *cookie) {
|
||||
SystemTraversalParameter([](ParamHandle handle, void *cookie) {
|
||||
struct Context *context = (struct Context *)(cookie);
|
||||
SystemGetParameterName(handle, context->buffer, PARAM_NAME_LEN_MAX);
|
||||
if (!FilterParam(context->buffer, context->keyPrefix)) {
|
||||
@@ -211,11 +210,10 @@ void WatcherManager::SendLocalChange(const std::string &keyPrefix, ParamWatcherP
|
||||
void WatcherManager::RunLoop()
|
||||
{
|
||||
const int32_t RECV_BUFFER_MAX = 5 * 1024;
|
||||
char *buffer = (char *)malloc(RECV_BUFFER_MAX);
|
||||
PARAM_CHECK(buffer != NULL, return, "Failed to create buffer for recv");
|
||||
std::vector<char> buffer(RECV_BUFFER_MAX, 0);
|
||||
while (!stop) {
|
||||
int fd = GetServerFd(false);
|
||||
ssize_t recvLen = recv(fd, buffer, RECV_BUFFER_MAX, 0);
|
||||
ssize_t recvLen = recv(fd, buffer.data(), RECV_BUFFER_MAX, 0);
|
||||
if (recvLen <= 0) {
|
||||
if (errno == EAGAIN) { // 超时,继续等待
|
||||
continue;
|
||||
@@ -241,6 +239,7 @@ void WatcherManager::StartLoop()
|
||||
|
||||
int WatcherManager::GetServerFd(bool retry)
|
||||
{
|
||||
const int32_t SLEEP_TIME = 2000;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (retry && serverFd_ != INVALID_SOCKET) {
|
||||
close(serverFd_);
|
||||
@@ -249,7 +248,7 @@ int WatcherManager::GetServerFd(bool retry)
|
||||
if (serverFd_ != INVALID_SOCKET) {
|
||||
return serverFd_;
|
||||
}
|
||||
struct timeval time;
|
||||
struct timeval time {};
|
||||
time.tv_sec = 1;
|
||||
time.tv_usec = 0;
|
||||
do {
|
||||
|
||||
@@ -101,7 +101,7 @@ private:
|
||||
void RunLoop();
|
||||
void StartLoop();
|
||||
void SendLocalChange(const std::string &keyPrefix, ParamWatcherPtr watcher);
|
||||
void ProcessWatcherMessage(const char *buffer, uint32_t dataSize);
|
||||
void ProcessWatcherMessage(const std::vector<char> &buffer, uint32_t dataSize);
|
||||
int SendMessage(WatcherGroupPtr group, int type);
|
||||
int GetServerFd(bool retry);
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user