Author: ion Date: Wed Jun 7 01:02:55 2006 New Revision: 22249
URL: http://svn.reactos.ru/svn/reactos?rev=22249&view=rev Log: - Formatting/name/comment/declaration/calling convention changes. - Make ObpCreateHandleTable return NTSTATUS instead of VOID, so that it can return STATUS_INSUFFIENT_RESOURCES if the handle table couldn't be allocated.
Modified: trunk/reactos/ntoskrnl/include/internal/ob.h trunk/reactos/ntoskrnl/ob/obhandle.c trunk/reactos/ntoskrnl/ps/process.c trunk/reactos/ntoskrnl/ps/psmgr.c
Modified: trunk/reactos/ntoskrnl/include/internal/ob.h URL: http://svn.reactos.ru/svn/reactos/trunk/reactos/ntoskrnl/include/internal/ob... ============================================================================== --- trunk/reactos/ntoskrnl/include/internal/ob.h (original) +++ trunk/reactos/ntoskrnl/include/internal/ob.h Wed Jun 7 01:02:55 2006 @@ -42,25 +42,33 @@
BOOLEAN NTAPI -ObpDeleteEntryDirectory(POBP_LOOKUP_CONTEXT Context); +ObpDeleteEntryDirectory( + IN POBP_LOOKUP_CONTEXT Context +);
BOOLEAN NTAPI -ObpInsertEntryDirectory(IN POBJECT_DIRECTORY Parent, - IN POBP_LOOKUP_CONTEXT Context, - IN POBJECT_HEADER ObjectHeader); +ObpInsertEntryDirectory( + IN POBJECT_DIRECTORY Parent, + IN POBP_LOOKUP_CONTEXT Context, + IN POBJECT_HEADER ObjectHeader +);
PVOID NTAPI -ObpLookupEntryDirectory(IN POBJECT_DIRECTORY Directory, - IN PUNICODE_STRING Name, - IN ULONG Attributes, - IN UCHAR SearchShadow, - IN POBP_LOOKUP_CONTEXT Context); - -VOID -NTAPI -ObInitSymbolicLinkImplementation(VOID); +ObpLookupEntryDirectory( + IN POBJECT_DIRECTORY Directory, + IN PUNICODE_STRING Name, + IN ULONG Attributes, + IN UCHAR SearchShadow, + IN POBP_LOOKUP_CONTEXT Context +); + +VOID +NTAPI +ObInitSymbolicLinkImplementation( + VOID +);
NTSTATUS NTAPI @@ -73,21 +81,15 @@
NTSTATUS NTAPI -ObpParseDirectory( - PVOID Object, - PVOID * NextObject, - PUNICODE_STRING FullPath, - PWSTR * Path, - ULONG Attributes, - POBP_LOOKUP_CONTEXT Context -); - -VOID -NTAPI -ObCreateHandleTable( - struct _EPROCESS* Parent, - BOOLEAN Inherit, - struct _EPROCESS* Process +ObpCreateHandleTable( + IN PEPROCESS Parent, + IN PEPROCESS Process +); + +VOID +NTAPI +ObKillProcess( + IN PEPROCESS Process );
NTSTATUS @@ -137,10 +139,10 @@ ObpGetHandleCountByHandleTable(PHANDLE_TABLE HandleTable);
VOID -STDCALL +NTAPI ObQueryDeviceMapInformation( - PEPROCESS Process, - PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo + IN PEPROCESS Process, + OUT PPROCESS_DEVICEMAP_INFORMATION DeviceMapInfo );
VOID @@ -149,10 +151,6 @@ IN PVOID ObjectBody, IN BOOLEAN Permanent ); - -VOID -STDCALL -ObKillProcess(PEPROCESS Process);
VOID FASTCALL
Modified: trunk/reactos/ntoskrnl/ob/obhandle.c URL: http://svn.reactos.ru/svn/reactos/trunk/reactos/ntoskrnl/ob/obhandle.c?rev=2... ============================================================================== --- trunk/reactos/ntoskrnl/ob/obhandle.c (original) +++ trunk/reactos/ntoskrnl/ob/obhandle.c Wed Jun 7 01:02:55 2006 @@ -323,315 +323,6 @@ return Status; }
-/*++ -* @name ObpSetHandleAttributes -* -* The ObpSetHandleAttributes routine <FILLMEIN> -* -* @param HandleTable -* <FILLMEIN>. -* -* @param HandleTableEntry -* <FILLMEIN>. -* -* @param Context -* <FILLMEIN>. -* -* @return <FILLMEIN>. -* -* @remarks None. -* -*--*/ -BOOLEAN -NTAPI -ObpSetHandleAttributes(IN PHANDLE_TABLE HandleTable, - IN OUT PHANDLE_TABLE_ENTRY HandleTableEntry, - IN PVOID Context) -{ - POBP_SET_HANDLE_ATTRIBUTES_CONTEXT SetHandleInfo = - (POBP_SET_HANDLE_ATTRIBUTES_CONTEXT)Context; - POBJECT_HEADER ObjectHeader = EX_HTE_TO_HDR(HandleTableEntry); - PAGED_CODE(); - - /* Don't allow operations on kernel objects */ - if ((ObjectHeader->Flags & OB_FLAG_KERNEL_MODE) && - (SetHandleInfo->PreviousMode != KernelMode)) - { - /* Fail */ - return FALSE; - } - - /* Check if making the handle inheritable */ - if (SetHandleInfo->Information.Inherit) - { - /* Set the flag. FIXME: Need to check if this is allowed */ - HandleTableEntry->ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE; - } - else - { - /* Otherwise this implies we're removing the flag */ - HandleTableEntry->ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE; - } - - /* Check if making the handle protected */ - if (SetHandleInfo->Information.ProtectFromClose) - { - /* Set the flag */ - HandleTableEntry->ObAttributes |= EX_HANDLE_ENTRY_PROTECTFROMCLOSE; - } - else - { - /* Otherwise, remove it */ - HandleTableEntry->ObAttributes &= ~EX_HANDLE_ENTRY_PROTECTFROMCLOSE; - } - - /* Return success */ - return TRUE; -} - -NTSTATUS -NTAPI -ObDuplicateObject(PEPROCESS SourceProcess, - PEPROCESS TargetProcess, - HANDLE SourceHandle, - PHANDLE TargetHandle, - ACCESS_MASK DesiredAccess, - ULONG HandleAttributes, - ULONG Options) -{ - PHANDLE_TABLE_ENTRY SourceHandleEntry; - HANDLE_TABLE_ENTRY NewHandleEntry; - BOOLEAN AttachedToProcess = FALSE; - PVOID ObjectBody; - POBJECT_HEADER ObjectHeader; - ULONG NewHandleCount; - HANDLE NewTargetHandle; - PEPROCESS CurrentProcess; - KAPC_STATE ApcState; - NTSTATUS Status = STATUS_SUCCESS; - - PAGED_CODE(); - - if(SourceProcess == NULL || - ObIsKernelHandle(SourceHandle, ExGetPreviousMode())) - { - SourceProcess = PsInitialSystemProcess; - SourceHandle = ObKernelHandleToHandle(SourceHandle); - } - - CurrentProcess = PsGetCurrentProcess(); - - KeEnterCriticalRegion(); - - if (SourceProcess != CurrentProcess) - { - KeStackAttachProcess(&SourceProcess->Pcb, - &ApcState); - AttachedToProcess = TRUE; - } - SourceHandleEntry = ExMapHandleToPointer(SourceProcess->ObjectTable, - SourceHandle); - if (SourceHandleEntry == NULL) - { - if (AttachedToProcess) - { - KeUnstackDetachProcess(&ApcState); - } - - KeLeaveCriticalRegion(); - return STATUS_INVALID_HANDLE; - } - - ObjectHeader = EX_HTE_TO_HDR(SourceHandleEntry); - ObjectBody = &ObjectHeader->Body; - - NewHandleEntry.Object = SourceHandleEntry->Object; - if(HandleAttributes & OBJ_INHERIT) - NewHandleEntry.ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE; - else - NewHandleEntry.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE; - NewHandleEntry.GrantedAccess = ((Options & DUPLICATE_SAME_ACCESS) ? - SourceHandleEntry->GrantedAccess : - DesiredAccess); - if (Options & DUPLICATE_SAME_ACCESS) - { - NewHandleEntry.GrantedAccess = SourceHandleEntry->GrantedAccess; - } - else - { - if (DesiredAccess & GENERIC_ACCESS) - { - RtlMapGenericMask(&DesiredAccess, - &ObjectHeader->Type->TypeInfo.GenericMapping); - } - NewHandleEntry.GrantedAccess = DesiredAccess; - } - - /* reference the object so it doesn't get deleted after releasing the lock - and before creating a new handle for it */ - ObReferenceObject(ObjectBody); - - /* increment the handle count of the object, it should always be >= 2 because - we're holding a handle lock to this object! if the new handle count was - 1 here, we're in big trouble... it would've been safe to increment and - check the handle count without using interlocked functions because the - entry is locked, which means the handle count can't change. */ - NewHandleCount = InterlockedIncrement(&ObjectHeader->HandleCount); - ASSERT(NewHandleCount >= 2); - - ExUnlockHandleTableEntry(SourceProcess->ObjectTable, - SourceHandleEntry); - - if (AttachedToProcess) - { - KeUnstackDetachProcess(&ApcState); - AttachedToProcess = FALSE; - } - - if (TargetProcess != CurrentProcess) - { - KeStackAttachProcess(&TargetProcess->Pcb, - &ApcState); - AttachedToProcess = TRUE; - } - - /* attempt to create the new handle */ - NewTargetHandle = ExCreateHandle(TargetProcess->ObjectTable, - &NewHandleEntry); - if (AttachedToProcess) - { - KeUnstackDetachProcess(&ApcState); - AttachedToProcess = FALSE; - } - - if (NewTargetHandle != NULL) - { - if (Options & DUPLICATE_CLOSE_SOURCE) - { - if (SourceProcess != CurrentProcess) - { - KeStackAttachProcess(&SourceProcess->Pcb, - &ApcState); - AttachedToProcess = TRUE; - } - - /* delete the source handle */ - ObpDeleteHandle(SourceHandle); - - if (AttachedToProcess) - { - KeUnstackDetachProcess(&ApcState); - } - } - - ObDereferenceObject(ObjectBody); - - *TargetHandle = NewTargetHandle; - } - else - { - /* decrement the handle count we previously incremented, but don't call the - closing procedure because we're not closing a handle! */ - if(InterlockedDecrement(&ObjectHeader->HandleCount) == 0) - { - ObDereferenceObject(ObjectBody); - } - - ObDereferenceObject(ObjectBody); - Status = STATUS_UNSUCCESSFUL; - } - - KeLeaveCriticalRegion(); - - return Status; -} - -static VOID STDCALL -SweepHandleCallback(PHANDLE_TABLE HandleTable, - PVOID Object, - ULONG GrantedAccess, - PVOID Context) -{ - POBJECT_HEADER ObjectHeader; - PVOID ObjectBody; - - PAGED_CODE(); - - ObjectHeader = EX_OBJ_TO_HDR(Object); - ObjectBody = &ObjectHeader->Body; - - ObpDecrementHandleCount(ObjectBody, PsGetCurrentProcess(), GrantedAccess); -} - -static BOOLEAN STDCALL -DuplicateHandleCallback(PHANDLE_TABLE HandleTable, - PHANDLE_TABLE_ENTRY HandleTableEntry, - PVOID Context) -{ - POBJECT_HEADER ObjectHeader; - BOOLEAN Ret = FALSE; - - PAGED_CODE(); - - Ret = (HandleTableEntry->ObAttributes & EX_HANDLE_ENTRY_INHERITABLE) != 0; - if(Ret) - { - ObjectHeader = EX_HTE_TO_HDR(HandleTableEntry); - if(InterlockedIncrement(&ObjectHeader->HandleCount) == 1) - { - ObReferenceObject(&ObjectHeader->Body); - } - } - - return Ret; -} - -VOID -NTAPI -ObCreateHandleTable(PEPROCESS Parent, - BOOLEAN Inherit, - PEPROCESS Process) - /* - * FUNCTION: Creates a handle table for a process - * ARGUMENTS: - * Parent = Parent process (or NULL if this is the first process) - * Inherit = True if the process should inherit its parent's handles - * Process = Process whose handle table is to be created - */ -{ - PAGED_CODE(); - - DPRINT("ObCreateHandleTable(Parent %x, Inherit %d, Process %x)\n", - Parent,Inherit,Process); - if(Parent != NULL) - { - Process->ObjectTable = ExDupHandleTable(Process, - DuplicateHandleCallback, - NULL, - Parent->ObjectTable); - } - else - { - Process->ObjectTable = ExCreateHandleTable(Process); - } -} - - -VOID -STDCALL -ObKillProcess(PEPROCESS Process) -{ - PAGED_CODE(); - - /* FIXME - Temporary hack: sweep and destroy here, needs to be fixed!!! */ - ExSweepHandleTable(Process->ObjectTable, - SweepHandleCallback, - Process); - ExDestroyHandleTable(Process->ObjectTable); - Process->ObjectTable = NULL; -} - - NTSTATUS NTAPI ObpCreateHandle(PVOID ObjectBody, @@ -730,6 +421,321 @@ }
return STATUS_UNSUCCESSFUL; +} + +/*++ +* @name ObpSetHandleAttributes +* +* The ObpSetHandleAttributes routine <FILLMEIN> +* +* @param HandleTable +* <FILLMEIN>. +* +* @param HandleTableEntry +* <FILLMEIN>. +* +* @param Context +* <FILLMEIN>. +* +* @return <FILLMEIN>. +* +* @remarks None. +* +*--*/ +BOOLEAN +NTAPI +ObpSetHandleAttributes(IN PHANDLE_TABLE HandleTable, + IN OUT PHANDLE_TABLE_ENTRY HandleTableEntry, + IN PVOID Context) +{ + POBP_SET_HANDLE_ATTRIBUTES_CONTEXT SetHandleInfo = + (POBP_SET_HANDLE_ATTRIBUTES_CONTEXT)Context; + POBJECT_HEADER ObjectHeader = EX_HTE_TO_HDR(HandleTableEntry); + PAGED_CODE(); + + /* Don't allow operations on kernel objects */ + if ((ObjectHeader->Flags & OB_FLAG_KERNEL_MODE) && + (SetHandleInfo->PreviousMode != KernelMode)) + { + /* Fail */ + return FALSE; + } + + /* Check if making the handle inheritable */ + if (SetHandleInfo->Information.Inherit) + { + /* Set the flag. FIXME: Need to check if this is allowed */ + HandleTableEntry->ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE; + } + else + { + /* Otherwise this implies we're removing the flag */ + HandleTableEntry->ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE; + } + + /* Check if making the handle protected */ + if (SetHandleInfo->Information.ProtectFromClose) + { + /* Set the flag */ + HandleTableEntry->ObAttributes |= EX_HANDLE_ENTRY_PROTECTFROMCLOSE; + } + else + { + /* Otherwise, remove it */ + HandleTableEntry->ObAttributes &= ~EX_HANDLE_ENTRY_PROTECTFROMCLOSE; + } + + /* Return success */ + return TRUE; +} + +VOID +NTAPI +ObpCloseHandleCallback(IN PHANDLE_TABLE HandleTable, + IN PVOID Object, + IN ULONG GrantedAccess, + IN PVOID Context) +{ + PAGED_CODE(); + + /* Simply decrement the handle count */ + ObpDecrementHandleCount(&EX_OBJ_TO_HDR(Object)->Body, + PsGetCurrentProcess(), + GrantedAccess); +} + +BOOLEAN +NTAPI +ObpDuplicateHandleCallback(IN PHANDLE_TABLE HandleTable, + IN PHANDLE_TABLE_ENTRY HandleTableEntry, + IN PVOID Context) +{ + POBJECT_HEADER ObjectHeader; + BOOLEAN Ret = FALSE; + PAGED_CODE(); + + /* Make sure that the handle is inheritable */ + Ret = (HandleTableEntry->ObAttributes & EX_HANDLE_ENTRY_INHERITABLE) != 0; + if(Ret) + { + /* Get the object header and increment the handle and pointer counts */ + ObjectHeader = EX_HTE_TO_HDR(HandleTableEntry); + InterlockedIncrement(&ObjectHeader->HandleCount); + InterlockedIncrement(&ObjectHeader->PointerCount); + } + + /* Return duplication result */ + return Ret; +} + +NTSTATUS +NTAPI +ObpCreateHandleTable(IN PEPROCESS Parent, + IN PEPROCESS Process) +{ + PHANDLE_TABLE HandleTable; + PAGED_CODE(); + + /* Check if we have a parent */ + if (Parent) + { + /* Duplicate the parent's */ + HandleTable = ExDupHandleTable(Process, + ObpDuplicateHandleCallback, + NULL, + Parent->ObjectTable); + } + else + { + /* Create a new one */ + HandleTable = ExCreateHandleTable(Process); + } + + /* Now write it and make sure we got one */ + Process->ObjectTable = HandleTable; + if (!HandleTable) return STATUS_INSUFFICIENT_RESOURCES; + + /* If we got here then the table was created OK */ + return STATUS_SUCCESS; +} + +VOID +NTAPI +ObKillProcess(IN PEPROCESS Process) +{ + PAGED_CODE(); + + /* Enter a critical region */ + KeEnterCriticalRegion(); + + /* Sweep the handle table to close all handles */ + ExSweepHandleTable(Process->ObjectTable, + ObpCloseHandleCallback, + Process); + + /* Destroy the table and leave the critical region */ + ExDestroyHandleTable(Process->ObjectTable); + KeLeaveCriticalRegion(); + + /* Clear the object table */ + Process->ObjectTable = NULL; +} + +NTSTATUS +NTAPI +ObDuplicateObject(PEPROCESS SourceProcess, + PEPROCESS TargetProcess, + HANDLE SourceHandle, + PHANDLE TargetHandle, + ACCESS_MASK DesiredAccess, + ULONG HandleAttributes, + ULONG Options) +{ + PHANDLE_TABLE_ENTRY SourceHandleEntry; + HANDLE_TABLE_ENTRY NewHandleEntry; + BOOLEAN AttachedToProcess = FALSE; + PVOID ObjectBody; + POBJECT_HEADER ObjectHeader; + ULONG NewHandleCount; + HANDLE NewTargetHandle; + PEPROCESS CurrentProcess; + KAPC_STATE ApcState; + NTSTATUS Status = STATUS_SUCCESS; + + PAGED_CODE(); + + if(SourceProcess == NULL || + ObIsKernelHandle(SourceHandle, ExGetPreviousMode())) + { + SourceProcess = PsInitialSystemProcess; + SourceHandle = ObKernelHandleToHandle(SourceHandle); + } + + CurrentProcess = PsGetCurrentProcess(); + + KeEnterCriticalRegion(); + + if (SourceProcess != CurrentProcess) + { + KeStackAttachProcess(&SourceProcess->Pcb, + &ApcState); + AttachedToProcess = TRUE; + } + SourceHandleEntry = ExMapHandleToPointer(SourceProcess->ObjectTable, + SourceHandle); + if (SourceHandleEntry == NULL) + { + if (AttachedToProcess) + { + KeUnstackDetachProcess(&ApcState); + } + + KeLeaveCriticalRegion(); + return STATUS_INVALID_HANDLE; + } + + ObjectHeader = EX_HTE_TO_HDR(SourceHandleEntry); + ObjectBody = &ObjectHeader->Body; + + NewHandleEntry.Object = SourceHandleEntry->Object; + if(HandleAttributes & OBJ_INHERIT) + NewHandleEntry.ObAttributes |= EX_HANDLE_ENTRY_INHERITABLE; + else + NewHandleEntry.ObAttributes &= ~EX_HANDLE_ENTRY_INHERITABLE; + NewHandleEntry.GrantedAccess = ((Options & DUPLICATE_SAME_ACCESS) ? + SourceHandleEntry->GrantedAccess : + DesiredAccess); + if (Options & DUPLICATE_SAME_ACCESS) + { + NewHandleEntry.GrantedAccess = SourceHandleEntry->GrantedAccess; + } + else + { + if (DesiredAccess & GENERIC_ACCESS) + { + RtlMapGenericMask(&DesiredAccess, + &ObjectHeader->Type->TypeInfo.GenericMapping); + } + NewHandleEntry.GrantedAccess = DesiredAccess; + } + + /* reference the object so it doesn't get deleted after releasing the lock + and before creating a new handle for it */ + ObReferenceObject(ObjectBody); + + /* increment the handle count of the object, it should always be >= 2 because + we're holding a handle lock to this object! if the new handle count was + 1 here, we're in big trouble... it would've been safe to increment and + check the handle count without using interlocked functions because the + entry is locked, which means the handle count can't change. */ + NewHandleCount = InterlockedIncrement(&ObjectHeader->HandleCount); + ASSERT(NewHandleCount >= 2); + + ExUnlockHandleTableEntry(SourceProcess->ObjectTable, + SourceHandleEntry); + + if (AttachedToProcess) + { + KeUnstackDetachProcess(&ApcState); + AttachedToProcess = FALSE; + } + + if (TargetProcess != CurrentProcess) + { + KeStackAttachProcess(&TargetProcess->Pcb, + &ApcState); + AttachedToProcess = TRUE; + } + + /* attempt to create the new handle */ + NewTargetHandle = ExCreateHandle(TargetProcess->ObjectTable, + &NewHandleEntry); + if (AttachedToProcess) + { + KeUnstackDetachProcess(&ApcState); + AttachedToProcess = FALSE; + } + + if (NewTargetHandle != NULL) + { + if (Options & DUPLICATE_CLOSE_SOURCE) + { + if (SourceProcess != CurrentProcess) + { + KeStackAttachProcess(&SourceProcess->Pcb, + &ApcState); + AttachedToProcess = TRUE; + } + + /* delete the source handle */ + ObpDeleteHandle(SourceHandle); + + if (AttachedToProcess) + { + KeUnstackDetachProcess(&ApcState); + } + } + + ObDereferenceObject(ObjectBody); + + *TargetHandle = NewTargetHandle; + } + else + { + /* decrement the handle count we previously incremented, but don't call the + closing procedure because we're not closing a handle! */ + if(InterlockedDecrement(&ObjectHeader->HandleCount) == 0) + { + ObDereferenceObject(ObjectBody); + } + + ObDereferenceObject(ObjectBody); + Status = STATUS_UNSUCCESSFUL; + } + + KeLeaveCriticalRegion(); + + return Status; }
/* PUBLIC FUNCTIONS *********************************************************/
Modified: trunk/reactos/ntoskrnl/ps/process.c URL: http://svn.reactos.ru/svn/reactos/trunk/reactos/ntoskrnl/ps/process.c?rev=22... ============================================================================== --- trunk/reactos/ntoskrnl/ps/process.c (original) +++ trunk/reactos/ntoskrnl/ps/process.c Wed Jun 7 01:02:55 2006 @@ -340,7 +340,7 @@
/* Create or Clone the Handle Table */ DPRINT("Initialzing Process Handle Table\n"); - ObCreateHandleTable(pParentProcess, InheritObjectTable, Process); + ObpCreateHandleTable(pParentProcess, Process); DPRINT("Handle Table: %x\n", Process->ObjectTable);
/* Set Process's Directory Base */
Modified: trunk/reactos/ntoskrnl/ps/psmgr.c URL: http://svn.reactos.ru/svn/reactos/trunk/reactos/ntoskrnl/ps/psmgr.c?rev=2224... ============================================================================== --- trunk/reactos/ntoskrnl/ps/psmgr.c (original) +++ trunk/reactos/ntoskrnl/ps/psmgr.c Wed Jun 7 01:02:55 2006 @@ -303,7 +303,7 @@ process a PID */ PsInitClientIDManagment();
- ObCreateHandleTable(NULL, FALSE, PsInitialSystemProcess); + ObpCreateHandleTable(NULL, PsInitialSystemProcess); ObpKernelHandleTable = PsInitialSystemProcess->ObjectTable;
CidEntry.Object = PsInitialSystemProcess;