!77 [sync] PR-73: fix data free, plugin display, command plugin bugs
From: @openeuler-sync-bot Reviewed-by: @Lostwayzxc Signed-off-by: @Lostwayzxc
This commit is contained in:
commit
8f4ac662bb
2098
0001-remove-old-code.patch
Normal file
2098
0001-remove-old-code.patch
Normal file
File diff suppressed because it is too large
Load Diff
524
0002-add-data-free-and-fix-unsubscribe-error.patch
Normal file
524
0002-add-data-free-and-fix-unsubscribe-error.patch
Normal file
@ -0,0 +1,524 @@
|
||||
From 9fb0aa74fece1deea72ef7c8eef5e45da2113b21 Mon Sep 17 00:00:00 2001
|
||||
From: fly_1997 <flylove7@outlook.com>
|
||||
Date: Tue, 26 Nov 2024 15:59:15 +0800
|
||||
Subject: [PATCH 2/5] add data free and fix unsubscribe error
|
||||
|
||||
---
|
||||
src/common/CMakeLists.txt | 7 +
|
||||
src/common/data_register.cpp | 247 +++++++++++++++----
|
||||
src/common/data_register.h | 20 +-
|
||||
src/plugin/collect/include/command_data.h | 2 +-
|
||||
src/plugin_mgr/event/unsubscribe_handler.cpp | 7 +-
|
||||
src/plugin_mgr/instance_run_handler.cpp | 2 +-
|
||||
src/sdk/oe_client.cpp | 1 +
|
||||
7 files changed, 227 insertions(+), 59 deletions(-)
|
||||
|
||||
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
|
||||
index 2d49b3e..46084e5 100644
|
||||
--- a/src/common/CMakeLists.txt
|
||||
+++ b/src/common/CMakeLists.txt
|
||||
@@ -22,6 +22,13 @@ target_link_libraries(${PROJECT_NAME} log4cplus)
|
||||
target_link_libraries(${PROJECT_NAME} yaml-cpp)
|
||||
target_link_libraries(${PROJECT_NAME} curl boundscheck)
|
||||
|
||||
+if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
|
||||
+target_link_directories(${PROJECT_NAME} PUBLIC
|
||||
+ ${LIB_KPERF_LIBPATH}
|
||||
+)
|
||||
+target_link_libraries(${PROJECT_NAME} kperf)
|
||||
+endif()
|
||||
+
|
||||
file(COPY
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/data_list.h"
|
||||
DESTINATION "${CMAKE_BINARY_DIR}/output/include")
|
||||
diff --git a/src/common/data_register.cpp b/src/common/data_register.cpp
|
||||
index 89e660e..cdf6d97 100644
|
||||
--- a/src/common/data_register.cpp
|
||||
+++ b/src/common/data_register.cpp
|
||||
@@ -16,68 +16,107 @@
|
||||
|
||||
namespace oeaware {
|
||||
|
||||
-int TopicSerialize(const void *topic, OutStream &out)
|
||||
+void TopicFree(CTopic *topic)
|
||||
{
|
||||
- auto tmpTopic = static_cast<const CTopic*>(topic);
|
||||
- std::string instanceName(tmpTopic->instanceName);
|
||||
- std::string topicName(tmpTopic->topicName);
|
||||
- std::string params(tmpTopic->params);
|
||||
+ if (topic == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ if (topic->instanceName != nullptr) {
|
||||
+ delete[] topic->instanceName;
|
||||
+ topic->instanceName = nullptr;
|
||||
+ }
|
||||
+ if (topic->topicName != nullptr) {
|
||||
+ delete[] topic->topicName;
|
||||
+ topic->topicName = nullptr;
|
||||
+ }
|
||||
+ if (topic->params != nullptr) {
|
||||
+ delete[] topic->params;
|
||||
+ topic->params = nullptr;
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+int TopicSerialize(const CTopic *topic, OutStream &out)
|
||||
+{
|
||||
+ std::string instanceName(topic->instanceName);
|
||||
+ std::string topicName(topic->topicName);
|
||||
+ std::string params(topic->params);
|
||||
out << instanceName << topicName << params;
|
||||
return 0;
|
||||
}
|
||||
|
||||
-int TopicDeserialize(void *topic, InStream &in)
|
||||
+int TopicDeserialize(CTopic *topic, InStream &in)
|
||||
{
|
||||
std::string instanceName;
|
||||
std::string topicName;
|
||||
std::string params;
|
||||
in >> instanceName >> topicName >> params;
|
||||
- ((CTopic*)topic)->instanceName = new char[instanceName.size() + 1];
|
||||
- ((CTopic*)topic)->topicName = new char[topicName.size() + 1];
|
||||
- ((CTopic*)topic)->params = new char[params.size() + 1];
|
||||
+ topic->instanceName = new char[instanceName.size() + 1];
|
||||
+ topic->topicName = new char[topicName.size() + 1];
|
||||
+ topic->params = new char[params.size() + 1];
|
||||
|
||||
- auto ret = strcpy_s(((CTopic*)topic)->instanceName, instanceName.size() + 1, instanceName.data());
|
||||
+ auto ret = strcpy_s(topic->instanceName, instanceName.size() + 1, instanceName.data());
|
||||
if (ret != EOK) return ret;
|
||||
- ret = strcpy_s(((CTopic*)topic)->topicName, topicName.size() + 1, topicName.data());
|
||||
+ ret = strcpy_s(topic->topicName, topicName.size() + 1, topicName.data());
|
||||
if (ret != EOK) return ret;
|
||||
- ret = strcpy_s(((CTopic*)topic)->params, params.size() + 1, params.data());
|
||||
+ ret = strcpy_s(topic->params, params.size() + 1, params.data());
|
||||
if (ret != EOK) return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
-int DataListSerialize(const void *dataList, OutStream &out)
|
||||
+void DataListFree(DataList *dataList)
|
||||
+{
|
||||
+ if (dataList == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ auto ® = Register::GetInstance();
|
||||
+ DataFreeFunc free = reg.GetDataFreeFunc(Concat({dataList->topic.instanceName, dataList->topic.topicName}, "::"));
|
||||
+ if (free == nullptr) {
|
||||
+ free = reg.GetDataFreeFunc(dataList->topic.instanceName);
|
||||
+ }
|
||||
+ if (free != nullptr) {
|
||||
+ for (uint64_t i = 0; i < dataList->len; ++i) {
|
||||
+ free(dataList->data[i]);
|
||||
+ }
|
||||
+ }
|
||||
+ TopicFree(&dataList->topic);
|
||||
+ if (dataList->data != nullptr) {
|
||||
+ delete[] dataList->data;
|
||||
+ dataList->data = nullptr;
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+int DataListSerialize(const DataList *dataList, OutStream &out)
|
||||
{
|
||||
- auto tmpList = static_cast<const DataList*>(dataList);
|
||||
- TopicSerialize(&tmpList->topic, out);
|
||||
- out << tmpList->len;
|
||||
+ TopicSerialize(&dataList->topic, out);
|
||||
+ out << dataList->len;
|
||||
auto ® = Register::GetInstance();
|
||||
- auto func = reg.GetDataSerialize(Concat({tmpList->topic.instanceName, tmpList->topic.topicName}, "::"));
|
||||
+ auto func = reg.GetDataSerialize(Concat({dataList->topic.instanceName, dataList->topic.topicName}, "::"));
|
||||
if (func == nullptr) {
|
||||
- func = reg.GetDataSerialize(tmpList->topic.instanceName);
|
||||
+ func = reg.GetDataSerialize(dataList->topic.instanceName);
|
||||
}
|
||||
- for (uint64_t i = 0; i < tmpList->len; ++i) {
|
||||
- func(tmpList->data[i], out);
|
||||
+ for (uint64_t i = 0; i < dataList->len; ++i) {
|
||||
+ func(dataList->data[i], out);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
-int DataListDeserialize(void *dataList, InStream &in)
|
||||
+int DataListDeserialize(DataList *dataList, InStream &in)
|
||||
{
|
||||
CTopic topic;
|
||||
TopicDeserialize(&topic, in);
|
||||
uint64_t size;
|
||||
in >> size;
|
||||
- ((DataList*)dataList)->topic = topic;
|
||||
- ((DataList*)dataList)->len = size;
|
||||
- ((DataList*)dataList)->data = new void* [size];
|
||||
+ dataList->topic = topic;
|
||||
+ dataList->len = size;
|
||||
+ dataList->data = new void* [size];
|
||||
auto ® = Register::GetInstance();
|
||||
auto func = reg.GetDataDeserialize(Concat({topic.instanceName, topic.topicName}, "::"));
|
||||
if (func == nullptr) {
|
||||
func = reg.GetDataDeserialize(topic.instanceName);
|
||||
}
|
||||
for (uint64_t i = 0; i < size; ++i) {
|
||||
- ((DataList*)dataList)->data[i] = nullptr;
|
||||
- auto ret = func(&(((DataList*)dataList)->data[i]), in);
|
||||
+ dataList->data[i] = nullptr;
|
||||
+ auto ret = func(&(dataList->data[i]), in);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
@@ -85,6 +124,16 @@ int DataListDeserialize(void *dataList, InStream &in)
|
||||
return 0;
|
||||
}
|
||||
|
||||
+void ResultFree(Result *result)
|
||||
+{
|
||||
+ if (result == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ if (result->payload != nullptr) {
|
||||
+ delete[] result->payload;
|
||||
+ result->payload = nullptr;
|
||||
+ }
|
||||
+}
|
||||
|
||||
int ResultDeserialize(void *data, InStream &in)
|
||||
{
|
||||
@@ -96,6 +145,17 @@ int ResultDeserialize(void *data, InStream &in)
|
||||
return ret;
|
||||
}
|
||||
#if defined(__arm__) || defined(__aarch64__)
|
||||
+void PmuBaseDataFree(void *data)
|
||||
+{
|
||||
+ auto tmpData = static_cast<PmuCountingData*>(data);
|
||||
+ if (tmpData == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ PmuDataFree(tmpData->pmuData);
|
||||
+ tmpData->pmuData = nullptr;
|
||||
+ tmpData->len = 0;
|
||||
+}
|
||||
+
|
||||
int PmuCountingDataSerialize(const void *data, OutStream &out)
|
||||
{
|
||||
auto tmpData = static_cast<const PmuCountingData*>(data);
|
||||
@@ -456,6 +516,19 @@ int PmuUncoreDataDeserialize(void **data, InStream &in)
|
||||
}
|
||||
#endif
|
||||
|
||||
+void ThreadInfoFree(void *data)
|
||||
+{
|
||||
+ auto threadInfo = static_cast<ThreadInfo*>(data);
|
||||
+ if (threadInfo == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ if (threadInfo->name != nullptr) {
|
||||
+ delete[] threadInfo->name;
|
||||
+ threadInfo->name = nullptr;
|
||||
+ }
|
||||
+ delete threadInfo;
|
||||
+}
|
||||
+
|
||||
int ThreadInfoSerialize(const void *data, OutStream &out)
|
||||
{
|
||||
auto threadInfo = static_cast<const ThreadInfo*>(data);
|
||||
@@ -476,6 +549,32 @@ int ThreadInfoDeserialize(void **data, InStream &in)
|
||||
return 0;
|
||||
}
|
||||
|
||||
+void KernelDataFree(void *data)
|
||||
+{
|
||||
+ auto tmpData = static_cast<const KernelData*>(data);
|
||||
+ if (tmpData == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ KernelDataNode *node = tmpData->kernelData;
|
||||
+ for (int i = 0; i < tmpData->len; ++i) {
|
||||
+ auto tmp = node->next;
|
||||
+ if (node == nullptr) {
|
||||
+ break;
|
||||
+ }
|
||||
+ if (node->key != nullptr) {
|
||||
+ delete[] node->key;
|
||||
+ node->key = nullptr;
|
||||
+ }
|
||||
+ if (node->value != nullptr) {
|
||||
+ delete[] node->value;
|
||||
+ node->value = nullptr;
|
||||
+ }
|
||||
+ delete node;
|
||||
+ node = tmp;
|
||||
+ }
|
||||
+ delete tmpData;
|
||||
+}
|
||||
+
|
||||
int KernelDataSerialize(const void *data, OutStream &out)
|
||||
{
|
||||
auto tmpData = static_cast<const KernelData*>(data);
|
||||
@@ -523,17 +622,47 @@ int KernelDataDeserialize(void **data, InStream &in)
|
||||
return 0;
|
||||
}
|
||||
|
||||
+void CommandDataFree(void *data)
|
||||
+{
|
||||
+ CommandData *commandData = (CommandData*)data;
|
||||
+ if (commandData == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ for (int i = 0; i < commandData->attrLen; ++i) {
|
||||
+ if (commandData->itemAttr[i] != nullptr) {
|
||||
+ delete[] commandData->itemAttr[i];
|
||||
+ commandData->itemAttr[i] = nullptr;
|
||||
+ }
|
||||
+ }
|
||||
+ if (commandData->items == nullptr) {
|
||||
+ delete commandData;
|
||||
+ return;
|
||||
+ }
|
||||
+ for (int i = 0; i < commandData->itemLen; ++i) {
|
||||
+ for (int j = 0; j < commandData->attrLen; ++j) {
|
||||
+ if (commandData->items[i].value[j] != nullptr) {
|
||||
+ delete[] commandData->items[i].value[j];
|
||||
+ commandData->items[i].value[j] = nullptr;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ delete[] commandData->items;
|
||||
+ commandData->items = nullptr;
|
||||
+
|
||||
+ delete commandData;
|
||||
+}
|
||||
+
|
||||
int CommandDataSerialize(const void *data, OutStream &out)
|
||||
{
|
||||
- auto sarData = (SarData*)data;
|
||||
- out << sarData->attrLen << sarData->itemLen;
|
||||
- for (int i = 0; i < sarData->attrLen; ++i) {
|
||||
- std::string attr(sarData->itemAttr[i]);
|
||||
+ auto commandData = (CommandData*)data;
|
||||
+ out << commandData->attrLen << commandData->itemLen;
|
||||
+ for (int i = 0; i < commandData->attrLen; ++i) {
|
||||
+ std::string attr(commandData->itemAttr[i]);
|
||||
out << attr;
|
||||
}
|
||||
- for (int i = 0; i < sarData->itemLen; ++i) {
|
||||
- for (int j = 0; j < sarData->attrLen; ++j) {
|
||||
- std::string item(sarData->items[i].value[j]);
|
||||
+ for (int i = 0; i < commandData->itemLen; ++i) {
|
||||
+ for (int j = 0; j < commandData->attrLen; ++j) {
|
||||
+ std::string item(commandData->items[i].value[j]);
|
||||
out << item;
|
||||
}
|
||||
}
|
||||
@@ -542,8 +671,8 @@ int CommandDataSerialize(const void *data, OutStream &out)
|
||||
|
||||
int CommandDataDeserialize(void **data, InStream &in)
|
||||
{
|
||||
- *data = new SarData();
|
||||
- auto sarData = static_cast<SarData*>(*data);
|
||||
+ *data = new CommandData();
|
||||
+ auto sarData = static_cast<CommandData*>(*data);
|
||||
in >> sarData->attrLen >> sarData->itemLen;
|
||||
int ret;
|
||||
for (int i = 0; i < sarData->attrLen; ++i) {
|
||||
@@ -570,6 +699,15 @@ int CommandDataDeserialize(void **data, InStream &in)
|
||||
return 0;
|
||||
}
|
||||
|
||||
+void AnalysisDataFree(void *data)
|
||||
+{
|
||||
+ auto analysisData = static_cast<AdaptData*>(data);
|
||||
+ if (analysisData == nullptr) {
|
||||
+ return;
|
||||
+ }
|
||||
+ delete analysisData;
|
||||
+}
|
||||
+
|
||||
int AnalysisDataSerialize(const void *data, OutStream &out)
|
||||
{
|
||||
auto analysisData = static_cast<const AdaptData*>(data);
|
||||
@@ -600,44 +738,55 @@ int AnalysisDataDeserialize(void **data, InStream &in)
|
||||
|
||||
void Register::RegisterData(const std::string &name, const RegisterEntry &entry)
|
||||
{
|
||||
- deserializeFuncs[name] = entry;
|
||||
+ registerEntry[name] = entry;
|
||||
}
|
||||
|
||||
void Register::InitRegisterData()
|
||||
{
|
||||
#if defined(__arm__) || defined(__aarch64__)
|
||||
- RegisterData("pmu_counting_collector", RegisterEntry(PmuCountingDataSerialize, PmuCountingDataDeserialize));
|
||||
+ RegisterData("pmu_counting_collector", RegisterEntry(PmuCountingDataSerialize, PmuCountingDataDeserialize,
|
||||
+ PmuBaseDataFree));
|
||||
|
||||
- RegisterData("pmu_sampling_collector", RegisterEntry(PmuSamplingDataSerialize, PmuSamplingDataDeserialize));
|
||||
+ RegisterData("pmu_sampling_collector", RegisterEntry(PmuSamplingDataSerialize, PmuSamplingDataDeserialize,
|
||||
+ PmuBaseDataFree));
|
||||
|
||||
- RegisterData("pmu_spe_collector", RegisterEntry(PmuSpeDataSerialize, PmuSpeDataDeserialize));
|
||||
+ RegisterData("pmu_spe_collector", RegisterEntry(PmuSpeDataSerialize, PmuSpeDataDeserialize, PmuBaseDataFree));
|
||||
|
||||
- RegisterData("pmu_uncore_collector", RegisterEntry(PmuUncoreDataSerialize, PmuUncoreDataDeserialize));
|
||||
+ RegisterData("pmu_uncore_collector", RegisterEntry(PmuUncoreDataSerialize, PmuUncoreDataDeserialize,
|
||||
+ PmuBaseDataFree));
|
||||
#endif
|
||||
- RegisterData("thread_collector", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize));
|
||||
+ RegisterData("thread_collector", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize, ThreadInfoFree));
|
||||
+
|
||||
+ RegisterData("kernel_config", RegisterEntry(KernelDataSerialize, KernelDataDeserialize, KernelDataFree));
|
||||
|
||||
- RegisterData("kernel_config", RegisterEntry(KernelDataSerialize, KernelDataDeserialize));
|
||||
+ RegisterData("thread_scenario", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize, ThreadInfoFree));
|
||||
|
||||
- RegisterData("thread_scenario", RegisterEntry(ThreadInfoSerialize, ThreadInfoDeserialize));
|
||||
+ RegisterData("command_collector", RegisterEntry(CommandDataSerialize, CommandDataDeserialize, CommandDataFree));
|
||||
|
||||
- RegisterData("command_collector", RegisterEntry(CommandDataSerialize, CommandDataDeserialize));
|
||||
- RegisterData("command_collector", RegisterEntry(CommandDataSerialize, CommandDataDeserialize));
|
||||
RegisterData("analysis_aware", RegisterEntry(AnalysisDataSerialize, AnalysisDataDeserialize));
|
||||
}
|
||||
|
||||
SerializeFunc Register::GetDataSerialize(const std::string &name)
|
||||
{
|
||||
- if (!deserializeFuncs.count(name)) {
|
||||
+ if (!registerEntry.count(name)) {
|
||||
return nullptr;
|
||||
}
|
||||
- return deserializeFuncs[name].se;
|
||||
+ return registerEntry[name].se;
|
||||
}
|
||||
|
||||
DeserializeFunc Register::GetDataDeserialize(const std::string &name)
|
||||
{
|
||||
- if (!deserializeFuncs.count(name)) {
|
||||
+ if (!registerEntry.count(name)) {
|
||||
+ return nullptr;
|
||||
+ }
|
||||
+ return registerEntry[name].de;
|
||||
+}
|
||||
+
|
||||
+DataFreeFunc Register::GetDataFreeFunc(const std::string &name)
|
||||
+{
|
||||
+ if (!registerEntry.count(name)) {
|
||||
return nullptr;
|
||||
}
|
||||
- return deserializeFuncs[name].de;
|
||||
+ return registerEntry[name].free;
|
||||
}
|
||||
}
|
||||
diff --git a/src/common/data_register.h b/src/common/data_register.h
|
||||
index 5e1ae4a..b72cd97 100644
|
||||
--- a/src/common/data_register.h
|
||||
+++ b/src/common/data_register.h
|
||||
@@ -13,18 +13,21 @@
|
||||
#define COMMON_DATA_REGISTER_H
|
||||
#include <unordered_map>
|
||||
#include "serialize.h"
|
||||
+#include "data_list.h"
|
||||
|
||||
namespace oeaware {
|
||||
using DeserializeFunc = int(*)(void**, InStream &in);
|
||||
using SerializeFunc = int(*)(const void*, OutStream &out);
|
||||
-using FreeData = void(*)(void *);
|
||||
+using DataFreeFunc = void(*)(void *);
|
||||
|
||||
struct RegisterEntry {
|
||||
RegisterEntry() { }
|
||||
RegisterEntry(const SerializeFunc &se, const DeserializeFunc &de) : se(se), de(de) { }
|
||||
+ RegisterEntry(const SerializeFunc &se, const DeserializeFunc &de, const DataFreeFunc &free) : se(se),
|
||||
+ de(de), free(free) { }
|
||||
SerializeFunc se;
|
||||
DeserializeFunc de;
|
||||
- FreeData free;
|
||||
+ DataFreeFunc free;
|
||||
};
|
||||
|
||||
class Register {
|
||||
@@ -40,17 +43,20 @@ public:
|
||||
void InitRegisterData();
|
||||
DeserializeFunc GetDataDeserialize(const std::string &name);
|
||||
SerializeFunc GetDataSerialize(const std::string &name);
|
||||
+ DataFreeFunc GetDataFreeFunc(const std::string &name);
|
||||
void RegisterData(const std::string &name, const RegisterEntry &func);
|
||||
private:
|
||||
Register() { };
|
||||
|
||||
- std::unordered_map<std::string, RegisterEntry> deserializeFuncs;
|
||||
+ std::unordered_map<std::string, RegisterEntry> registerEntry;
|
||||
};
|
||||
-
|
||||
-int DataListSerialize(const void *data, OutStream &out);
|
||||
-int DataListDeserialize(void *data, InStream &in);
|
||||
+void DataListFree(DataList *dataList);
|
||||
+int DataListSerialize(const DataList *dataList, OutStream &out);
|
||||
+int DataListDeserialize(DataList *dataList, InStream &in);
|
||||
int ResultDeserialize(void *data, InStream &in);
|
||||
-int TopicSerialize(const void *topic, OutStream &out);
|
||||
+int TopicSerialize(const CTopic *topic, OutStream &out);
|
||||
+int TopicDeserialize(CTopic *topic, InStream &in);
|
||||
+void TopicFree(CTopic *topic);
|
||||
}
|
||||
|
||||
#endif
|
||||
diff --git a/src/plugin/collect/include/command_data.h b/src/plugin/collect/include/command_data.h
|
||||
index f466bd3..e7a8540 100644
|
||||
--- a/src/plugin/collect/include/command_data.h
|
||||
+++ b/src/plugin/collect/include/command_data.h
|
||||
@@ -26,7 +26,7 @@ typedef struct {
|
||||
int attrLen;
|
||||
char *itemAttr[ATTR_MAX_LENGTH];
|
||||
CommandIter *items;
|
||||
-} SarData, CommandData;
|
||||
+} CommandData;
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
diff --git a/src/plugin_mgr/event/unsubscribe_handler.cpp b/src/plugin_mgr/event/unsubscribe_handler.cpp
|
||||
index 72b53bf..749a683 100644
|
||||
--- a/src/plugin_mgr/event/unsubscribe_handler.cpp
|
||||
+++ b/src/plugin_mgr/event/unsubscribe_handler.cpp
|
||||
@@ -23,7 +23,12 @@ EventResult UnsubscribeHandler::Handle(const Event &event)
|
||||
INFO(logger, "sdk " << event.payload[0] << " disconnected and has been unsubscribed related topics.");
|
||||
return eventResult;
|
||||
}
|
||||
- auto msg = std::make_shared<InstanceRunMessage>(RunType::UNSUBSCRIBE, event.payload);
|
||||
+ CTopic cTopic;
|
||||
+ InStream in(event.payload[0]);
|
||||
+ TopicDeserialize(&cTopic, in);
|
||||
+ Topic topic{cTopic.instanceName, cTopic.topicName, cTopic.params};
|
||||
+ auto msg = std::make_shared<InstanceRunMessage>(RunType::UNSUBSCRIBE,
|
||||
+ std::vector<std::string>{topic.GetType(), event.payload[1]});
|
||||
instanceRunHandler->RecvQueuePush(msg);
|
||||
msg->Wait();
|
||||
result = msg->result;
|
||||
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
|
||||
index 2e11d0d..30dc886 100644
|
||||
--- a/src/plugin_mgr/instance_run_handler.cpp
|
||||
+++ b/src/plugin_mgr/instance_run_handler.cpp
|
||||
@@ -181,7 +181,7 @@ void InstanceRunHandler::PublishData(std::shared_ptr<InstanceRunMessage> &msg)
|
||||
auto instance = memoryStore->GetInstance(subscriber);
|
||||
instance->interface->UpdateData(msg->dataList);
|
||||
}
|
||||
- // free dataList
|
||||
+ DataListFree(&msg->dataList);
|
||||
}
|
||||
|
||||
bool InstanceRunHandler::HandleMessage()
|
||||
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
|
||||
index 4eb04ae..9452a37 100644
|
||||
--- a/src/sdk/oe_client.cpp
|
||||
+++ b/src/sdk/oe_client.cpp
|
||||
@@ -70,6 +70,7 @@ void Impl::HandleRecv()
|
||||
for (auto handle : topicHandle[key]) {
|
||||
handle(&dataList);
|
||||
}
|
||||
+ DataListFree(&dataList);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
--
|
||||
2.33.0
|
||||
|
||||
412
0003-fix-failed-to-connect-to-the-sdk-and-command-executi.patch
Normal file
412
0003-fix-failed-to-connect-to-the-sdk-and-command-executi.patch
Normal file
@ -0,0 +1,412 @@
|
||||
From 51355e66be3eeec6ebe79faa88324c788e5a3829 Mon Sep 17 00:00:00 2001
|
||||
From: fly_1997 <flylove7@outlook.com>
|
||||
Date: Wed, 27 Nov 2024 05:13:26 +0800
|
||||
Subject: [PATCH 3/5] fix failed to connect to the sdk and command execution is
|
||||
stuck
|
||||
|
||||
---
|
||||
src/common/utils.cpp | 16 ++++
|
||||
src/common/utils.h | 1 +
|
||||
.../collect/system/command/command_base.cpp | 48 +++++++++++-
|
||||
.../collect/system/command/command_base.h | 9 +++
|
||||
.../system/command/command_collector.cpp | 9 ++-
|
||||
src/plugin/collect/system/kernel_config.cpp | 1 +
|
||||
src/plugin_mgr/config.cpp | 17 -----
|
||||
src/plugin_mgr/config.h | 1 -
|
||||
src/plugin_mgr/message_manager.cpp | 76 +++++++++++++++++--
|
||||
src/plugin_mgr/message_manager.h | 3 +-
|
||||
src/sdk/oe_client.cpp | 12 ++-
|
||||
11 files changed, 160 insertions(+), 33 deletions(-)
|
||||
|
||||
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
|
||||
index 4e12a57..a300a6a 100644
|
||||
--- a/src/common/utils.cpp
|
||||
+++ b/src/common/utils.cpp
|
||||
@@ -156,4 +156,20 @@ std::vector<std::string> SplitString(const std::string &str, const std::string &
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
+bool CreateDir(const std::string &path)
|
||||
+{
|
||||
+ size_t pos = 0;
|
||||
+ do {
|
||||
+ pos = path.find_first_of("/", pos + 1);
|
||||
+ std::string subPath = path.substr(0, pos);
|
||||
+ struct stat buffer;
|
||||
+ if (stat(subPath.c_str(), &buffer) == 0) {
|
||||
+ continue;
|
||||
+ }
|
||||
+ if (mkdir(subPath.c_str(), S_IRWXU | S_IRWXG) != 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ } while (pos != std::string::npos);
|
||||
+ return true;
|
||||
+}
|
||||
}
|
||||
diff --git a/src/common/utils.h b/src/common/utils.h
|
||||
index ba787fe..48b72bd 100644
|
||||
--- a/src/common/utils.h
|
||||
+++ b/src/common/utils.h
|
||||
@@ -28,6 +28,7 @@ bool EndWith(const std::string &s, const std::string &ending);
|
||||
std::string Concat(const std::vector<std::string>& strings, const std::string &split);
|
||||
// Separate "str" with the separator "split"
|
||||
std::vector<std::string> SplitString(const std::string &str, const std::string &split);
|
||||
+bool CreateDir(const std::string &path);
|
||||
}
|
||||
|
||||
#endif // !COMMON_UTILS_H
|
||||
\ No newline at end of file
|
||||
diff --git a/src/plugin/collect/system/command/command_base.cpp b/src/plugin/collect/system/command/command_base.cpp
|
||||
index d5a30dd..bf658b8 100644
|
||||
--- a/src/plugin/collect/system/command/command_base.cpp
|
||||
+++ b/src/plugin/collect/system/command/command_base.cpp
|
||||
@@ -10,6 +10,45 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
******************************************************************************/
|
||||
#include "command_base.h"
|
||||
+#include <unistd.h>
|
||||
+#include <sys/wait.h>
|
||||
+
|
||||
+int PopenProcess::Pclose()
|
||||
+{
|
||||
+ if (fclose(stream) == EOF) {
|
||||
+ return -1;
|
||||
+ }
|
||||
+ stream = nullptr;
|
||||
+ if (kill(pid, SIGTERM) == -1) {
|
||||
+ return -1;
|
||||
+ }
|
||||
+ return 0;
|
||||
+}
|
||||
+
|
||||
+void PopenProcess::Popen(const std::string &cmd)
|
||||
+{
|
||||
+ int pipeFd[2];
|
||||
+ if (pipe(pipeFd) == -1) {
|
||||
+ return;
|
||||
+ }
|
||||
+ pid = fork();
|
||||
+ if (pid == -1) {
|
||||
+ close(pipeFd[0]);
|
||||
+ close(pipeFd[1]);
|
||||
+ } else if (pid == 0) {
|
||||
+ close(pipeFd[0]);
|
||||
+ dup2(pipeFd[1], STDOUT_FILENO);
|
||||
+ close(pipeFd[1]);
|
||||
+ execl("/bin/bash", "bash", "-c", cmd.data(), nullptr);
|
||||
+ _exit(1);
|
||||
+ }
|
||||
+ close(pipeFd[1]);
|
||||
+ stream = fdopen(pipeFd[0], "r");
|
||||
+ if (!stream) {
|
||||
+ close(pipeFd[0]);
|
||||
+ return;
|
||||
+ }
|
||||
+}
|
||||
|
||||
CommandBase::CommandBase()
|
||||
{
|
||||
@@ -23,18 +62,19 @@ CommandBase::CommandBase()
|
||||
bool CommandBase::ValidateArgs(const oeaware::Topic& topic)
|
||||
{
|
||||
auto cmd = GetCommand(topic);
|
||||
- FILE *pipe = popen(cmd.c_str(), "r");
|
||||
- if (!pipe) {
|
||||
+ PopenProcess p;
|
||||
+ p.Popen(cmd);
|
||||
+ if (!p.stream) {
|
||||
return false;
|
||||
}
|
||||
char buffer[128];
|
||||
bool isValid = false;
|
||||
- if (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
|
||||
+ if (fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
|
||||
if (strstr(buffer, "Linux") != nullptr || strstr(buffer, "procs") != nullptr) {
|
||||
isValid = true;
|
||||
}
|
||||
}
|
||||
- pclose(pipe);
|
||||
+ p.Pclose();
|
||||
return isValid;
|
||||
}
|
||||
|
||||
diff --git a/src/plugin/collect/system/command/command_base.h b/src/plugin/collect/system/command/command_base.h
|
||||
index eb75c49..a6d7627 100644
|
||||
--- a/src/plugin/collect/system/command/command_base.h
|
||||
+++ b/src/plugin/collect/system/command/command_base.h
|
||||
@@ -44,4 +44,13 @@ public:
|
||||
void Close();
|
||||
};
|
||||
|
||||
+class PopenProcess {
|
||||
+public:
|
||||
+ int Pclose();
|
||||
+ void Popen(const std::string &cmd);
|
||||
+
|
||||
+ FILE *stream;
|
||||
+ pid_t pid;
|
||||
+};
|
||||
+
|
||||
#endif
|
||||
diff --git a/src/plugin/collect/system/command/command_collector.cpp b/src/plugin/collect/system/command/command_collector.cpp
|
||||
index 6c54555..640a406 100644
|
||||
--- a/src/plugin/collect/system/command/command_collector.cpp
|
||||
+++ b/src/plugin/collect/system/command/command_collector.cpp
|
||||
@@ -32,15 +32,16 @@ CommandCollector::CommandCollector(): oeaware::Interface()
|
||||
void CommandCollector::CollectThread(const oeaware::Topic &topic, CommandBase* collector)
|
||||
{
|
||||
std::string cmd = collector->GetCommand(topic);
|
||||
- FILE* pipe = popen(cmd.c_str(), "r");
|
||||
- if (!pipe) {
|
||||
+ PopenProcess p;
|
||||
+ p.Popen(cmd);
|
||||
+ if (!p.stream) {
|
||||
return;
|
||||
}
|
||||
char buffer[256];
|
||||
- while (collector->isRunning && fgets(buffer, sizeof(buffer), pipe) != nullptr) {
|
||||
+ while (collector->isRunning && fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
|
||||
collector->ParseLine(std::string(buffer));
|
||||
}
|
||||
- pclose(pipe);
|
||||
+ p.Pclose();
|
||||
|
||||
collector->Close();
|
||||
}
|
||||
diff --git a/src/plugin/collect/system/kernel_config.cpp b/src/plugin/collect/system/kernel_config.cpp
|
||||
index 63aafea..6bdfc8a 100644
|
||||
--- a/src/plugin/collect/system/kernel_config.cpp
|
||||
+++ b/src/plugin/collect/system/kernel_config.cpp
|
||||
@@ -113,6 +113,7 @@ static bool IsSymlink(const std::string &path)
|
||||
}
|
||||
return S_ISLNK(st.st_mode);
|
||||
}
|
||||
+
|
||||
void KernelConfig::GetAllEth()
|
||||
{
|
||||
const std::string path = "/sys/class/net";
|
||||
diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp
|
||||
index 341880b..8cd1432 100644
|
||||
--- a/src/plugin_mgr/config.cpp
|
||||
+++ b/src/plugin_mgr/config.cpp
|
||||
@@ -15,23 +15,6 @@
|
||||
#include "default_path.h"
|
||||
|
||||
namespace oeaware {
|
||||
-bool CreateDir(const std::string &path)
|
||||
-{
|
||||
- size_t pos = 0;
|
||||
- do {
|
||||
- pos = path.find_first_of("/", pos + 1);
|
||||
- std::string subPath = path.substr(0, pos);
|
||||
- struct stat buffer;
|
||||
- if (stat(subPath.c_str(), &buffer) == 0) {
|
||||
- continue;
|
||||
- }
|
||||
- if (mkdir(subPath.c_str(), S_IRWXU | S_IRWXG) != 0) {
|
||||
- return false;
|
||||
- }
|
||||
- } while (pos != std::string::npos);
|
||||
- return true;
|
||||
-}
|
||||
-
|
||||
bool CheckPluginList(YAML::Node pluginListItem)
|
||||
{
|
||||
if (pluginListItem["name"].IsNull()) {
|
||||
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
|
||||
index 640ab00..19e6d33 100644
|
||||
--- a/src/plugin_mgr/config.h
|
||||
+++ b/src/plugin_mgr/config.h
|
||||
@@ -130,7 +130,6 @@ private:
|
||||
};
|
||||
|
||||
std::string GetPath();
|
||||
-bool CreateDir(const std::string &path);
|
||||
}
|
||||
|
||||
#endif
|
||||
diff --git a/src/plugin_mgr/message_manager.cpp b/src/plugin_mgr/message_manager.cpp
|
||||
index 1ffa4cc..bfa5b0c 100644
|
||||
--- a/src/plugin_mgr/message_manager.cpp
|
||||
+++ b/src/plugin_mgr/message_manager.cpp
|
||||
@@ -11,6 +11,7 @@
|
||||
******************************************************************************/
|
||||
#include "message_manager.h"
|
||||
#include <thread>
|
||||
+#include <pwd.h>
|
||||
#include <securec.h>
|
||||
#include "default_path.h"
|
||||
#include "utils.h"
|
||||
@@ -41,15 +42,56 @@ int Epoll::EventWait(struct epoll_event *events, int maxEvents, int timeout)
|
||||
return epoll_wait(epfd, events, maxEvents, timeout);
|
||||
}
|
||||
|
||||
+static std::vector<std::string> GetUserFromGroup(const std::string &groupName)
|
||||
+{
|
||||
+ std::vector<std::string> users;
|
||||
+ std::ifstream file("/etc/group");
|
||||
+ if (!file.is_open()) {
|
||||
+ return users;
|
||||
+ }
|
||||
+ std::string line;
|
||||
+ size_t userPartIndex = 3;
|
||||
+ while (std::getline(file, line)) {
|
||||
+ std::vector<std::string> parts = SplitString(line, ":");
|
||||
+ if (parts.size() > userPartIndex && parts[0] == groupName) {
|
||||
+ std::vector<std::string> userParts = SplitString(parts[userPartIndex], ",");
|
||||
+ users.insert(users.end(), userParts.begin(), userParts.end());
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+ file.close();
|
||||
+ return users;
|
||||
+}
|
||||
+
|
||||
+static int GetUid(const std::string &name)
|
||||
+{
|
||||
+ struct passwd pwd;
|
||||
+ struct passwd *result;
|
||||
+ char buf[1024];
|
||||
+ int res = getpwnam_r(name.c_str(), &pwd, buf, sizeof(buf), &result);
|
||||
+ if (res != 0 || result == nullptr) {
|
||||
+ return -1;
|
||||
+ }
|
||||
+ return pwd.pw_uid;
|
||||
+}
|
||||
+
|
||||
void TcpSocket::InitGroups()
|
||||
{
|
||||
std::vector<std::string> groupNames{"oeaware", "root"};
|
||||
+ groups[0].emplace_back(0);
|
||||
for (auto &groupName : groupNames) {
|
||||
auto gid = GetGidByGroupName(groupName);
|
||||
if (gid < 0) {
|
||||
continue;
|
||||
}
|
||||
- groups.emplace_back(gid);
|
||||
+ auto users = GetUserFromGroup(groupName);
|
||||
+ for (auto &user : users) {
|
||||
+ auto uid = GetUid(user);
|
||||
+ if (uid < 0) {
|
||||
+ continue;
|
||||
+ }
|
||||
+ groups[gid].emplace_back(uid);
|
||||
+ }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +110,11 @@ bool TcpSocket::StartListen()
|
||||
ERROR(logger, path << " chmod error!");
|
||||
return false;
|
||||
}
|
||||
+ std::string cmd = "setfacl -m g:oeaware:rw " + path;
|
||||
+ auto ret = system(cmd.c_str());
|
||||
+ if (ret) {
|
||||
+ WARN(logger, "failed to set the communication permission of the oeaware user group.");
|
||||
+ }
|
||||
if (domainSocket->Listen() < 0) {
|
||||
ERROR(logger, "listen error!");
|
||||
return false;
|
||||
@@ -136,7 +183,6 @@ static void GetEventResult(Message &msg, EventResultQueue sendMessage)
|
||||
}
|
||||
|
||||
const int DISCONNECTED = -1;
|
||||
-const int DISCONNECTED_AND_UNSUBCRIBE = -2;
|
||||
|
||||
void TcpMessageHandler::Init(EventQueue newRecvMessage, EventResultQueue newSendMessage, EventQueue newRecvData)
|
||||
{
|
||||
@@ -240,6 +286,23 @@ void TcpMessageHandler::Start()
|
||||
}
|
||||
}
|
||||
|
||||
+bool TcpSocket::CheckFileGroups(const std::string &path)
|
||||
+{
|
||||
+ struct stat st;
|
||||
+ if (lstat(path.c_str(), &st) < 0) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ for (auto &p : groups) {
|
||||
+ bool ok = std::any_of(p.second.begin(), p.second.end(), [&](uid_t uid) {
|
||||
+ return uid == st.st_uid;
|
||||
+ });
|
||||
+ if (ok) {
|
||||
+ return true;
|
||||
+ }
|
||||
+ }
|
||||
+ return false;
|
||||
+}
|
||||
+
|
||||
void TcpSocket::SaveConnection()
|
||||
{
|
||||
struct sockaddr_un un;
|
||||
@@ -255,12 +318,12 @@ void TcpSocket::SaveConnection()
|
||||
memcpy_s(name, maxNameLength, un.sun_path, len);
|
||||
name[len] = 0;
|
||||
bool isSdk = false;
|
||||
- if (strcmp(name, DEFAULT_SDK_CONN_PATH.c_str()) == 0) {
|
||||
+ if (len > 0) {
|
||||
isSdk = true;
|
||||
}
|
||||
// check permission
|
||||
- if (isSdk && !CheckFileGroups(DEFAULT_SDK_CONN_PATH, groups)) {
|
||||
- WARN(logger, "sdk permission error");
|
||||
+ if (isSdk && !CheckFileGroups(name)) {
|
||||
+ WARN(logger, "sdk permission error, " << name);
|
||||
return;
|
||||
}
|
||||
if (!epoll->EventCtl(EPOLL_CTL_ADD, conn)) {
|
||||
@@ -274,6 +337,9 @@ void TcpSocket::SaveConnection()
|
||||
type |= CMD_CONN;
|
||||
}
|
||||
tcpMessageHandler.AddConn(conn, type);
|
||||
+ if (isSdk) {
|
||||
+ INFO(logger, "a sdk connection is established, " << name);
|
||||
+ }
|
||||
DEBUG(logger, "client connected!");
|
||||
}
|
||||
|
||||
diff --git a/src/plugin_mgr/message_manager.h b/src/plugin_mgr/message_manager.h
|
||||
index 38f544d..3c67096 100644
|
||||
--- a/src/plugin_mgr/message_manager.h
|
||||
+++ b/src/plugin_mgr/message_manager.h
|
||||
@@ -73,12 +73,13 @@ private:
|
||||
bool StartListen();
|
||||
void SaveConnection();
|
||||
void HandleEvents(struct epoll_event *events, int num);
|
||||
+ bool CheckFileGroups(const std::string &path);
|
||||
private:
|
||||
log4cplus::Logger logger;
|
||||
std::unique_ptr<DomainSocket> domainSocket;
|
||||
std::unique_ptr<Epoll> epoll;
|
||||
TcpMessageHandler tcpMessageHandler;
|
||||
- std::vector<gid_t> groups;
|
||||
+ std::unordered_map<int, std::vector<uid_t>> groups;
|
||||
const int maxRequestNum = 20;
|
||||
const int maxNameLength = 108;
|
||||
};
|
||||
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
|
||||
index 9452a37..040433a 100644
|
||||
--- a/src/sdk/oe_client.cpp
|
||||
+++ b/src/sdk/oe_client.cpp
|
||||
@@ -80,7 +80,17 @@ void Impl::HandleRecv()
|
||||
}
|
||||
int Impl::Init()
|
||||
{
|
||||
- domainSocket = std::make_shared<DomainSocket>(DEFAULT_SDK_CONN_PATH);
|
||||
+ auto home = getenv("HOME");
|
||||
+ std::string homeDir;
|
||||
+ if (home == nullptr) {
|
||||
+ homeDir = "/var/run/oeAware";
|
||||
+ } else {
|
||||
+ homeDir = home;
|
||||
+ homeDir += "/.oeaware";
|
||||
+ }
|
||||
+
|
||||
+ CreateDir(homeDir);
|
||||
+ domainSocket = std::make_shared<DomainSocket>(homeDir + "/oeaware-sdk.sock");
|
||||
domainSocket->SetRemotePath(DEFAULT_SERVER_LISTEN_PATH);
|
||||
resultQueue = std::make_shared<SafeQueue<Result>>();
|
||||
int sock = domainSocket->Socket();
|
||||
--
|
||||
2.33.0
|
||||
|
||||
204
0004-add-command-verification.patch
Normal file
204
0004-add-command-verification.patch
Normal file
@ -0,0 +1,204 @@
|
||||
From eff9e77d5a695f2ef800c54206dbe0ac11dc0272 Mon Sep 17 00:00:00 2001
|
||||
From: fly_1997 <flylove7@outlook.com>
|
||||
Date: Wed, 27 Nov 2024 10:18:57 +0800
|
||||
Subject: [PATCH 4/5] add command verification
|
||||
|
||||
---
|
||||
src/plugin/collect/system/CMakeLists.txt | 2 +-
|
||||
.../collect/system/command/command_base.cpp | 29 ++++++++++---------
|
||||
.../collect/system/command/command_base.h | 3 ++
|
||||
.../system/command/command_collector.h | 3 +-
|
||||
src/plugin/collect/system/kernel_config.cpp | 9 ++++++
|
||||
src/plugin/collect/system/kernel_config.h | 1 +
|
||||
src/plugin_mgr/event/subscribe_handler.cpp | 10 ++-----
|
||||
src/plugin_mgr/instance_run_handler.cpp | 3 +-
|
||||
8 files changed, 36 insertions(+), 24 deletions(-)
|
||||
|
||||
diff --git a/src/plugin/collect/system/CMakeLists.txt b/src/plugin/collect/system/CMakeLists.txt
|
||||
index ee6044f..af4a239 100644
|
||||
--- a/src/plugin/collect/system/CMakeLists.txt
|
||||
+++ b/src/plugin/collect/system/CMakeLists.txt
|
||||
@@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.11)
|
||||
project(system_collector)
|
||||
-include_directories(../include)
|
||||
+include_directories(command)
|
||||
add_compile_options(-O2 -fPIC -Wall -Wextra)
|
||||
add_library(system_collector SHARED
|
||||
thread_collector.cpp
|
||||
diff --git a/src/plugin/collect/system/command/command_base.cpp b/src/plugin/collect/system/command/command_base.cpp
|
||||
index bf658b8..e6c0a83 100644
|
||||
--- a/src/plugin/collect/system/command/command_base.cpp
|
||||
+++ b/src/plugin/collect/system/command/command_base.cpp
|
||||
@@ -10,6 +10,7 @@
|
||||
* See the Mulan PSL v2 for more details.
|
||||
******************************************************************************/
|
||||
#include "command_base.h"
|
||||
+#include <algorithm>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
@@ -59,23 +60,25 @@ CommandBase::CommandBase()
|
||||
attrsFirst["vmstat"] = {"swpd"};
|
||||
}
|
||||
|
||||
+std::vector<std::string> CommandBase::command{"mpstat", "iostat", "vmstat", "sar", "pidstat"};
|
||||
+std::vector<std::string> CommandBase::illegal{"|", ";", "&", "$", ">", "<", "`", "\n"};
|
||||
+
|
||||
+bool CommandBase::ValidateCmd(const std::string &cmd)
|
||||
+{
|
||||
+ for (auto word : illegal) {
|
||||
+ if (strstr(cmd.c_str(), word.c_str())) {
|
||||
+ return false;
|
||||
+ }
|
||||
+ }
|
||||
+ return true;
|
||||
+}
|
||||
+
|
||||
bool CommandBase::ValidateArgs(const oeaware::Topic& topic)
|
||||
{
|
||||
- auto cmd = GetCommand(topic);
|
||||
- PopenProcess p;
|
||||
- p.Popen(cmd);
|
||||
- if (!p.stream) {
|
||||
+ if (std::find(command.begin(), command.end(), topic.topicName) == command.end()) {
|
||||
return false;
|
||||
}
|
||||
- char buffer[128];
|
||||
- bool isValid = false;
|
||||
- if (fgets(buffer, sizeof(buffer), p.stream) != nullptr) {
|
||||
- if (strstr(buffer, "Linux") != nullptr || strstr(buffer, "procs") != nullptr) {
|
||||
- isValid = true;
|
||||
- }
|
||||
- }
|
||||
- p.Pclose();
|
||||
- return isValid;
|
||||
+ return ValidateCmd(topic.params);
|
||||
}
|
||||
|
||||
void CommandBase::ParseLine(const std::string& line)
|
||||
diff --git a/src/plugin/collect/system/command/command_base.h b/src/plugin/collect/system/command/command_base.h
|
||||
index a6d7627..ef9bd7c 100644
|
||||
--- a/src/plugin/collect/system/command/command_base.h
|
||||
+++ b/src/plugin/collect/system/command/command_base.h
|
||||
@@ -35,9 +35,12 @@ public:
|
||||
oeaware::Topic topic;
|
||||
std::unordered_map<std::string, std::vector<std::string>> attrsFirst;
|
||||
std::vector<std::string> skipLine{"---swap--"};
|
||||
+ static std::vector<std::string> command;
|
||||
+ static std::vector<std::string> illegal;
|
||||
CommandBase();
|
||||
virtual ~CommandBase() = default;
|
||||
static bool ValidateArgs(const oeaware::Topic& topic);
|
||||
+ static bool ValidateCmd(const std::string &cmd);
|
||||
void ParseLine(const std::string& line);
|
||||
static std::string GetCommand(const oeaware::Topic& topic);
|
||||
bool FillDataStruct(void* dataStruct);
|
||||
diff --git a/src/plugin/collect/system/command/command_collector.h b/src/plugin/collect/system/command/command_collector.h
|
||||
index 26fc7e7..72553e3 100644
|
||||
--- a/src/plugin/collect/system/command/command_collector.h
|
||||
+++ b/src/plugin/collect/system/command/command_collector.h
|
||||
@@ -27,8 +27,7 @@ public:
|
||||
void Disable() override;
|
||||
void Run() override;
|
||||
private:
|
||||
- std::vector<std::string> topicStr = {"mpstat", "iostat", "vmstat", "sar", "pidstat", "lscpu", "zone_reclaim_mode",
|
||||
- "meminfo", "ethtool", "ifconfig", "os-release", "version"};
|
||||
+ std::vector<std::string> topicStr = {"mpstat", "iostat", "vmstat", "sar", "pidstat"};
|
||||
std::unordered_map<std::string, std::unique_ptr<CommandBase>> collectors;
|
||||
std::unordered_map<std::string, std::thread> collectThreads;
|
||||
std::unordered_map<std::string, std::thread> publishThreads;
|
||||
diff --git a/src/plugin/collect/system/kernel_config.cpp b/src/plugin/collect/system/kernel_config.cpp
|
||||
index 6bdfc8a..3d25251 100644
|
||||
--- a/src/plugin/collect/system/kernel_config.cpp
|
||||
+++ b/src/plugin/collect/system/kernel_config.cpp
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <securec.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
+#include "command_base.h"
|
||||
|
||||
KernelConfig::KernelConfig(): oeaware::Interface()
|
||||
{
|
||||
@@ -258,12 +259,20 @@ void KernelConfig::WriteSysParam(const std::string &path, const std::string &val
|
||||
INFO(logger, "successfully wrote value{" << value <<"} to " << path << ".");
|
||||
}
|
||||
|
||||
+std::vector<std::string> KernelConfig::cmdGroup{"sysctl", "ifconfig", "/sbin/blockdev"};
|
||||
+
|
||||
void KernelConfig::SetKernelConfig()
|
||||
{
|
||||
for (auto &p : setSystemParams) {
|
||||
WriteSysParam(p.first, p.second);
|
||||
}
|
||||
for (auto &cmd : cmdRun) {
|
||||
+ auto cmdParts = oeaware::SplitString(cmd, " ");
|
||||
+ if (cmdParts.empty() || std::find(cmdGroup.begin(), cmdGroup.end(), cmdParts[0]) == cmdGroup.end() ||
|
||||
+ !CommandBase::ValidateCmd(cmd)) {
|
||||
+ WARN(logger, "cmd{" << cmd << "} invalid.");
|
||||
+ continue;
|
||||
+ }
|
||||
FILE *pipe = popen(cmd.data(), "r");
|
||||
if (!pipe) {
|
||||
WARN(logger, "{" << cmd << "} run failed.");
|
||||
diff --git a/src/plugin/collect/system/kernel_config.h b/src/plugin/collect/system/kernel_config.h
|
||||
index aa96886..32049d4 100644
|
||||
--- a/src/plugin/collect/system/kernel_config.h
|
||||
+++ b/src/plugin/collect/system/kernel_config.h
|
||||
@@ -62,6 +62,7 @@ private:
|
||||
std::unordered_map<std::string, std::string> kernelParams;
|
||||
|
||||
std::vector<std::string> cmdRun;
|
||||
+ static std::vector<std::string> cmdGroup;
|
||||
std::vector<std::string> allEths;
|
||||
};
|
||||
|
||||
diff --git a/src/plugin_mgr/event/subscribe_handler.cpp b/src/plugin_mgr/event/subscribe_handler.cpp
|
||||
index 8697958..f29b455 100644
|
||||
--- a/src/plugin_mgr/event/subscribe_handler.cpp
|
||||
+++ b/src/plugin_mgr/event/subscribe_handler.cpp
|
||||
@@ -14,24 +14,20 @@
|
||||
namespace oeaware {
|
||||
Result SubscribeHandler::Subscribe(const std::string &name, const Topic &topic)
|
||||
{
|
||||
- Result result;
|
||||
if (!memoryStore->IsInstanceExist(topic.instanceName)) {
|
||||
WARN(logger, "The subscribed instance " << topic.instanceName << " does not exist.");
|
||||
- result.code = -1;
|
||||
- return result;
|
||||
+ return Result(FAILED, "instance does not exist.");
|
||||
}
|
||||
auto instance = memoryStore->GetInstance(topic.instanceName);
|
||||
if (!instance->supportTopics.count(topic.topicName)) {
|
||||
WARN(logger, "The subscribed topic " << topic.topicName << " does not exist.");
|
||||
- result.code = -1;
|
||||
- return result;
|
||||
+ return Result(FAILED, "topic does not exist.");
|
||||
}
|
||||
auto msg = std::make_shared<InstanceRunMessage>(RunType::SUBSCRIBE,
|
||||
std::vector<std::string>{topic.GetType(), name});
|
||||
instanceRunHandler->RecvQueuePush(msg);
|
||||
msg->Wait();
|
||||
- result = msg->result;
|
||||
- return result;
|
||||
+ return msg->result;
|
||||
}
|
||||
|
||||
EventResult SubscribeHandler::Handle(const Event &event)
|
||||
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
|
||||
index 30dc886..abee581 100644
|
||||
--- a/src/plugin_mgr/instance_run_handler.cpp
|
||||
+++ b/src/plugin_mgr/instance_run_handler.cpp
|
||||
@@ -61,7 +61,8 @@ Result InstanceRunHandler::Subscribe(const std::vector<std::string> &payload)
|
||||
if (!topicState[topic.instanceName][topic.topicName][topic.params]) {
|
||||
result = instance->interface->OpenTopic(topic);
|
||||
if (result.code < 0) {
|
||||
- WARN(logger, "topic open failed, " << result.payload);
|
||||
+ WARN(logger, "topic{" << LogText(topic.instanceName) << ", " << LogText(topic.topicName) << ", " <<
|
||||
+ LogText(topic.params) << "} open failed, " << result.payload);
|
||||
DisableInstance(instance->name);
|
||||
return result;
|
||||
}
|
||||
--
|
||||
2.33.0
|
||||
|
||||
233
0005-modify-C-interface-name-add-enable-count-and-fix-bug.patch
Normal file
233
0005-modify-C-interface-name-add-enable-count-and-fix-bug.patch
Normal file
@ -0,0 +1,233 @@
|
||||
From 1a84134fbe63c1a7bea679b5696bafd99a1b6666 Mon Sep 17 00:00:00 2001
|
||||
From: fly_1997 <flylove7@outlook.com>
|
||||
Date: Sat, 30 Nov 2024 15:25:03 +0800
|
||||
Subject: [PATCH 5/5] modify C interface name, add enable count and fix bugs
|
||||
|
||||
---
|
||||
src/client/cmd_handler.cpp | 7 ++++---
|
||||
src/client/main.cpp | 4 ++--
|
||||
src/plugin_mgr/config.cpp | 5 +++++
|
||||
src/plugin_mgr/config.h | 1 +
|
||||
src/plugin_mgr/event/enable_handler.cpp | 3 +++
|
||||
src/plugin_mgr/instance_run_handler.cpp | 3 ++-
|
||||
src/plugin_mgr/plugin.cpp | 2 +-
|
||||
src/plugin_mgr/plugin.h | 1 +
|
||||
src/sdk/oe_client.cpp | 14 +++++++++-----
|
||||
src/sdk/oe_client.h | 10 +++++-----
|
||||
10 files changed, 33 insertions(+), 17 deletions(-)
|
||||
|
||||
diff --git a/src/client/cmd_handler.cpp b/src/client/cmd_handler.cpp
|
||||
index b1e2ba0..289461b 100644
|
||||
--- a/src/client/cmd_handler.cpp
|
||||
+++ b/src/client/cmd_handler.cpp
|
||||
@@ -33,7 +33,7 @@ void LoadHandler::Handler(Message &msg)
|
||||
void LoadHandler::ResHandler(Message &msg)
|
||||
{
|
||||
if (msg.opt == Opt::RESPONSE_OK) {
|
||||
- std::cout << "Plugin loaded successfully.";
|
||||
+ std::cout << "Plugin loaded successfully.\n";
|
||||
} else {
|
||||
std::cout << "Plugin loaded failed, because "<< msg.payload[0] << ".\n";
|
||||
}
|
||||
@@ -54,9 +54,10 @@ void QueryHandler::PrintFormat()
|
||||
{
|
||||
std::cout << "format:\n"
|
||||
"[plugin]\n"
|
||||
- "\t[instance]([dependency status], [running status])\n"
|
||||
+ "\t[instance]([dependency status], [running status], [enable cnt])\n"
|
||||
"dependency status: available means satisfying dependency, otherwise unavailable.\n"
|
||||
- "running status: running means that instance is running, otherwise close.\n";
|
||||
+ "running status: running means that instance is running, otherwise close.\n"
|
||||
+ "enable cnt: number of instances enabled.\n";
|
||||
}
|
||||
|
||||
void QueryHandler::ResHandler(Message &msg)
|
||||
diff --git a/src/client/main.cpp b/src/client/main.cpp
|
||||
index 188d5e2..1c50f1a 100644
|
||||
--- a/src/client/main.cpp
|
||||
+++ b/src/client/main.cpp
|
||||
@@ -41,8 +41,8 @@ int main(int argc, char *argv[])
|
||||
std::string analysis = argv[1];
|
||||
if (analysis == "analysis") {
|
||||
CTopic topic = {"analysis_aware", "analysis_aware", ""};
|
||||
- Init();
|
||||
- Subscribe(&topic, AnalysisCallback);
|
||||
+ OeInit();
|
||||
+ OeSubscribe(&topic, AnalysisCallback);
|
||||
std::unique_lock<std::mutex> lock(g_mutex);
|
||||
g_cv.wait(lock, []{ return g_finish; });
|
||||
return 0;
|
||||
diff --git a/src/plugin_mgr/config.cpp b/src/plugin_mgr/config.cpp
|
||||
index 8cd1432..aee3243 100644
|
||||
--- a/src/plugin_mgr/config.cpp
|
||||
+++ b/src/plugin_mgr/config.cpp
|
||||
@@ -61,6 +61,10 @@ void Config::SetEnableList(const YAML::Node &node)
|
||||
std::string pluginName = enableList[i]["name"].as<std::string>();
|
||||
YAML::Node instances = enableList[i]["instances"];
|
||||
EnableItem enableItem(pluginName);
|
||||
+ if (!instances.IsSequence()) {
|
||||
+ WARN(logger, "the format of the enable list is incorrect.");
|
||||
+ continue;
|
||||
+ }
|
||||
if (!instances.IsDefined() || instances.IsNull()) {
|
||||
enableItem.SetEnabled(true);
|
||||
} else {
|
||||
@@ -75,6 +79,7 @@ void Config::SetEnableList(const YAML::Node &node)
|
||||
|
||||
bool Config::Load(const std::string &path)
|
||||
{
|
||||
+ logger = Logger::GetInstance().Get("Main");
|
||||
YAML::Node node;
|
||||
struct stat buffer;
|
||||
if (stat(path.c_str(), &buffer) != 0) {
|
||||
diff --git a/src/plugin_mgr/config.h b/src/plugin_mgr/config.h
|
||||
index 19e6d33..dc02b37 100644
|
||||
--- a/src/plugin_mgr/config.h
|
||||
+++ b/src/plugin_mgr/config.h
|
||||
@@ -127,6 +127,7 @@ private:
|
||||
std::string logType;
|
||||
std::unordered_map<std::string, PluginInfo> pluginList;
|
||||
std::vector<EnableItem> enableList;
|
||||
+ log4cplus::Logger logger;
|
||||
};
|
||||
|
||||
std::string GetPath();
|
||||
diff --git a/src/plugin_mgr/event/enable_handler.cpp b/src/plugin_mgr/event/enable_handler.cpp
|
||||
index 66eba6c..a5d8e1a 100644
|
||||
--- a/src/plugin_mgr/event/enable_handler.cpp
|
||||
+++ b/src/plugin_mgr/event/enable_handler.cpp
|
||||
@@ -29,6 +29,9 @@ ErrorCode EnableHandler::InstanceEnabled(const std::string &name)
|
||||
instanceRunHandler->RecvQueuePush(msg);
|
||||
/* Wait for InstanceRunHandler to finsh this task. */
|
||||
msg->Wait();
|
||||
+ if (msg->result.code < 0) {
|
||||
+ return ErrorCode::ENABLE_INSTANCE_ENV;
|
||||
+ }
|
||||
return ErrorCode::OK;
|
||||
}
|
||||
|
||||
diff --git a/src/plugin_mgr/instance_run_handler.cpp b/src/plugin_mgr/instance_run_handler.cpp
|
||||
index abee581..aa428ff 100644
|
||||
--- a/src/plugin_mgr/instance_run_handler.cpp
|
||||
+++ b/src/plugin_mgr/instance_run_handler.cpp
|
||||
@@ -26,6 +26,7 @@ Result InstanceRunHandler::EnableInstance(const std::string &name)
|
||||
return result;
|
||||
}
|
||||
instance->enabled = true;
|
||||
+ instance->enableCnt++;
|
||||
if (instance->interface->GetType() & SCENARIO) {
|
||||
scheduleQueue.push(ScheduleInstance{instance, time + instance->interface->GetPeriod()});
|
||||
} else if (instance->interface->GetType() & TUNE) {
|
||||
@@ -196,7 +197,7 @@ bool InstanceRunHandler::HandleMessage()
|
||||
DEBUG(logger, "handle message " << (int)msg->GetType());
|
||||
switch (msg->GetType()) {
|
||||
case RunType::ENABLED: {
|
||||
- EnableInstance(msg->payload[0]);
|
||||
+ msg->result = EnableInstance(msg->payload[0]);
|
||||
break;
|
||||
}
|
||||
case RunType::DISABLED: {
|
||||
diff --git a/src/plugin_mgr/plugin.cpp b/src/plugin_mgr/plugin.cpp
|
||||
index 5789737..8b894b6 100644
|
||||
--- a/src/plugin_mgr/plugin.cpp
|
||||
+++ b/src/plugin_mgr/plugin.cpp
|
||||
@@ -72,6 +72,6 @@ std::string Instance::GetInfo() const
|
||||
{
|
||||
std::string stateText = this->state ? pluginStateOn : pluginStateOff;
|
||||
std::string runText = this->enabled ? pluginEnabled : pluginDisabled;
|
||||
- return name + "(" + stateText + ", " + runText + ")";
|
||||
+ return name + "(" + stateText + ", " + runText + ", count: " + std::to_string(enableCnt) + ")";
|
||||
}
|
||||
}
|
||||
diff --git a/src/plugin_mgr/plugin.h b/src/plugin_mgr/plugin.h
|
||||
index ef5fc1d..bf63852 100644
|
||||
--- a/src/plugin_mgr/plugin.h
|
||||
+++ b/src/plugin_mgr/plugin.h
|
||||
@@ -23,6 +23,7 @@ struct Instance {
|
||||
std::string pluginName;
|
||||
bool state = true;
|
||||
bool enabled;
|
||||
+ uint64_t enableCnt = 0;
|
||||
std::shared_ptr<Interface> interface;
|
||||
std::unordered_map<std::string, Topic> supportTopics;
|
||||
const static std::string pluginEnabled;
|
||||
diff --git a/src/sdk/oe_client.cpp b/src/sdk/oe_client.cpp
|
||||
index 040433a..a9f82ef 100644
|
||||
--- a/src/sdk/oe_client.cpp
|
||||
+++ b/src/sdk/oe_client.cpp
|
||||
@@ -22,6 +22,7 @@
|
||||
namespace oeaware {
|
||||
class Impl {
|
||||
public:
|
||||
+ Impl() noexcept : domainSocket(nullptr), socketStream(nullptr) { }
|
||||
int Init();
|
||||
int Subscribe(const CTopic &topic, Callback callback);
|
||||
int Unsubscribe(const CTopic &topic);
|
||||
@@ -114,6 +115,9 @@ int Impl::Init()
|
||||
int Impl::HandleRequest(const Opt &opt, const std::vector<std::string> &payload)
|
||||
{
|
||||
MessageProtocol protocol(MessageHeader(MessageType::REQUEST), Message(opt, payload));
|
||||
+ if (socketStream == nullptr) {
|
||||
+ return -1;
|
||||
+ }
|
||||
SendMessage(*socketStream, protocol);
|
||||
Result result;
|
||||
if (!resultQueue->WaitTimeAndPop(result)) {
|
||||
@@ -166,28 +170,28 @@ void Impl::Close()
|
||||
|
||||
static oeaware::Impl impl;
|
||||
|
||||
-int Init()
|
||||
+int OeInit()
|
||||
{
|
||||
oeaware::Register::GetInstance().InitRegisterData();
|
||||
return impl.Init();
|
||||
}
|
||||
|
||||
-int Subscribe(const CTopic *topic, Callback callback)
|
||||
+int OeSubscribe(const CTopic *topic, Callback callback)
|
||||
{
|
||||
return impl.Subscribe(*topic, callback);
|
||||
}
|
||||
|
||||
-int Unsubscribe(const CTopic *topic)
|
||||
+int OeUnsubscribe(const CTopic *topic)
|
||||
{
|
||||
return impl.Unsubscribe(*topic);
|
||||
}
|
||||
|
||||
-int Publish(const DataList *dataList)
|
||||
+int OePublish(const DataList *dataList)
|
||||
{
|
||||
return impl.Publish(*dataList);
|
||||
}
|
||||
|
||||
-void Close()
|
||||
+void OeClose()
|
||||
{
|
||||
impl.Close();
|
||||
}
|
||||
diff --git a/src/sdk/oe_client.h b/src/sdk/oe_client.h
|
||||
index 17d8e68..267462c 100644
|
||||
--- a/src/sdk/oe_client.h
|
||||
+++ b/src/sdk/oe_client.h
|
||||
@@ -16,11 +16,11 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef int(*Callback)(const DataList *);
|
||||
-int Init();
|
||||
-int Subscribe(const CTopic *topic, Callback callback);
|
||||
-int Unsubscribe(const CTopic *topic);
|
||||
-int Publish(const DataList *dataList);
|
||||
-void Close();
|
||||
+int OeInit();
|
||||
+int OeSubscribe(const CTopic *topic, Callback callback);
|
||||
+int OeUnsubscribe(const CTopic *topic);
|
||||
+int OePublish(const DataList *dataList);
|
||||
+void OeClose();
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
--
|
||||
2.33.0
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
Name: oeAware-manager
|
||||
Version: v2.0.0
|
||||
Release: 1
|
||||
Release: 2
|
||||
Summary: OeAware is a framework for implementing low-load collection, sensing, and tuning on openEuler.
|
||||
License: MulanPSL2
|
||||
URL: https://gitee.com/openeuler/%{name}
|
||||
Source0: %{name}-%{version}.tar.gz
|
||||
Patch1: 0001-remove-old-code.patch
|
||||
Patch2: 0002-add-data-free-and-fix-unsubscribe-error.patch
|
||||
Patch3: 0003-fix-failed-to-connect-to-the-sdk-and-command-executi.patch
|
||||
Patch4: 0004-add-command-verification.patch
|
||||
Patch5: 0005-modify-C-interface-name-add-enable-count-and-fix-bug.patch
|
||||
|
||||
BuildRequires: cmake make gcc-c++
|
||||
BuildRequires: boost-devel
|
||||
@ -76,6 +81,10 @@ if [ "${VERSION}" == "22.03 (LTS-SP4)" ]; then
|
||||
systemctl enable oeaware.service
|
||||
fi
|
||||
|
||||
if ! grep -q "oeaware:" /etc/group; then
|
||||
groupadd oeaware
|
||||
fi
|
||||
|
||||
%files
|
||||
%attr(0750, root, root) %{_bindir}/oeaware
|
||||
%attr(0750, root, root) %{_bindir}/oeawarectl
|
||||
@ -92,6 +101,10 @@ fi
|
||||
%attr(0440, root, root) %{_includedir}/oeaware/interface/*.h
|
||||
|
||||
%changelog
|
||||
* Mon Dec 2 2024 fly_1997 <flylove7@outlook.com> -v2.0.0-2
|
||||
- fix data free, plugin display, command plugin bugs
|
||||
- add oeaware group
|
||||
|
||||
* Wed Nov 20 2024 LHesperus <liuchanggeng@huawei.com> -v2.0.0-1
|
||||
- merge the plugin repository into this repository
|
||||
- add oeaware sdk library
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user