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 "nativetoken_kit.h"
32 #include "token_setproc.h"
33 #include "accesstoken_kit.h"
34
35 using namespace OHOS::WuKong;
36
37 static const unsigned int NUMBER_ZERO = 0;
38 static const unsigned int NUMBER_ONE = 1;
39 static const unsigned int NUMBER_TWO = 2;
40 static const unsigned int NUMBER_THREE = 3;
41
FreeSingtion()42 static bool FreeSingtion()
43 {
44 AppManager::DestroyInstance();
45 ComponentManager::DestroyInstance();
46 ExceptionManager::DestroyInstance();
47 MultimodeManager::DestroyInstance();
48 Report::DestroyInstance();
49 SceneDelegate::DestroyInstance();
50 TreeManager::DestroyInstance();
51 WuKongUtil::DestroyInstance();
52 return true;
53 }
54
WuKongMutexFile()55 static void WuKongMutexFile()
56 {
57 int fileExist = access("/dev/shm", F_OK);
58 if (fileExist == 0) {
59 DEBUG_LOG("File exist. Now create wukong test mutex.");
60 } else {
61 const int wuKongGm = mkdir("/dev/shm", 0777);
62 DEBUG_LOG("File create. Now create wukong test mutex.");
63 if (wuKongGm == -1) {
64 DEBUG_LOG("Error creating directory!");
65 }
66 }
67 }
68
SetNativeTokenInfo()69 static void SetNativeTokenInfo()
70 {
71 const char **perms = new const char *[NUMBER_THREE];
72 if (NUMBER_ZERO < NUMBER_THREE && NUMBER_ONE < NUMBER_THREE && NUMBER_TWO < NUMBER_THREE) {
73 uint64_t tokenId;
74 perms[NUMBER_ZERO] = "ohos.permission.SET_ABILITY_CONTROLLER";
75 perms[NUMBER_ONE] = "ohos.permission.CAPTURE_SCREEN";
76 perms[NUMBER_TWO] = "ohos.permission.INPUT_MONITORING";
77 NativeTokenInfoParams infoInstance = {
78 .dcapsNum = 0,
79 .permsNum = 3,
80 .aclsNum = 0,
81 .dcaps = nullptr,
82 .perms = perms,
83 .acls = nullptr,
84 .processName = "wukong",
85 .aplStr = "system_basic",
86 };
87 tokenId = GetAccessTokenId(&infoInstance);
88 SetSelfTokenID(tokenId);
89 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
90 }
91 delete[] perms;
92 }
93
InitSemaphore(NamedSemaphore & sem,const int count)94 static void InitSemaphore(NamedSemaphore& sem, const int count)
95 {
96 bool res = sem.Open();
97 if (!res) {
98 WuKongMutexFile();
99 res = sem.Create();
100 }
101 if (res) {
102 DEBUG_LOG("Open Semaphore success");
103 int value = sem.GetValue();
104 if (value > count) {
105 DEBUG_LOG_STR("the semaphore value is invalid (%d), and reopen Semaphore", value);
106 res = sem.Create();
107 if (!res) {
108 ERROR_LOG("create sem failed");
109 return;
110 }
111 } else {
112 DEBUG_LOG_STR("Semaphore Value: (%d)", value);
113 }
114 }
115 sem.Close();
116 }
117
IsRunning(NamedSemaphore & sem)118 static bool IsRunning(NamedSemaphore& sem)
119 {
120 bool result = false;
121 sem.Open();
122 // the wukong pidof buffer size.
123 const int bufferSize = 32;
124 int value = sem.GetValue();
125 TRACK_LOG_STR("Semaphore Is Open: (%d)", value);
126 if (value <= 0) {
127 FILE* fp = nullptr;
128 fp = popen("pidof wukong", "r");
129 TRACK_LOG("Run pidof wukong");
130 if (fp == nullptr) {
131 ERROR_LOG("popen function failed");
132 return true;
133 }
134 char pid[bufferSize] = {0};
135 if (fgets(pid, bufferSize - 1, fp) != nullptr) {
136 std::string pidStr(pid);
137 pidStr = OHOS::ReplaceStr(pidStr, "\n", " ");
138 TRACK_LOG_STR("Wukong Pid: (%s)", pidStr.c_str());
139 std::vector<std::string> strs;
140 OHOS::SplitStr(pidStr, " ", strs);
141 for (auto i : strs) {
142 DEBUG_LOG_STR("Pid: (%s)", i.c_str());
143 }
144 if (strs.size() >= NUMBER_TWO) {
145 result = true;
146 } else {
147 sem.Create();
148 result = false;
149 }
150 } else {
151 result = true;
152 }
153 pclose(fp);
154 }
155 return result;
156 }
157
main(int argc,char * argv[])158 int main(int argc, char* argv[])
159 {
160 std::shared_ptr<WuKongLogger> WuKonglogger = WuKongLogger::GetInstance();
161 // first start logger
162 WuKonglogger->SetLevel(LOG_LEVEL_INFO);
163 bool isStop = false;
164 for (int index = argc - 1; index >= 1; index--) {
165 std::string arg = argv[index];
166 if (arg == "--track") {
167 argv[index][0] = '\0';
168 WuKonglogger->SetLevel(LOG_LEVEL_TRACK);
169 }
170 if (arg == "--debug") {
171 argv[index][0] = '\0';
172 WuKonglogger->SetLevel(LOG_LEVEL_DEBUG);
173 }
174 if (arg == "stop") {
175 isStop = true;
176 }
177 }
178 if (!WuKonglogger->Start()) {
179 return 1;
180 }
181 NamedSemaphore semRun(SEMPHORE_RUN_NAME, 1);
182 InitSemaphore(semRun, 1);
183 NamedSemaphore semStop(SEMPHORE_STOP_NAME, 1);
184 InitSemaphore(semStop, 1);
185 SetNativeTokenInfo();
186 WuKongShellCommand cmd(argc, argv);
187 if (isStop) {
188 std::cout << cmd.ExecCommand();
189 } else {
190 if (IsRunning(semRun)) {
191 ERROR_LOG("error: wukong has running, allow one program run.");
192 } else {
193 semRun.Open();
194 semRun.Wait();
195 std::cout << cmd.ExecCommand();
196 semRun.Post();
197 semRun.Close();
198 }
199 }
200 FreeSingtion();
201 WuKonglogger->Stop();
202 WuKongLogger::DestroyInstance();
203 INFO_LOG("exit main");
204 return 0;
205 }
206