From e2822009d7af4f5a52971542686755a39c9b6ea9 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Thu, 13 Jun 2002 15:14:28 +0000 Subject: [PATCH] Fixed driver unloading upon failing initialization. svn path=/trunk/; revision=3062 --- reactos/ntoskrnl/include/internal/ldr.h | 3 ++ reactos/ntoskrnl/io/device.c | 6 ++-- reactos/ntoskrnl/io/driver.c | 36 ++++++++++++++++-------- reactos/ntoskrnl/ldr/loader.c | 37 +++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/reactos/ntoskrnl/include/internal/ldr.h b/reactos/ntoskrnl/include/internal/ldr.h index ff1bd5540dc..09c42a27f5f 100644 --- a/reactos/ntoskrnl/include/internal/ldr.h +++ b/reactos/ntoskrnl/include/internal/ldr.h @@ -97,6 +97,9 @@ NTSTATUS LdrLoadModule(PUNICODE_STRING Filename, PMODULE_OBJECT *ModuleObject); +NTSTATUS +LdrUnloadModule(PMODULE_OBJECT ModuleObject); + PMODULE_OBJECT LdrGetModuleObject(PUNICODE_STRING ModuleName); diff --git a/reactos/ntoskrnl/io/device.c b/reactos/ntoskrnl/io/device.c index eba762b9de1..0cf6e3a1b08 100644 --- a/reactos/ntoskrnl/io/device.c +++ b/reactos/ntoskrnl/io/device.c @@ -1,4 +1,4 @@ -/* $Id: device.c,v 1.45 2002/06/12 23:29:23 ekohl Exp $ +/* $Id: device.c,v 1.46 2002/06/13 15:13:54 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -424,11 +424,12 @@ IopInitializeService( Status = IopInitializeDriver(ModuleObject->EntryPoint, DeviceNode, FALSE); if (!NT_SUCCESS(Status)) { + LdrUnloadModule(ModuleObject); + /* FIXME: Log the error */ CPRINT("A driver failed to initialize\n"); return(Status); } - ObDereferenceObject(ModuleObject); } Status = IopInitializeDevice(DeviceNode, TRUE); @@ -532,6 +533,7 @@ IopInitializeDriver(PDRIVER_INITIALIZE DriverEntry, { DeviceNode->DriverObject = NULL; ExFreePool(DriverObject->DriverExtension); + ObMakeTemporaryObject(DriverObject); ObDereferenceObject(DriverObject); return(Status); } diff --git a/reactos/ntoskrnl/io/driver.c b/reactos/ntoskrnl/io/driver.c index 656987c8ba7..c13a937f418 100644 --- a/reactos/ntoskrnl/io/driver.c +++ b/reactos/ntoskrnl/io/driver.c @@ -1,4 +1,4 @@ -/* $Id: driver.c,v 1.4 2002/06/12 23:29:23 ekohl Exp $ +/* $Id: driver.c,v 1.5 2002/06/13 15:13:54 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -64,9 +64,6 @@ POBJECT_TYPE EXPORTED IoDriverObjectType = NULL; #define TAG_DRIVER TAG('D', 'R', 'V', 'R') #define TAG_DRIVER_EXTENSION TAG('D', 'R', 'V', 'E') -//#define REGSTR_PATH_SERVICES L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\" -//#define REGSTR_PATH_SERVICES_LENGTH (sizeof(REGSTR_PATH_SERVICES)-sizeof(WCHAR)) - /* FUNCTIONS ***************************************************************/ @@ -192,7 +189,6 @@ NtLoadDriver(IN PUNICODE_STRING DriverServiceName) DPRINT("FullImagePath: '%S'\n", FullImagePathBuffer); DPRINT("Type %lx\n", Type); - /* Use IopRootDeviceNode for now */ Status = IopCreateDeviceNode(IopRootDeviceNode, NULL, &DeviceNode); if (!NT_SUCCESS(Status)) @@ -221,8 +217,8 @@ NtLoadDriver(IN PUNICODE_STRING DriverServiceName) (Type == 2 || Type == 8)); if (!NT_SUCCESS(Status)) { - DPRINT1("IopInitializeDriver() failed (Status %lx)\n", Status); - ObDereferenceObject(ModuleObject); + DPRINT("IopInitializeDriver() failed (Status %lx)\n", Status); + LdrUnloadModule(ModuleObject); IopFreeDeviceNode(DeviceNode); } @@ -233,7 +229,9 @@ NtLoadDriver(IN PUNICODE_STRING DriverServiceName) NTSTATUS STDCALL NtUnloadDriver(IN PUNICODE_STRING DriverServiceName) { - UNIMPLEMENTED; + DPRINT("DriverServiceName: '%wZ'\n", DriverServiceName); + + return(STATUS_NOT_IMPLEMENTED); } @@ -463,7 +461,9 @@ IoCreateDriverList(VOID) 0x10001, &ObjectAttributes); if (!NT_SUCCESS(Status)) - return(Status); + { + return(Status); + } KeyInfoLength = sizeof(KEY_BASIC_INFORMATION) + MAX_PATH * sizeof(WCHAR); KeyInfo = ExAllocatePool(NonPagedPool, KeyInfoLength); @@ -494,7 +494,6 @@ IoCreateDriverList(VOID) DPRINT("KeyName: '%wZ'\n", &SubKeyName); IopCreateServiceListEntry(&SubKeyName); - } } @@ -522,6 +521,9 @@ LdrLoadAutoConfigDrivers(VOID) PSERVICE CurrentService; NTSTATUS Status; + CHAR TextBuffer [256]; + ULONG x, y, cx, cy; + DPRINT("LdrLoadAutoConfigDrivers() called\n"); GroupEntry = GroupListHead.Flink; @@ -539,11 +541,23 @@ LdrLoadAutoConfigDrivers(VOID) if ((RtlCompareUnicodeString(&CurrentGroup->GroupName, &CurrentService->ServiceGroup, TRUE) == 0) && (CurrentService->Start == 1 /*SERVICE_SYSTEM_START*/)) { + + HalQueryDisplayParameters(&x, &y, &cx, &cy); + RtlFillMemory(TextBuffer, x, ' '); + TextBuffer[x] = '\0'; + HalSetDisplayParameters(0, y-1); + HalDisplayString(TextBuffer); + + sprintf(TextBuffer, "Loading %S...\n", CurrentService->ServiceName.Buffer); + HalSetDisplayParameters(0, y-1); + HalDisplayString(TextBuffer); + HalSetDisplayParameters(cx, cy); + DPRINT(" Path: %wZ\n", &CurrentService->RegistryPath); Status = NtLoadDriver(&CurrentService->RegistryPath); if (!NT_SUCCESS(Status)) { - DPRINT1("NtLoadDriver() failed (Status %lx)\n", Status); + DPRINT("NtLoadDriver() failed (Status %lx)\n", Status); #if 0 if (CurrentService->ErrorControl == 1) { diff --git a/reactos/ntoskrnl/ldr/loader.c b/reactos/ntoskrnl/ldr/loader.c index d0c2b79b412..a411a5daefd 100644 --- a/reactos/ntoskrnl/ldr/loader.c +++ b/reactos/ntoskrnl/ldr/loader.c @@ -1,4 +1,4 @@ -/* $Id: loader.c,v 1.110 2002/06/12 23:30:36 ekohl Exp $ +/* $Id: loader.c,v 1.111 2002/06/13 15:14:28 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -985,6 +985,39 @@ LdrLoadModule(PUNICODE_STRING Filename, } +NTSTATUS +LdrUnloadModule(PMODULE_OBJECT ModuleObject) +{ + KIRQL Irql; + + /* Remove the module from the module list */ + KeAcquireSpinLock(&ModuleListLock,&Irql); + RemoveEntryList(&ModuleObject->ListEntry); + KeReleaseSpinLock(&ModuleListLock, Irql); + +#ifdef KDBG + /* Unload symbols for module if available */ +// LdrpUnloadModuleSymbols(Module); +#endif /* KDBG */ + + /* Free text section */ + if (ModuleObject->TextSection != NULL) + { + ExFreePool(ModuleObject->TextSection->Name); + RemoveEntryList(&ModuleObject->TextSection->ListEntry); + ExFreePool(ModuleObject->TextSection); + ModuleObject->TextSection = NULL; + } + + /* Free module section */ +// MmFreeSection(ModuleObject->Base); + + ExFreePool(ModuleObject); + + return(STATUS_SUCCESS); +} + + NTSTATUS LdrInitializeBootStartDriver(PVOID ModuleLoadBase, PCHAR FileName, @@ -1377,7 +1410,7 @@ LdrGetModuleObject(PUNICODE_STRING ModuleName) KeReleaseSpinLock(&ModuleListLock, Irql); - CPRINT("LdrpGetModuleObject: Failed to find dll %wZ\n", ModuleName); + CPRINT("LdrpGetModuleObject: Failed to find module %wZ\n", ModuleName); return(NULL); }