aboutsummaryrefslogtreecommitdiff
path: root/src/base/SubProcess.cpp
blob: 9a6ee64f37461d8a2caec4655ba5285aad992a2c (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
203
204
205
206
207
208
209
210
#include "cru/base/SubProcess.h"

#include <thread>

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

namespace cru {
PlatformSubProcess::PlatformSubProcess(
    SubProcessStartInfo start_info,
    std::shared_ptr<IPlatformSubProcessImpl> impl)
    : state_(new State(std::move(start_info), std::move(impl))),
      lock_(state_->mutex, std::defer_lock) {}

PlatformSubProcess::~PlatformSubProcess() {}

void PlatformSubProcess::Start() {
  std::lock_guard lock_guard(this->lock_);

  if (this->state_->status != SubProcessStatus::Prepare) {
    throw SubProcessException(u"The process has already tried to start.");
  }

  try {
    this->state_->impl->PlatformCreateProcess(this->state_->start_info);
    this->state_->status = SubProcessStatus::Running;

    auto thread = std::thread([state = state_] {
      std::unique_lock lock(state->mutex);
      state->exit_result = state->impl->PlatformWaitForProcess();
      state->status = SubProcessStatus::Exited;
      state->condition_variable.notify_all();
    });

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

void PlatformSubProcess::Wait(
    std::optional<std::chrono::milliseconds> wait_time) {
  std::lock_guard lock_guard(this->lock_);

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

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

  if (this->state_->status == SubProcessStatus::Exited) {
    return;
  }

  auto predicate = [this] {
    return this->state_->status == SubProcessStatus::Exited;
  };

  if (wait_time) {
    this->state_->condition_variable.wait_for(this->lock_, *wait_time,
                                              predicate);
  } else {
    this->state_->condition_variable.wait(this->lock_, predicate);
  }
}

void PlatformSubProcess::Kill() {
  std::lock_guard lock_guard(this->lock_);

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

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

  if (this->state_->status == SubProcessStatus::Exited) {
    return;
  }

  if (this->state_->killed) {
    return;
  }

  this->state_->impl->PlatformKillProcess();
  this->state_->killed = true;
}

SubProcessStatus PlatformSubProcess::GetStatus() {
  std::lock_guard lock_guard(this->lock_);
  return this->state_->status;
}

SubProcessExitResult PlatformSubProcess::GetExitResult() {
  std::lock_guard lock_guard(this->lock_);

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

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

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

  return this->state_->exit_result;
}

io::Stream* PlatformSubProcess::GetStdinStream() {
  return this->state_->impl->GetStdinStream();
}

io::Stream* PlatformSubProcess::GetStdoutStream() {
  return this->state_->impl->GetStdoutStream();
}

io::Stream* PlatformSubProcess::GetStderrStream() {
  return this->state_->impl->GetStderrStream();
}

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));
}

SubProcessExitResult SubProcess::Call(
    String program, std::vector<String> arguments,
    std::unordered_map<String, String> environments) {
  auto process =
      Create(std::move(program), std::move(arguments), std::move(environments));
  process.Wait();
  return process.GetExitResult();
}

SubProcess::SubProcess(SubProcessStartInfo start_info) {
  
#ifdef CRU_PLATFORM_UNIX
  platform_process_.reset(new PlatformSubProcess(
    std::move(start_info),
    std::make_shared<platform::unix::PosixSpawnSubProcessImpl>()));
#else
  NotImplemented();
#endif
  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(); }

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

}  // namespace cru