100600bfbSopenharmony_ci/*
200600bfbSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
400600bfbSopenharmony_ci * you may not use this file except in compliance with the License.
500600bfbSopenharmony_ci * You may obtain a copy of the License at
600600bfbSopenharmony_ci *
700600bfbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
800600bfbSopenharmony_ci *
900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and
1300600bfbSopenharmony_ci * limitations under the License.
1400600bfbSopenharmony_ci */
1500600bfbSopenharmony_ci#include "dump_client_main.h"
1600600bfbSopenharmony_ci
1700600bfbSopenharmony_ci#include <ipc_skeleton.h>
1800600bfbSopenharmony_ci#include <sstream>
1900600bfbSopenharmony_ci#include <string_ex.h>
2000600bfbSopenharmony_ci#include <vector>
2100600bfbSopenharmony_ci
2200600bfbSopenharmony_ci#include "common.h"
2300600bfbSopenharmony_ci#include "dump_controller.h"
2400600bfbSopenharmony_ci#include "dump_manager_client.h"
2500600bfbSopenharmony_ci#include "dump_utils.h"
2600600bfbSopenharmony_ci#include "hilog_wrapper.h"
2700600bfbSopenharmony_ci
2800600bfbSopenharmony_cinamespace OHOS {
2900600bfbSopenharmony_cinamespace HiviewDFX {
3000600bfbSopenharmony_ciDumpClientMain::DumpClientMain()
3100600bfbSopenharmony_ci{
3200600bfbSopenharmony_ci}
3300600bfbSopenharmony_ci
3400600bfbSopenharmony_ciDumpClientMain::~DumpClientMain()
3500600bfbSopenharmony_ci{
3600600bfbSopenharmony_ci}
3700600bfbSopenharmony_ci
3800600bfbSopenharmony_ciint DumpClientMain::Main(int argc, char* argv[], int outFd)
3900600bfbSopenharmony_ci{
4000600bfbSopenharmony_ci    if (argc > ARG_MAX_COUNT) {
4100600bfbSopenharmony_ci        LOG_ERR("too many arguments(%d), limit size %d.\n", argc, ARG_MAX_COUNT);
4200600bfbSopenharmony_ci        return DumpStatus::DUMP_INVALID_ARG;
4300600bfbSopenharmony_ci    }
4400600bfbSopenharmony_ci    if (argv == nullptr) {
4500600bfbSopenharmony_ci        LOG_ERR("argument is null.\n");
4600600bfbSopenharmony_ci        return DumpStatus::DUMP_INVALID_ARG;
4700600bfbSopenharmony_ci    }
4800600bfbSopenharmony_ci    for (int i = 0; i < argc; i++) {
4900600bfbSopenharmony_ci        if (argv[i] == nullptr) {
5000600bfbSopenharmony_ci            LOG_ERR("argument(%d) is null.\n", i);
5100600bfbSopenharmony_ci            return DumpStatus::DUMP_INVALID_ARG;
5200600bfbSopenharmony_ci        }
5300600bfbSopenharmony_ci        size_t len = strlen(argv[i]);
5400600bfbSopenharmony_ci        if (len == 0) {
5500600bfbSopenharmony_ci            LOG_ERR("argument(%d) is empty.\n", i);
5600600bfbSopenharmony_ci            return DumpStatus::DUMP_INVALID_ARG;
5700600bfbSopenharmony_ci        }
5800600bfbSopenharmony_ci        if (len > SINGLE_ARG_MAXLEN) {
5900600bfbSopenharmony_ci            LOG_ERR("too long argument(%d), limit size %d.\n", i, SINGLE_ARG_MAXLEN);
6000600bfbSopenharmony_ci            return DumpStatus::DUMP_INVALID_ARG;
6100600bfbSopenharmony_ci        }
6200600bfbSopenharmony_ci    }
6300600bfbSopenharmony_ci    std::vector<std::u16string> args;
6400600bfbSopenharmony_ci    SetCmdArgs(argc, argv, args);
6500600bfbSopenharmony_ci    auto& dumpManagerClient = DumpManagerClient::GetInstance();
6600600bfbSopenharmony_ci    if (dumpManagerClient.Connect() != ERR_OK) {
6700600bfbSopenharmony_ci        (void)dprintf(outFd, "connect error\n");
6800600bfbSopenharmony_ci        DUMPER_HILOGE(MODULE_SERVICE, "connect error.");
6900600bfbSopenharmony_ci        return -1;
7000600bfbSopenharmony_ci    }
7100600bfbSopenharmony_ci    DumpUtils::IgnoreStdoutCache();
7200600bfbSopenharmony_ci    int32_t ret = dumpManagerClient.Request(args, outFd);
7300600bfbSopenharmony_ci    if (ret < DumpStatus::DUMP_OK) {
7400600bfbSopenharmony_ci        if (ret != DumpStatus::DUMP_INVALID_ARG) {
7500600bfbSopenharmony_ci            (void)dprintf(outFd, "request error\n");
7600600bfbSopenharmony_ci            DUMPER_HILOGE(MODULE_SERVICE, "request error, ret: %{public}d.", ret);
7700600bfbSopenharmony_ci        }
7800600bfbSopenharmony_ci        return ret;
7900600bfbSopenharmony_ci    }
8000600bfbSopenharmony_ci    if (!dumpManagerClient.IsConnected()) {
8100600bfbSopenharmony_ci        (void)dprintf(outFd, "service error\n");
8200600bfbSopenharmony_ci        DUMPER_HILOGE(MODULE_SERVICE, "service error.");
8300600bfbSopenharmony_ci    }
8400600bfbSopenharmony_ci    return ret;
8500600bfbSopenharmony_ci}
8600600bfbSopenharmony_ci
8700600bfbSopenharmony_civoid DumpClientMain::SetCmdArgs(int argc, char* argv[], std::vector<std::u16string>& args)
8800600bfbSopenharmony_ci{
8900600bfbSopenharmony_ci    std::stringstream dumpCmdSs;
9000600bfbSopenharmony_ci    for (int i = 0; i < argc; i++) {
9100600bfbSopenharmony_ci        args.push_back(Str8ToStr16(std::string(argv[i])));
9200600bfbSopenharmony_ci        dumpCmdSs << std::string(argv[i]) << " ";
9300600bfbSopenharmony_ci    }
9400600bfbSopenharmony_ci    int32_t calllingUid = IPCSkeleton::GetCallingUid();
9500600bfbSopenharmony_ci    int32_t calllingPid = IPCSkeleton::GetCallingPid();
9600600bfbSopenharmony_ci    DUMPER_HILOGI(MODULE_SERVICE, "hidumper cmd:%{public}s, calllingUid=%{public}d, calllingPid=%{public}d.",
9700600bfbSopenharmony_ci        dumpCmdSs.str().c_str(), calllingUid, calllingPid);
9800600bfbSopenharmony_ci}
9900600bfbSopenharmony_ci} // namespace HiviewDFX
10000600bfbSopenharmony_ci} // namespace OHOS
101