/
SubProcess_win.cpp
347 lines (270 loc) · 8.52 KB
/
SubProcess_win.cpp
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* SubProcess_win.cpp
*
* Created on: Feb 11, 2016
* Author: lion
*/
#ifdef _WIN32
#include "object.h"
#include "ifs/process.h"
#include "SubProcess.h"
#include <vector>
#include <psapi.h>
#include <tlhelp32.h>
#include "utf8.h"
namespace fibjs {
class PSTimer : public Timer {
public:
PSTimer(int32_t timeout, HANDLE pid)
: Timer(timeout)
, m_pid(pid)
{
}
public:
virtual void on_timer()
{
TerminateProcess(m_pid, -1);
}
private:
HANDLE m_pid;
};
result_t create_name_pipe(HANDLE* hd1, HANDLE* hd2, bool in)
{
HANDLE hMine, hChild;
char fname[128];
do {
sprintf(fname, "\\\\.\\pipe\\fibs.namedpipe.%d", rand());
hMine = CreateNamedPipe(fname, (in ? PIPE_ACCESS_OUTBOUND : PIPE_ACCESS_INBOUND) | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
1, 4096, 4096, 0, NULL);
} while ((hMine == INVALID_HANDLE_VALUE) && (GetLastError() == ERROR_PIPE_BUSY));
if (hMine == INVALID_HANDLE_VALUE)
return CHECK_ERROR(LastError());
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
hChild = CreateFile(fname, in ? GENERIC_READ : GENERIC_WRITE, 0, &sa, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if (hChild == INVALID_HANDLE_VALUE) {
CloseHandle(hMine);
return CHECK_ERROR(LastError());
}
*hd1 = hMine;
*hd2 = hChild;
return 0;
}
result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8::Local<v8::Object> opts,
bool redirect, obj_ptr<SubProcess_base>& retVal)
{
result_t hr;
PROCESS_INFORMATION pi;
STARTUPINFOW si;
int32_t len = args->Length();
int32_t i;
HANDLE cin_pipe[2] = { 0 };
HANDLE cout_pipe[2] = { 0 };
int32_t timeout;
exlib::wstring wstr;
wstr.append(1, '\"');
wstr.append(utf8to16String(command));
wstr.append(1, '\"');
Isolate* isolate = Isolate::current();
hr = GetConfigValue(isolate->m_isolate, opts, "timeout", timeout);
if (hr == CALL_E_PARAMNOTOPTIONAL)
timeout = 0;
else if (hr < 0)
return CHECK_ERROR(hr);
for (i = 0; i < len; i++) {
exlib::string str;
hr = GetArgumentValue(isolate->m_isolate, args->Get(i), str);
if (hr < 0)
return hr;
wstr.append(1, ' ');
wstr.append(utf8to16String(str));
}
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
if (redirect) {
hr = create_name_pipe(&cin_pipe[0], &cin_pipe[1], true);
if (hr < 0)
return hr;
hr = create_name_pipe(&cout_pipe[0], &cout_pipe[1], false);
if (hr < 0) {
::CloseHandle(cin_pipe[0]);
::CloseHandle(cin_pipe[1]);
return hr;
}
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
si.hStdOutput = cout_pipe[1];
si.hStdInput = cin_pipe[1];
si.dwFlags |= STARTF_USESTDHANDLES;
}
exlib::wstring envstr;
v8::Local<v8::Object> envs;
hr = GetConfigValue(isolate->m_isolate, opts, "env", envs, true);
if (hr == CALL_E_PARAMNOTOPTIONAL)
hr = process_base::get_env(envs);
if (hr < 0)
return hr;
v8::Local<v8::Array> keys = envs->GetPropertyNames();
len = (int32_t)keys->Length();
for (i = 0; i < len; i++) {
v8::Local<v8::Value> k = keys->Get(i);
v8::Local<v8::Value> v = envs->Get(k);
exlib::string ks, vs;
hr = GetArgumentValue(k, ks);
if (hr < 0)
return hr;
hr = GetArgumentValue(v, vs);
if (hr < 0)
return hr;
ks.append(1, '=');
ks.append(vs);
envstr.append(utf8to16String(ks));
envstr.append(1, 0);
}
envstr.append(1, 0);
if (!CreateProcessW(NULL, &wstr[0], NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT,
&envstr[0], NULL, &si, &pi)) {
::CloseHandle(cin_pipe[0]);
::CloseHandle(cin_pipe[1]);
::CloseHandle(cout_pipe[0]);
::CloseHandle(cout_pipe[1]);
return CHECK_ERROR(LastError());
}
::CloseHandle(pi.hThread);
obj_ptr<SubProcess> sub = new SubProcess((intptr_t)pi.hProcess);
if (redirect) {
::CloseHandle(cin_pipe[1]);
::CloseHandle(cout_pipe[1]);
wrap_pipe((intptr_t)cin_pipe[0], sub->m_stdin);
wrap_pipe((intptr_t)cout_pipe[0], sub->m_stdout);
}
if (timeout > 0) {
sub->m_timer = new PSTimer(timeout, pi.hProcess);
sub->m_timer->sleep();
}
retVal = sub;
return 0;
}
result_t SubProcess::get_pid(int32_t& retVal)
{
retVal = GetProcessId((HANDLE)m_pid);
return 0;
}
result_t SubProcess::kill(int32_t signal)
{
if (m_timer)
m_timer->clear();
if (!TerminateProcess((HANDLE)m_pid, -1))
return CHECK_ERROR(LastError());
return 0;
}
result_t SubProcess::wait(int32_t& retVal, AsyncEvent* ac)
{
class asyncWaitHandle : public AsyncState {
public:
asyncWaitHandle(intptr_t pid, Timer* timer, int32_t& retVal, AsyncEvent* ac)
: AsyncState(ac)
, m_hProcess((HANDLE)pid)
, m_timer(timer)
, m_retVal(retVal)
{
set(wait);
}
public:
static int32_t wait(AsyncState* pState, int32_t n)
{
asyncWaitHandle* pThis = (asyncWaitHandle*)pState;
pThis->set(result);
BOOL result = RegisterWaitForSingleObject(&pThis->m_hWait, pThis->m_hProcess, OnExited, pThis, INFINITE, WT_EXECUTEONLYONCE);
if (!result)
return CHECK_ERROR(LastError());
return CALL_E_PENDDING;
}
static int32_t result(AsyncState* pState, int32_t n)
{
asyncWaitHandle* pThis = (asyncWaitHandle*)pState;
::UnregisterWaitEx(pThis->m_hWait, INVALID_HANDLE_VALUE);
DWORD dwCode;
GetExitCodeProcess(pThis->m_hProcess, &dwCode);
pThis->m_retVal = (int32_t)dwCode;
if (pThis->m_timer)
pThis->m_timer->clear();
return pThis->done();
}
private:
static void CALLBACK OnExited(void* context, BOOLEAN isTimeOut)
{
((asyncWaitHandle*)context)->apost(0);
}
private:
HANDLE m_hProcess;
HANDLE m_hWait;
obj_ptr<Timer> m_timer;
int32_t& m_retVal;
};
if (m_pid == -1)
return CHECK_ERROR(CALL_E_INVALID_CALL);
if (ac->isSync())
return CHECK_ERROR(CALL_E_NOSYNC);
return (new asyncWaitHandle(m_pid, m_timer, retVal, ac))->post(0);
}
struct enumdata {
LPCWSTR name;
wchar_t buf[1024];
bool found;
RECT rect;
};
static BOOL CALLBACK find_window(HWND hwnd, LPARAM lParam)
{
enumdata& ed = *(enumdata*)lParam;
if (IsWindowVisible(hwnd)) {
GetWindowTextW(hwnd, ed.buf, sizeof(ed.buf));
if (!qstrcmp(ed.name, ed.buf)) {
GetWindowRect(hwnd, &ed.rect);
ed.found = TRUE;
return FALSE;
}
EnumChildWindows(hwnd, find_window, lParam);
}
return TRUE;
}
result_t SubProcess::findWindow(exlib::string name, v8::Local<v8::Value>& retVal)
{
DWORD pid = GetProcessId((HANDLE)m_pid);
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (h == INVALID_HANDLE_VALUE)
return CHECK_ERROR(LastError());
enumdata ed = { UTF8_W(name), false };
THREADENTRY32 te;
te.dwSize = sizeof(te);
if (Thread32First(h, &te)) {
do {
if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID) && te.th32OwnerProcessID == pid)
EnumThreadWindows(te.th32ThreadID, find_window, (LPARAM)&ed);
te.dwSize = sizeof(te);
} while (Thread32Next(h, &te) && !ed.found);
}
CloseHandle(h);
Isolate* isolate = holder();
if (ed.found) {
v8::Local<v8::Object> o = v8::Object::New(isolate->m_isolate);
o->Set(isolate->NewString("left"),
v8::Number::New(isolate->m_isolate, ed.rect.left));
o->Set(isolate->NewString("top"),
v8::Number::New(isolate->m_isolate, ed.rect.top));
o->Set(isolate->NewString("right"),
v8::Number::New(isolate->m_isolate, ed.rect.right));
o->Set(isolate->NewString("bottom"),
v8::Number::New(isolate->m_isolate, ed.rect.bottom));
retVal = o;
} else {
retVal = v8::Undefined(isolate->m_isolate);
}
// retVal = ed.found;
return 0;
}
}
#endif