From 19e205fb9797c36df30637ced0c396557592fc78 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Tue, 5 Sep 2000 11:01:03 +0000 Subject: [PATCH] Implemented dll detach on process termination svn path=/trunk/; revision=1332 --- reactos/lib/kernel32/misc/dllmain.c | 16 +++++- reactos/lib/kernel32/process/proc.c | 8 ++- reactos/lib/ntdll/ldr/startup.c | 4 +- reactos/lib/ntdll/ldr/utils.c | 76 +++++++++++++++++++++++++++-- 4 files changed, 97 insertions(+), 7 deletions(-) diff --git a/reactos/lib/kernel32/misc/dllmain.c b/reactos/lib/kernel32/misc/dllmain.c index 5cb1c67cc72..c6109103107 100644 --- a/reactos/lib/kernel32/misc/dllmain.c +++ b/reactos/lib/kernel32/misc/dllmain.c @@ -1,4 +1,4 @@ -/* $Id: dllmain.c,v 1.13 2000/08/15 12:39:18 ekohl Exp $ +/* $Id: dllmain.c,v 1.14 2000/09/05 10:59:27 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -22,6 +22,7 @@ extern UNICODE_STRING SystemDirectory; extern UNICODE_STRING WindowsDirectory; +static WINBOOL DllInitialized = FALSE; WINBOOL STDCALL DllMain (HANDLE hInst, ULONG ul_reason_for_call, @@ -75,12 +76,23 @@ WINBOOL STDCALL DllMain(HANDLE hInst, wcscpy (SystemDirectory.Buffer, WindowsDirectory.Buffer); wcscat (SystemDirectory.Buffer, L"\\System32"); + /* Insert more dll attach stuff here! */ + + DllInitialized = TRUE; + break; } case DLL_PROCESS_DETACH: { DPRINT("DLL_PROCESS_DETACH\n"); - HeapDestroy(NtCurrentPeb()->ProcessHeap); + if (DllInitialized == TRUE) + { + RtlFreeUnicodeString (&SystemDirectory); + RtlFreeUnicodeString (&WindowsDirectory); + + /* Insert more dll detach stuff here! */ + + } break; } default: diff --git a/reactos/lib/kernel32/process/proc.c b/reactos/lib/kernel32/process/proc.c index 7bbf1f430d0..261e409eb21 100644 --- a/reactos/lib/kernel32/process/proc.c +++ b/reactos/lib/kernel32/process/proc.c @@ -1,4 +1,4 @@ -/* $Id: proc.c,v 1.33 2000/06/29 23:35:26 dwelch Exp $ +/* $Id: proc.c,v 1.34 2000/09/05 11:00:07 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -22,6 +22,7 @@ #include #include #include +#include #define NDEBUG @@ -444,6 +445,11 @@ ExitProcess ( UINT uExitCode ) { + /* unload all dll's */ + LdrShutdownProcess (); + + /* FIXME: notify csrss of process termination */ + NtTerminateProcess (NtCurrentProcess (), uExitCode); } diff --git a/reactos/lib/ntdll/ldr/startup.c b/reactos/lib/ntdll/ldr/startup.c index 1667b0cbf3d..46c7a6ec50b 100644 --- a/reactos/lib/ntdll/ldr/startup.c +++ b/reactos/lib/ntdll/ldr/startup.c @@ -1,4 +1,4 @@ -/* $Id: startup.c,v 1.30 2000/09/01 17:05:09 ekohl Exp $ +/* $Id: startup.c,v 1.31 2000/09/05 11:01:02 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -167,6 +167,8 @@ LdrInitializeThunk (ULONG Unknown1, InsertTailList(&Peb->Ldr->InLoadOrderModuleList, &NtModule->InLoadOrderModuleList); + InsertTailList(&Peb->Ldr->InInitializationOrderModuleList, + &NtModule->InInitializationOrderModuleList); /* add entry for executable (becomes first list entry) */ diff --git a/reactos/lib/ntdll/ldr/utils.c b/reactos/lib/ntdll/ldr/utils.c index cde1968fc2e..1e4d43ae608 100644 --- a/reactos/lib/ntdll/ldr/utils.c +++ b/reactos/lib/ntdll/ldr/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.33 2000/09/04 19:05:03 ekohl Exp $ +/* $Id: utils.c,v 1.34 2000/09/05 11:01:03 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -399,6 +399,8 @@ LdrLoadDll (IN PWSTR SearchPath OPTIONAL, /* FIXME: aquire loader lock */ InsertTailList(&NtCurrentPeb()->Ldr->InLoadOrderModuleList, &Module->InLoadOrderModuleList); + InsertTailList(&NtCurrentPeb()->Ldr->InInitializationOrderModuleList, + &Module->InInitializationOrderModuleList); /* FIXME: release loader lock */ /* initialize dll */ @@ -1530,7 +1532,41 @@ LdrGetProcedureAddress (IN PVOID BaseAddress, NTSTATUS STDCALL LdrShutdownProcess (VOID) { - DPRINT1("LdrShutdownProcess() called\n"); + PLIST_ENTRY ModuleListHead; + PLIST_ENTRY Entry; + PLDR_MODULE Module; + + DPRINT("LdrShutdownProcess() called\n"); + + RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock); + + ModuleListHead = &NtCurrentPeb()->Ldr->InInitializationOrderModuleList; + Entry = ModuleListHead->Blink; + + while (Entry != ModuleListHead) + { + Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); + + DPRINT(" Unloading %wZ\n", + &Module->BaseDllName); + + if (Module->EntryPoint != 0) + { + PDLLMAIN_FUNC Entrypoint = (PDLLMAIN_FUNC)Module->EntryPoint; + + DPRINT("Calling entry point at 0x%08x\n", Entrypoint); + Entrypoint (Module->BaseAddress, + DLL_PROCESS_DETACH, + NULL); + } + + Entry = Entry->Blink; + } + + RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock); + + DPRINT("LdrShutdownProcess() done\n"); + return STATUS_SUCCESS; } @@ -1538,7 +1574,41 @@ LdrShutdownProcess (VOID) NTSTATUS STDCALL LdrShutdownThread (VOID) { - DPRINT1("LdrShutdownThread() called\n"); + PLIST_ENTRY ModuleListHead; + PLIST_ENTRY Entry; + PLDR_MODULE Module; + + DPRINT("LdrShutdownThread() called\n"); + + RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock); + + ModuleListHead = &NtCurrentPeb()->Ldr->InInitializationOrderModuleList; + Entry = ModuleListHead->Blink; + + while (Entry != ModuleListHead) + { + Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); + + DPRINT(" Unloading %wZ\n", + &Module->BaseDllName); + + if (Module->EntryPoint != 0) + { + PDLLMAIN_FUNC Entrypoint = (PDLLMAIN_FUNC)Module->EntryPoint; + + DPRINT("Calling entry point at 0x%08x\n", Entrypoint); + Entrypoint (Module->BaseAddress, + DLL_THREAD_DETACH, + NULL); + } + + Entry = Entry->Blink; + } + + RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock); + + DPRINT("LdrShutdownThread() done\n"); + return STATUS_SUCCESS; }