summaryrefslogtreecommitdiff
path: root/desktop/win32/source/guistdio/guistdio.inc
blob: aaf51acc96c56425c2569030b29265da32050afe (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
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
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: guistdio.inc,v $
 * $Revision: 1.3 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#define UNICODE
#define WIN32_LEAN_AND_MEAN
#ifdef _MSC_VER
#pragma warning(push,1) // disable warnings within system headers
#endif
#include <windows.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif

#define _UNICODE
#include <tchar.h>

#include <string.h>
#include <stdlib.h>
#include <systools/win32/uwinapi.h>

//---------------------------------------------------------------------------
// Thread that reads from child process standard output pipe
//---------------------------------------------------------------------------

DWORD WINAPI OutputThread( LPVOID pParam )
{
	BYTE	aBuffer[256];
	DWORD	dwRead = 0;
	HANDLE	hReadPipe = (HANDLE)pParam;

	while ( ReadFile( hReadPipe, &aBuffer, sizeof(aBuffer), &dwRead, NULL ) )
	{
		BOOL	fSuccess;
		DWORD	dwWritten;

		fSuccess = WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), aBuffer, dwRead, &dwWritten, NULL );
	}

	return 0;
}

//---------------------------------------------------------------------------
// Thread that reads from child process standard error pipe
//---------------------------------------------------------------------------

DWORD WINAPI ErrorThread( LPVOID pParam )
{
	BYTE	aBuffer[256];
	DWORD	dwRead = 0;
	HANDLE	hReadPipe = (HANDLE)pParam;

	while ( ReadFile( hReadPipe, &aBuffer, sizeof(aBuffer), &dwRead, NULL ) )
	{
		BOOL	fSuccess;
		DWORD	dwWritten;

		fSuccess = WriteFile( GetStdHandle( STD_ERROR_HANDLE ), aBuffer, dwRead, &dwWritten, NULL );
	}

	return 0;
}

//---------------------------------------------------------------------------
// Thread that writes to child process standard input pipe
//---------------------------------------------------------------------------

DWORD WINAPI InputThread( LPVOID pParam )
{
	BYTE	aBuffer[256];
	DWORD	dwRead = 0;
	HANDLE	hWritePipe = (HANDLE)pParam;

	while ( ReadFile( GetStdHandle( STD_INPUT_HANDLE ), &aBuffer, sizeof(aBuffer), &dwRead, NULL ) )
	{
		BOOL	fSuccess;
		DWORD	dwWritten;

		fSuccess = WriteFile( hWritePipe, aBuffer, dwRead, &dwWritten, NULL );
	}

	return 0;
}

//---------------------------------------------------------------------------
// Thread that waits until child process reached input idle
//---------------------------------------------------------------------------

DWORD WINAPI WaitForUIThread( LPVOID pParam )
{
	HANDLE	hProcess = (HANDLE)pParam;

#ifndef GUISTDIO_KEEPRUNNING
	if ( !_tgetenv( TEXT("GUISTDIO_KEEPRUNNING") ) )
		WaitForInputIdle( hProcess, INFINITE );
	else
#endif
		WaitForSingleObject( hProcess, INFINITE );

	return 0;
}


//---------------------------------------------------------------------------
// Ctrl-Break handler that terminates the child process if Ctrl-C was pressed
//---------------------------------------------------------------------------

HANDLE	hTargetProcess = INVALID_HANDLE_VALUE;

BOOL WINAPI CtrlBreakHandler(
  DWORD   //  control signal type
)
{
	TerminateProcess( hTargetProcess, 255 );
	return TRUE;
}


//---------------------------------------------------------------------------

//---------------------------------------------------------------------------

#ifdef __MINGW32__
int main( int, char ** )
#else
int _tmain( int, _TCHAR ** )
#endif
{
	TCHAR				szTargetFileName[MAX_PATH] = TEXT("");
	STARTUPINFO			aStartupInfo;
	PROCESS_INFORMATION	aProcessInfo;

	ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
	aStartupInfo.cb = sizeof(aStartupInfo);
	aStartupInfo.dwFlags = STARTF_USESTDHANDLES;

	// Create an output pipe where the write end is inheritable

	HANDLE	hOutputRead, hOutputWrite;
	
	if ( CreatePipe( &hOutputRead, &hOutputWrite, NULL, 0 ) )
	{
		HANDLE	hTemp;

		DuplicateHandle( GetCurrentProcess(), hOutputWrite, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS );
		CloseHandle( hOutputWrite );
		hOutputWrite = hTemp;

		aStartupInfo.hStdOutput = hOutputWrite;
	}

	// Create an error pipe where the write end is inheritable

	HANDLE	hErrorRead, hErrorWrite;
	
	if ( CreatePipe( &hErrorRead, &hErrorWrite, NULL, 0 ) )
	{
		HANDLE	hTemp;

		DuplicateHandle( GetCurrentProcess(), hErrorWrite, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS );
		CloseHandle( hErrorWrite );
		hErrorWrite = hTemp;

		aStartupInfo.hStdError = hErrorWrite;
	}

	// Create an input pipe where the read end is inheritable

	HANDLE	hInputRead, hInputWrite;

	if ( CreatePipe( &hInputRead, &hInputWrite, NULL, 0 ) )
	{
		HANDLE	hTemp;

		DuplicateHandle( GetCurrentProcess(), hInputRead, GetCurrentProcess(), &hTemp, 0, TRUE, DUPLICATE_SAME_ACCESS );
		CloseHandle( hInputRead );
		hInputRead = hTemp;

		aStartupInfo.hStdInput = hInputRead;
	}

	// Get image path with same name but with .exe extension

	TCHAR				szModuleFileName[MAX_PATH];

	GetModuleFileName( NULL, szModuleFileName, MAX_PATH );
	_TCHAR	*lpLastDot = _tcsrchr( szModuleFileName, '.' );
	if ( lpLastDot && 0 == _tcsicmp( lpLastDot, _T(".COM") ) )
	{
		size_t len = lpLastDot - szModuleFileName;
		_tcsncpy( szTargetFileName, szModuleFileName, len );
		_tcsncpy( szTargetFileName + len, _T(".EXE"), sizeof(szTargetFileName)/sizeof(szTargetFileName[0]) - len );
	}

	// Create process with same command line, environment and stdio handles which
	// are directed to the created pipes

	BOOL	fSuccess = CreateProcess(
		szTargetFileName,
		GetCommandLine(),
		NULL,
		NULL,
		TRUE,
		0,
		NULL,
		NULL,
		&aStartupInfo,
		&aProcessInfo );

	if ( fSuccess )
	{
		// These pipe ends are inherited by the child process and no longer used
		CloseHandle( hOutputWrite );
		CloseHandle( hErrorWrite );
		CloseHandle( hInputRead );

		// Set the Ctrl-Break handler
		hTargetProcess = aProcessInfo.hProcess;
		SetConsoleCtrlHandler( CtrlBreakHandler, TRUE );

		// Create threads that redirect remote pipe io to current process's console stdio

		DWORD	dwOutputThreadId, dwErrorThreadId, dwInputThreadId;

		HANDLE	hOutputThread = CreateThread( NULL, 0, OutputThread, (LPVOID)hOutputRead, 0, &dwOutputThreadId );
		HANDLE	hErrorThread = CreateThread( NULL, 0, OutputThread, (LPVOID)hErrorRead, 0, &dwErrorThreadId );
		HANDLE	hInputThread = CreateThread( NULL, 0, InputThread, (LPVOID)hInputWrite, 0, &dwInputThreadId );

		// Create thread that wait until child process entered input idle

		DWORD	dwWaitForUIThreadId;
		HANDLE	hWaitForUIThread = CreateThread( NULL, 0, WaitForUIThread, (LPVOID)aProcessInfo.hProcess, 0, &dwWaitForUIThreadId );

		DWORD	dwWaitResult;
		bool	bDetach = false;
		int		nOpenPipes = 3;
		HANDLE	hObjects[] = 
			{
				hTargetProcess,
				hWaitForUIThread,
				hInputThread,
				hOutputThread,
				hErrorThread
			};

		do
		{
			dwWaitResult = WaitForMultipleObjects( elementsof(hObjects), hObjects, FALSE, INFINITE );

			switch ( dwWaitResult )
			{
			case WAIT_OBJECT_0:	// The child process has terminated
			case WAIT_OBJECT_0 + 1: // The child process entered input idle
				bDetach = true;
				break;
			case WAIT_OBJECT_0 + 2: // The remote end of stdin pipe was closed
			case WAIT_OBJECT_0 + 3: // The remote end of stdout pipe was closed
			case WAIT_OBJECT_0 + 4: // The remote end of stderr pipe was closed
				bDetach = --nOpenPipes <= 0;
				break;
			default: // Something went wrong
				bDetach = true;
				break;
			}
		} while( !bDetach );
            
        //Even if the child process terminates it is not garanteed that all three pipe threads terminate 
        //as tests have proven. The loop above will be typically terminate because the process has 
        //terminated. Then the pipe threads may not have read all data from the pipes yet. When we close
        //the threads then data may be lost. For example running unopkg without arguments shall print out
        //the help text. Without this workaround some text would be missing.
        //ifdef only for unopkg
#ifdef GUISTDIO_KEEPRUNNING
        Sleep(1000);    
#endif        
		CloseHandle( hOutputThread );
		CloseHandle( hErrorThread );
		CloseHandle( hInputThread );
		CloseHandle( hWaitForUIThread );

		DWORD	dwExitCode = 0;
		GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
		CloseHandle( aProcessInfo.hProcess );
		CloseHandle( aProcessInfo.hThread );

		return dwExitCode;
	}

	return -1;
}