-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_manager.cpp
More file actions
285 lines (251 loc) · 6.76 KB
/
Copy pathprocess_manager.cpp
File metadata and controls
285 lines (251 loc) · 6.76 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "process_manager.hpp"
#include <filesystem>
#include <string>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <csignal>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
namespace droidcli::cli {
ProcessManager::~ProcessManager()
{
std::lock_guard<std::mutex> lock(mutex_);
for (auto& pair : entries_)
{
stop_locked(pair.second);
}
}
core::Array<ProcessInfo> ProcessManager::snapshot()
{
std::lock_guard<std::mutex> lock(mutex_);
core::Array<ProcessInfo> result;
result.reserve(entries_.size());
for (auto& pair : entries_)
{
refresh_locked(pair.second);
result.push_back(pair.second.info);
}
return result;
}
#ifdef _WIN32
bool ProcessManager::launch(
const core::String& key,
const core::String& label,
const core::String& working_dir,
const core::String& command,
core::String& error_out)
{
std::lock_guard<std::mutex> lock(mutex_);
Entry& entry = entries_[key];
stop_locked(entry);
entry.info = ProcessInfo {};
entry.info.key = key;
entry.info.label = label;
entry.info.command = command;
entry.info.working_dir = working_dir;
HANDLE job = CreateJobObjectA(nullptr, nullptr);
if (job == nullptr)
{
error_out = "CreateJobObject failed";
entry.info.status_text = error_out;
return false;
}
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits {};
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
SetInformationJobObject(job, JobObjectExtendedLimitInformation, &limits, sizeof(limits));
// Run through cmd.exe so shell commands (make, .bat) resolve via PATH.
// With NoDefaultCurrentDirectoryInExePath set (as on hardened systems), cmd
// does NOT resolve bare names from the working directory — so if the command
// starts with a file that exists in working_dir, make it explicitly relative.
core::String effective_command = command;
const size_t first_space = effective_command.find(' ');
const core::String first_token =
first_space == core::String::npos ? effective_command : effective_command.substr(0, first_space);
if (!working_dir.empty() && first_token.find_first_of("/\\") == core::String::npos)
{
std::error_code ec;
if (std::filesystem::exists(std::filesystem::path(working_dir) / first_token, ec))
{
effective_command = ".\\" + effective_command;
}
}
core::String command_line = "cmd.exe /c " + effective_command;
std::vector<char> mutable_command(command_line.begin(), command_line.end());
mutable_command.push_back('\0');
STARTUPINFOA startup {};
startup.cb = sizeof(startup);
PROCESS_INFORMATION process {};
const char* cwd = working_dir.empty() ? nullptr : working_dir.c_str();
const DWORD flags = CREATE_SUSPENDED | CREATE_NEW_CONSOLE;
const BOOL created = CreateProcessA(
nullptr, mutable_command.data(), nullptr, nullptr, FALSE, flags, nullptr, cwd, &startup, &process);
if (created == FALSE)
{
const DWORD last_error = GetLastError();
CloseHandle(job);
error_out = "CreateProcess failed (" + std::to_string(last_error) + ")";
entry.info.status_text = error_out;
return false;
}
// Best effort: nested jobs are allowed on Windows 8+, so failure here is non-fatal.
AssignProcessToJobObject(job, process.hProcess);
ResumeThread(process.hThread);
CloseHandle(process.hThread);
entry.job_handle = job;
entry.process_handle = process.hProcess;
entry.info.pid = static_cast<int64_t>(process.dwProcessId);
entry.info.running = true;
entry.info.status_text = "running";
error_out.clear();
return true;
}
void ProcessManager::stop_locked(Entry& entry)
{
if (entry.job_handle != nullptr)
{
TerminateJobObject(static_cast<HANDLE>(entry.job_handle), 1);
CloseHandle(static_cast<HANDLE>(entry.job_handle));
entry.job_handle = nullptr;
}
if (entry.process_handle != nullptr)
{
CloseHandle(static_cast<HANDLE>(entry.process_handle));
entry.process_handle = nullptr;
}
if (entry.info.running)
{
entry.info.status_text = "stopped";
}
entry.info.running = false;
}
void ProcessManager::refresh_locked(Entry& entry)
{
if (entry.process_handle == nullptr)
{
entry.info.running = false;
return;
}
DWORD exit_code = 0;
if (GetExitCodeProcess(static_cast<HANDLE>(entry.process_handle), &exit_code) && exit_code == STILL_ACTIVE)
{
entry.info.running = true;
entry.info.status_text = "running";
return;
}
entry.info.running = false;
entry.info.status_text = "exited (" + std::to_string(exit_code) + ")";
CloseHandle(static_cast<HANDLE>(entry.process_handle));
entry.process_handle = nullptr;
if (entry.job_handle != nullptr)
{
CloseHandle(static_cast<HANDLE>(entry.job_handle));
entry.job_handle = nullptr;
}
}
#else // POSIX
bool ProcessManager::launch(
const core::String& key,
const core::String& label,
const core::String& working_dir,
const core::String& command,
core::String& error_out)
{
std::lock_guard<std::mutex> lock(mutex_);
Entry& entry = entries_[key];
stop_locked(entry);
entry.info = ProcessInfo {};
entry.info.key = key;
entry.info.label = label;
entry.info.command = command;
entry.info.working_dir = working_dir;
const pid_t pid = fork();
if (pid < 0)
{
error_out = "fork failed";
entry.info.status_text = error_out;
return false;
}
if (pid == 0)
{
// Child: new process group so the whole tree can be killed via killpg.
setpgid(0, 0);
if (!working_dir.empty())
{
if (chdir(working_dir.c_str()) != 0)
{
_exit(127);
}
}
execl("/bin/sh", "sh", "-c", command.c_str(), static_cast<char*>(nullptr));
_exit(127);
}
setpgid(pid, pid);
entry.pgid = pid;
entry.info.pid = static_cast<int64_t>(pid);
entry.info.running = true;
entry.info.status_text = "running";
error_out.clear();
return true;
}
void ProcessManager::stop_locked(Entry& entry)
{
if (entry.pgid > 0)
{
killpg(entry.pgid, SIGTERM);
entry.pgid = 0;
if (entry.info.running)
{
entry.info.status_text = "stopped";
}
}
entry.info.running = false;
}
void ProcessManager::refresh_locked(Entry& entry)
{
if (entry.pgid <= 0)
{
entry.info.running = false;
return;
}
int status = 0;
const pid_t result = waitpid(static_cast<pid_t>(entry.info.pid), &status, WNOHANG);
if (result == 0)
{
entry.info.running = true;
entry.info.status_text = "running";
return;
}
entry.info.running = false;
entry.info.status_text = "exited";
entry.pgid = 0;
}
#endif
bool ProcessManager::stop(const core::String& key, core::String& error_out)
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = entries_.find(key);
if (it == entries_.end())
{
error_out = "no process for key";
return false;
}
#ifdef _WIN32
const bool had_process = it->second.process_handle != nullptr;
#else
const bool had_process = it->second.pgid > 0;
#endif
if (!had_process)
{
error_out = "process not running";
return false;
}
stop_locked(it->second);
error_out.clear();
return true;
}
} // namespace droidcli::cli