-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnProcess.cpp
More file actions
302 lines (251 loc) · 9.91 KB
/
SpawnProcess.cpp
File metadata and controls
302 lines (251 loc) · 9.91 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#include <stdio.h>
#include <fcntl.h>
#include <regex>
#if defined(_WIN32) || defined(_WIN64)
#include <io.h>
#include <process.h>
#include <stdlib.h>
#include <Windows.h>
static const std::vector<std::string> vEnvFilter{"COMPUTERNAME=","HOMEDRIVE=","HOMEPATH=","USERNAME=","USERPROFILE=","SystemRoot=","TMP=","TEMP=","Path="};
#else
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
extern char **environ;
#define _environ environ
#define _close close
#define _write write
#define _read read
static const std::vector<std::string> vEnvFilter{"USER=", "HOME="};
#endif
#include "SpawnProcess.h"
#define READ_FD 0
#define WRITE_FD 1
mutex SpawnProcess::s_mtxIOstreams;
SpawnProcess::SpawnProcess() : m_fdStdOutPipe{-1,-1}, m_fdStdInPipe{-1,-1}, m_fdStdErrPipe{-1,-1}
{
#if defined(_WIN32) || defined(_WIN64)
m_hProcess = INVALID_HANDLE_VALUE;
#endif
char** aszEnv {_environ};
while (*aszEnv)
{
if (std::find_if(vEnvFilter.begin(), vEnvFilter.end(), [&](auto& strFilter) { return std::string(*aszEnv).find(strFilter) == 0 ? true : false; }) != vEnvFilter.end())
m_envp.push_back(*aszEnv);
++aszEnv;
}
}
SpawnProcess::~SpawnProcess() noexcept
{
if (m_fdStdOutPipe[READ_FD] != -1)
_close(m_fdStdOutPipe[READ_FD]);
if (m_fdStdInPipe[WRITE_FD] != -1)
_close(m_fdStdInPipe[WRITE_FD]);
if (m_fdStdErrPipe[READ_FD] != -1)
_close(m_fdStdErrPipe[READ_FD]);
#if defined(_WIN32) || defined(_WIN64)
if (m_hProcess != INVALID_HANDLE_VALUE)
CloseHandle(m_hProcess);
#endif
}
#if defined(_WIN32) || defined(_WIN64)
int SpawnProcess::Spawn(const wstring& strCmd, const wstring& strWorkingDir/* = wstring()*/)
{
if (_pipe(m_fdStdOutPipe, 65536, O_NOINHERIT | O_BINARY) == -1)
return 1;
DWORD dwMode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
SetNamedPipeHandleState(reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdOutPipe[READ_FD])), &dwMode, nullptr, nullptr);
SetHandleInformation(reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdOutPipe[WRITE_FD])), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
if (_pipe(m_fdStdInPipe, 65536, O_NOINHERIT | O_BINARY) == -1)
return 1;
SetHandleInformation(reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdInPipe[READ_FD])), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
if (_pipe(m_fdStdErrPipe, 65536, O_NOINHERIT | O_BINARY) == -1)
return 1;
SetNamedPipeHandleState(reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdErrPipe[READ_FD])), &dwMode, nullptr, nullptr);
SetHandleInformation(reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdErrPipe[WRITE_FD])), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
STARTUPINFOA stInfo{};
PROCESS_INFORMATION ProcInfo = { nullptr, nullptr, 0, 0 };
stInfo.cb = sizeof(STARTUPINFO);
stInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
stInfo.wShowWindow = 0/*SW_HIDE*/;
stInfo.hStdInput = reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdInPipe[READ_FD]));
stInfo.hStdOutput = reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdOutPipe[WRITE_FD]));
stInfo.hStdError = reinterpret_cast<HANDLE>(_get_osfhandle(m_fdStdErrPipe[WRITE_FD]));
string strEnvironment;
for (auto& pstr : m_envp)
strEnvironment += string(pstr) + '\0';
for (auto& str : m_vstrEnvironment)
strEnvironment += str + '\0';
strEnvironment += '\0';
if (CreateProcessA(nullptr, &wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(strCmd)[0], nullptr, nullptr, TRUE, CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP | NORMAL_PRIORITY_CLASS, &strEnvironment[0], &wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(strWorkingDir)[0], &stInfo, &ProcInfo) == TRUE)
{
CloseHandle(ProcInfo.hThread);
m_hProcess = ProcInfo.hProcess;
}
else
{
static DWORD dwError = GetLastError();
m_hProcess = INVALID_HANDLE_VALUE;
}
_close(m_fdStdInPipe[READ_FD]);
m_fdStdInPipe[READ_FD] = -1;
_close(m_fdStdOutPipe[WRITE_FD]);
m_fdStdOutPipe[WRITE_FD] = -1;
_close(m_fdStdErrPipe[WRITE_FD]);
m_fdStdErrPipe[WRITE_FD] = -1;
if (m_hProcess == INVALID_HANDLE_VALUE)
{
_close(m_fdStdOutPipe[READ_FD]);
m_fdStdOutPipe[READ_FD] = -1;
_close(m_fdStdInPipe[WRITE_FD]);
m_fdStdInPipe[WRITE_FD] = -1;
_close(m_fdStdErrPipe[READ_FD]);
m_fdStdErrPipe[READ_FD] = -1;
return 4;
}
return 0;
}
#else
int SpawnProcess::Spawn(const wstring& strCmd, const wstring& strWorkingDir/* = wstring()*/)
{
string l_strCmd = wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(strCmd);
//const static regex rx("([^\\s\\\"]+|(?:\\\"[^\\\"]*\\\")+)+\\s*");
const static regex rx("([^\\s\\\"]+)(?:\\s|$)|((?:[^\\s]*\\\"[^\\\"]*\\\"[^\\s\\\"]*)+)+(?:\\s|$)");
vector<string> token;
smatch mr;
while (regex_search(l_strCmd, mr, rx) == true && mr[0].matched == true)
{
token.push_back(mr[0].str());
l_strCmd.erase(0, mr[0].length());
token.back().erase(token.back().find_last_not_of("\" \t\r\n") + 1); // Trim Whitespace and " character on the right
token.back().erase(0, token.back().find_first_not_of("\" \t")); // Trim Whitespace and " character on the left
}
unique_ptr<char*[]> wargv(new char*[token.size() + 1]);
for (size_t n = 0; n < token.size(); ++n)
wargv.get()[n] = &token[n][0];
wargv.get()[token.size()] = nullptr;
for (auto& str : m_vstrEnvironment)
m_envp.push_back(&str[0]);
m_envp.push_back(nullptr);
if (pipe2(m_fdStdOutPipe, O_CLOEXEC) == -1)
return 1;
fcntl(m_fdStdOutPipe[READ_FD], F_SETFL, fcntl(m_fdStdOutPipe[READ_FD], F_GETFL, 0) | O_NONBLOCK);
if (pipe2(m_fdStdInPipe, O_CLOEXEC) == -1)
return 1;
if (pipe2(m_fdStdErrPipe, O_CLOEXEC) == -1)
return 1;
fcntl(m_fdStdErrPipe[READ_FD], F_SETFL, fcntl(m_fdStdErrPipe[READ_FD], F_GETFL, 0) | O_NONBLOCK);
lock_guard<mutex> lock(s_mtxIOstreams);
// Duplicate stdout file descriptor (next line will close original)
int fdStdOut = dup(fileno(stdout)); // Out as seen by the spawn process
int fdStdIn = dup(fileno(stdin)); // In as seen by the spawn process
int fdStdErr = dup(fileno(stderr)); // Err as seen by the spawn process
// Duplicate write end of pipe to stdout file descriptor
if (dup2(m_fdStdOutPipe[WRITE_FD], fileno(stdout)) < 0)
return 2;
if (dup2(m_fdStdInPipe[READ_FD], fileno(stdin)) < 0)
return 2;
if (dup2(m_fdStdErrPipe[WRITE_FD], fileno(stderr)) < 0)
return 2;
// Close original write end of pipe
_close(m_fdStdOutPipe[WRITE_FD]);
_close(m_fdStdInPipe[READ_FD]);
_close(m_fdStdErrPipe[WRITE_FD]);
string strCurDir(FILENAME_MAX, 0);
if (strWorkingDir.empty() == false && getcwd(&strCurDir[0], FILENAME_MAX) == &strCurDir[0])
{
strCurDir.resize(strCurDir.find_first_of('\0'));
chdir(&wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(strWorkingDir)[0]);
}
int iResult = posix_spawn(&m_hProcess, wargv.get()[0], NULL, NULL, wargv.get(), &m_envp[0]);
if (strCurDir.size() != FILENAME_MAX)
chdir(&strCurDir[0]);
// Duplicate copy of original stdout back into stdout
if (dup2(fdStdOut, fileno(stdout)) < 0)
return 3;
if (dup2(fdStdIn, fileno(stdin)) < 0)
return 3;
if (dup2(fdStdErr, fileno(stderr)) < 0)
return 3;
// Close duplicate copy of original stdout
_close(fdStdOut);
_close(fdStdIn);
_close(fdStdErr);
if (iResult != 0 || m_hProcess == -1)
{
_close(m_fdStdOutPipe[READ_FD]);
m_fdStdOutPipe[READ_FD] = -1;
_close(m_fdStdInPipe[WRITE_FD]);
m_fdStdInPipe[WRITE_FD] = -1;
_close(m_fdStdErrPipe[READ_FD]);
m_fdStdErrPipe[READ_FD] = -1;
return 4;
}
return 0;
}
#endif
int SpawnProcess::KillProcess() noexcept
{
#if defined(_WIN32) || defined(_WIN64)
return TerminateProcess(m_hProcess, 0) != 0 ? 0 : GetLastError();
#else
int iRet = kill(m_hProcess, SIGTERM);
sleep(2); // The process has 2 sec. to shut down until we kill him hard
return iRet | kill(m_hProcess, SIGKILL);
#endif
}
bool SpawnProcess::StillSpawning() noexcept
{
if (m_hProcess)
{
#if defined(_WIN32) || defined(_WIN64)
DWORD nExitCode = STILL_ACTIVE;
if (!GetExitCodeProcess(m_hProcess, &nExitCode))
return false;
return nExitCode == STILL_ACTIVE ? true : false;
#else
int nExitCode = 0;
int status = waitpid(m_hProcess, &nExitCode, WNOHANG);
if (status < 0)
return false;
else if (status == 0)
return true;
return WIFEXITED(nExitCode) || WIFSIGNALED(nExitCode) ? false : true;
#endif
}
return false;
}
size_t SpawnProcess::ReadFromSpawn(unsigned char* const pBuffer, const uint32_t nBufSize) noexcept
{
const int iRead = _read(m_fdStdOutPipe[READ_FD], pBuffer, nBufSize);
if (iRead >= 0)
return iRead;
return 0;
}
size_t SpawnProcess::ReadErrFromSpawn(unsigned char* const pBuffer, const uint32_t nBufSize) noexcept
{
const int iRead = _read(m_fdStdErrPipe[READ_FD], pBuffer, nBufSize);
if (iRead >= 0)
return iRead;
return 0;
}
uint32_t SpawnProcess::WriteToSpawn(const unsigned char* const pBuffer, const uint32_t nBufSize) noexcept
{
const int iWrite = _write(m_fdStdInPipe[WRITE_FD], pBuffer, nBufSize);
if (iWrite >= 0)
return static_cast<uint32_t>(iWrite);
return 0;
}
void SpawnProcess::CloseWritePipe() noexcept
{
_close(m_fdStdInPipe[WRITE_FD]);
m_fdStdInPipe[WRITE_FD] = -1;
}