1 /*
2 * Copyright (c) 2021 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 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <unistd.h>
17
18 #include "common.h"
19 #include "command_poller.h"
20 #include "logging.h"
21 #include "plugin_manager.h"
22 #include "plugin_service_types.pb.h"
23 #include "plugin_watcher.h"
24 #include "schedule_task_manager.h"
25 #include "writer_adapter.h"
26
27 namespace {
28 #if defined(__i386__) || defined(__x86_64__)
29 const char DEFAULT_PLUGIN_PATH[] = "./";
30 #else
31 const char DEFAULT_PLUGIN_PATH[] = "/data/local/tmp/";
32 #endif
33
34 const int SLEEP_ONE_SECOND = 1000;
35 #if defined(__LP64__)
36 const char DEFAULT_LIB_PATH[] = "/system/lib64/";
37 #else
38 const char DEFAULT_LIB_PATH[] = "/system/lib/";
39 #endif
40
41 std::vector<std::string> presetPluginVec = {
42 "libcpudataplugin.z.so",
43 "libdiskiodataplugin.z.so",
44 "libftrace_plugin.z.so",
45 "libhidumpplugin.z.so",
46 "libhilogplugin.z.so",
47 "libhiperfplugin.z.so",
48 "libhisyseventplugin.z.so",
49 "libmemdataplugin.z.so",
50 "libnetworkplugin.z.so",
51 "libprocessplugin.z.so",
52 };
53 } // namespace
54
main(int argc,char * argv[])55 int main(int argc, char* argv[])
56 {
57 if (COMMON::IsProcessRunning()) { // process is running
58 return 0;
59 }
60
61 const int connectRetrySeconds = 3;
62 std::string pluginDir(DEFAULT_PLUGIN_PATH);
63 if (argv[1] != nullptr) {
64 HILOG_DEBUG(LOG_CORE, "%s:pluginDir = %s", __func__, argv[1]);
65 pluginDir = argv[1];
66 }
67
68 auto pluginManager = std::make_shared<PluginManager>();
69 CHECK_NOTNULL(pluginManager, 1, "create PluginManager FAILED!");
70
71 auto commandPoller = std::make_shared<CommandPoller>(pluginManager);
72 CHECK_NOTNULL(commandPoller, 1, "create CommandPoller FAILED!");
73
74 while (true) {
75 if (commandPoller->OnConnect()) {
76 break;
77 }
78 HILOG_DEBUG(LOG_CORE, "%s:connect failed, try again", __func__);
79 sleep(connectRetrySeconds);
80 }
81 pluginManager->SetCommandPoller(commandPoller);
82
83 // add preset plugin
84 for (size_t i = 0; i < presetPluginVec.size(); i++) {
85 const std::string pluginPath = DEFAULT_LIB_PATH + presetPluginVec[i];
86 if (pluginManager->AddPlugin(pluginPath)) {
87 HILOG_INFO(LOG_CORE, "add preset plugin %s success!", pluginPath.c_str());
88 } else {
89 HILOG_INFO(LOG_CORE, "add preset plugin %s failed!", pluginPath.c_str());
90 }
91 }
92
93 while (true) {
94 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
95 }
96
97 return 0;
98 }
99