1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dllentry.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #pragma warning(push,1) /* disable warnings within system headers */
39 #include <systools/win32/uwinapi.h>
41 #include <osl/diagnose.h>
42 #include <sal/types.h>
45 #include <osl/diagnose.h>
46 #include <osl/mutex.h>
47 #include <sal/types.h>
49 //------------------------------------------------------------------------------
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 extern void rtl_locale_fini (void);
60 extern void rtl_memory_fini (void);
61 extern void rtl_cache_fini (void);
62 extern void rtl_arena_fini (void);
66 typedef void (*func_ptr
) (void);
67 extern func_ptr __CTOR_LIST__
[];
68 extern func_ptr __DTOR_LIST__
[];
70 static void do_startup(void);
71 static void do_cleanup(void);
76 This is needed because DllMain is called after static constructors. A DLL's
77 startup and shutdown sequence looks like this:
89 static BOOL WINAPI
_RawDllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
);
90 extern BOOL (WINAPI
*_pRawDllMain
)(HANDLE
, DWORD
, LPVOID
) = _RawDllMain
;
94 //------------------------------------------------------------------------------
96 //------------------------------------------------------------------------------
98 DWORD g_dwPlatformId
= VER_PLATFORM_WIN32_WINDOWS
; // remember plattform
100 //------------------------------------------------------------------------------
102 //------------------------------------------------------------------------------
104 int osl_isSingleCPU
= 0;
110 __do_global_dtors (void)
112 static func_ptr
*p
= __DTOR_LIST__
+ 1;
115 * Call each destructor in the destructor list until a null pointer
126 __do_global_ctors (void)
128 unsigned long nptrs
= (unsigned long) __CTOR_LIST__
[0];
132 * If the first entry in the constructor list is -1 then the list
133 * is terminated with a null entry. Otherwise the first entry was
134 * the number of pointers in the list.
138 for (nptrs
= 0; __CTOR_LIST__
[nptrs
+ 1] != 0; nptrs
++)
143 * Go through the list backwards calling constructors.
145 for (i
= nptrs
; i
>= 1; i
--)
151 * Register the destructors for processing on exit.
153 atexit (__do_global_dtors
);
156 static int initialized
= 0;
165 __do_global_ctors ();
169 static void do_startup( void )
172 static BOOL WINAPI
_RawDllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
174 (void)hinstDLL
; /* avoid warnings */
175 (void)lpvReserved
; /* avoid warnings */
179 case DLL_PROCESS_ATTACH
:
185 SYSTEM_INFO SystemInfo
;
187 GetSystemInfo(&SystemInfo
);
189 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
191 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
192 * comes with a cost and is especially expensive on pre HT x86 single processor
193 * systems, where it isn't needed at all.
195 if ( SystemInfo
.dwNumberOfProcessors
== 1 ) {
199 /* Suppress file error messages from system like "Floppy A: not inserted" */
200 SetErrorMode( SEM_NOOPENFILEERRORBOX
| SEM_FAILCRITICALERRORS
);
202 /* initialize global mutex */
203 g_Mutex
= osl_createMutex();
205 /* initialize "current directory" mutex */
206 g_CurrentDirectoryMutex
= osl_createMutex();
209 /* initialize Win9x unicode functions */
210 aInfo
.dwOSVersionInfoSize
= sizeof( OSVERSIONINFO
);
212 if ( GetVersionEx(&aInfo
) )
213 g_dwPlatformId
= aInfo
.dwPlatformId
;
215 g_dwTLSTextEncodingIndex
= TlsAlloc();
216 InitializeCriticalSection( &g_ThreadKeyListCS
);
218 //We disable floating point exceptions. This is the usual state at program startup
219 //but on Windows 98 and ME this is not always the case.
220 _control87(_MCW_EM
, _MCW_EM
);
225 void do_cleanup( void )
231 case DLL_PROCESS_DETACH
:
236 TlsFree( g_dwTLSTextEncodingIndex
);
237 DeleteCriticalSection( &g_ThreadKeyListCS
);
239 osl_destroyMutex( g_Mutex
);
241 osl_destroyMutex( g_CurrentDirectoryMutex
);
247 On a product build memory management finalization might
248 cause a crash without assertion (assertions off) if heap is
249 corrupted. But a crash report won't help here because at
250 this point all other threads have been terminated and only
251 ntdll is on the stack. No chance to find the reason for the
252 corrupted heap if so.
254 So annoying the user with a crash report is completly useless.
262 /* cleanup locale hashtable */
265 /* finalize memory management */
271 __except( EXCEPTION_EXECUTE_HANDLER
)
282 static DWORD
GetParentProcessId()
284 DWORD dwParentProcessId
= 0;
285 HANDLE hSnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 );
287 if ( IsValidHandle( hSnapshot
) )
292 ZeroMemory( &pe
, sizeof(pe
) );
293 pe
.dwSize
= sizeof(pe
);
294 fSuccess
= Process32First( hSnapshot
, &pe
);
298 if ( GetCurrentProcessId() == pe
.th32ProcessID
)
300 dwParentProcessId
= pe
.th32ParentProcessID
;
304 fSuccess
= Process32Next( hSnapshot
, &pe
);
307 CloseHandle( hSnapshot
);
310 return dwParentProcessId
;
313 static DWORD WINAPI
ParentMonitorThreadProc( LPVOID lpParam
)
315 DWORD dwParentProcessId
= (DWORD
)lpParam
;
317 HANDLE hParentProcess
= OpenProcess( SYNCHRONIZE
, FALSE
, dwParentProcessId
);
318 if ( IsValidHandle( hParentProcess
) )
320 if ( WAIT_OBJECT_0
== WaitForSingleObject( hParentProcess
, INFINITE
) )
322 TerminateProcess( GetCurrentProcess(), 0 );
324 CloseHandle( hParentProcess
);
329 BOOL WINAPI
DllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
331 (void)hinstDLL
; /* avoid warning */
332 (void)lpvReserved
; /* avoid warning */
335 case DLL_PROCESS_ATTACH
:
339 // This code will attach the process to it's parent process
340 // if the parent process had set the environment variable.
341 // The corresponding code (setting the environment variable)
342 // is is desktop/win32/source/officeloader.cxx
345 DWORD dwResult
= GetEnvironmentVariable( "ATTACHED_PARENT_PROCESSID", szBuffer
, sizeof(szBuffer
) );
347 if ( dwResult
&& dwResult
< sizeof(szBuffer
) )
349 DWORD dwThreadId
= 0;
351 DWORD dwParentProcessId
= (DWORD
)atol( szBuffer
);
353 if ( dwParentProcessId
&& GetParentProcessId() == dwParentProcessId
)
355 // No error check, it works or it does not
356 // Thread should only be started for headless mode, see desktop/win32/source/officeloader.cxx
357 CreateThread( NULL
, 0, ParentMonitorThreadProc
, (LPVOID
)dwParentProcessId
, 0, &dwThreadId
); //
364 case DLL_THREAD_ATTACH
:
367 case DLL_THREAD_DETACH
:
368 _osl_callThreadKeyCallbackOnThreadDetach( );