Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / w32 / dllentry.c
blob39c6c6137d638203edbc77eaf633e2994496f290
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 #ifdef _MSC_VER
21 #pragma warning(push,1) /* disable warnings within system headers */
22 #endif
23 #include <windows.h>
24 #ifdef _MSC_VER
25 #pragma warning(pop)
26 #endif
27 #include <tlhelp32.h>
28 #include <systools/win32/uwinapi.h>
29 #include <winsock.h>
30 #include <osl/diagnose.h>
31 #include <sal/types.h>
32 #include <float.h>
34 #include <osl/diagnose.h>
35 #include <osl/mutex.h>
36 #include <sal/types.h>
38 #include "internal/rtllifecycle.h"
40 //------------------------------------------------------------------------------
41 // externals
42 //------------------------------------------------------------------------------
44 extern DWORD g_dwTLSTextEncodingIndex;
45 extern void SAL_CALL _osl_callThreadKeyCallbackOnThreadDetach(void);
46 extern CRITICAL_SECTION g_ThreadKeyListCS;
47 extern oslMutex g_Mutex;
48 extern oslMutex g_CurrentDirectoryMutex;
50 #ifdef __MINGW32__
52 typedef void (*func_ptr) (void);
53 extern func_ptr __CTOR_LIST__[];
54 extern func_ptr __DTOR_LIST__[];
56 static void do_startup(void);
57 static void do_cleanup(void);
59 #else
62 This is needed because DllMain is called after static constructors. A DLL's
63 startup and shutdown sequence looks like this:
65 _pRawDllMain()
66 _CRT_INIT()
67 DllMain()
68 ....
69 DllMain()
70 _CRT_INIT()
71 _pRawDllMain()
75 static BOOL WINAPI _RawDllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved );
76 extern BOOL (WINAPI *_pRawDllMain)(HANDLE, DWORD, LPVOID) = _RawDllMain;
78 #endif
80 #ifdef __MINGW32__
82 void
83 __do_global_dtors (void)
85 static func_ptr *p = __DTOR_LIST__ + 1;
88 * Call each destructor in the destructor list until a null pointer
89 * is encountered.
91 while (*p)
93 (*(p)) ();
94 p++;
98 void
99 __do_global_ctors (void)
101 sal_uIntPtr nptrs = (sal_uIntPtr) __CTOR_LIST__[0];
102 unsigned i;
105 * If the first entry in the constructor list is -1 then the list
106 * is terminated with a null entry. Otherwise the first entry was
107 * the number of pointers in the list.
109 if (nptrs == (sal_uIntPtr)-1)
111 for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
116 * Go through the list backwards calling constructors.
118 for (i = nptrs; i >= 1; i--)
120 __CTOR_LIST__[i] ();
124 * Register the destructors for processing on exit.
126 atexit (__do_global_dtors);
129 static int initialized = 0;
131 void
132 __main (void)
134 if (!initialized)
136 initialized = 1;
137 do_startup();
138 __do_global_ctors ();
142 static void do_startup( void )
144 #else
145 static BOOL WINAPI _RawDllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
147 (void)hinstDLL; /* avoid warnings */
148 (void)lpvReserved; /* avoid warnings */
150 switch (fdwReason)
152 case DLL_PROCESS_ATTACH:
154 #endif
156 #if OSL_DEBUG_LEVEL < 2
157 /* Suppress file error messages from system like "Floppy A: not inserted" */
158 SetErrorMode( SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS );
159 #endif
161 /* initialize global mutex */
162 g_Mutex = osl_createMutex();
164 /* initialize "current directory" mutex */
165 g_CurrentDirectoryMutex = osl_createMutex();
167 g_dwTLSTextEncodingIndex = TlsAlloc();
168 InitializeCriticalSection( &g_ThreadKeyListCS );
170 //We disable floating point exceptions. This is the usual state at program startup
171 //but on Windows 98 and ME this is not always the case.
172 _control87(_MCW_EM, _MCW_EM);
173 #ifdef __MINGW32__
174 atexit(do_cleanup);
177 void do_cleanup( void )
179 #else
180 break;
183 case DLL_PROCESS_DETACH:
184 #endif
186 WSACleanup( );
188 TlsFree( g_dwTLSTextEncodingIndex );
189 DeleteCriticalSection( &g_ThreadKeyListCS );
191 osl_destroyMutex( g_Mutex );
193 osl_destroyMutex( g_CurrentDirectoryMutex );
195 #ifndef __MINGW32__
199 On a product build memory management finalization might
200 cause a crash without assertion (assertions off) if heap is
201 corrupted. But a crash report won't help here because at
202 this point all other threads have been terminated and only
203 ntdll is on the stack. No chance to find the reason for the
204 corrupted heap if so.
206 So annoying the user with a crash report is completely useless.
210 #ifndef DBG_UTIL
211 __try
212 #endif
214 /* cleanup locale hashtable */
215 rtl_locale_fini();
217 /* finalize memory management */
218 rtl_memory_fini();
219 rtl_cache_fini();
220 rtl_arena_fini();
222 #ifndef DBG_UTIL
223 __except( EXCEPTION_EXECUTE_HANDLER )
226 #endif
227 break;
230 return TRUE;
231 #endif
234 static DWORD GetParentProcessId()
236 DWORD dwParentProcessId = 0;
237 HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
239 if ( IsValidHandle( hSnapshot ) )
241 PROCESSENTRY32 pe;
242 BOOL fSuccess;
244 ZeroMemory( &pe, sizeof(pe) );
245 pe.dwSize = sizeof(pe);
246 fSuccess = Process32First( hSnapshot, &pe );
248 while( fSuccess )
250 if ( GetCurrentProcessId() == pe.th32ProcessID )
252 dwParentProcessId = pe.th32ParentProcessID;
253 break;
256 fSuccess = Process32Next( hSnapshot, &pe );
259 CloseHandle( hSnapshot );
262 return dwParentProcessId;
265 static DWORD WINAPI ParentMonitorThreadProc( LPVOID lpParam )
267 DWORD_PTR dwParentProcessId = (DWORD_PTR)lpParam;
269 HANDLE hParentProcess = OpenProcess( SYNCHRONIZE, FALSE, dwParentProcessId );
270 if ( IsValidHandle( hParentProcess ) )
272 if ( WAIT_OBJECT_0 == WaitForSingleObject( hParentProcess, INFINITE ) )
274 TerminateProcess( GetCurrentProcess(), 0 );
276 CloseHandle( hParentProcess );
278 return 0;
281 BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
283 (void)hinstDLL; /* avoid warning */
284 (void)lpvReserved; /* avoid warning */
285 switch (fdwReason)
287 case DLL_PROCESS_ATTACH:
289 TCHAR szBuffer[64];
291 // This code will attach the process to it's parent process
292 // if the parent process had set the environment variable.
293 // The corresponding code (setting the environment variable)
294 // is is desktop/win32/source/officeloader.cxx
297 DWORD dwResult = GetEnvironmentVariable( "ATTACHED_PARENT_PROCESSID", szBuffer, sizeof(szBuffer) );
299 if ( dwResult && dwResult < sizeof(szBuffer) )
301 DWORD dwThreadId = 0;
303 DWORD_PTR dwParentProcessId = (DWORD_PTR)atol( szBuffer );
305 if ( dwParentProcessId && GetParentProcessId() == dwParentProcessId )
307 // No error check, it works or it does not
308 // Thread should only be started for headless mode, see desktop/win32/source/officeloader.cxx
309 CreateThread( NULL, 0, ParentMonitorThreadProc, (LPVOID)dwParentProcessId, 0, &dwThreadId ); //
313 return TRUE;
316 case DLL_THREAD_ATTACH:
317 break;
319 case DLL_THREAD_DETACH:
320 _osl_callThreadKeyCallbackOnThreadDetach( );
321 break;
324 return TRUE;
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */