148f512ceSopenharmony_ci/*
248f512ceSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
448f512ceSopenharmony_ci * you may not use this file except in compliance with the License.
548f512ceSopenharmony_ci * You may obtain a copy of the License at
648f512ceSopenharmony_ci *
748f512ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
848f512ceSopenharmony_ci *
948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and
1348f512ceSopenharmony_ci * limitations under the License.
1448f512ceSopenharmony_ci */
1548f512ceSopenharmony_ci#ifndef HIPERF_TRACKED_COMMAND_H_
1648f512ceSopenharmony_ci#define HIPERF_TRACKED_COMMAND_H_
1748f512ceSopenharmony_ci
1848f512ceSopenharmony_ci#include <memory>
1948f512ceSopenharmony_ci#include <string>
2048f512ceSopenharmony_ci#include <utilities.h>
2148f512ceSopenharmony_cinamespace OHOS {
2248f512ceSopenharmony_cinamespace Developtools {
2348f512ceSopenharmony_cinamespace HiPerf {
2448f512ceSopenharmony_ciclass TrackedCommand : public Noncopyable {
2548f512ceSopenharmony_cipublic:
2648f512ceSopenharmony_ci    enum class State {
2748f512ceSopenharmony_ci        COMMAND_WAITING, // child process blocked to execute command
2848f512ceSopenharmony_ci        COMMAND_STARTED, // child process executing command
2948f512ceSopenharmony_ci        COMMAND_FAILURE, // command failed to start
3048f512ceSopenharmony_ci        COMMAND_STOPPED  // no child process or command execution
3148f512ceSopenharmony_ci    };
3248f512ceSopenharmony_ci
3348f512ceSopenharmony_ci    static std::unique_ptr<TrackedCommand> CreateInstance(const std::vector<std::string> &args);
3448f512ceSopenharmony_ci
3548f512ceSopenharmony_ci    ~TrackedCommand();
3648f512ceSopenharmony_ci
3748f512ceSopenharmony_ci    bool CreateChildProcess();
3848f512ceSopenharmony_ci    bool StartCommand();
3948f512ceSopenharmony_ci    bool WaitCommand(int &wstatus);
4048f512ceSopenharmony_ci    void Stop();
4148f512ceSopenharmony_ci
4248f512ceSopenharmony_ci    inline std::string GetCommandName()
4348f512ceSopenharmony_ci    {
4448f512ceSopenharmony_ci        if (!command_.empty()) {
4548f512ceSopenharmony_ci            return command_[0];
4648f512ceSopenharmony_ci        }
4748f512ceSopenharmony_ci        return EMPTY_STRING;
4848f512ceSopenharmony_ci    }
4948f512ceSopenharmony_ci
5048f512ceSopenharmony_ci    inline State GetState()
5148f512ceSopenharmony_ci    {
5248f512ceSopenharmony_ci        return state_;
5348f512ceSopenharmony_ci    }
5448f512ceSopenharmony_ci
5548f512ceSopenharmony_ci    inline pid_t GetChildPid()
5648f512ceSopenharmony_ci    {
5748f512ceSopenharmony_ci        return childPid_;
5848f512ceSopenharmony_ci    }
5948f512ceSopenharmony_ci
6048f512ceSopenharmony_ciprivate:
6148f512ceSopenharmony_ci    explicit TrackedCommand(const std::vector<std::string> &args);
6248f512ceSopenharmony_ci
6348f512ceSopenharmony_ci    bool InitSignalPipes(int &startFd, int &ackFd);
6448f512ceSopenharmony_ci    void ExecuteCommand(const int &startFd, const int &ackFd);
6548f512ceSopenharmony_ci    void MakeInvalid();
6648f512ceSopenharmony_ci
6748f512ceSopenharmony_ci    std::vector<std::string> command_ {};
6848f512ceSopenharmony_ci    int startFd_ {-1};
6948f512ceSopenharmony_ci    int ackFd_ {-1};
7048f512ceSopenharmony_ci    pid_t childPid_ {-1};
7148f512ceSopenharmony_ci    State state_ {State::COMMAND_STOPPED};
7248f512ceSopenharmony_ci};
7348f512ceSopenharmony_ci} // namespace HiPerf
7448f512ceSopenharmony_ci} // namespace Developtools
7548f512ceSopenharmony_ci} // namespace OHOS
7648f512ceSopenharmony_ci#endif // HIPERF_TRACKED_COMMAND_H_
77