• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "command_poller.h"
19 #include "logging.h"
20 #include "plugin_manager.h"
21 #include "plugin_service_types.pb.h"
22 #include "plugin_watcher.h"
23 #include "schedule_task_manager.h"
24 #include "writer_adapter.h"
25 
26 namespace {
27 #if defined(__i386__) || defined(__x86_64__)
28 const char DEFAULT_PLUGIN_PATH[] = "./";
29 #else
30 const char DEFAULT_PLUGIN_PATH[] = "/data/local/tmp/";
31 #endif
32 
33 const int SLEEP_ONE_SECOND = 1000;
34 const char DEFAULT_LIB_PATH[] = "/system/lib/";
35 std::vector<std::string> presetPluginVec = {
36     "libcpudataplugin.z.so",
37     "libdiskiodataplugin.z.so",
38     "libftrace_plugin.z.so",
39     "libhidumpplugin.z.so",
40     "libhilogplugin.z.so",
41     "libmemdataplugin.z.so",
42     "libnetworkplugin.z.so",
43     "libprocessplugin.z.so",
44 };
45 } // namespace
46 
main(int argc,char * argv[])47 int main(int argc, char* argv[])
48 {
49     const int connectRetrySeconds = 3;
50 
51     std::string pluginDir(DEFAULT_PLUGIN_PATH);
52     if (argv[1] != nullptr) {
53         HILOG_DEBUG(LOG_CORE, "%s:pluginDir = %s", __func__, argv[1]);
54         pluginDir = argv[1];
55     }
56 
57     auto pluginManager = std::make_shared<PluginManager>();
58     CHECK_NOTNULL(pluginManager, 1, "create PluginManager FAILED!");
59 
60     auto commandPoller = std::make_shared<CommandPoller>(pluginManager);
61     CHECK_NOTNULL(commandPoller, 1, "create CommandPoller FAILED!");
62 
63     while (true) {
64         if (commandPoller->OnConnect()) {
65             break;
66         }
67         HILOG_DEBUG(LOG_CORE, "%s:connect failed, try again", __func__);
68         sleep(connectRetrySeconds);
69     }
70     pluginManager->SetCommandPoller(commandPoller);
71 
72     // add preset plugin
73     for (size_t i = 0; i < presetPluginVec.size(); i++) {
74         const std::string pluginPath = DEFAULT_LIB_PATH + presetPluginVec[i];
75         if (pluginManager->AddPlugin(pluginPath)) {
76             HILOG_INFO(LOG_CORE, "add preset plugin %s success!", pluginPath.c_str());
77         } else {
78             HILOG_INFO(LOG_CORE, "add preset plugin %s failed!", pluginPath.c_str());
79         }
80     }
81 
82     PluginWatcher watcher(pluginManager);
83     if (!watcher.ScanPlugins(pluginDir)) {
84         HILOG_DEBUG(LOG_CORE, "%s:scan pluginDir %s failed!", __func__, DEFAULT_PLUGIN_PATH);
85         return 0;
86     }
87 
88     if (!watcher.WatchPlugins(pluginDir)) {
89         HILOG_DEBUG(LOG_CORE, "%s:monitor pluginDir %s failed!", __func__, DEFAULT_PLUGIN_PATH);
90         return 0;
91     }
92 
93     while (true) {
94         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_ONE_SECOND));
95     }
96 
97     return 0;
98 }
99