提交 e63b8cb7 编写于 作者: C cheng_jinsong

init ut memory

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 ed5e8f4d
......@@ -25,6 +25,7 @@ common_include_dirs = [
"//base/startup/init/services/init/include",
"//base/startup/init/services/log",
"//base/startup/init/interfaces/innerkits/include",
"//base/startup/init/interfaces/innerkits/include/syspara",
"//base/security/selinux/interfaces/policycoreutils/include",
"//third_party/bounds_checking_function/include",
"//third_party/selinux/libselinux/include",
......
......@@ -422,6 +422,7 @@ static int32_t BShellParamCmdShell(BShellHandle shell, int32_t argc, char *argv[
return -1;
}
}
SetInitLogLevel(INIT_INFO);
pid_t pid = fork();
if (pid == 0) {
setuid(uid);
......
......@@ -645,6 +645,9 @@ static int32_t BShellParamSetValue(BShellParam *param, void *value)
sizeof(uint8_t), sizeof(uint16_t), sizeof(uint32_t), sizeof(char *)
};
if (param->type == PARAM_STRING) {
if (param->value.string != NULL) {
free(param->value.string);
}
param->value.string = strdup((char *)value);
BSH_CHECK(param->value.string != NULL, return BSH_SYSTEM_ERR, "Failed to copy value for %s", param->name);
} else if (param->type < PARAM_STRING) {
......
......@@ -38,7 +38,7 @@ typedef int (*HashNodeCompare)(const HashNode *node1, const HashNode *node2);
typedef int (*HashKeyCompare)(const HashNode *node1, const void *key);
typedef int (*HashNodeFunction)(const HashNode *node);
typedef int (*HashKeyFunction)(const void *key);
typedef void (*HashNodeOnFree)(const HashNode *node);
typedef void (*HashNodeOnFree)(const HashNode *node, void *context);
typedef struct {
HashNodeCompare nodeCompare;
......@@ -53,7 +53,7 @@ typedef void *HashMapHandle;
int OH_HashMapIsEmpty(HashMapHandle handle);
int32_t OH_HashMapCreate(HashMapHandle *handle, const HashInfo *info);
void OH_HashMapDestory(HashMapHandle handle);
void OH_HashMapDestory(HashMapHandle handle, void *context);
int32_t OH_HashMapAdd(HashMapHandle handle, HashNode *hashNode);
void OH_HashMapRemove(HashMapHandle handle, const void *key);
HashNode *OH_HashMapGet(HashMapHandle handle, const void *key);
......
......@@ -69,6 +69,7 @@ typedef struct {
} InitWorkspace;
void InitServiceSpace(void);
void CloseServiceSpace(void);
int InitParseGroupCfg(void);
int GenerateHashCode(const char *key);
......@@ -83,6 +84,7 @@ InitWorkspace *GetInitWorkspace(void);
#endif
int GetBootModeFromMisc(void);
void clearMisc(void);
void ReleaseCmd(PluginCmd *cmd);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -212,10 +212,7 @@ static int WritePid(const Service *service)
void CloseServiceFds(Service *service, bool needFree)
{
if (service == NULL) {
return;
}
INIT_ERROR_CHECK(service != NULL, return, "Service null");
INIT_LOGI("Closing service \' %s \' fds", service->name);
// fdCount > 0, There is no reason fds is NULL
if (service->fdCount != 0) {
......@@ -223,8 +220,10 @@ void CloseServiceFds(Service *service, bool needFree)
int *fds = service->fds;
for (size_t i = 0; i < fdCount; i++) {
INIT_LOGV("Closing fd: %d", fds[i]);
close(fds[i]);
fds[i] = -1;
if (fds[i] != -1) {
close(fds[i]);
fds[i] = -1;
}
}
}
service->fdCount = 0;
......
......@@ -152,9 +152,16 @@ static int GroupNodeGetNodeHashCode(const HashNode *node)
return GenerateHashCode((const char *)groupNode->name);
}
static void GroupNodeFree(const HashNode *node)
static void GroupNodeFree(const HashNode *node, void *context)
{
InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
if (groupNode->type == NODE_TYPE_SERVICES) {
ReleaseService(groupNode->data.service);
groupNode->data.service = NULL;
} else if (groupNode->type == NODE_TYPE_CMDS) {
ReleaseCmd(groupNode->data.cmd);
groupNode->data.cmd = NULL;
}
free(groupNode);
}
......@@ -299,7 +306,7 @@ int CheckNodeValid(int type, const char *name)
}
HashNode *node = OH_HashMapGet(g_initWorkspace.hashMap[type], name);
if (node != NULL) {
INIT_LOGI("Found %s in %s group", name, type == NODE_TYPE_JOBS ? "job" : "service");
INIT_LOGV("Found %s in %s group", name, type == NODE_TYPE_JOBS ? "job" : "service");
return 0;
}
if (g_initWorkspace.groupMode == GROUP_BOOT) {
......@@ -320,6 +327,36 @@ HashMapHandle GetGroupHashMap(int type)
return g_initWorkspace.hashMap[type];
}
void CloseServiceSpace(void)
{
if (g_initWorkspace.initFlags == 0) {
return;
}
for (size_t i = 0; i < ARRAY_LENGTH(g_initWorkspace.hashMap); i++) {
if (g_initWorkspace.hashMap[i] != NULL) {
HashMapHandle handle = g_initWorkspace.hashMap[i];
g_initWorkspace.hashMap[i] = NULL;
OH_HashMapDestory(handle, NULL);
}
}
g_initWorkspace.initFlags = 0;
}
void ReleaseCmd(PluginCmd *cmd)
{
if (cmd == NULL) {
return;
}
ListNode *node = cmd->cmdExecutor.next;
while (node != &cmd->cmdExecutor) {
PluginCmdExecutor *cmdExec = ListEntry(node, PluginCmdExecutor, node);
OH_ListRemove(&cmdExec->node);
free(cmdExec);
node = cmd->cmdExecutor.next;
}
free(cmd);
}
#ifdef STARTUP_INIT_TEST
InitWorkspace *GetInitWorkspace(void)
{
......
......@@ -90,6 +90,10 @@ Service *AddService(const char *name)
INIT_LOGE("Failed to create service name %s", name);
return NULL;
}
if (node->data.service != NULL) {
ReleaseService(node->data.service);
node->data.service = NULL;
}
Service *service = (Service *)calloc(1, sizeof(Service));
INIT_ERROR_CHECK(service != NULL, return NULL, "Failed to malloc for service");
node->data.service = service;
......@@ -116,11 +120,19 @@ static void FreeServiceFile(ServiceFile *fileOpt)
return;
}
static void ExecuteServiceClear(Service *service)
{
#ifndef OHOS_LITE
// clear ext data
SERVICE_INFO_CTX ctx = {0};
ctx.serviceName = service->name;
HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)&ctx, NULL);
#endif
}
void ReleaseService(Service *service)
{
if (service == NULL) {
return;
}
INIT_CHECK(service != NULL, return);
FreeServiceArg(&service->pathArgs);
FreeServiceArg(&service->writePidArgs);
FreeServiceArg(&service->capsArgs);
......@@ -138,7 +150,9 @@ void ReleaseService(Service *service)
}
service->servPerm.gIDCnt = 0;
FreeServiceSocket(service->socketCfg);
service->socketCfg = NULL;
FreeServiceFile(service->fileCfg);
service->fileCfg = NULL;
if (service->apl != NULL) {
free(service->apl);
......@@ -154,12 +168,12 @@ void ReleaseService(Service *service)
free(service->cpuSet);
service->cpuSet = NULL;
}
#ifndef OHOS_LITE
// clear ext data
SERVICE_INFO_CTX ctx = {0};
ctx.serviceName = service->name;
HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)&ctx, NULL);
#endif
if (service->restartArg != NULL) {
free(service->restartArg);
service->restartArg = NULL;
}
CloseServiceFds(service, true);
ExecuteServiceClear(service);
g_serviceSpace.serviceCount--;
InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_SERVICES, service->name);
if (groupNode != NULL) {
......
......@@ -43,10 +43,11 @@ static int TaskGetKeyHasCode(const void *key)
return taskId->taskId.fd;
}
static void TaskNodeFree(const HashNode *node)
static void TaskNodeFree(const HashNode *node, void *context)
{
BaseTask *task = HASHMAP_ENTRY(node, BaseTask, hashNode);
CloseTask(LE_GetDefaultLoop(), task);
CloseTask((const LoopHandle)context, task);
free(task);
}
static LE_STATUS CreateLoop_(EventLoop **loop, uint32_t maxevents, uint32_t timeout)
......@@ -76,7 +77,7 @@ LE_STATUS CloseLoop(EventLoop *loop)
if (!loop->stop) {
return LE_SUCCESS;
}
OH_HashMapDestory(loop->taskMap);
OH_HashMapDestory(loop->taskMap, loop);
if (loop->close) {
loop->close(loop);
}
......@@ -100,9 +101,9 @@ LE_STATUS ProcessEvent(const EventLoop *loop, int fd, uint32_t oper)
LE_STATUS AddTask(EventLoop *loop, BaseTask *task)
{
LoopMutexLock(&loop->mutex);
OH_HashMapAdd(loop->taskMap, &task->hashNode);
int ret = OH_HashMapAdd(loop->taskMap, &task->hashNode);
LoopMutexUnlock(&loop->mutex);
return LE_SUCCESS;
return ret;
}
BaseTask *GetTaskByFd(EventLoop *loop, int fd)
......
......@@ -43,6 +43,7 @@ static void HandleSignalTaskClose_(const LoopHandle loopHandle, const TaskHandle
{
BaseTask *task = (BaseTask *)signalHandle;
CloseTask(loopHandle, task);
DelTask((EventLoop *)loopHandle, task);
close(task->taskId.fd);
}
......
......@@ -90,6 +90,7 @@ static void HandleAsyncTaskClose_(const LoopHandle loopHandle, const TaskHandle
{
BaseTask *task = (BaseTask *)taskHandle;
CloseTask(loopHandle, task);
DelTask((EventLoop *)loopHandle, task);
close(task->taskId.fd);
}
......
......@@ -69,13 +69,13 @@ static LE_STATUS HandleRecvMsg_(const LoopHandle loopHandle,
}
}
if (status != LE_SUCCESS) {
FreeBuffer(loopHandle, (StreamTask *)taskHandle, buffer);
FreeBuffer(loopHandle, NULL, buffer);
return status;
}
if (recvMessage) {
recvMessage(taskHandle, buffer->data, readLen);
}
FreeBuffer(loopHandle, (StreamTask *)taskHandle, buffer);
FreeBuffer(loopHandle, NULL, buffer);
return status;
}
......@@ -130,6 +130,7 @@ static void HandleStreamTaskClose_(const LoopHandle loopHandle, const TaskHandle
{
BaseTask *task = (BaseTask *)taskHandle;
CloseTask(loopHandle, task);
DelTask((EventLoop *)loopHandle, task);
if (task->taskId.fd > 0) {
close(task->taskId.fd);
}
......
......@@ -69,7 +69,6 @@ void CloseTask(const LoopHandle loopHandle, BaseTask *task)
if (task->close != NULL) {
task->close((TaskHandle)task);
}
DelTask((EventLoop *)loopHandle, task);
}
LE_Buffer *CreateBuffer(uint32_t bufferSize)
......
......@@ -46,6 +46,7 @@ static LE_STATUS HandleWatcherEvent_(const LoopHandle loopHandle, const TaskHand
static void HandleWatcherTaskClose_(const LoopHandle loopHandle, const TaskHandle taskHandle)
{
CloseTask(loopHandle, (BaseTask *)taskHandle);
DelTask((EventLoop *)loopHandle, (BaseTask *)taskHandle);
}
LE_STATUS LE_StartWatcher(const LoopHandle loopHandle,
......
......@@ -67,6 +67,7 @@ static void HandleTimerClose_(const LoopHandle loopHandle, const TaskHandle task
{
BaseTask *task = (BaseTask *)taskHandle;
CloseTask(loopHandle, task);
DelTask((EventLoop *)loopHandle, task);
close(task->taskId.fd);
}
......
......@@ -167,3 +167,20 @@ MODULE_CONSTRUCTOR(void)
// 执行reboot时调用,安装reboot模块
InitAddRebootHook(InitRebootHook_);
}
MODULE_DESTRUCTOR(void)
{
for (int i = 0; i < g_rebootParamCmdValidNumber; i++) {
if (g_rebootParamCmdInfos[i].name != NULL) {
free(g_rebootParamCmdInfos[i].name);
}
if (g_rebootParamCmdInfos[i].replace != NULL) {
free(g_rebootParamCmdInfos[i].replace);
}
if (g_rebootParamCmdInfos[i].cmd != NULL) {
free(g_rebootParamCmdInfos[i].cmd);
}
}
free(g_rebootParamCmdInfos);
g_rebootParamCmdInfos = NULL;
}
\ No newline at end of file
......@@ -106,7 +106,7 @@ static int RestoreContentRecurse(int id, const char *name, int argc, const char
{
PLUGIN_CHECK(name != NULL && argc >= 1 && argv != NULL, return -1, "Invalid parameter");
PLUGIN_LOGV("RestoreContentRecurse path %s", argv[0]);
if (RestoreconRecurse(argv[0])) {
if (RestoreconRecurse(argv[0]) && errno != 0) {
PLUGIN_LOGE("restoreContentRecurse failed for '%s', err %d.", argv[0], errno);
}
return 0;
......
......@@ -194,6 +194,8 @@ INIT_LOCAL_API void CloseParamWorkSpace(void)
}
g_paramWorkSpace.workSpace[i] = NULL;
}
free(g_paramWorkSpace.workSpace);
g_paramWorkSpace.workSpace = NULL;
for (int i = 0; i < PARAM_SECURITY_MAX; i++) {
if (g_paramWorkSpace.paramSecurityOps[i].securityFreeLabel != NULL) {
g_paramWorkSpace.paramSecurityOps[i].securityFreeLabel(&g_paramWorkSpace.securityLabel);
......
......@@ -135,6 +135,9 @@ static int StartRequest(int clientFd, ParamMessage *request, int timeout)
}
PARAM_CHECK(sendLen >= 0, return PARAM_CODE_FAIL_CONNECT, "Failed to send message err: %d", errno);
PARAM_LOGV("sendMessage sendLen fd %d %zd", clientFd, sendLen);
if (timeout <= 0) {
return 0;
}
int ret = ReadMessage(clientFd, (char *)request, timeout);
if (ret == 0) {
ret = ProcessRecvMsg(request);
......@@ -142,7 +145,7 @@ static int StartRequest(int clientFd, ParamMessage *request, int timeout)
return ret;
}
int SystemSetParameter(const char *name, const char *value)
static int SystemSetParameter_(const char *name, const char *value, int timeout)
{
PARAM_CHECK(name != NULL && value != NULL, return -1, "Invalid name or value");
int ret = CheckParamName(name, 0);
......@@ -173,7 +176,7 @@ int SystemSetParameter(const char *name, const char *value)
ret = DAC_RESULT_FORBIDED;
break;
}
ret = StartRequest(g_clientFd, request, DEFAULT_PARAM_SET_TIMEOUT);
ret = StartRequest(g_clientFd, request, timeout);
if (ret == PARAM_CODE_INVALID_SOCKET) {
close(g_clientFd);
g_clientFd = INVALID_SOCKET;
......@@ -189,6 +192,16 @@ int SystemSetParameter(const char *name, const char *value)
return ret;
}
int SystemSetParameter(const char *name, const char *value)
{
return SystemSetParameter_(name, value, DEFAULT_PARAM_SET_TIMEOUT);
}
int SystemSetParameterNoWait(const char *name, const char *value)
{
return SystemSetParameter_(name, value, 0);
}
int SystemWaitParameter(const char *name, const char *value, int32_t timeout)
{
PARAM_CHECK(name != NULL, return -1, "Invalid name");
......
......@@ -485,11 +485,12 @@ INIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, cons
{
PARAM_CHECK(srcLabel != NULL, return DAC_RESULT_FORBIDED, "The srcLabel is null");
ParamWorkSpace *paramSpace = GetParamWorkSpace();
PARAM_CHECK(paramSpace != NULL, return DAC_RESULT_FORBIDED, "Invalid param workspace");
WorkSpace *dacSpace = GetWorkSpace(WORKSPACE_INDEX_DAC);
PARAM_WORKSPACE_CHECK(paramSpace, return DAC_RESULT_FORBIDED, "Invalid workspace");
PARAM_CHECK(paramSpace->checkParamPermission != NULL, return DAC_RESULT_FORBIDED, "Invalid check permission");
ParamLabelIndex labelIndex = {0};
// search node from dac space, and get selinux label index
WorkSpace *dacSpace = GetWorkSpace(WORKSPACE_INDEX_DAC);
(void)FindTrieNode(dacSpace, name, strlen(name), &labelIndex.dacLabelIndex);
ParamSecurityNode *securityNode = (ParamSecurityNode *)GetTrieNode(dacSpace, labelIndex.dacLabelIndex);
if ((securityNode == NULL) || (securityNode->selinuxIndex == 0) ||
......
......@@ -622,7 +622,7 @@ static int JobNodeGetKeyHasCode(const void *key)
return code;
}
static void JobNodeFree(const HashNode *node)
static void JobNodeFree(const HashNode *node, void *context)
{
JobNode *jobNode = HASHMAP_ENTRY(node, JobNode, hashNode);
FreeTrigger(GetTriggerWorkSpace(), (TriggerNode *)jobNode);
......
......@@ -319,6 +319,8 @@ void CloseTriggerWorkSpace(void)
for (size_t i = 0; i < sizeof(g_triggerWorkSpace.triggerHead) / sizeof(g_triggerWorkSpace.triggerHead[0]); i++) {
ClearTrigger(&g_triggerWorkSpace, i);
}
OH_HashMapDestory(g_triggerWorkSpace.hashMap, NULL);
g_triggerWorkSpace.hashMap = NULL;
free(g_triggerWorkSpace.executeQueue.executeQueue);
g_triggerWorkSpace.executeQueue.executeQueue = NULL;
ParamTaskClose(g_triggerWorkSpace.eventHandle);
......
......@@ -132,7 +132,7 @@ HashNode *OH_HashMapGet(HashMapHandle handle, const void *key)
return GetHashNodeByKey(tab, tab->buckets[hashCode], key, tab->keyCompare);
}
static void HashListFree(HashTab *tab, HashNode *root)
static void HashListFree(HashTab *tab, HashNode *root, void *context)
{
if (root == NULL) {
return;
......@@ -141,18 +141,18 @@ static void HashListFree(HashTab *tab, HashNode *root)
while (node != NULL) {
HashNode *next = node->next;
if (tab->nodeFree != NULL) {
tab->nodeFree(node);
tab->nodeFree(node, context);
}
node = next;
}
}
void OH_HashMapDestory(HashMapHandle handle)
void OH_HashMapDestory(HashMapHandle handle, void *context)
{
INIT_ERROR_CHECK(handle != NULL, return, "Invalid hash handle");
HashTab *tab = (HashTab *)handle;
for (int i = 0; i < tab->maxBucket; i++) {
HashListFree(tab, tab->buckets[i]);
HashListFree(tab, tab->buckets[i], context);
}
free(tab);
}
......
......@@ -184,8 +184,7 @@ HWTEST_F(DeviceInfoUnittest, TestDeviceInfoProxy1, TestSize.Level1)
proxy->GetSerialID(serialId);
char localDeviceId[UDID_LEN] = {0};
int ret = AclGetDevUdid(localDeviceId, UDID_LEN);
ASSERT_NE(ret, 0);
(void)AclGetDevUdid(localDeviceId, UDID_LEN);
const char *serialNumber = AclGetSerial();
EXPECT_NE(nullptr, serialNumber);
}
......
......@@ -82,7 +82,7 @@ HWTEST_F(CmdsUnitTest, TestCommonChown, TestSize.Level1)
{
const char *testFile = "/data/init_ut/test_dir0";
DoCmdByName("chown ", "system system /data/init_ut/test_dir0");
struct stat info;
struct stat info = {};
stat(testFile, &info);
const unsigned int systemUidGid = 1000;
EXPECT_EQ(info.st_uid, systemUidGid);
......
......@@ -68,7 +68,7 @@ static int TestHashKeyFunction(const void *key)
return code;
}
static void TestHashNodeFree(const HashNode *node)
static void TestHashNodeFree(const HashNode *node, void *context)
{
TestHashNode *testNode = HASHMAP_ENTRY(node, TestHashNode, node);
printf("TestHashNodeFree %s\n", testNode->name);
......@@ -108,7 +108,7 @@ HashInfo g_info = {
2
};
HWTEST_F(InitGroupManagerUnitTest, TestHashMap, TestSize.Level1)
HWTEST_F(InitGroupManagerUnitTest, TestHashMap001, TestSize.Level1)
{
HashMapHandle handle;
OH_HashMapCreate(&handle, &g_info);
......@@ -143,14 +143,24 @@ HWTEST_F(InitGroupManagerUnitTest, TestHashMap, TestSize.Level1)
TestHashNode *tmp = HASHMAP_ENTRY(node, TestHashNode, node);
EXPECT_EQ(strcmp(tmp->name, str3), 0);
}
OH_HashMapIsEmpty(handle);
OH_HashMapTraverse(handle, [](const HashNode *node, const void *context) {return;}, nullptr);
OH_HashMapDestory(handle, NULL);
}
HWTEST_F(InitGroupManagerUnitTest, TestHashMap002, TestSize.Level1)
{
HashMapHandle handle;
OH_HashMapCreate(&handle, &g_info);
TestHashNode *node4 = TestCreateHashNode("pre-init");
OH_HashMapAdd(handle, &node4->node);
OH_HashMapRemove(handle, "pre-init");
TestHashNodeFree(&node4->node, NULL);
const char *act = "load_persist_props_action";
TestHashNode *node5 = TestCreateHashNode(act);
OH_HashMapAdd(handle, &node5->node);
OH_HashMapRemove(handle, "pre-init");
node = OH_HashMapGet(handle, (const void *)act);
HashNode *node = OH_HashMapGet(handle, (const void *)act);
EXPECT_NE(node != nullptr, 0);
if (node) {
TestHashNode *tmp = HASHMAP_ENTRY(node, TestHashNode, node);
......@@ -158,7 +168,7 @@ HWTEST_F(InitGroupManagerUnitTest, TestHashMap, TestSize.Level1)
}
OH_HashMapIsEmpty(handle);
OH_HashMapTraverse(handle, [](const HashNode *node, const void *context) {return;}, nullptr);
OH_HashMapDestory(handle);
OH_HashMapDestory(handle, NULL);
}
HWTEST_F(InitGroupManagerUnitTest, TestInitGroupMgrInit, TestSize.Level1)
......@@ -366,6 +376,7 @@ HWTEST_F(InitGroupManagerUnitTest, TestProcessWatchEvent, TestSize.Level1)
ASSERT_EQ(ret, 0);
uint32_t event;
((WatcherTask *)watcher)->processEvent((WatcherHandle)watcher, 0, &event, service);
service->socketCfg = NULL;
}
HWTEST_F(InitGroupManagerUnitTest, TestCheckNodeValid, TestSize.Level1)
......
......@@ -57,8 +57,6 @@ public:
HWTEST_F(InitUnitTest, TestSignalHandle, TestSize.Level1)
{
SignalInit();
struct signalfd_siginfo siginfo;
siginfo.ssi_signo = SIGCHLD;
ProcessSignal(&siginfo);
......@@ -106,6 +104,7 @@ static void TestProcessTimer(const TimerHandle taskHandle, void *context)
size_t fdCount = 0;
int *fds = ServiceGetFd("param_watcher", &fdCount);
EXPECT_TRUE(fds != nullptr);
free(fds);
ServiceSaveFd("testservice", fds1, ARRAY_LENGTH(fds1));
ServiceSaveFd("deviceinfoservice", fds1, ARRAY_LENGTH(fds1));
......@@ -125,7 +124,6 @@ HWTEST_F(InitUnitTest, TestFdHoldService, TestSize.Level1)
EXPECT_EQ(ret, 0);
ret = LE_StartTimer(LE_GetDefaultLoop(), timer, 500, 2);
EXPECT_EQ(ret, 0);
SystemRun();
}
HWTEST_F(InitUnitTest, TestInitLog, TestSize.Level1)
......
......@@ -71,6 +71,7 @@ HWTEST_F(ServiceUnitTest, case01, TestSize.Level1)
EXPECT_EQ(ret, 0);
ReleaseService(service);
cJSON_Delete(jobItem);
}
HWTEST_F(ServiceUnitTest, case02, TestSize.Level1)
......@@ -113,6 +114,7 @@ HWTEST_F(ServiceUnitTest, case02, TestSize.Level1)
ret = ServiceStop(service);
EXPECT_EQ(ret, 0);
ReleaseService(service);
cJSON_Delete(jobItem);
}
HWTEST_F(ServiceUnitTest, TestServiceStartAbnormal, TestSize.Level1)
......@@ -140,6 +142,7 @@ HWTEST_F(ServiceUnitTest, TestServiceStartAbnormal, TestSize.Level1)
ret = ServiceStop(service);
EXPECT_EQ(ret, 0);
ReleaseService(service);
cJSON_Delete(jobItem);
}
HWTEST_F(ServiceUnitTest, TestServiceReap, TestSize.Level1)
......@@ -202,6 +205,7 @@ HWTEST_F(ServiceUnitTest, TestServiceReapOther, TestSize.Level1)
ret = ServiceStop(service);
EXPECT_EQ(ret, 0);
ReleaseService(service);
cJSON_Delete(jobItem);
}
HWTEST_F(ServiceUnitTest, TestServiceManagerRelease, TestSize.Level1)
......@@ -254,6 +258,7 @@ HWTEST_F(ServiceUnitTest, TestServiceManagerGetService, TestSize.Level1)
ASSERT_NE(nullptr, service);
int ret = ParseOneService(serviceItem, service);
EXPECT_NE(ret, 0);
cJSON_Delete(jobItem);
const char *jsonStr1 = "{\"services\":{\"name\":\"test_service3\",\"path\":[\"/data/init_ut/test_service\"],"
"\"importance\":-20,\"uid\":\"system\",\"writepid\":[\"/dev/test_service\"],\"console\":1,"
"\"gid\":[\"system\"], \"critical\":[\"on\"]}}";
......@@ -265,6 +270,7 @@ HWTEST_F(ServiceUnitTest, TestServiceManagerGetService, TestSize.Level1)
ASSERT_NE(nullptr, service);
ret = ParseOneService(serviceItem, service);
EXPECT_NE(ret, 0);
cJSON_Delete(jobItem);
}
/**
......
......@@ -415,12 +415,23 @@ HWTEST_F(InnerkitsUnitTest, TestHoldFd, TestSize.Level1)
fds = ServiceGetFd("testServiceName", &fdCount);
EXPECT_NE(fds, nullptr);
struct msghdr msghdr;
struct msghdr msghdr = {};
BuildControlMessage(nullptr, nullptr, 1, 0);
BuildControlMessage(&msghdr, nullptr, 1, 0);
if (msghdr.msg_control != NULL) {
free(msghdr.msg_control);
msghdr.msg_control = NULL;
}
BuildControlMessage(&msghdr, fds, -1, 0);
if (msghdr.msg_control != NULL) {
free(msghdr.msg_control);
msghdr.msg_control = NULL;
}
BuildControlMessage(&msghdr, fds, -1, 1);
if (msghdr.msg_control != NULL) {
free(msghdr.msg_control);
msghdr.msg_control = NULL;
}
if (fds != nullptr)
{
free(fds);
......
......@@ -79,6 +79,8 @@ if (defined(ohos_lite)) {
}
include_dirs = [
"//base/startup/init/services/begetctl",
"//base/startup/init/services/begetctl/shell",
"//base/startup/init/services/include",
"//base/startup/init/services/include/param",
"//base/startup/init/services/init/include",
......@@ -103,6 +105,8 @@ if (defined(ohos_lite)) {
"//base/startup/init/interfaces/innerkits/include/syspara",
"//base/startup/init/interfaces/innerkits/init_module_engine/include",
"//base/startup/init/interfaces/innerkits/syspara",
"//base/startup/init/ueventd",
"//base/startup/init/ueventd/include",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
"//base/hiviewdfx/hilog_lite/interfaces/native/kits",
......
......@@ -45,9 +45,6 @@ using HashTab = struct {
HashNode *buckets[0];
};
extern "C" {
void OnClose(ParamTaskPtr client);
}
static LE_STATUS TestHandleTaskEvent(const LoopHandle loop, const TaskHandle task, uint32_t oper)
{
return LE_SUCCESS;
......@@ -140,7 +137,7 @@ public:
LE_StreamServerInfo info = {};
info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_SERVER | TASK_TEST;
info.server = (char *)"/data/testpipe";
info.baseInfo.close = OnClose;
info.baseInfo.close = NULL;
info.incommingConnect = IncomingConnect;
LE_CreateStreamServer(LE_GetDefaultLoop(), &serverTask_, &info);
if (serverTask_ == nullptr) {
......@@ -152,7 +149,7 @@ public:
ParamStreamInfo paramStreamInfo = {};
paramStreamInfo.flags = PARAM_TEST_FLAGS;
paramStreamInfo.server = NULL;
paramStreamInfo.close = OnClose;
paramStreamInfo.close = NULL;
paramStreamInfo.recvMessage = ProcessMessage;
paramStreamInfo.incomingConnect = NULL;
ParamTaskPtr client = NULL;
......@@ -174,7 +171,6 @@ public:
ParamWatcher *watcher = (ParamWatcher *)ParamGetTaskUserData(client);
PARAM_CHECK(watcher != nullptr, return, "Failed to get watcher");
OH_ListInit(&watcher->triggerHead);
OnClose(client);
LE_FreeBuffer(LE_GetDefaultLoop(), (TaskHandle)client, nullptr);
return;
}
......@@ -188,7 +184,6 @@ public:
task->handleEvent = TestHandleTaskEvent;
ProcessEvent((EventLoop *)LE_GetDefaultLoop(), testfd, Event_Read);
}
((HashTab *)(((EventLoop *)LE_GetDefaultLoop())->taskMap))->nodeFree(&task->hashNode);
}
void ProcessasynEvent()
{
......@@ -217,6 +212,7 @@ public:
((WatcherTask *)handle)->base.handleEvent(LE_GetDefaultLoop(), (TaskHandle)handle, 0);
((WatcherTask *)handle)->base.flags = WATCHER_ONCE;
((WatcherTask *)handle)->base.handleEvent(LE_GetDefaultLoop(), (TaskHandle)handle, Event_Read);
LE_RemoveWatcher(LE_GetDefaultLoop(), handle);
}
void CreateSocketTest()
{
......@@ -224,7 +220,7 @@ public:
LE_StreamServerInfo info = {};
info.baseInfo.flags = TASK_PIPE | TASK_CONNECT | TASK_TEST;
info.server = (char *)"/data/testpipe";
info.baseInfo.close = OnClose;
info.baseInfo.close = NULL;
info.incommingConnect = IncomingConnect;
info.socketId = 1111; // 1111 is test fd
LE_CreateStreamServer(LE_GetDefaultLoop(), &serverTask, &info);
......
......@@ -268,7 +268,7 @@ public:
void StartServer()
{
serverThread_ = new (std::nothrow)std::thread(&LoopServerUnitTest::RunServer, this);
std::thread(&LoopServerUnitTest::RunServer, this).detach();
sleep(1);
}
......
......@@ -59,6 +59,7 @@ HWTEST_F(LoopSignalUnitTest, SignalInitTestRmSig, TestSize.Level1)
((BaseTask *)g_sigHandler)->handleEvent(loopClient, (TaskHandle)&g_sigHandler, Event_Write);
LE_CloseSignalTask(loopClient, g_sigHandler);
ASSERT_EQ(ret, 0);
LE_StopLoop(loopClient);
LE_CloseLoop(loopClient);
loopClient = nullptr;
}
......
......@@ -18,6 +18,7 @@
#include <sys/prctl.h>
#include <unistd.h>
#include "begetctl.h"
#include "bootstage.h"
#include "init.h"
#include "init_log.h"
......@@ -35,6 +36,7 @@
#ifdef PARAM_LOAD_CFG_FROM_CODE
#include "param_cfg.h"
#endif
#include "ueventd.h"
#ifdef __cplusplus
#if __cplusplus
......@@ -492,13 +494,11 @@ void PrepareInitUnitTestEnv(void)
return;
}
printf("PrepareInitUnitTestEnv \n");
SignalInit();
#ifdef PARAM_SUPPORT_SELINUX
RegisterSecuritySelinuxOps(nullptr, 0);
#endif
int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
SetInitLogLevel((InitLogLevel)loglevel);
#ifndef OHOS_LITE
InitAddGlobalInitHook(0, TestHook);
InitAddPreParamServiceHook(0, TestHook);
......@@ -520,6 +520,9 @@ void PrepareInitUnitTestEnv(void)
LoadParamsFile(STARTUP_INIT_UT_PATH "/system/etc/param", LOAD_PARAM_ONLY_ADD);
LoadParamFromCfg();
int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
SetInitLogLevel((InitLogLevel)loglevel);
// for test int get
SystemWriteParam("test.int.get", "-101");
SystemWriteParam("test.uint.get", "101");
......@@ -568,6 +571,7 @@ static __attribute__((constructor(101))) void ParamTestStubInit(void)
{
printf("Init unit test start \n");
EnableInitLog(INIT_ERROR);
// prepare data
mkdir(STARTUP_INIT_UT_PATH, S_IRWXU | S_IRWXG | S_IRWXO);
PrepareUeventdcfg();
......@@ -593,9 +597,17 @@ __attribute__((destructor)) static void ParamTestStubExit(void)
{
PARAM_LOGI("ParamTestStubExit");
#ifndef OHOS_LITE
StopParamService();
HookMgrExecute(GetBootStageHookMgr(), INIT_BOOT_COMPLETE, NULL, NULL);
CloseUeventConfig();
const char *clearBootEventArgv[] = {"bootevent"};
PluginExecCmd("clear", ARRAY_LENGTH(clearBootEventArgv), clearBootEventArgv);
CloseServiceSpace();
demoExit();
LE_CloseLoop(LE_GetDefaultLoop());
HookMgrDestroy(GetBootStageHookMgr());
#endif
StopParamService();
}
#ifdef OHOS_LITE
......
......@@ -434,6 +434,7 @@ HWTEST_F(ParamUnitTest, TestWorkSpace2, TestSize.Level0)
int ret = ParamStrCpy(workSpace->fileName, size, spaceName);
EXPECT_EQ(ret, 0);
CloseWorkSpace(workSpace);
free(workSpace);
}
#if !(defined __LITEOS_A__ || defined __LITEOS_M__) // can not support parameter type
......
......@@ -76,7 +76,7 @@ void ParseUeventMessage(const char *buffer, ssize_t length, struct Uevent *ueven
void RetriggerUevent(int sockFd, char **devices, int num);
void RetriggerUeventByPath(int sockFd, char *path);
void ProcessUevent(int sockFd, char **devices, int num);
void CloseUeventConfig(void);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -81,5 +81,6 @@ int main(int argc, char **argv)
ProcessUevent(ueventSockFd, NULL, 0); // Not require boot devices
}
PollUeventdSocketTimeout(ueventSockFd, ondemand);
CloseUeventConfig();
return 0;
}
......@@ -412,3 +412,34 @@ void ChangeSysAttributePermissions(const char *sysPath)
INIT_LOGE("[uevent][error] chmod for file %s failed, err = %d", sysAttr, errno);
}
}
static void FreeDeviceConfig(ListNode *node)
{
struct DeviceUdevConf *config = ListEntry(node, struct DeviceUdevConf, list);
free((void *)config->name);
free((void *)config->parameter);
OH_ListRemove(&config->paramNode);
free(config);
}
static void FreeSysUdevConf(ListNode *node)
{
struct SysUdevConf *config = ListEntry(node, struct SysUdevConf, list);
free((void *)config->sysPath);
free((void *)config->attr);
free(config);
}
static void FreeFirmwareUdevConf(ListNode *node)
{
struct FirmwareUdevConf *config = ListEntry(node, struct FirmwareUdevConf, list);
free((void *)config->fmPath);
free(config);
}
void CloseUeventConfig(void)
{
OH_ListRemoveAll(&g_devices, FreeDeviceConfig);
OH_ListRemoveAll(&g_sysDevices, FreeSysUdevConf);
OH_ListRemoveAll(&g_firmwares, FreeFirmwareUdevConf);
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册