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 * 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 <cstdio>
17 #include <sys/stat.h>
18
19 #include "app_manager.h"
20 #include "component_manager.h"
21 #include "exception_manager.h"
22 #include "multimode_manager.h"
23 #include "report.h"
24 #include "scene_delegate.h"
25 #include "string_ex.h"
26 #include "tree_manager.h"
27 #include "wukong_define.h"
28 #include "wukong_logger.h"
29 #include "wukong_shell_command.h"
30 #include "wukong_util.h"
31 #include "parameters.h"
32 #include "hisysevent.h"
33 #include "directory_ex.h"
34
35 using namespace OHOS::WuKong;
36
37 static const unsigned int NUMBER_TWO = 2;
38
FreeSingtion()39 static bool FreeSingtion()
40 {
41 AppManager::DestroyInstance();
42 ComponentManager::DestroyInstance();
43 ExceptionManager::DestroyInstance();
44 MultimodeManager::DestroyInstance();
45 Report::DestroyInstance();
46 SceneDelegate::DestroyInstance();
47 TreeManager::DestroyInstance();
48 WuKongUtil::DestroyInstance();
49 return true;
50 }
51
WuKongMutexFile()52 static void WuKongMutexFile()
53 {
54 int fileExist = access("/dev/shm", F_OK);
55 if (fileExist == 0) {
56 DEBUG_LOG("File exist. Now create wukong test mutex.");
57 } else {
58 const int wuKongGm = mkdir("/dev/shm", 0777);
59 DEBUG_LOG("File create. Now create wukong test mutex.");
60 if (wuKongGm == -1) {
61 DEBUG_LOG("Error creating directory!");
62 }
63 }
64 }
65
InitSemaphore(NamedSemaphore & sem,const int count)66 static void InitSemaphore(NamedSemaphore& sem, const int count)
67 {
68 bool res = sem.Open();
69 if (!res) {
70 WuKongMutexFile();
71 res = sem.Create();
72 }
73 if (res) {
74 DEBUG_LOG("Open Semaphore success");
75 int value = sem.GetValue();
76 if (value > count) {
77 DEBUG_LOG_STR("the semaphore value is invalid (%d), and reopen Semaphore", value);
78 res = sem.Create();
79 if (!res) {
80 ERROR_LOG("create sem failed");
81 return;
82 }
83 } else {
84 DEBUG_LOG_STR("Semaphore Value: (%d)", value);
85 }
86 }
87 sem.Close();
88 }
89
IsRunning(NamedSemaphore & sem)90 static bool IsRunning(NamedSemaphore& sem)
91 {
92 bool result = false;
93 sem.Open();
94 // the wukong pidof buffer size.
95 const int bufferSize = 32;
96 int value = sem.GetValue();
97 TRACK_LOG_STR("Semaphore Is Open: (%d)", value);
98 if (value <= 0) {
99 FILE* fp = nullptr;
100 fp = popen("pidof wukong", "r");
101 TRACK_LOG("Run pidof wukong");
102 if (fp == nullptr) {
103 ERROR_LOG("popen function failed");
104 return true;
105 }
106 char pid[bufferSize] = {0};
107 if (fgets(pid, bufferSize - 1, fp) != nullptr) {
108 std::string pidStr(pid);
109 pidStr = OHOS::ReplaceStr(pidStr, "\n", " ");
110 TRACK_LOG_STR("Wukong Pid: (%s)", pidStr.c_str());
111 std::vector<std::string> strs;
112 OHOS::SplitStr(pidStr, " ", strs);
113 for (auto i : strs) {
114 DEBUG_LOG_STR("Pid: (%s)", i.c_str());
115 }
116 if (strs.size() >= NUMBER_TWO) {
117 result = true;
118 } else {
119 sem.Create();
120 result = false;
121 }
122 } else {
123 result = true;
124 }
125 pclose(fp);
126 }
127 return result;
128 }
129
main(int argc,char * argv[])130 int main(int argc, char* argv[])
131 {
132 if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
133 std::cout << "Not a development mode state, please check device mode." << std::endl;
134 return 0;
135 }
136 const uint64_t dataPathSize = OHOS::GetFolderSize(DATA_PATH);
137 const uint64_t wukongPathSize = OHOS::GetFolderSize(WUKONG_PATH);
138 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, "USER_DATA_SIZE", OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "COMPONENT_NAME", "wukong","PARTITION_NAME",DATA_PATH,"REMAIN_PARTITION_SIZE",dataPathSize,"FILE_OR_FOLDER_PATH",WUKONG_PATH,"FILE_OR_FOLDER_SIZE",wukongPathSize);
139 std::shared_ptr<WuKongLogger> WuKonglogger = WuKongLogger::GetInstance();
140 // first start logger
141 WuKonglogger->SetLevel(LOG_LEVEL_INFO);
142 for (int index = argc - 1; index >= 1; index--) {
143 std::string arg = argv[index];
144 if (arg == "--track") {
145 argv[index][0] = '\0';
146 WuKonglogger->SetLevel(LOG_LEVEL_TRACK);
147 }
148 if (arg == "--debug") {
149 argv[index][0] = '\0';
150 WuKonglogger->SetLevel(LOG_LEVEL_DEBUG);
151 }
152 }
153 if (!WuKonglogger->Start()) {
154 return 1;
155 }
156 NamedSemaphore semRun(SEMPHORE_RUN_NAME, 1);
157 InitSemaphore(semRun, 1);
158 NamedSemaphore semStop(SEMPHORE_STOP_NAME, 1);
159 InitSemaphore(semStop, 1);
160 WuKongShellCommand cmd(argc, argv);
161 if (IsRunning(semRun)) {
162 ERROR_LOG("error: wukong has running, allow one program run.");
163 } else {
164 semRun.Open();
165 semRun.Wait();
166 std::cout << cmd.ExecCommand();
167 semRun.Post();
168 semRun.Close();
169 }
170 FreeSingtion();
171 WuKonglogger->Stop();
172 std::cout << "exit main" << std::endl;
173 return 0;
174 }
175