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
348
349
350
351
352
353
354
355
356
357
358
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
//////////////////////////////////////////////////////////////////////
//
// Redirector - to redirect the input / output of a console
//
// Developer: Jeff Lee
// Dec 10, 2001
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Redir.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//#define _TEST_REDIR
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRedirector::CRedirector() :
m_hStdinWrite(NULL),
m_hStdoutRead(NULL),
m_hChildProcess(NULL),
m_hThread(NULL),
m_hEvtStop(NULL),
m_dwThreadId(0),
m_dwWaitTime(1000)
{
}
CRedirector::~CRedirector()
{
Close();
}
//////////////////////////////////////////////////////////////////////
// CRedirector implementation
//////////////////////////////////////////////////////////////////////
BOOL CRedirector::Open(LPCTSTR pszCmdLine, LPCTSTR pszCurrentDirectory)
{
HANDLE hStdoutReadTmp; // parent stdout read handle
HANDLE hStdoutWrite, hStderrWrite; // child stdout write handle
HANDLE hStdinWriteTmp; // parent stdin write handle
HANDLE hStdinRead; // child stdin read handle
SECURITY_ATTRIBUTES sa;
Close();
hStdoutReadTmp = NULL;
hStdoutWrite = hStderrWrite = NULL;
hStdinWriteTmp = NULL;
hStdinRead = NULL;
// Set up the security attributes struct.
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
BOOL bOK = FALSE;
__try
{
// Create a child stdout pipe.
if (!::CreatePipe(&hStdoutReadTmp, &hStdoutWrite, &sa, 0))
__leave;
// Create a duplicate of the stdout write handle for the std
// error write handle. This is necessary in case the child
// application closes one of its std output handles.
if (!::DuplicateHandle(
::GetCurrentProcess(),
hStdoutWrite,
::GetCurrentProcess(),
&hStderrWrite,
0, TRUE,
DUPLICATE_SAME_ACCESS))
__leave;
// Create a child stdin pipe.
if (!::CreatePipe(&hStdinRead, &hStdinWriteTmp, &sa, 0))
__leave;
// Create new stdout read handle and the stdin write handle.
// Set the inheritance properties to FALSE. Otherwise, the child
// inherits the these handles; resulting in non-closeable
// handles to the pipes being created.
if (!::DuplicateHandle(
::GetCurrentProcess(),
hStdoutReadTmp,
::GetCurrentProcess(),
&m_hStdoutRead,
0, FALSE, // make it uninheritable.
DUPLICATE_SAME_ACCESS))
__leave;
if (!::DuplicateHandle(
::GetCurrentProcess(),
hStdinWriteTmp,
::GetCurrentProcess(),
&m_hStdinWrite,
0, FALSE, // make it uninheritable.
DUPLICATE_SAME_ACCESS))
__leave;
// Close inheritable copies of the handles we do not want to
// be inherited.
DestroyHandle(hStdoutReadTmp);
DestroyHandle(hStdinWriteTmp);
// launch the child process
if (!LaunchChild(pszCmdLine, pszCurrentDirectory,
hStdoutWrite, hStdinRead, hStderrWrite))
__leave;
// Child is launched. Close the parents copy of those pipe
// handles that only the child should have open.
// Make sure that no handles to the write end of the stdout pipe
// are maintained in this process or else the pipe will not
// close when the child process exits and ReadFile will hang.
DestroyHandle(hStdoutWrite);
DestroyHandle(hStdinRead);
DestroyHandle(hStderrWrite);
// Launch a thread to receive output from the child process.
m_hEvtStop = ::CreateEvent(NULL, TRUE, FALSE, NULL);
m_hThread = ::CreateThread(
NULL, 0,
OutputThread,
this,
0,
&m_dwThreadId);
if (!m_hThread)
__leave;
bOK = TRUE;
}
__finally
{
if (!bOK)
{
DWORD dwOsErr = ::GetLastError();
char szMsg[40];
::sprintf(szMsg, "Redirect console error: %x\r\n", dwOsErr);
WriteStdError(szMsg);
DestroyHandle(hStdoutReadTmp);
DestroyHandle(hStdoutWrite);
DestroyHandle(hStderrWrite);
DestroyHandle(hStdinWriteTmp);
DestroyHandle(hStdinRead);
Close();
::SetLastError(dwOsErr);
}
}
return bOK;
}
void CRedirector::Close()
{
if (m_hThread != NULL)
{
// this function might be called from redir thread
if (::GetCurrentThreadId() != m_dwThreadId)
{
ASSERT(m_hEvtStop != NULL);
::SetEvent(m_hEvtStop);
//::WaitForSingleObject(m_hThread, INFINITE);
if (::WaitForSingleObject(m_hThread, 5000) == WAIT_TIMEOUT)
{
WriteStdError(_T("The redir thread is dead\r\n"));
::TerminateThread(m_hThread, -2);
}
}
DestroyHandle(m_hThread);
}
DestroyHandle(m_hEvtStop);
DestroyHandle(m_hChildProcess);
DestroyHandle(m_hStdinWrite);
DestroyHandle(m_hStdoutRead);
m_dwThreadId = 0;
}
// write data to the child's stdin
BOOL CRedirector::Printf(LPCTSTR pszFormat, ...)
{
if (!m_hStdinWrite)
return FALSE;
CString strInput;
va_list argList;
va_start(argList, pszFormat);
strInput.FormatV(pszFormat, argList);
va_end(argList);
DWORD dwWritten;
return ::WriteFile(m_hStdinWrite, (LPCTSTR)strInput,
strInput.GetLength(), &dwWritten, NULL);
}
BOOL CRedirector::LaunchChild(LPCTSTR pszCmdLine,
LPCTSTR pszCurrentDirectory,
HANDLE hStdOut,
HANDLE hStdIn,
HANDLE hStdErr)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
ASSERT(::AfxIsValidString(pszCmdLine));
ASSERT(m_hChildProcess == NULL);
// Set up the start up info struct.
::ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdOutput = hStdOut;
si.hStdInput = hStdIn;
si.hStdError = hStdErr;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
// Note that dwFlags must include STARTF_USESHOWWINDOW if we
// use the wShowWindow flags. This also assumes that the
// CreateProcess() call will use CREATE_NEW_CONSOLE.
// Launch the child process.
if (!::CreateProcess(
NULL,
(LPTSTR)pszCmdLine,
NULL, NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL, pszCurrentDirectory,
&si,
&pi))
return FALSE;
m_hChildProcess = pi.hProcess;
// Close any unuseful handles
::CloseHandle(pi.hThread);
return TRUE;
}
// redirect the child process's stdout:
// return: 1: no more data, 0: child terminated, -1: os error
int CRedirector::RedirectStdout()
{
ASSERT(m_hStdoutRead != NULL);
for (;;)
{
DWORD dwAvail = 0;
if (!::PeekNamedPipe(m_hStdoutRead, NULL, 0, NULL,
&dwAvail, NULL)) // error
break;
if (!dwAvail) // not data available
return 1;
char szOutput[16*1024 + 1];
DWORD dwRead = 0;
if (!::ReadFile(m_hStdoutRead, szOutput, min(16*1024, dwAvail),
&dwRead, NULL) || !dwRead) // error, the child might ended
break;
szOutput[dwRead] = 0;
WriteStdOut(szOutput);
}
DWORD dwError = ::GetLastError();
if (dwError == ERROR_BROKEN_PIPE || // pipe has been ended
dwError == ERROR_NO_DATA) // pipe closing in progress
{
#ifdef _TEST_REDIR
WriteStdOut("\r\n<TEST INFO>: Child process ended\r\n");
#endif
return 0; // child process ended
}
WriteStdError("Read stdout pipe error\r\n");
return -1; // os error
}
void CRedirector::DestroyHandle(HANDLE& rhObject)
{
if (rhObject != NULL)
{
::CloseHandle(rhObject);
rhObject = NULL;
}
}
void CRedirector::WriteStdOut(LPCSTR pszOutput)
{
TRACE("%s", pszOutput);
}
void CRedirector::WriteStdError(LPCSTR pszError)
{
TRACE("%s", pszError);
}
// thread to receive output of the child process
DWORD WINAPI CRedirector::OutputThread(LPVOID lpvThreadParam)
{
HANDLE aHandles[2];
int nRet;
CRedirector* pRedir = (CRedirector*) lpvThreadParam;
ASSERT(pRedir != NULL);
aHandles[0] = pRedir->m_hChildProcess;
aHandles[1] = pRedir->m_hEvtStop;
aHandles[2] = pRedir->m_hStdoutRead;
for (;;)
{
// redirect stdout till there's no more data.
nRet = pRedir->RedirectStdout();
if (nRet <= 0)
break;
// check if the child process has terminated.
DWORD dwRc = ::WaitForMultipleObjects(
3, aHandles, FALSE, pRedir->m_dwWaitTime);
if (WAIT_OBJECT_0 == dwRc || WAIT_FAILED == dwRc ) // the child process ended
{
nRet = pRedir->RedirectStdout();
if (nRet > 0)
nRet = 0;
break;
}
if (WAIT_OBJECT_0+1 == dwRc) // m_hEvtStop was signalled
{
nRet = 1; // cancelled
break;
}
// If we don't sleep here, then syncfrommirror will eat lots of CPU looping here.
Sleep( 20 );
}
// close handles
pRedir->Close();
return nRet;
}
|