aboutsummaryrefslogtreecommitdiff
path: root/src/common/SubProcess.cpp
blob: e2738248e1906cbbc77a7a68c8adda5cbfc4197f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "cru/common/SubProcess.h"
#include <exception>
#include "cru/common/Exception.h"
#include "cru/common/log/Logger.h"

#ifdef CRU_PLATFORM_UNIX
#include "cru/common/platform/unix/PosixSpawnSubProcess.h"
#endif

#include <mutex>

namespace cru {
PlatformSubProcessBase::PlatformSubProcessBase(SubProcessStartInfo start_info)
    : start_info_(std::move(start_info)),
      delete_self_(false),
      process_lock_(process_mutex_, std::defer_lock) {}

PlatformSubProcessBase::~PlatformSubProcessBase() {
  std::lock_guard lock_guard(process_lock_);
  if (status_ == SubProcessStatus::Running) {
    CRU_LOG_ERROR(
        u"PlatformSubProcessBase is destroyed but process is still running.");
    std::terminate();
  }
}

void PlatformSubProcessBase::Start() {
  std::lock_guard lock_guard(process_lock_);

  if (status_ != SubProcessStatus::Prepare) {
    throw SubProcessException(u"The process has already tried to start.");
  }

  try {
    PlatformCreateProcess();

    status_ = SubProcessStatus::Running;

    process_thread_ = std::thread([this] {
      auto exit_result = PlatformWaitForProcess();
      {
        std::lock_guard lock_guard(process_lock_);
        exit_result_ = std::move(exit_result);
        status_ = SubProcessStatus::Exited;
      }
      this->process_condition_variable_.notify_all();
      if (this->delete_self_) {
        delete this;
      }
    });

    process_thread_.detach();
  } catch (const std::exception& e) {
    status_ = SubProcessStatus::FailedToStart;
    throw SubProcessFailedToStartException(u"Sub-process failed to start. " +
                                           String::FromUtf8(e.what()));
  }
}

void PlatformSubProcessBase::Wait(
    std::optional<std::chrono::milliseconds> wait_time) {
  std::lock_guard lock_guard(process_lock_);

  if (status_ == SubProcessStatus::Prepare) {
    throw SubProcessException(
        u"The process does not start. Can't wait for it.");
  }

  if (status_ == SubProcessStatus::FailedToStart) {
    throw SubProcessException(
        u"The process failed to start. Can't wait for it.");
  }

  if (status_ == SubProcessStatus::Exited) {
    return;
  }

  auto predicate = [this] { return status_ == SubProcessStatus::Exited; };

  if (wait_time) {
    process_condition_variable_.wait_for(process_lock_, *wait_time, predicate);
  } else {
    process_condition_variable_.wait(process_lock_, predicate);
  }
}

void PlatformSubProcessBase::Kill() {
  std::lock_guard data_lock_guard(process_lock_);

  if (status_ == SubProcessStatus::Prepare) {
    throw SubProcessException(u"The process does not start. Can't kill it.");
  }

  if (status_ == SubProcessStatus::FailedToStart) {
    throw SubProcessException(u"The process failed to start. Can't kill it.");
  }

  if (status_ == SubProcessStatus::Exited) {
    return;
  }

  PlatformKillProcess();
}

SubProcessStatus PlatformSubProcessBase::GetStatus() {
  std::lock_guard data_lock_guard(process_lock_);
  return status_;
}

SubProcessExitResult PlatformSubProcessBase::GetExitResult() {
  std::lock_guard lock_guard(process_lock_);

  if (status_ == SubProcessStatus::Prepare) {
    throw SubProcessException(
        u"The process does not start. Can't get exit result.");
  }

  if (status_ == SubProcessStatus::FailedToStart) {
    throw SubProcessException(
        u"The process failed to start. Can't get exit result.");
  }

  if (status_ == SubProcessStatus::Running) {
    throw SubProcessException(
        u"The process is running. Can't get exit result.");
  }

  return exit_result_;
}

void PlatformSubProcessBase::SetDeleteSelfOnExit(bool enable) {
  std::lock_guard lock_guard(process_lock_);
  delete_self_ = enable;
}

#ifdef CRU_PLATFORM_UNIX
using PlatformSubProcess = platform::unix::PosixSpawnSubProcess;
#endif

SubProcess SubProcess::Create(String program, std::vector<String> arguments,
                              std::unordered_map<String, String> environments) {
  SubProcessStartInfo start_info;
  start_info.program = std::move(program);
  start_info.arguments = std::move(arguments);
  start_info.environments = std::move(environments);
  return SubProcess(std::move(start_info));
}

SubProcess::SubProcess(SubProcessStartInfo start_info) {
  platform_process_.reset(new PlatformSubProcess(std::move(start_info)));
  platform_process_->Start();
}

SubProcess::~SubProcess() {}

void SubProcess::Wait(std::optional<std::chrono::milliseconds> wait_time) {
  CheckValid();
  platform_process_->Wait(wait_time);
}

void SubProcess::Kill() {
  CheckValid();
  platform_process_->Kill();
}

SubProcessStatus SubProcess::GetStatus() {
  CheckValid();
  return platform_process_->GetStatus();
}

SubProcessExitResult SubProcess::GetExitResult() {
  CheckValid();
  return platform_process_->GetExitResult();
}

io::Stream* SubProcess::GetStdinStream() {
  CheckValid();
  return platform_process_->GetStdinStream();
}

io::Stream* SubProcess::GetStdoutStream() {
  CheckValid();
  return platform_process_->GetStdoutStream();
}

io::Stream* SubProcess::GetStderrStream() {
  CheckValid();
  return platform_process_->GetStderrStream();
}

void SubProcess::Detach() {
  auto p = platform_process_.release();
  p->SetDeleteSelfOnExit(true);
}

void SubProcess::CheckValid() const {
  if (!IsValid()) {
    throw SubProcessException(u"SubProcess instance is invalid.");
  }
}

}  // namespace cru