• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 #include <csignal>
16 #include <mutex>
17 #include <array>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/wait.h>
21 
22 #include "common.h"
23 #include "hiebpf_plugin_config.pb.h"
24 #include "logging.h"
25 #include "plugin_module_api.h"
26 
27 namespace {
28 constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
29 std::mutex g_taskMutex;
30 constexpr int32_t RET_OK = 0;
31 constexpr int32_t RET_ERR = -1;
32 bool g_releaseResources = false;
33 
RunCmd(std::string & cmd)34 void RunCmd(std::string& cmd)
35 {
36     std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
37     if (pipe == nullptr) {
38         HILOG_ERROR(LOG_CORE, "HiebpfPlugin::RunCmd: create popen FAILED!");
39         return;
40     }
41     constexpr uint32_t readBufferSize = 4096;
42     std::array<char, readBufferSize> buffer;
43     std::string result;
44     while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
45         result += buffer.data();
46     }
47     HILOG_INFO(LOG_CORE, "HiebpfPlugin::run command result: %s", result.c_str());
48 }
49 } // namespace
50 
HiebpfSessionStart(const uint8_t * configData,uint32_t configSize)51 static int32_t HiebpfSessionStart(const uint8_t* configData, uint32_t configSize)
52 {
53     std::lock_guard<std::mutex> guard(g_taskMutex);
54     CHECK_TRUE(!g_releaseResources, 0, "%s: hiebpf released resources, return", __func__);
55     HILOG_DEBUG(LOG_CORE, "enter");
56     if (configData == nullptr || configSize < 0) {
57         HILOG_ERROR(LOG_CORE, "Parameter error");
58         return RET_ERR;
59     }
60     HiebpfConfig config;
61     CHECK_TRUE(config.ParseFromArray(configData, configSize) > 0, RET_ERR, "Parameter parsing failed");
62 
63     size_t defaultSize = sizeof(g_pluginModule.outFileName);
64     CHECK_TRUE(sizeof(config.outfile_name().c_str()) <= defaultSize - 1, RET_ERR,
65                "The out file path more than %zu bytes", defaultSize);
66     int32_t ret = strncpy_s(g_pluginModule.outFileName, defaultSize, config.outfile_name().c_str(), defaultSize - 1);
67     CHECK_TRUE(ret == EOK, RET_ERR, "strncpy_s error! outfile is %s", config.outfile_name().c_str());
68     std::string cmd = config.cmd_line();
69     cmd += " --start true --output_file " + config.outfile_name();
70     RunCmd(cmd);
71     HILOG_DEBUG(LOG_CORE, "leave");
72     return RET_OK;
73 }
74 
HiebpfSessionStop()75 static int32_t HiebpfSessionStop()
76 {
77     std::lock_guard<std::mutex> guard(g_taskMutex);
78     CHECK_TRUE(!g_releaseResources, 0, "%s: hiebpf released resources, return", __func__);
79     HILOG_DEBUG(LOG_CORE, "enter");
80     std::string stop = "hiebpf --stop true";
81     RunCmd(stop);
82     HILOG_DEBUG(LOG_CORE, "leave");
83     return RET_OK;
84 }
85 
86 static PluginModuleCallbacks g_callbacks = {
87     .onPluginSessionStart = HiebpfSessionStart,
88     .onPluginReportResult = nullptr,
89     .onPluginSessionStop = HiebpfSessionStop,
90     .onRegisterWriterStruct = nullptr,
91 };
92 
93 EXPORT_API PluginModuleStruct g_pluginModule = {
94     .callbacks = &g_callbacks,
95     .name = "hiebpf-plugin",
96     .resultBufferSizeHint = MAX_BUFFER_SIZE,
97     .isStandaloneFileData = true,
98     .outFileName = "/data/local/tmp/hiebpf.data",
99 };