169570cc8Sopenharmony_ci/*
269570cc8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License.
569570cc8Sopenharmony_ci * You may obtain a copy of the License at
669570cc8Sopenharmony_ci *
769570cc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
869570cc8Sopenharmony_ci *
969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and
1369570cc8Sopenharmony_ci * limitations under the License.
1469570cc8Sopenharmony_ci */
1569570cc8Sopenharmony_ci#include <stdio.h>
1669570cc8Sopenharmony_ci#include <stdlib.h>
1769570cc8Sopenharmony_ci#include <signal.h>
1869570cc8Sopenharmony_ci
1969570cc8Sopenharmony_ci#include "appspawn.h"
2069570cc8Sopenharmony_ci#include "appspawn_utils.h"
2169570cc8Sopenharmony_ci
2269570cc8Sopenharmony_ci#include "devicedebug_base.h"
2369570cc8Sopenharmony_ci#include "devicedebug_kill.h"
2469570cc8Sopenharmony_ci#include "cJSON.h"
2569570cc8Sopenharmony_ci
2669570cc8Sopenharmony_ci#ifdef __cplusplus
2769570cc8Sopenharmony_ciextern "C" {
2869570cc8Sopenharmony_ci#endif
2969570cc8Sopenharmony_ci
3069570cc8Sopenharmony_ci#define DEVICEDEBUG_KILL_CMD_NUM 4
3169570cc8Sopenharmony_ci#define DEVICEDEBUG_KILL_CMD_SIGNAL_INDEX 2
3269570cc8Sopenharmony_ci#define DEVICEDEBUG_KILL_CMD_PID_INDEX 3
3369570cc8Sopenharmony_ci
3469570cc8Sopenharmony_ciAPPSPAWN_STATIC void DeviceDebugShowKillHelp(void)
3569570cc8Sopenharmony_ci{
3669570cc8Sopenharmony_ci    printf("\r\nusage: devicedebug kill [options] <pid>"
3769570cc8Sopenharmony_ci        "\r\noptions list:"
3869570cc8Sopenharmony_ci        "\r\n         -h, --help                list available commands"
3969570cc8Sopenharmony_ci        "\r\n         kill -<signal> <pid>      send a signal to a process\r\n");
4069570cc8Sopenharmony_ci}
4169570cc8Sopenharmony_ci
4269570cc8Sopenharmony_ciAPPSPAWN_STATIC char* DeviceDebugJsonStringGeneral(int pid, const char *op, cJSON *args)
4369570cc8Sopenharmony_ci{
4469570cc8Sopenharmony_ci    cJSON *root = cJSON_CreateObject();
4569570cc8Sopenharmony_ci    if (root == NULL) {
4669570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug json write create root object unsuccess");
4769570cc8Sopenharmony_ci        return NULL;
4869570cc8Sopenharmony_ci    }
4969570cc8Sopenharmony_ci
5069570cc8Sopenharmony_ci    cJSON_AddNumberToObject(root, "app", pid);
5169570cc8Sopenharmony_ci    cJSON_AddStringToObject(root, "op", op);
5269570cc8Sopenharmony_ci    cJSON_AddItemToObject(root, "args", args);
5369570cc8Sopenharmony_ci
5469570cc8Sopenharmony_ci    char *jsonString = cJSON_Print(root);
5569570cc8Sopenharmony_ci    cJSON_Delete(root);
5669570cc8Sopenharmony_ci    return jsonString;
5769570cc8Sopenharmony_ci}
5869570cc8Sopenharmony_ci
5969570cc8Sopenharmony_ciAPPSPAWN_STATIC int DeviceDebugKill(int pid, int signal)
6069570cc8Sopenharmony_ci{
6169570cc8Sopenharmony_ci    AppSpawnClientHandle clientHandle;
6269570cc8Sopenharmony_ci    int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle);
6369570cc8Sopenharmony_ci    if (ret != 0) {
6469570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug appspawn client init unsuccess, ret=%{public}d", ret);
6569570cc8Sopenharmony_ci        return ret;
6669570cc8Sopenharmony_ci    }
6769570cc8Sopenharmony_ci
6869570cc8Sopenharmony_ci    AppSpawnReqMsgHandle reqHandle;
6969570cc8Sopenharmony_ci    ret = AppSpawnReqMsgCreate(MSG_DEVICE_DEBUG, "devicedebug", &reqHandle);
7069570cc8Sopenharmony_ci    if (ret != 0) {
7169570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug appspawn message create unsuccess, ret=%{public}d", ret);
7269570cc8Sopenharmony_ci        return ret;
7369570cc8Sopenharmony_ci    }
7469570cc8Sopenharmony_ci
7569570cc8Sopenharmony_ci    cJSON *args = cJSON_CreateObject();
7669570cc8Sopenharmony_ci    if (args == NULL) {
7769570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug json write create args object unsuccess");
7869570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_JSON_CREATED_FAILED;
7969570cc8Sopenharmony_ci    }
8069570cc8Sopenharmony_ci    cJSON_AddNumberToObject(args, "signal", signal);
8169570cc8Sopenharmony_ci    char *jsonString = DeviceDebugJsonStringGeneral(pid, "kill", args);
8269570cc8Sopenharmony_ci    if (jsonString == NULL) {
8369570cc8Sopenharmony_ci        cJSON_Delete(args);
8469570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_JSON_CREATED_FAILED;
8569570cc8Sopenharmony_ci    }
8669570cc8Sopenharmony_ci
8769570cc8Sopenharmony_ci    ret = AppSpawnReqMsgAddExtInfo(reqHandle, "devicedebug", (uint8_t *)jsonString, strlen(jsonString) + 1);
8869570cc8Sopenharmony_ci    if (ret != 0) {
8969570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug appspawn message add devicedebug[%{public}s] unsuccess, ret=%{public}d",
9069570cc8Sopenharmony_ci            jsonString, ret);
9169570cc8Sopenharmony_ci        return ret;
9269570cc8Sopenharmony_ci    }
9369570cc8Sopenharmony_ci
9469570cc8Sopenharmony_ci    AppSpawnResult result = {0};
9569570cc8Sopenharmony_ci    ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
9669570cc8Sopenharmony_ci    AppSpawnClientDestroy(clientHandle);
9769570cc8Sopenharmony_ci    if (ret != 0) {
9869570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug appspawn send msg unsuccess, ret=%{public}d", ret);
9969570cc8Sopenharmony_ci        return ret;
10069570cc8Sopenharmony_ci    }
10169570cc8Sopenharmony_ci
10269570cc8Sopenharmony_ci    if (result.result != 0) {
10369570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug appspawn kill process unsuccess, result=%{public}d", result.result);
10469570cc8Sopenharmony_ci        return result.result;
10569570cc8Sopenharmony_ci    }
10669570cc8Sopenharmony_ci
10769570cc8Sopenharmony_ci    return 0;
10869570cc8Sopenharmony_ci}
10969570cc8Sopenharmony_ci
11069570cc8Sopenharmony_ciint DeviceDebugCmdKill(int argc, char *argv[])
11169570cc8Sopenharmony_ci{
11269570cc8Sopenharmony_ci    if (!IsDeveloperModeOpen()) {
11369570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_NOT_IN_DEVELOPER_MODE;
11469570cc8Sopenharmony_ci    }
11569570cc8Sopenharmony_ci
11669570cc8Sopenharmony_ci    if (argc <= DEVICEDEBUG_NUM_2 || strcmp(argv[DEVICEDEBUG_NUM_2], "-h") == 0 ||
11769570cc8Sopenharmony_ci        strcmp(argv[DEVICEDEBUG_NUM_2], "help") == 0) {
11869570cc8Sopenharmony_ci        DeviceDebugShowKillHelp();
11969570cc8Sopenharmony_ci        return 0;
12069570cc8Sopenharmony_ci    }
12169570cc8Sopenharmony_ci
12269570cc8Sopenharmony_ci    if (argc < DEVICEDEBUG_KILL_CMD_NUM) {
12369570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("devicedebug cmd operator num is %{public}d < %{public}d", argc, DEVICEDEBUG_KILL_CMD_NUM);
12469570cc8Sopenharmony_ci        DeviceDebugShowKillHelp();
12569570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_OPERATOR_ARGV_MISS;
12669570cc8Sopenharmony_ci    }
12769570cc8Sopenharmony_ci
12869570cc8Sopenharmony_ci    int signal = atoi(argv[DEVICEDEBUG_KILL_CMD_SIGNAL_INDEX] + 1);
12969570cc8Sopenharmony_ci    if (signal > SIGRTMAX || signal <= 0) {
13069570cc8Sopenharmony_ci        DEVICEDEBUG_LOGE("signal is %{public}d > %{public}d", signal, SIGRTMAX);
13169570cc8Sopenharmony_ci        DeviceDebugShowKillHelp();
13269570cc8Sopenharmony_ci        return DEVICEDEBUG_ERRNO_PARAM_INVALID;
13369570cc8Sopenharmony_ci    }
13469570cc8Sopenharmony_ci
13569570cc8Sopenharmony_ci    int pid = atoi(argv[DEVICEDEBUG_KILL_CMD_PID_INDEX]);
13669570cc8Sopenharmony_ci    DEVICEDEBUG_LOGI("devicedebug cmd kill start signal[%{public}d], pid[%{public}d]", signal, pid);
13769570cc8Sopenharmony_ci
13869570cc8Sopenharmony_ci    return DeviceDebugKill(pid, signal);
13969570cc8Sopenharmony_ci}
14069570cc8Sopenharmony_ci
14169570cc8Sopenharmony_ci#ifdef __cplusplus
14269570cc8Sopenharmony_ci}
14369570cc8Sopenharmony_ci#endif
144