Bump for 3.6-28
[LibreOffice.git] / sal / osl / w32 / dllentry.c
blobcc39c99f5988a7411e539932df2f00385727861d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #ifdef _MSC_VER
30 #pragma warning(push,1) /* disable warnings within system headers */
31 #endif
32 #include <windows.h>
33 #ifdef _MSC_VER
34 #pragma warning(pop)
35 #endif
36 #include <tlhelp32.h>
37 #include <systools/win32/uwinapi.h>
38 #include <winsock.h>
39 #include <osl/diagnose.h>
40 #include <sal/types.h>
41 #include <float.h>
43 #include <osl/diagnose.h>
44 #include <osl/mutex.h>
45 #include <sal/types.h>
47 #include "internal/rtllifecycle.h"
49 //------------------------------------------------------------------------------
50 // externals
51 //------------------------------------------------------------------------------
53 extern DWORD g_dwTLSTextEncodingIndex;
54 extern void SAL_CALL _osl_callThreadKeyCallbackOnThreadDetach(void);
55 extern CRITICAL_SECTION g_ThreadKeyListCS;
56 extern oslMutex g_Mutex;
57 extern oslMutex g_CurrentDirectoryMutex;
59 #ifdef __MINGW32__
61 typedef void (*func_ptr) (void);
62 extern func_ptr __CTOR_LIST__[];
63 extern func_ptr __DTOR_LIST__[];
65 static void do_startup(void);
66 static void do_cleanup(void);
68 #else
71 This is needed because DllMain is called after static constructors. A DLL's
72 startup and shutdown sequence looks like this:
74 _pRawDllMain()
75 _CRT_INIT()
76 DllMain()
77 ....
78 DllMain()
79 _CRT_INIT()
80 _pRawDllMain()
84 static BOOL WINAPI _RawDllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved );
85 extern BOOL (WINAPI *_pRawDllMain)(HANDLE, DWORD, LPVOID) = _RawDllMain;
87 #endif
89 //------------------------------------------------------------------------------
90 // DllMain
91 //------------------------------------------------------------------------------
92 int osl_isSingleCPU = 0;
94 #ifdef __MINGW32__
96 void
97 __do_global_dtors (void)
99 static func_ptr *p = __DTOR_LIST__ + 1;
102 * Call each destructor in the destructor list until a null pointer
103 * is encountered.
105 while (*p)
107 (*(p)) ();
108 p++;
112 void
113 __do_global_ctors (void)
115 unsigned long nptrs = (unsigned long) __CTOR_LIST__[0];
116 unsigned i;
119 * If the first entry in the constructor list is -1 then the list
120 * is terminated with a null entry. Otherwise the first entry was
121 * the number of pointers in the list.
123 if (nptrs == (unsigned long)-1)
125 for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
130 * Go through the list backwards calling constructors.
132 for (i = nptrs; i >= 1; i--)
134 __CTOR_LIST__[i] ();
138 * Register the destructors for processing on exit.
140 atexit (__do_global_dtors);
143 static int initialized = 0;
145 void
146 __main (void)
148 if (!initialized)
150 initialized = 1;
151 do_startup();
152 __do_global_ctors ();
156 static void do_startup( void )
158 #else
159 static BOOL WINAPI _RawDllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
161 (void)hinstDLL; /* avoid warnings */
162 (void)lpvReserved; /* avoid warnings */
164 switch (fdwReason)
166 case DLL_PROCESS_ATTACH:
168 #endif
170 SYSTEM_INFO SystemInfo;
172 GetSystemInfo(&SystemInfo);
174 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
176 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
177 * comes with a cost and is especially expensive on pre HT x86 single processor
178 * systems, where it isn't needed at all.
180 if ( SystemInfo.dwNumberOfProcessors == 1 ) {
181 osl_isSingleCPU = 1;
184 #if OSL_DEBUG_LEVEL < 2
185 /* Suppress file error messages from system like "Floppy A: not inserted" */
186 SetErrorMode( SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS );
187 #endif
189 /* initialize global mutex */
190 g_Mutex = osl_createMutex();
192 /* initialize "current directory" mutex */
193 g_CurrentDirectoryMutex = osl_createMutex();
195 g_dwTLSTextEncodingIndex = TlsAlloc();
196 InitializeCriticalSection( &g_ThreadKeyListCS );
198 //We disable floating point exceptions. This is the usual state at program startup
199 //but on Windows 98 and ME this is not always the case.
200 _control87(_MCW_EM, _MCW_EM);
201 #ifdef __MINGW32__
202 atexit(do_cleanup);
205 void do_cleanup( void )
207 #else
208 break;
211 case DLL_PROCESS_DETACH:
212 #endif
214 WSACleanup( );
216 TlsFree( g_dwTLSTextEncodingIndex );
217 DeleteCriticalSection( &g_ThreadKeyListCS );
219 osl_destroyMutex( g_Mutex );
221 osl_destroyMutex( g_CurrentDirectoryMutex );
223 #ifndef __MINGW32__
227 On a product build memory management finalization might
228 cause a crash without assertion (assertions off) if heap is
229 corrupted. But a crash report won't help here because at
230 this point all other threads have been terminated and only
231 ntdll is on the stack. No chance to find the reason for the
232 corrupted heap if so.
234 So annoying the user with a crash report is completly useless.
238 #ifndef DBG_UTIL
239 __try
240 #endif
242 /* cleanup locale hashtable */
243 rtl_locale_fini();
245 /* finalize memory management */
246 rtl_memory_fini();
247 rtl_cache_fini();
248 rtl_arena_fini();
250 #ifndef DBG_UTIL
251 __except( EXCEPTION_EXECUTE_HANDLER )
254 #endif
255 break;
258 return TRUE;
259 #endif
262 static DWORD GetParentProcessId()
264 DWORD dwParentProcessId = 0;
265 HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
267 if ( IsValidHandle( hSnapshot ) )
269 PROCESSENTRY32 pe;
270 BOOL fSuccess;
272 ZeroMemory( &pe, sizeof(pe) );
273 pe.dwSize = sizeof(pe);
274 fSuccess = Process32First( hSnapshot, &pe );
276 while( fSuccess )
278 if ( GetCurrentProcessId() == pe.th32ProcessID )
280 dwParentProcessId = pe.th32ParentProcessID;
281 break;
284 fSuccess = Process32Next( hSnapshot, &pe );
287 CloseHandle( hSnapshot );
290 return dwParentProcessId;
293 static DWORD WINAPI ParentMonitorThreadProc( LPVOID lpParam )
295 DWORD dwParentProcessId = (DWORD)lpParam;
297 HANDLE hParentProcess = OpenProcess( SYNCHRONIZE, FALSE, dwParentProcessId );
298 if ( IsValidHandle( hParentProcess ) )
300 if ( WAIT_OBJECT_0 == WaitForSingleObject( hParentProcess, INFINITE ) )
302 TerminateProcess( GetCurrentProcess(), 0 );
304 CloseHandle( hParentProcess );
306 return 0;
309 BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
311 (void)hinstDLL; /* avoid warning */
312 (void)lpvReserved; /* avoid warning */
313 switch (fdwReason)
315 case DLL_PROCESS_ATTACH:
317 TCHAR szBuffer[64];
319 // This code will attach the process to it's parent process
320 // if the parent process had set the environment variable.
321 // The corresponding code (setting the environment variable)
322 // is is desktop/win32/source/officeloader.cxx
325 DWORD dwResult = GetEnvironmentVariable( "ATTACHED_PARENT_PROCESSID", szBuffer, sizeof(szBuffer) );
327 if ( dwResult && dwResult < sizeof(szBuffer) )
329 DWORD dwThreadId = 0;
331 DWORD dwParentProcessId = (DWORD)atol( szBuffer );
333 if ( dwParentProcessId && GetParentProcessId() == dwParentProcessId )
335 // No error check, it works or it does not
336 // Thread should only be started for headless mode, see desktop/win32/source/officeloader.cxx
337 CreateThread( NULL, 0, ParentMonitorThreadProc, (LPVOID)dwParentProcessId, 0, &dwThreadId ); //
341 return TRUE;
344 case DLL_THREAD_ATTACH:
345 break;
347 case DLL_THREAD_DETACH:
348 _osl_callThreadKeyCallbackOnThreadDetach( );
349 break;
352 return TRUE;
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */