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 #include <fcntl.h>
16 #include <pthread.h>
17 #include <string.h>
18 #include <sys/time.h>
19
20 #include "begetctl.h"
21 #include "init_param.h"
22 #include "init_utils.h"
23 #include "loop_event.h"
24 #include "parameter.h"
25 #include "plugin_test.h"
26 #include "service_watcher.h"
27 #include "parameter.h"
28 #include "param_base.h"
29
30 #define MAX_THREAD_NUMBER 100
31 #define MAX_NUMBER 10
32 #define READ_DURATION 100000
33 #define CMD_INDEX 2
34
GetLocalBuffer(uint32_t * buffSize)35 static char *GetLocalBuffer(uint32_t *buffSize)
36 {
37 static char buffer[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX] = {0};
38 if (buffSize != NULL) {
39 *buffSize = sizeof(buffer);
40 }
41 return buffer;
42 }
43
44 int g_stop = 0;
CmdReader(void * args)45 static void *CmdReader(void *args)
46 {
47 (void)srand((unsigned)time(NULL));
48 uint32_t buffSize = 0;
49 char *buffer = GetLocalBuffer(&buffSize);
50 while (g_stop == 0) {
51 int wait = READ_DURATION + READ_DURATION; // 100ms rand
52 uint32_t size = buffSize;
53 int ret = SystemGetParameter("test.randrom.read", buffer, &size);
54 if (ret == 0) {
55 printf("SystemGetParameter value %s %d \n", buffer, wait);
56 } else {
57 printf("SystemGetParameter fail %d \n", wait);
58 }
59 usleep(wait);
60 }
61 return NULL;
62 }
63
BShellParamCmdRead(BShellHandle shell,int32_t argc,char * argv[])64 static int32_t BShellParamCmdRead(BShellHandle shell, int32_t argc, char *argv[])
65 {
66 PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
67 static pthread_t thread = 0;
68 PLUGIN_LOGV("BShellParamCmdWatch %s, threadId %d", argv[1], thread);
69 if (strcmp(argv[1], "start") == 0) {
70 if (thread != 0) {
71 return 0;
72 }
73 SystemSetParameter("test.randrom.read.start", "1");
74 pthread_create(&thread, NULL, CmdReader, argv[1]);
75 } else if (strcmp(argv[1], "stop") == 0) {
76 if (thread == 0) {
77 return 0;
78 }
79 SystemSetParameter("test.randrom.read.start", "0");
80 g_stop = 1;
81 pthread_join(thread, NULL);
82 thread = 0;
83 }
84 return 0;
85 }
86
87 typedef struct {
88 char name[PARAM_NAME_LEN_MAX];
89 pthread_t thread;
90 } TestWatchContext;
91
HandleParamChange2(const char * key,const char * value,void * context)92 static void HandleParamChange2(const char *key, const char *value, void *context)
93 {
94 PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter");
95 long long commit = GetSystemCommitId();
96 size_t index = (size_t)context;
97 printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value);
98 static int addWatcher = 0;
99 int ret = 0;
100 if ((index == 4) && !addWatcher) { // 4 when context == 4 add
101 index = 5; // 5 add context
102 ret = SystemWatchParameter(key, HandleParamChange2, (void *)index);
103 if (ret != 0) {
104 printf("Add watcher %s fail %zu \n", key, index);
105 }
106 addWatcher = 1;
107 return;
108 }
109 if (index == 2) { // 2 when context == 2 delete 3
110 index = 3; // 3 delete context
111 RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
112 if (ret != 0) {
113 printf("Remove watcher fail %zu \n", index);
114 }
115 return;
116 }
117 if (index == 1) { // 1 when context == 1 delete 1
118 RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
119 if (ret != 0) {
120 printf("Remove watcher fail %zu \n", index);
121 }
122 return;
123 }
124 if ((index == 5) && (addWatcher == 1)) { // 5 when context == 5 delete 5
125 RemoveParameterWatcher(key, HandleParamChange2, (void *)index);
126 if (ret != 0) {
127 printf("Remove watcher fail %zu \n", index);
128 }
129 addWatcher = 0;
130 }
131 }
132
HandleParamChange1(const char * key,const char * value,void * context)133 static void HandleParamChange1(const char *key, const char *value, void *context)
134 {
135 PLUGIN_CHECK(key != NULL && value != NULL, return, "Invalid parameter");
136 long long commit = GetSystemCommitId();
137 size_t index = (size_t)context;
138 printf("[%zu] Receive parameter commit %lld change %s %s \n", index, commit, key, value);
139 }
140
CmdThreadWatcher(void * args)141 static void *CmdThreadWatcher(void *args)
142 {
143 TestWatchContext *context = (TestWatchContext *)args;
144 for (size_t i = 1; i <= MAX_NUMBER; i++) {
145 int ret = SystemWatchParameter(context->name, HandleParamChange2, (void *)i);
146 if (ret != 0) {
147 printf("Add watcher %s fail %zu \n", context->name, i);
148 }
149 ret = SetParameter(context->name, context->name);
150 if (ret != 0) {
151 printf("Set parameter %s fail %zu \n", context->name, i);
152 }
153 }
154 sleep(1);
155 for (size_t i = 1; i <= MAX_NUMBER; i++) {
156 int ret = RemoveParameterWatcher(context->name, HandleParamChange2, (void *)i);
157 if (ret != 0) {
158 printf("Remove watcher %s fail %zu \n", context->name, i);
159 }
160 }
161 free(context);
162 return NULL;
163 }
164
StartWatcherInThread(const char * prefix)165 static void StartWatcherInThread(const char *prefix)
166 {
167 TestWatchContext *context[MAX_THREAD_NUMBER] = { NULL };
168 for (size_t i = 0; i < MAX_THREAD_NUMBER; i++) {
169 context[i] = calloc(1, sizeof(TestWatchContext));
170 PLUGIN_CHECK(context[i] != NULL, return, "Failed to alloc context");
171 int len = sprintf_s(context[i]->name, sizeof(context[i]->name), "%s.%zu", prefix, i);
172 if (len > 0) {
173 printf("Add watcher %s \n", context[i]->name);
174 pthread_create(&context[i]->thread, NULL, CmdThreadWatcher, context[i]);
175 }
176 }
177 }
178
StartWatcher(const char * prefix)179 static void StartWatcher(const char *prefix)
180 {
181 static size_t index = 0;
182 int ret = SystemWatchParameter(prefix, HandleParamChange1, (void *)index);
183 if (ret != 0) {
184 printf("Add watcher %s fail \n", prefix);
185 return;
186 }
187 index++;
188 }
189
BShellParamCmdWatch(BShellHandle shell,int32_t argc,char * argv[])190 static int32_t BShellParamCmdWatch(BShellHandle shell, int32_t argc, char *argv[])
191 {
192 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
193 PLUGIN_LOGV("BShellParamCmdWatch %s", argv[1]);
194 StartWatcher(argv[1]);
195
196 if (argc <= CMD_INDEX) {
197 return 0;
198 }
199 if (strcmp(argv[CMD_INDEX], "thread") == 0) { // 2 cmd index
200 StartWatcherInThread(argv[1]);
201 return 0;
202 }
203
204 int maxCount = StringToInt(argv[CMD_INDEX], -1); // 2 cmd index
205 if (maxCount <= 0 || maxCount > 65535) { // 65535 max count
206 PLUGIN_LOGE("Invalid input %s", argv[CMD_INDEX]);
207 return 0;
208 }
209 uint32_t buffSize = 0;
210 char *buffer = GetLocalBuffer(&buffSize);
211 size_t count = 0;
212 while (count < (size_t)maxCount) { // 100 max count
213 int len = sprintf_s(buffer, buffSize, "%s.%zu", argv[1], count);
214 PLUGIN_CHECK(len > 0, return -1, "Invalid buffer");
215 buffer[len] = '\0';
216 StartWatcher(buffer);
217 count++;
218 }
219 return 0;
220 }
221
BShellParamCmdInstall(BShellHandle shell,int32_t argc,char * argv[])222 static int32_t BShellParamCmdInstall(BShellHandle shell, int32_t argc, char *argv[])
223 {
224 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
225 PLUGIN_LOGV("BShellParamCmdInstall %s %s", argv[0], argv[1]);
226 uint32_t buffSize = 0;
227 char *buffer = GetLocalBuffer(&buffSize);
228 int ret = sprintf_s(buffer, buffSize, "ohos.servicectrl.%s", argv[0]);
229 PLUGIN_CHECK(ret > 0, return -1, "Invalid buffer");
230 buffer[ret] = '\0';
231 SystemSetParameter(buffer, argv[1]);
232 return 0;
233 }
234
BShellParamCmdDisplay(BShellHandle shell,int32_t argc,char * argv[])235 static int32_t BShellParamCmdDisplay(BShellHandle shell, int32_t argc, char *argv[])
236 {
237 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
238 PLUGIN_LOGV("BShellParamCmdDisplay %s %s", argv[0], argv[1]);
239 SystemSetParameter("ohos.servicectrl.display", argv[1]);
240 return 0;
241 }
242
ServiceStatusChangeTest(const char * key,const ServiceInfo * status)243 void ServiceStatusChangeTest(const char *key, const ServiceInfo *status)
244 {
245 PLUGIN_LOGI("group-test-stage3: wait service %s status: %d", key, status->status);
246 if (status->status == SERVICE_READY || status->status == SERVICE_STARTED) {
247 PLUGIN_LOGI("Service %s start work", key);
248 }
249 }
250
BShellParamCmdGroupTest(BShellHandle shell,int32_t argc,char * argv[])251 static int32_t BShellParamCmdGroupTest(BShellHandle shell, int32_t argc, char *argv[])
252 {
253 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
254 PLUGIN_LOGI("BShellParamCmdGroupTest %s stage: %s", argv[0], argv[1]);
255 if (argc > 2 && strcmp(argv[1], "wait") == 0) { // 2 service name index
256 PLUGIN_LOGI("group-test-stage3: wait service %s", argv[2]); // 2 service name index
257 ServiceWatchForStatus(argv[2], ServiceStatusChangeTest); // 2 service name index
258 LE_RunLoop(LE_GetDefaultLoop());
259 LE_CloseLoop(LE_GetDefaultLoop());
260 }
261 return 0;
262 }
263
BShellParamCmdUdidGet(BShellHandle shell,int32_t argc,char * argv[])264 static int32_t BShellParamCmdUdidGet(BShellHandle shell, int32_t argc, char *argv[])
265 {
266 PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
267 PLUGIN_LOGI("BShellParamCmdUdidGet ");
268 char localDeviceId[65] = {0}; // 65 udid len
269 AclGetDevUdid(localDeviceId, 65); // 65 udid len
270 BShellEnvOutput(shell, " udid: %s\r\n", localDeviceId);
271 return 0;
272 }
273
CalcValue(const char * value)274 static int CalcValue(const char *value)
275 {
276 char *begin = (char *)value;
277 while (*begin != '\0') {
278 if (*begin == ' ') {
279 begin++;
280 } else {
281 break;
282 }
283 }
284 char *end = begin + strlen(begin);
285 while (end > begin) {
286 if (*end > '9' || *end < '0') {
287 *end = '\0';
288 }
289 end--;
290 }
291 return StringToInt(begin, -1);
292 }
293
BShellParamCmdMemGet(BShellHandle shell,int32_t argc,char * argv[])294 static int32_t BShellParamCmdMemGet(BShellHandle shell, int32_t argc, char *argv[])
295 {
296 PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
297 uint32_t buffSize = 0; // 1024 max buffer for decode
298 char *buff = GetLocalBuffer(&buffSize);
299 PLUGIN_CHECK(buff != NULL, return -1, "Failed to get local buffer");
300 int ret = sprintf_s(buff, buffSize - 1, "/proc/%s/smaps", argv[1]);
301 PLUGIN_CHECK(ret > 0, return -1, "Failed to format path %s", argv[1]);
302 buff[ret] = '\0';
303 char *realPath = GetRealPath(buff);
304 PLUGIN_CHECK(realPath != NULL, return -1, "Failed to get real path");
305 int all = 0;
306 if (argc > 2 && strcmp(argv[2], "all") == 0) { // 2 2 max arg
307 all = 1;
308 }
309 FILE *fp = fopen(realPath, "r");
310 free(realPath);
311 int value = 0;
312 while (fp != NULL && buff != NULL && fgets(buff, buffSize, fp) != NULL) {
313 buff[buffSize - 1] = '\0';
314 if (strncmp(buff, "Pss:", strlen("Pss:")) == 0) {
315 int v = CalcValue(buff + strlen("Pss:"));
316 if (all) {
317 printf("Pss: %d kb\n", v);
318 }
319 value += v;
320 } else if (strncmp(buff, "SwapPss:", strlen("SwapPss:")) == 0) {
321 int v = CalcValue(buff + strlen("SwapPss:"));
322 if (all) {
323 printf("SwapPss: %d kb\n", v);
324 }
325 value += v;
326 }
327 }
328 if (fp != NULL) {
329 fclose(fp);
330 printf("Total mem %d kb\n", value);
331 } else {
332 printf("Failed to get memory for %s %s \n", argv[1], buff);
333 }
334 return 0;
335 }
336
CmdServiceStatusChange(const char * key,const ServiceInfo * status)337 void CmdServiceStatusChange(const char *key, const ServiceInfo *status)
338 {
339 static const char *serviceStatusMap[] = {
340 "idle",
341 "starting",
342 "running",
343 "ready",
344 "stopping",
345 "stopped",
346 "error",
347 "suspended",
348 "freezed",
349 "disabled",
350 "critical",
351 };
352 PLUGIN_CHECK(key != NULL && status != NULL, return, "Invalid parameter");
353 if (status->status == SERVICE_STARTED || status->status == SERVICE_READY) {
354 printf("Service %s status: %s pid %d \n", key,
355 ((status->status < ARRAY_LENGTH(serviceStatusMap)) ? serviceStatusMap[status->status] : "unknown"),
356 status->pid);
357 } else {
358 printf("Service %s status: %s \n", key,
359 (status->status < ARRAY_LENGTH(serviceStatusMap)) ? serviceStatusMap[status->status] : "unknown");
360 }
361 }
362
BShellParamCmdWatchService(BShellHandle shell,int32_t argc,char * argv[])363 static int32_t BShellParamCmdWatchService(BShellHandle shell, int32_t argc, char *argv[])
364 {
365 PLUGIN_CHECK(argc > 1, return -1, "Invalid parameter");
366 return ServiceWatchForStatus(argv[1], CmdServiceStatusChange);
367 }
368
BShellCmdRegister(BShellHandle shell,int execMode)369 int32_t BShellCmdRegister(BShellHandle shell, int execMode)
370 {
371 if (execMode == 0) {
372 const CmdInfo infos[] = {
373 {"init", BShellParamCmdGroupTest, "init group test", "init group test [stage]", "init group test"},
374 {"display", BShellParamCmdMemGet, "display memory pid", "display memory [pid]", "display memory"},
375 };
376 for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
377 BShellEnvRegisterCmd(shell, &infos[i]);
378 }
379 } else {
380 const CmdInfo infos[] = {
381 {"display", BShellParamCmdDisplay, "display system service", "display service", "display service"},
382 {"read", BShellParamCmdRead, "read system parameter", "read [start | stop]", ""},
383 {"watcher", BShellParamCmdWatch,
384 "watcher system parameter", "watcher parameter [name]", "watcher parameter"},
385 {"install", BShellParamCmdInstall, "install plugin", "install [name]", ""},
386 {"uninstall", BShellParamCmdInstall, "uninstall plugin", "uninstall [name]", ""},
387 {"group", BShellParamCmdGroupTest, "group test", "group test [stage]", "group test"},
388 {"display", BShellParamCmdUdidGet, "display udid", "display udid", "display udid"},
389 {"display", BShellParamCmdMemGet, "display memory pid", "display memory [pid]", "display memory"},
390 {"watcher", BShellParamCmdWatchService,
391 "watcher service status", "watcher service [name]", "watcher service"},
392 };
393 for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
394 BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
395 }
396 }
397 return 0;
398 }
399