fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / desktop / win32 / source / officeloader / officeloader.cxx
blobe782c90c94b08451fe067b51c98f9394f15bbbe1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #define UNICODE
21 #define _UNICODE
23 #include <cstddef>
24 #include <cwchar>
26 #define WIN32_LEAN_AND_MEAN
27 #if defined _MSC_VER
28 #pragma warning(push, 1)
29 #endif
30 #include <windows.h>
31 #include <shellapi.h>
32 #if defined _MSC_VER
33 #pragma warning(pop)
34 #endif
36 #include <tchar.h>
38 #include <malloc.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <systools/win32/uwinapi.h>
43 #include <rtl/string.h>
44 #include <sal/macros.h>
46 #include "../../../source/inc/exithelper.h"
47 #include "../loader.hxx"
49 #include <config_version.h>
51 #define PIPE_PREFIX TEXT("\\\\.\\pipe\\OSL_PIPE_")
52 #define PIPE_POSTFIX TEXT("_SingleOfficeIPC_")
53 #define PIPE_TERMINATION_SEQUENCE "InternalIPC::ProcessingDone"
55 BOOL WINAPI ConvertSidToStringSid( PSID pSid, LPTSTR* StringSid )
57 PSID_IDENTIFIER_AUTHORITY psia;
58 DWORD dwSubAuthorities;
59 DWORD dwSidRev=SID_REVISION;
60 DWORD dwCounter;
61 DWORD dwSidSize;
63 // Validate the binary SID.
65 if(!IsValidSid(pSid)) return FALSE;
67 // Get the identifier authority value from the SID.
69 psia = GetSidIdentifierAuthority(pSid);
71 // Get the number of subauthorities in the SID.
73 dwSubAuthorities = *GetSidSubAuthorityCount(pSid);
75 // Compute the buffer length.
76 // S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL
78 dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);
80 *StringSid = (LPTSTR)LocalAlloc( LMEM_FIXED, dwSidSize );
82 // Add 'S' prefix and revision number to the string.
84 dwSidSize=wsprintf(*StringSid, TEXT("S-%lu-"), dwSidRev );
86 // Add a SID identifier authority to the string.
88 if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
90 dwSidSize+=wsprintf(*StringSid + lstrlen(*StringSid),
91 TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
92 (USHORT)psia->Value[0],
93 (USHORT)psia->Value[1],
94 (USHORT)psia->Value[2],
95 (USHORT)psia->Value[3],
96 (USHORT)psia->Value[4],
97 (USHORT)psia->Value[5]);
99 else
101 dwSidSize+=wsprintf(*StringSid + lstrlen(*StringSid),
102 TEXT("%lu"),
103 (ULONG)(psia->Value[5] ) +
104 (ULONG)(psia->Value[4] << 8) +
105 (ULONG)(psia->Value[3] << 16) +
106 (ULONG)(psia->Value[2] << 24) );
109 // Add SID subauthorities to the string.
110 for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
112 dwSidSize+=wsprintf(*StringSid + dwSidSize, TEXT("-%lu"),
113 *GetSidSubAuthority(pSid, dwCounter) );
116 return TRUE;
122 static LPTSTR *GetCommandArgs( int *pArgc )
124 #ifdef UNICODE
125 return CommandLineToArgvW( GetCommandLineW(), pArgc );
126 #else
127 *pArgc = __argc;
128 return __argv;
129 #endif
134 namespace {
136 bool writeArgument(HANDLE pipe, char prefix, WCHAR const * argument) {
137 CHAR szBuffer[4096];
138 int n = WideCharToMultiByte(
139 CP_UTF8, 0, argument, -1, szBuffer, sizeof (szBuffer), NULL, NULL);
140 char b[1 + 2 * ((sizeof szBuffer) - 1)]; // hopefully does not overflow
141 b[0] = prefix;
142 char * p = b + 1;
143 for (int i = 0; i < n - 1; ++i) { // cannot underflow (n >= 0)
144 char c = szBuffer[i];
145 switch (c) {
146 case '\0':
147 *p++ = '\\';
148 *p++ = '0';
149 break;
150 case ',':
151 *p++ = '\\';
152 *p++ = ',';
153 break;
154 case '\\':
155 *p++ = '\\';
156 *p++ = '\\';
157 break;
158 default:
159 *p++ = c;
160 break;
163 DWORD w;
164 return WriteFile(pipe, b, p - b, &w, NULL);
169 #ifdef __MINGW32__
170 int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int )
171 #else
172 int WINAPI _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
173 #endif
175 TCHAR szTargetFileName[MAX_PATH] = TEXT("");
176 TCHAR szIniDirectory[MAX_PATH];
177 TCHAR szPerfTuneIniFile[MAX_PATH] = TEXT("");
178 STARTUPINFO aStartupInfo;
180 desktop_win32::getPaths(szTargetFileName, szIniDirectory);
182 ZeroMemory( &aStartupInfo, sizeof(aStartupInfo) );
183 aStartupInfo.cb = sizeof(aStartupInfo);
185 GetStartupInfo( &aStartupInfo );
186 // Get image path with same name but with .bin extension
188 TCHAR szModuleFileName[MAX_PATH];
190 GetModuleFileName( NULL, szModuleFileName, MAX_PATH );
191 _TCHAR *lpLastSlash = _tcsrchr( szModuleFileName, '\\' );
192 if ( lpLastSlash )
194 size_t len = lpLastSlash - szModuleFileName + 1;
195 _tcsncpy( szPerfTuneIniFile, szModuleFileName, len );
196 _tcsncpy( szPerfTuneIniFile + len, _T("perftune.ini"), SAL_N_ELEMENTS(szPerfTuneIniFile) - len );
199 // Create process with same command line, environment and stdio handles which
200 // are directed to the created pipes
202 DWORD dwExitCode = (DWORD)-1;
204 BOOL fSuccess = FALSE;
205 LPTSTR lpCommandLine = NULL;
206 int argc = 0;
207 LPTSTR * argv = NULL;
208 bool bFirst = true;
209 WCHAR cwd[MAX_PATH];
210 DWORD cwdLen = GetCurrentDirectoryW(MAX_PATH, cwd);
211 if (cwdLen >= MAX_PATH) {
212 cwdLen = 0;
217 TCHAR szKey[32];
219 GetPrivateProfileString(
220 TEXT("PerformanceTuning"),
221 TEXT("FastPipeCommunication"),
222 TEXT("0"),
223 szKey,
224 SAL_N_ELEMENTS(szKey),
225 szPerfTuneIniFile
228 if ( 0 == _tcscmp( szKey, TEXT("1") ) )
230 HANDLE hProcessToken;
232 if ( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hProcessToken ) )
234 TCHAR szPipeName[4096];
237 DWORD dwTokenLength = 0;
240 fSuccess = GetTokenInformation( hProcessToken, TokenUser, NULL, dwTokenLength, &dwTokenLength );
242 PVOID pTokenInfo = _alloca(dwTokenLength);
243 fSuccess = GetTokenInformation( hProcessToken, TokenUser, pTokenInfo, dwTokenLength, &dwTokenLength );
244 CloseHandle( hProcessToken );
246 PSID pSid = ((PTOKEN_USER)pTokenInfo)->User.Sid;
247 LPTSTR szUserIdent = NULL;
248 TCHAR szSUPD[11] = TEXT("0");
250 fSuccess = ConvertSidToStringSid( pSid, &szUserIdent );
252 _tcsncpy( szPipeName, PIPE_PREFIX, SAL_N_ELEMENTS(szPipeName) );
253 _tcsncat( szPipeName, szUserIdent, SAL_N_ELEMENTS(szPipeName) - _tcslen(szPipeName) - 1 );
254 _tcsncat( szPipeName, PIPE_POSTFIX, SAL_N_ELEMENTS(szPipeName) - _tcslen(szPipeName) - 1 );
255 _tcsncat( szPipeName, _ultot( LIBO_VERSION_MAJOR * 10000 + LIBO_VERSION_MINOR * 100 + LIBO_VERSION_MICRO * 1
256 , szSUPD, 10), SAL_N_ELEMENTS(szPipeName) - _tcslen(szPipeName) - 1 );
258 LocalFree( szUserIdent );
260 HANDLE hPipe = CreateFile(
261 szPipeName,
262 GENERIC_READ|GENERIC_WRITE,
263 FILE_SHARE_READ | FILE_SHARE_WRITE,
264 NULL,
265 OPEN_EXISTING,
266 FILE_ATTRIBUTE_NORMAL,
267 NULL);
269 if ( INVALID_HANDLE_VALUE != hPipe )
271 DWORD dwBytesRead = 0;
272 char *pBuffer = (char *)_alloca( sizeof("InternalIPC::SendArguments") + 1);
273 fSuccess = ReadFile( hPipe, pBuffer, sizeof("InternalIPC::SendArguments") + 1, &dwBytesRead, NULL );
274 if ( fSuccess )
276 fSuccess = (dwBytesRead == (sizeof("InternalIPC::SendArguments") + 1) &&
277 0 == strncmp( "InternalIPC::SendArguments", pBuffer, dwBytesRead - 1 ) );
279 if ( fSuccess )
281 DWORD dwBytesWritten;
282 int argc2 = 0;
283 LPWSTR *argv2 = CommandLineToArgvW( GetCommandLine(), &argc2 );
285 fSuccess = WriteFile( hPipe, RTL_CONSTASCII_STRINGPARAM("InternalIPC::Arguments"), &dwBytesWritten, NULL );
286 if (fSuccess) {
287 if (cwdLen > 0) {
288 fSuccess = writeArgument(hPipe, '2', cwd);
289 } else {
290 fSuccess = WriteFile(
291 hPipe, RTL_CONSTASCII_STRINGPARAM("0"),
292 &dwBytesWritten, NULL);
295 for ( int argn = 1; fSuccess && argn < argc2; argn++ )
297 if (std::wcsncmp(
298 argv2[argn], L"-env:", std::wcslen(L"-env:"))
299 != 0)
301 fSuccess = writeArgument(hPipe, ',', argv2[argn]);
305 if ( fSuccess )
307 fSuccess = WriteFile( hPipe, "", 1, &dwBytesWritten, NULL );
308 if ( fSuccess )
310 DWORD dwBytesRead2 = 0;
311 char *pBuffer2 = (char *)_alloca( sizeof(PIPE_TERMINATION_SEQUENCE) );
312 fSuccess = ReadFile( hPipe, pBuffer2, sizeof(PIPE_TERMINATION_SEQUENCE) - 1, &dwBytesRead2, NULL );
313 if ( fSuccess )
315 pBuffer2[dwBytesRead2] = 0;
316 if ( 0 != strcmp( PIPE_TERMINATION_SEQUENCE, pBuffer2 ) )
317 fSuccess = FALSE;
322 CloseHandle( hPipe );
324 return fSuccess ? 0 : -1;
330 if ( bFirst ) {
331 argv = GetCommandArgs(&argc);
332 std::size_t n = wcslen(argv[0]) + 2;
333 for (int i = 1; i < argc; ++i) {
334 n += wcslen(argv[i]) + 3;
336 n += MY_LENGTH(L" \"-env:OOO_CWD=2") + 4 * cwdLen +
337 MY_LENGTH(L"\"") + 1;
338 // 4 * cwdLen: each char preceded by backslash, each trailing
339 // backslash doubled
340 lpCommandLine = new WCHAR[n];
342 WCHAR * p = desktop_win32::commandLineAppend(
343 lpCommandLine, MY_STRING(L"\""));
344 p = desktop_win32::commandLineAppend(p, argv[0]);
345 for (int i = 1; i < argc; ++i) {
346 if (bFirst || EXITHELPER_NORMAL_RESTART == dwExitCode || wcsncmp(argv[i], MY_STRING(L"-env:")) == 0) {
347 p = desktop_win32::commandLineAppend(p, MY_STRING(L"\" \""));
348 p = desktop_win32::commandLineAppend(p, argv[i]);
352 p = desktop_win32::commandLineAppend(
353 p, MY_STRING(L"\" \"-env:OOO_CWD="));
354 if (cwdLen == 0) {
355 p = desktop_win32::commandLineAppend(p, MY_STRING(L"0"));
356 } else {
357 p = desktop_win32::commandLineAppend(p, MY_STRING(L"2"));
358 p = desktop_win32::commandLineAppendEncoded(p, cwd);
360 desktop_win32::commandLineAppend(p, MY_STRING(L"\""));
361 bFirst = false;
363 TCHAR szParentProcessId[64]; // This is more than large enough for a 128 bit decimal value
364 BOOL bHeadlessMode( FALSE );
367 // Check command line arguments for "--headless" parameter. We only
368 // set the environment variable "ATTACHED_PARENT_PROCESSID" for the headless
369 // mode as self-destruction of the soffice.bin process can lead to
370 // certain side-effects (log-off can result in data-loss, ".lock" is not deleted.
371 // See 138244 for more information.
372 int argc2;
373 LPTSTR *argv2 = GetCommandArgs( &argc2 );
375 if ( argc2 > 1 )
377 int n;
379 for ( n = 1; n < argc2; n++ )
381 if ( 0 == _tcsnicmp( argv2[n], _T("-headless"), 9 ) ||
382 0 == _tcsnicmp( argv2[n], _T("--headless"), 10 ) )
384 bHeadlessMode = TRUE;
390 if ( _ltot( (long)GetCurrentProcessId(),szParentProcessId, 10 ) && bHeadlessMode )
391 SetEnvironmentVariable( TEXT("ATTACHED_PARENT_PROCESSID"), szParentProcessId );
393 PROCESS_INFORMATION aProcessInfo;
395 fSuccess = CreateProcess(
396 szTargetFileName,
397 lpCommandLine,
398 NULL,
399 NULL,
400 TRUE,
402 NULL,
403 szIniDirectory,
404 &aStartupInfo,
405 &aProcessInfo );
407 if ( fSuccess )
409 DWORD dwWaitResult;
413 // On Windows XP it seems as the desktop calls WaitForInputIdle after "OpenWidth" so we have to do so
414 // as if we where processing any messages
416 dwWaitResult = MsgWaitForMultipleObjects( 1, &aProcessInfo.hProcess, FALSE, INFINITE, QS_ALLEVENTS );
418 if ( WAIT_OBJECT_0 + 1 == dwWaitResult )
420 MSG msg;
422 PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );
424 } while ( WAIT_OBJECT_0 + 1 == dwWaitResult );
426 dwExitCode = 0;
427 GetExitCodeProcess( aProcessInfo.hProcess, &dwExitCode );
429 CloseHandle( aProcessInfo.hProcess );
430 CloseHandle( aProcessInfo.hThread );
432 } while ( fSuccess
433 && ( EXITHELPER_CRASH_WITH_RESTART == dwExitCode || EXITHELPER_NORMAL_RESTART == dwExitCode ));
434 delete[] lpCommandLine;
436 return fSuccess ? dwExitCode : -1;
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */