1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #pragma warning(push,1) /* disable warnings within system headers */
28 #include <systools/win32/uwinapi.h>
30 #include <osl/diagnose.h>
31 #include <sal/types.h>
34 #include <osl/diagnose.h>
35 #include <osl/mutex.h>
36 #include <sal/types.h>
38 #include "internal/rtllifecycle.h"
40 //------------------------------------------------------------------------------
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
;
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);
62 This is needed because DllMain is called after static constructors. A DLL's
63 startup and shutdown sequence looks like this:
75 static BOOL WINAPI
_RawDllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
);
76 extern BOOL (WINAPI
*_pRawDllMain
)(HANDLE
, DWORD
, LPVOID
) = _RawDllMain
;
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
99 __do_global_ctors (void)
101 sal_uIntPtr nptrs
= (sal_uIntPtr
) __CTOR_LIST__
[0];
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
--)
124 * Register the destructors for processing on exit.
126 atexit (__do_global_dtors
);
129 static int initialized
= 0;
138 __do_global_ctors ();
142 static void do_startup( void )
145 static BOOL WINAPI
_RawDllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
147 (void)hinstDLL
; /* avoid warnings */
148 (void)lpvReserved
; /* avoid warnings */
152 case DLL_PROCESS_ATTACH
:
156 #if OSL_DEBUG_LEVEL < 2
157 /* Suppress file error messages from system like "Floppy A: not inserted" */
158 SetErrorMode( SEM_NOOPENFILEERRORBOX
| SEM_FAILCRITICALERRORS
);
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
);
177 void do_cleanup( void )
183 case DLL_PROCESS_DETACH
:
188 TlsFree( g_dwTLSTextEncodingIndex
);
189 DeleteCriticalSection( &g_ThreadKeyListCS
);
191 osl_destroyMutex( g_Mutex
);
193 osl_destroyMutex( g_CurrentDirectoryMutex
);
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.
214 /* cleanup locale hashtable */
217 /* finalize memory management */
223 __except( EXCEPTION_EXECUTE_HANDLER
)
234 static DWORD
GetParentProcessId()
236 DWORD dwParentProcessId
= 0;
237 HANDLE hSnapshot
= CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS
, 0 );
239 if ( IsValidHandle( hSnapshot
) )
244 ZeroMemory( &pe
, sizeof(pe
) );
245 pe
.dwSize
= sizeof(pe
);
246 fSuccess
= Process32First( hSnapshot
, &pe
);
250 if ( GetCurrentProcessId() == pe
.th32ProcessID
)
252 dwParentProcessId
= pe
.th32ParentProcessID
;
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
);
281 BOOL WINAPI
DllMain( HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
283 (void)hinstDLL
; /* avoid warning */
284 (void)lpvReserved
; /* avoid warning */
287 case DLL_PROCESS_ATTACH
:
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
); //
316 case DLL_THREAD_ATTACH
:
319 case DLL_THREAD_DETACH
:
320 _osl_callThreadKeyCallbackOnThreadDetach( );
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */