From a921eb42bbd50626c0dd86af966994bfac367b1a Mon Sep 17 00:00:00 2001 From: chengjinsong Date: Thu, 20 Oct 2022 16:48:51 +0800 Subject: [PATCH] codex Signed-off-by: chengjinsong --- services/init/standard/init.c | 10 +- services/modules/bootevent/bootevent.c | 160 +++++++++--------- .../watcher/agent/watcher_manager_kits.cpp | 2 +- .../watcher/agent/watcher_manager_kits.h | 2 +- .../param/watcher/proxy/watcher_manager.cpp | 10 +- .../param/watcher/proxy/watcher_manager.h | 58 +++++-- test/moduletest/param_test_cmds.c | 11 +- test/moduletest/syspara_moduleTest.cpp | 2 +- test/unittest/param/param_stub.cpp | 6 +- .../unittest/param/watcher_agent_unittest.cpp | 9 +- 10 files changed, 154 insertions(+), 116 deletions(-) diff --git a/services/init/standard/init.c b/services/init/standard/init.c index 359a84b3..3c4da79d 100755 --- a/services/init/standard/init.c +++ b/services/init/standard/init.c @@ -343,12 +343,18 @@ static void TriggerServices(int startMode) node = GetNextGroupNode(NODE_TYPE_SERVICES, node); continue; } + if (sprintf_s(cmd, sizeof(cmd), "start %s", service->name) <= 0) { + node = GetNextGroupNode(NODE_TYPE_SERVICES, node); + continue; + } if (index == 0) { - (void)sprintf_s(jobName, sizeof(jobName), "boot-service:service-%d-%03d", startMode, jobNum); + if (sprintf_s(jobName, sizeof(jobName), "boot-service:service-%d-%03d", startMode, jobNum) <= 0) { + node = GetNextGroupNode(NODE_TYPE_SERVICES, node); + continue; + } jobNum++; } index++; - (void)sprintf_s(cmd, sizeof(cmd), "start %s", service->name); AddCompleteJob(jobName, NULL, cmd); INIT_LOGV("Add %s to job %s", service->name, jobName); if (index == maxServiceInJob) { diff --git a/services/modules/bootevent/bootevent.c b/services/modules/bootevent/bootevent.c index 3ca380d8..6b2802a5 100755 --- a/services/modules/bootevent/bootevent.c +++ b/services/modules/bootevent/bootevent.c @@ -147,7 +147,81 @@ static void BootEventDestroy(ListNode *node) free((void *)bootEvent); } -static int SaveServiceBootEvent(); +static int AddItemToJson(cJSON *root, const char *name, double startTime, int pid, double durTime) +{ + cJSON *obj = cJSON_CreateObject(); // release obj at traverse done + INIT_CHECK_RETURN_VALUE(obj != NULL, -1); + cJSON_AddStringToObject(obj, "name", name); + cJSON_AddNumberToObject(obj, "ts", startTime); + cJSON_AddStringToObject(obj, "ph", "X"); + cJSON_AddNumberToObject(obj, "pid", pid); + cJSON_AddNumberToObject(obj, "tid", pid); + cJSON_AddNumberToObject(obj, "dur", durTime); + cJSON_AddItemToArray(root, obj); + return 0; +} + +static int BootEventTraversal(ListNode *node, void *root) +{ + static int start = 0; + BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)node; + double forkTime = item->timestamp[BOOTEVENT_FORK].tv_sec * SECTOMSEC + + (double)item->timestamp[BOOTEVENT_FORK].tv_nsec / MSECTONSEC; + double readyTime = item->timestamp[BOOTEVENT_READY].tv_sec * SECTOMSEC + + (double)item->timestamp[BOOTEVENT_READY].tv_nsec / MSECTONSEC; + double durTime = readyTime - forkTime; + if (item->pid == 0) { + if (durTime < SAVEINITBOOTEVENTMSEC) { + return 0; + } + item->pid = 1; // 1 is init pid + } + if (start == 0) { + // set trace start time 0 + INIT_CHECK_RETURN_VALUE(AddItemToJson((cJSON *)root, item->paramName, 0, + 1, 0) == 0, -1); + start++; + } + INIT_CHECK_RETURN_VALUE(AddItemToJson((cJSON *)root, item->paramName, forkTime, + item->pid, durTime > 0 ? durTime : 0) == 0, -1); + return 0; +} + +static int SaveServiceBootEvent() +{ + if (g_bootEventEnable == 0) { + return 0; + } + time_t nowTime = time(NULL); + INIT_CHECK_RETURN_VALUE(nowTime > 0, -1); + struct tm *p = localtime(&nowTime); + INIT_CHECK_RETURN_VALUE(p != NULL, -1); + char bootEventFileName[BOOT_EVENT_FILEPATH_MAX_LEN] = ""; + INIT_CHECK_RETURN_VALUE(snprintf_s(bootEventFileName, BOOT_EVENT_FILEPATH_MAX_LEN, BOOT_EVENT_FILEPATH_MAX_LEN - 1, + BOOTEVENT_OUTPUT_PATH"%d%d%d-%d%d.bootevent", + 1900 + p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min) >= 0, -1); // 1900 is start year + CheckAndCreatFile(bootEventFileName, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); + FILE *tmpFile = fopen(bootEventFileName, "wr"); + INIT_CHECK_RETURN_VALUE(tmpFile != NULL, -1); + cJSON *root = cJSON_CreateArray(); + if (root == NULL) { + (void)fclose(tmpFile); + return -1; + } + OH_ListTraversal(&bootEventList, (void *)root, BootEventTraversal, 0); + char *buff = cJSON_Print(root); + if (buff == NULL) { + cJSON_Delete(root); + (void)fclose(tmpFile); + return -1; + } + INIT_CHECK_ONLY_ELOG(fprintf(tmpFile, "%s\n", buff) >= 0, "save boot event file failed"); + free(buff); + cJSON_Delete(root); + (void)fflush(tmpFile); + (void)fclose(tmpFile); + return 0; +} static void BootEventParaFireByName(const char *paramName) { @@ -280,82 +354,6 @@ static int DoBootEventCmd(int id, const char *name, int argc, const char **argv) return 0; } -static int AddItemToJson(cJSON *root, const char *name, double startTime, int pid, double durTime) -{ - cJSON *obj = cJSON_CreateObject(); // release obj at traverse done - INIT_CHECK_RETURN_VALUE(obj != NULL, -1); - cJSON_AddStringToObject(obj, "name", name); - cJSON_AddNumberToObject(obj, "ts", startTime); - cJSON_AddStringToObject(obj, "ph", "X"); - cJSON_AddNumberToObject(obj, "pid", pid); - cJSON_AddNumberToObject(obj, "tid", pid); - cJSON_AddNumberToObject(obj, "dur", durTime); - cJSON_AddItemToArray(root, obj); - return 0; -} - -static int BootEventTraversal(ListNode *node, void *root) -{ - static int start = 0; - BOOT_EVENT_PARAM_ITEM *item = (BOOT_EVENT_PARAM_ITEM *)node; - double forkTime = item->timestamp[BOOTEVENT_FORK].tv_sec * SECTOMSEC + - (double)item->timestamp[BOOTEVENT_FORK].tv_nsec / MSECTONSEC; - double readyTime = item->timestamp[BOOTEVENT_READY].tv_sec * SECTOMSEC + - (double)item->timestamp[BOOTEVENT_READY].tv_nsec / MSECTONSEC; - double durTime = readyTime - forkTime; - if (item->pid == 0) { - if (durTime < SAVEINITBOOTEVENTMSEC) { - return 0; - } - item->pid = 1; // 1 is init pid - } - if (start == 0) { - // set trace start time 0 - INIT_CHECK_RETURN_VALUE(AddItemToJson((cJSON *)root, item->paramName, 0, - 1, 0) == 0, -1); - start++; - } - INIT_CHECK_RETURN_VALUE(AddItemToJson((cJSON *)root, item->paramName, forkTime, - item->pid, durTime > 0 ? durTime : 0) == 0, -1); - return 0; -} - -static int SaveServiceBootEvent() -{ - if (g_bootEventEnable == 0) { - return 0; - } - time_t nowTime = time(NULL); - INIT_CHECK_RETURN_VALUE(nowTime > 0, -1); - struct tm *p = localtime(&nowTime); - INIT_CHECK_RETURN_VALUE(p != NULL, -1); - char bootEventFileName[BOOT_EVENT_FILEPATH_MAX_LEN] = ""; - INIT_CHECK_RETURN_VALUE(snprintf(bootEventFileName, BOOT_EVENT_FILEPATH_MAX_LEN, - BOOTEVENT_OUTPUT_PATH"%d%d%d-%d%d.bootevent", - 1900 + p->tm_year, p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min) >= 0, -1); // 1900 is start year - CheckAndCreatFile(bootEventFileName, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); - FILE *tmpFile = fopen(bootEventFileName, "wr"); - INIT_CHECK_RETURN_VALUE(tmpFile != NULL, -1); - cJSON *root = cJSON_CreateArray(); - if (root == NULL) { - (void)fclose(tmpFile); - return -1; - } - OH_ListTraversal(&bootEventList, (void *)root, BootEventTraversal, 0); - char *buff = cJSON_Print(root); - if (buff == NULL) { - cJSON_Delete(root); - (void)fclose(tmpFile); - return -1; - } - INIT_CHECK_ONLY_ELOG(fprintf(tmpFile, "%s\n", buff) >= 0, "save boot event file failed"); - free(buff); - cJSON_Delete(root); - (void)fflush(tmpFile); - (void)fclose(tmpFile); - return 0; -} - static int32_t g_executorId = -1; static int ParamSetBootEventHook(const HOOK_INFO *hookInfo, void *cookie) { @@ -411,10 +409,10 @@ static void SetServiceBootEventFork(SERVICE_INFO_CTX *serviceCtx) static int GetBootEventFlag(const HOOK_INFO *info, void *cookie) { - char BootEventOpen[6] = ""; // 6 is length of bool value - uint32_t len = sizeof(BootEventOpen); - SystemReadParam("persist.init.bootevent.enable", BootEventOpen, &len); - if (strcmp(BootEventOpen, "true") != 0) { + char bootEventOpen[6] = ""; // 6 is length of bool value + uint32_t len = sizeof(bootEventOpen); + SystemReadParam("persist.init.bootevent.enable", bootEventOpen, &len); + if (strcmp(bootEventOpen, "true") != 0) { g_bootEventEnable = 0; } return 0; diff --git a/services/param/watcher/agent/watcher_manager_kits.cpp b/services/param/watcher/agent/watcher_manager_kits.cpp index 47c011b6..eb4555ce 100644 --- a/services/param/watcher/agent/watcher_manager_kits.cpp +++ b/services/param/watcher/agent/watcher_manager_kits.cpp @@ -281,7 +281,7 @@ int WatcherManagerKits::ParamWatcher::DelParameterListener(ParameterChangePtr ca } void WatcherManagerKits::RemoteWatcher::OnParameterChange( - const std::string &prefix, const std::string &name, const std::string &value) + const std::string &prefix, const std::string &name, const std::string &value) { // get param watcher WatcherManagerKits::ParamWatcher *watcher = watcherManager_->GetParamWatcher(prefix); diff --git a/services/param/watcher/agent/watcher_manager_kits.h b/services/param/watcher/agent/watcher_manager_kits.h index 9450187a..2ceee594 100644 --- a/services/param/watcher/agent/watcher_manager_kits.h +++ b/services/param/watcher/agent/watcher_manager_kits.h @@ -88,7 +88,7 @@ private: class RemoteWatcher final : public Watcher { public: - RemoteWatcher(WatcherManagerKits *watcherManager) : watcherManager_(watcherManager) {} + explicit RemoteWatcher(WatcherManagerKits *watcherManager) : watcherManager_(watcherManager) {} ~RemoteWatcher(void) override {} void OnParameterChange(const std::string &prefix, const std::string &name, const std::string &value) final; diff --git a/services/param/watcher/proxy/watcher_manager.cpp b/services/param/watcher/proxy/watcher_manager.cpp index f322bd65..3a0c0c83 100644 --- a/services/param/watcher/proxy/watcher_manager.cpp +++ b/services/param/watcher/proxy/watcher_manager.cpp @@ -464,7 +464,7 @@ int WatcherManager::Dump(int fd, const std::vector& args) dprintf(fd, "%s\n", dumpInfo.c_str()); return 0; } - auto DumpParamWatcher = [this, fd](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) { + auto dumpParamWatcher = [this, fd](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) { auto remoteWatcher = GetRemoteWatcher(node->GetNodeId()); if (remoteWatcher != nullptr) { dprintf(fd, "%s%u(%u)", (index == 0) ? "Watch id list : " : ", ", @@ -482,11 +482,11 @@ int WatcherManager::Dump(int fd, const std::vector& args) } { std::lock_guard lock(watcherMutex_); - group->TraversalNode(DumpParamWatcher); + group->TraversalNode(dumpParamWatcher); } return 0; } - DumpAllGroup(fd, DumpParamWatcher); + DumpAllGroup(fd, dumpParamWatcher); return 0; } @@ -662,7 +662,7 @@ void ParamWatcherList::TraversalNode(ParamWatcherProcessor handle) uint32_t index = 0; // get first WatcherNodePtr node = WatcherNode::GetNextFromList(&nodeList_, 0); - while (node != NULL) { + while (node != nullptr) { WatcherNodePtr next = node->GetNext(&nodeList_); handle(this, node, index); node = next; @@ -675,7 +675,7 @@ void ParamWatcherList::TraversalNodeSafe(ParamWatcherProcessor processor) uint32_t index = 0; // get first WatcherNodePtr node = WatcherNode::GetNextFromList(&nodeList_, 0); - while (node != NULL) { + while (node != nullptr) { uint32_t nodeId = node->GetNodeId(); // notify free, must be free processor(this, node, index); diff --git a/services/param/watcher/proxy/watcher_manager.h b/services/param/watcher/proxy/watcher_manager.h index cc5956a0..8b8a7ef5 100644 --- a/services/param/watcher/proxy/watcher_manager.h +++ b/services/param/watcher/proxy/watcher_manager.h @@ -133,23 +133,30 @@ private: class WatcherNode { public: - WatcherNode(uint32_t nodeId) : nodeId_(nodeId) + explicit WatcherNode(uint32_t nodeId) : nodeId_(nodeId) { OH_ListInit(&node_); } virtual ~WatcherNode(void) = default; - uint32_t GetNodeId(void) const { return nodeId_; } + uint32_t GetNodeId(void) const + { + return nodeId_; + } void AddToList(ListHeadPtr list); void RemoveFromList(ListHeadPtr list); WatcherNodePtr GetNext(ListHeadPtr list); static WatcherNodePtr GetFromList(ListHeadPtr list, uint32_t nodeId); static WatcherNodePtr GetNextFromList(ListHeadPtr list, uint32_t nodeId); - static WatcherNodePtr ConvertNodeToBase(ListNodePtr node) { - WatcherNodePtr tmp = NULL; + static WatcherNodePtr ConvertNodeToBase(ListNodePtr node) + { + WatcherNodePtr tmp = nullptr; return reinterpret_cast((char *)node - (char *)tmp->GetListNode()); } - ListNodePtr GetListNode() { return &node_; } + ListNodePtr GetListNode() + { + return &node_; + } private: static int CompareNode(ListNodePtr node, ListNodePtr newNode); static int CompareData(ListNodePtr node, void *data); @@ -177,7 +184,7 @@ inline T *ConvertTo(WatcherNodePtr node) class ParamWatcher : public WatcherNode { public: - ParamWatcher(uint32_t watcherId) : WatcherNode(watcherId) {} + explicit ParamWatcher(uint32_t watcherId) : WatcherNode(watcherId) {} ~ParamWatcher(void) override {} }; @@ -193,7 +200,10 @@ public: { return nodeList_.next == &nodeList_; } - uint32_t GetNodeCount(void) const { return nodeCount_; } + uint32_t GetNodeCount(void) const + { + return nodeCount_; + } int AddNode(WatcherNodePtr node); int RemoveNode(WatcherNodePtr node); WatcherNodePtr GetNode(uint32_t nodeId); @@ -209,7 +219,7 @@ public: class RemoteWatcher : public WatcherNode, public ParamWatcherList { public: RemoteWatcher(uint32_t watcherId, const sptr &watcher) - : WatcherNode(watcherId), ParamWatcherList(), watcher_(watcher) {} + : WatcherNode(watcherId), ParamWatcherList(), watcher_(watcher) {} ~RemoteWatcher(void) override { watcher_ = nullptr; @@ -220,14 +230,26 @@ public: }); } - uint32_t GetRemoteWatcherId(void) const { return GetNodeId(); } - uint32_t GetAgentId(void) const { return id_; } - void SetAgentId(uint32_t id) { id_ = id; } + uint32_t GetRemoteWatcherId(void) const + { + return GetNodeId(); + } + uint32_t GetAgentId(void) const + { + return id_; + } + void SetAgentId(uint32_t id) + { + id_ = id; + } void ProcessParameterChange(const std::string &prefix, const std::string &name, const std::string &value) { watcher_->OnParameterChange(prefix, name, value); } - sptr GetWatcher(void) { return watcher_; } + sptr GetWatcher(void) + { + return watcher_; + } private: uint32_t id_ = { 0 }; sptr watcher_ {}; @@ -236,7 +258,7 @@ private: class WatcherGroup : public WatcherNode, public ParamWatcherList { public: WatcherGroup(uint32_t groupId, const std::string &key) - : WatcherNode(groupId), ParamWatcherList(), keyPrefix_(key) {} + : WatcherNode(groupId), ParamWatcherList(), keyPrefix_(key) {} ~WatcherGroup() override { TraversalNodeSafe([](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) { @@ -246,8 +268,14 @@ public: }); } void ProcessParameterChange(WatcherManager *mananger, const std::string &name, const std::string &value); - const std::string GetKeyPrefix() const { return keyPrefix_; } - uint32_t GetGroupId() const { return GetNodeId(); } + const std::string GetKeyPrefix() const + { + return keyPrefix_; + } + uint32_t GetGroupId() const + { + return GetNodeId(); + } private: std::string keyPrefix_ { }; }; diff --git a/test/moduletest/param_test_cmds.c b/test/moduletest/param_test_cmds.c index bf7641c0..7aab9a77 100644 --- a/test/moduletest/param_test_cmds.c +++ b/test/moduletest/param_test_cmds.c @@ -93,7 +93,7 @@ static void HandleParamChange2(const char *key, const char *value, void *context PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter"); long long commit = GetSystemCommitId(); size_t index = (size_t)context; - printf("[%zu] Receive parameter %p commit %lld change %s %s \n", index, context, commit, key, value); + printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value); static int addWatcher = 0; int ret = 0; if ((index == 4) && !addWatcher) { // 4 when context == 4 add @@ -202,14 +202,14 @@ static int32_t BShellParamCmdWatch(BShellHandle shell, int32_t argc, char *argv[ int maxCount = StringToInt(argv[CMD_INDEX], -1); // 2 cmd index if (maxCount <= 0 || maxCount > 65535) { // 65535 max count - PLUGIN_LOGE("Invalid input %s", argv[2]); + PLUGIN_LOGE("Invalid input %s", argv[CMD_INDEX]); return 0; } uint32_t buffSize = 0; char *buffer = GetLocalBuffer(&buffSize); size_t count = 0; while (count < (size_t)maxCount) { // 100 max count - int len = sprintf_s(buffer, buffSize, "%s.%d", argv[1], count); + int len = sprintf_s(buffer, buffSize, "%s.%zu", argv[1], count); PLUGIN_CHECK(len > 0, return -1, "Invalid buffer"); buffer[len] = '\0'; StartWatcher(buffer); @@ -298,11 +298,14 @@ static int32_t BShellParamCmdMemGet(BShellHandle shell, int32_t argc, char *argv int ret = sprintf_s(buff, buffSize - 1, "/proc/%s/smaps", argv[1]); PLUGIN_CHECK(ret > 0, return -1, "Failed to format path %s", argv[1]); buff[ret] = '\0'; + char *realPath = GetRealPath(buff); + PLUGIN_CHECK(realPath != NULL, return -1, "Failed to get real path"); int all = 0; if (argc > 2 && strcmp(argv[2], "all") == 0) { // 2 2 max arg all = 1; } - FILE *fp = fopen(buff, "r"); + FILE *fp = fopen(realPath, "r"); + free(realPath); int value = 0; while (fp != NULL && buff != NULL && fgets(buff, buffSize, fp) != NULL) { buff[buffSize - 1] = '\0'; diff --git a/test/moduletest/syspara_moduleTest.cpp b/test/moduletest/syspara_moduleTest.cpp index 2c14d730..e488d90f 100644 --- a/test/moduletest/syspara_moduleTest.cpp +++ b/test/moduletest/syspara_moduleTest.cpp @@ -80,7 +80,7 @@ static void SetParameterTestFunc(const char *key, const char *value) EXPECT_EQ(ret, strlen(nameGet)); EXPECT_STREQ(key, nameGet); char valueGet[PARAM_VALUE_LEN_MAX] = {0}; - ret = GetParameterValue(handle, valueGet, PARAM_VALUE_LEN_MAX); + ret = GetParameterValue(handle, valueGet, PARAM_VALUE_LEN_MAX); EXPECT_EQ(ret, strlen(valueGet)); EXPECT_STREQ(value, valueGet); EXPECT_NE(GetSystemCommitId(), 0); diff --git a/test/unittest/param/param_stub.cpp b/test/unittest/param/param_stub.cpp index 53d8cd2a..75dc929c 100644 --- a/test/unittest/param/param_stub.cpp +++ b/test/unittest/param/param_stub.cpp @@ -74,15 +74,15 @@ static const char *TestGetParamLabel(const char *paraName) return selinuxLabels[code][1]; } -static const char *forbidReadParamName[] = { +static const char *g_forbidReadParamName[] = { "ohos.servicectrl.", // "test.permission.write", }; static int TestReadParamCheck(const char *paraName) { // forbid to read ohos.servicectrl. - for (size_t i = 0; i < ARRAY_LENGTH(forbidReadParamName); i++) { - if (strncmp(paraName, forbidReadParamName[i], strlen(forbidReadParamName[i])) == 0) { + for (size_t i = 0; i < ARRAY_LENGTH(g_forbidReadParamName); i++) { + if (strncmp(paraName, g_forbidReadParamName[i], strlen(g_forbidReadParamName[i])) == 0) { return 1; } } diff --git a/test/unittest/param/watcher_agent_unittest.cpp b/test/unittest/param/watcher_agent_unittest.cpp index 4e3dec24..c39079b3 100644 --- a/test/unittest/param/watcher_agent_unittest.cpp +++ b/test/unittest/param/watcher_agent_unittest.cpp @@ -112,11 +112,14 @@ public: int TestDelWatcher() { size_t index = 1; - int ret = SystemWatchParameter("test.permission.watcher.test3.1", TestParameterChange, (void *)index); + int ret = SystemWatchParameter("test.permission.watcher.test3.1", + TestParameterChange, reinterpret_cast(index)); EXPECT_EQ(ret, 0); - ret = SystemWatchParameter("test.permission.watcher.test3.1*", TestParameterChange, (void *)index); + ret = SystemWatchParameter("test.permission.watcher.test3.1*", + TestParameterChange, reinterpret_cast(index)); EXPECT_EQ(ret, 0); - ret = SystemWatchParameter("test.permission.watcher.test3.2", TestParameterChange, (void *)index); + ret = SystemWatchParameter("test.permission.watcher.test3.2", + TestParameterChange, reinterpret_cast(index)); EXPECT_EQ(ret, 0); ret = SystemWatchParameter("test.permission.watcher.test3.1", nullptr, nullptr); EXPECT_EQ(ret, 0); -- GitLab