1 /*
2  * Copyright (c) 2024 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 <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 
19 #include "devicedebug_base.h"
20 #include "devicedebug_kill.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 typedef int (*DEVICEDEBUG_CMD_PROCESS_FUNC)(int argc, char *argv[]);
27 
28 typedef struct DeviceDebugManagerCmdInfoStru {
29     char *cmd;
30     DEVICEDEBUG_CMD_PROCESS_FUNC process;
31 } DeviceDebugManagerCmdInfo;
32 
DeviceDebugShowHelp(int argc,char * argv[])33 APPSPAWN_STATIC int DeviceDebugShowHelp(int argc, char *argv[])
34 {
35     (void)argc;
36     (void)argv;
37 
38     printf("\r\nusage: devicedebug <command> <options>\r\n"
39         "\r\nThese are common devicedebug commands list:\r\n"
40         "\r\n         help              list available commands"
41         "\r\n         kill              send a signal(1-64) to a process\r\n");
42 
43     return 0;
44 }
45 
46 DeviceDebugManagerCmdInfo g_deviceDebugManagerCmd[] = {
47     {"help", DeviceDebugShowHelp},
48     {"-h", DeviceDebugShowHelp},
49     {"kill", DeviceDebugCmdKill},
50 };
51 
DeviceDebugCmdCheck(const char * cmd)52 APPSPAWN_STATIC DeviceDebugManagerCmdInfo* DeviceDebugCmdCheck(const char *cmd)
53 {
54     int cmdNum = sizeof(g_deviceDebugManagerCmd) / sizeof(DeviceDebugManagerCmdInfo);
55 
56     for (int i = 0; i < cmdNum; i++) {
57         if (!strcmp(cmd, g_deviceDebugManagerCmd[i].cmd)) {
58             return &g_deviceDebugManagerCmd[i];
59         }
60     }
61     return NULL;
62 }
63 
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66     int ret = 0;
67     DeviceDebugManagerCmdInfo *cmdInfo = NULL;
68 
69     if (argc < DEVICEDEBUG_NUM_2) {
70         DeviceDebugShowHelp(argc, argv);
71         return DEVICEDEBUG_ERRNO_PARAM_INVALID;
72     }
73 
74     DEVICEDEBUG_LOGI("devicedebug manager process start.");
75 
76     /* 检验用户命令,获取对应的处理函数 */
77     cmdInfo = DeviceDebugCmdCheck(argv[DEVICEDEBUG_NUM_1]);
78     if (cmdInfo == NULL) {
79         printf("devicedebug: '%s' is not a valid devicedebug command. See 'devicedebug help'.",
80             argv[DEVICEDEBUG_NUM_1]);
81         DeviceDebugShowHelp(argc, argv);
82         return DEVICEDEBUG_ERRNO_OPERATOR_TYPE_INVALID;
83     }
84 
85     /* 执行命令 */
86     ret = cmdInfo->process(argc, argv);
87 
88     /* 返回值依赖此条log打印,切勿随意修改 */
89     DEVICEDEBUG_LOGI("devicedebug manager process exit. ret=%{public}d \r\n", ret);
90     return ret;
91 }
92 
93 #ifdef __cplusplus
94 }
95 #endif