- Remove cid.c
- Do direct handle creation/deletion where needed.
Modified: trunk/reactos/ntoskrnl/include/internal/ps.h
Modified: trunk/reactos/ntoskrnl/ntoskrnl.xml
Modified: trunk/reactos/ntoskrnl/ps/cid.c
Modified: trunk/reactos/ntoskrnl/ps/kill.c
Modified: trunk/reactos/ntoskrnl/ps/process.c
Modified: trunk/reactos/ntoskrnl/ps/psmgr.c
Modified: trunk/reactos/ntoskrnl/ps/thread.c

Modified: trunk/reactos/ntoskrnl/include/internal/ps.h
--- trunk/reactos/ntoskrnl/include/internal/ps.h	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/include/internal/ps.h	2005-08-07 22:48:07 UTC (rev 17182)
@@ -216,10 +216,6 @@
 
 /* CLIENT ID */
 
-NTSTATUS PsCreateCidHandle(PVOID Object, POBJECT_TYPE ObjectType, PHANDLE Handle);
-NTSTATUS PsDeleteCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType);
-PHANDLE_TABLE_ENTRY PsLookupCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType, PVOID *Object);
-VOID PsUnlockCidHandle(PHANDLE_TABLE_ENTRY CidEntry);
 NTSTATUS PsLockProcess(PEPROCESS Process, BOOLEAN Timeout);
 VOID PsUnlockProcess(PEPROCESS Process);
 

Modified: trunk/reactos/ntoskrnl/ntoskrnl.xml
--- trunk/reactos/ntoskrnl/ntoskrnl.xml	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/ntoskrnl.xml	2005-08-07 22:48:07 UTC (rev 17182)
@@ -290,7 +290,6 @@
 				<file>continue.c</file>
 			</directory>
 		</if>
-		<file>cid.c</file>
 		<file>debug.c</file>
 		<file>idle.c</file>
 		<file>job.c</file>

Modified: trunk/reactos/ntoskrnl/ps/cid.c
--- trunk/reactos/ntoskrnl/ps/cid.c	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/ps/cid.c	2005-08-07 22:48:07 UTC (rev 17182)
@@ -18,117 +18,16 @@
 
 PHANDLE_TABLE PspCidTable = NULL;
 
-#define CID_FLAG_PROCESS 0x1
-#define CID_FLAG_THREAD 0x2
-#define CID_FLAGS_MASK (CID_FLAG_PROCESS | CID_FLAG_THREAD)
-
 /* FUNCTIONS *****************************************************************/
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
 PsInitClientIDManagment(VOID)
 {
   PspCidTable = ExCreateHandleTable(NULL);
   ASSERT(PspCidTable);
 }
 
-NTSTATUS
-PsCreateCidHandle(PVOID Object, POBJECT_TYPE ObjectType, PHANDLE Handle)
-{
-  HANDLE_TABLE_ENTRY NewEntry;
-  LONG ExHandle;
-
-  PAGED_CODE();
-
-  NewEntry.u1.Object = Object;
-  if(ObjectType == PsThreadType)
-    NewEntry.u2.GrantedAccess = CID_FLAG_THREAD;
-  else if(ObjectType == PsProcessType)
-    NewEntry.u2.GrantedAccess = CID_FLAG_PROCESS;
-  else
-  {
-    DPRINT1("Can't create CID handles for %wZ objects\n", &ObjectType->Name);
-    KEBUGCHECK(0);
-  }
-
-  ExHandle = ExCreateHandle(PspCidTable,
-                            &NewEntry);
-  if(ExHandle != EX_INVALID_HANDLE)
-  {
-    *Handle = EX_HANDLE_TO_HANDLE(ExHandle);
-    return STATUS_SUCCESS;
-  }
-
-  return STATUS_UNSUCCESSFUL;
-}
-
-NTSTATUS
-PsDeleteCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType)
-{
-  PHANDLE_TABLE_ENTRY Entry;
-  LONG ExHandle = HANDLE_TO_EX_HANDLE(CidHandle);
-
-  PAGED_CODE();
-
-  KeEnterCriticalRegion();
-
-  Entry = ExMapHandleToPointer(PspCidTable,
-                               ExHandle);
-  if(Entry != NULL)
-  {
-    if((ObjectType == PsThreadType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_THREAD)) ||
-       (ObjectType == PsProcessType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_PROCESS)))
-    {
-      ExDestroyHandleByEntry(PspCidTable,
-                             Entry,
-                             ExHandle);
-      KeLeaveCriticalRegion();
-      return STATUS_SUCCESS;
-    }
-    else
-    {
-      ExUnlockHandleTableEntry(PspCidTable,
-                               Entry);
-      KeLeaveCriticalRegion();
-      return STATUS_OBJECT_TYPE_MISMATCH;
-    }
-  }
-  KeLeaveCriticalRegion();
-  return STATUS_INVALID_HANDLE;
-}
-
-PHANDLE_TABLE_ENTRY
-PsLookupCidHandle(HANDLE CidHandle, POBJECT_TYPE ObjectType, PVOID *Object)
-{
-  PHANDLE_TABLE_ENTRY Entry;
-
-  PAGED_CODE();
-
-  KeEnterCriticalRegion();
-
-  Entry = ExMapHandleToPointer(PspCidTable,
-                               HANDLE_TO_EX_HANDLE(CidHandle));
-  if(Entry != NULL)
-  {
-    if((ObjectType == PsProcessType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_PROCESS)) ||
-       (ObjectType == PsThreadType && ((Entry->u2.GrantedAccess & CID_FLAGS_MASK) == CID_FLAG_THREAD)))
-    {
-      *Object = Entry->u1.Object;
-      return Entry;
-    }
-    else
-    {
-      DPRINT1("CID Obj type mismatch handle 0x%x %wZ vs 0x%x\n", CidHandle,
-              &ObjectType->Name, Entry->u2.GrantedAccess);
-      ExUnlockHandleTableEntry(PspCidTable,
-                               Entry);
-    }
-  }
-
-  KeLeaveCriticalRegion();
-
-  return NULL;
-}
-
 /*
  * @implemented
  */

Modified: trunk/reactos/ntoskrnl/ps/kill.c
--- trunk/reactos/ntoskrnl/ps/kill.c	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/ps/kill.c	2005-08-07 22:48:07 UTC (rev 17182)
@@ -21,6 +21,7 @@
 BOOLEAN PspReaping = FALSE;
 extern LIST_ENTRY PsActiveProcessHead;
 extern FAST_MUTEX PspActiveProcessMutex;
+extern PHANDLE_TABLE PspCidTable;
 
 /* FUNCTIONS *****************************************************************/
 
@@ -153,9 +154,9 @@
     ExReleaseFastMutex(&PspActiveProcessMutex);
 
     /* Delete the CID Handle */
-    if(Process->UniqueProcessId != NULL) {
-
-        PsDeleteCidHandle(Process->UniqueProcessId, PsProcessType);
+    if(Process->UniqueProcessId)
+    {
+        ExDestroyHandle(PspCidTable, Process->UniqueProcessId);
     }
 
     /* KDB hook */
@@ -184,9 +185,9 @@
     Thread->ThreadsProcess = NULL;
 
     /* Delete the CID Handle */
-    if(Thread->Cid.UniqueThread != NULL) {
-
-        PsDeleteCidHandle(Thread->Cid.UniqueThread, PsThreadType);
+    if(Thread->Cid.UniqueThread)
+    {
+        ExDestroyHandle(PspCidTable, Thread->Cid.UniqueThread);
     }
 
     /* Free the W32THREAD structure if present */

Modified: trunk/reactos/ntoskrnl/ps/process.c
--- trunk/reactos/ntoskrnl/ps/process.c	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/ps/process.c	2005-08-07 22:48:07 UTC (rev 17182)
@@ -19,6 +19,7 @@
 PEPROCESS EXPORTED PsInitialSystemProcess = NULL;
 PEPROCESS PsIdleProcess = NULL;
 POBJECT_TYPE EXPORTED PsProcessType = NULL;
+extern PHANDLE_TABLE PspCidTable;
 
 EPROCESS_QUOTA_BLOCK PspDefaultQuotaBlock;
 
@@ -189,6 +190,7 @@
     KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     PHYSICAL_ADDRESS DirectoryTableBase;
     KAFFINITY Affinity;
+    HANDLE_TABLE_ENTRY CidEntry;
     DirectoryTableBase.QuadPart = (ULONGLONG)0;
 
     DPRINT("PspCreateProcess(ObjectAttributes %x)\n", ObjectAttributes);
@@ -362,13 +364,13 @@
 
     /* Create a handle for the Process */
     DPRINT("Initialzing Process CID Handle\n");
-    Status = PsCreateCidHandle(Process,
-                               PsProcessType,
-                               &Process->UniqueProcessId);
+    CidEntry.u1.Object = Process;
+    CidEntry.u2.GrantedAccess = 0;
+    Process->UniqueProcessId = (ExCreateHandle(PspCidTable, &CidEntry));
     DPRINT("Created CID: %d\n", Process->UniqueProcessId);
-    if(!NT_SUCCESS(Status))
+    if(!Process->UniqueProcessId)
     {
-        DPRINT1("Failed to create CID handle (unique process ID)! Status: 0x%x\n", Status);
+        DPRINT1("Failed to create CID handle\n");
         ObDereferenceObject(Process);
         goto exitdereferenceobjects;
     }
@@ -471,25 +473,80 @@
 PsLookupProcessByProcessId(IN HANDLE ProcessId,
                            OUT PEPROCESS *Process)
 {
-   PHANDLE_TABLE_ENTRY CidEntry;
-   PEPROCESS FoundProcess;
+    PHANDLE_TABLE_ENTRY CidEntry;
+    PEPROCESS FoundProcess;
+    NTSTATUS Status = STATUS_INVALID_PARAMETER;
+    PAGED_CODE();
 
-   PAGED_CODE();
+    /* Get the CID Handle Entry */
+    if (!(CidEntry = ExMapHandleToPointer(PspCidTable,
+                                          HANDLE_TO_EX_HANDLE(ProcessId))))
+    {
+        /* Get the Process */
+        FoundProcess = CidEntry->u1.Object;
 
-   ASSERT(Process);
+        /* Make sure it's really a process */
+        if (FoundProcess->Pcb.Header.Type == ProcessObject)
+        {
+            /* Reference and return it */
+            ObReferenceObject(FoundProcess);
+            *Process = FoundProcess;
+            Status = STATUS_SUCCESS;
+        }
 
-   CidEntry = PsLookupCidHandle(ProcessId, PsProcessType, (PVOID*)&FoundProcess);
-   if(CidEntry != NULL)
-   {
-       ObReferenceObject(FoundProcess);
+        /* Unlock the Entry */
+        ExUnlockHandleTableEntry(PspCidTable, CidEntry);
+    }
 
-        PsUnlockCidHandle(CidEntry);
+    /* Return to caller */
+    return Status;
+}
 
-        *Process = FoundProcess;
-        return STATUS_SUCCESS;
+/*
+ * @implemented
+ */
+NTSTATUS
+STDCALL
+PsLookupProcessThreadByCid(IN PCLIENT_ID Cid,
+                           OUT PEPROCESS *Process OPTIONAL,
+                           OUT PETHREAD *Thread)
+{
+    PHANDLE_TABLE_ENTRY CidEntry;
+    PETHREAD FoundThread;
+    NTSTATUS Status = STATUS_INVALID_PARAMETER;
+    PAGED_CODE();
+
+    /* Get the CID Handle Entry */
+    if (!(CidEntry = ExMapHandleToPointer(PspCidTable,
+                                          HANDLE_TO_EX_HANDLE(Cid->UniqueThread))))
+    {
+        /* Get the Process */
+        FoundThread = CidEntry->u1.Object;
+
+        /* Make sure it's really a thread and this process' */
+        if ((FoundThread->Tcb.DispatcherHeader.Type == ThreadObject) &&
+            (FoundThread->Cid.UniqueProcess == Cid->UniqueProcess))
+        {
+            /* Reference and return it */
+            ObReferenceObject(FoundThread);
+            *Thread = FoundThread;
+            Status = STATUS_SUCCESS;
+
+            /* Check if we should return the Process too */
+            if (Process)
+            {
+                /* Return it and reference it */
+                *Process = FoundThread->ThreadsProcess;
+                ObReferenceObject(*Process);
+            }
+        }
+
+        /* Unlock the Entry */
+        ExUnlockHandleTableEntry(PspCidTable, CidEntry);
     }
 
-    return STATUS_INVALID_PARAMETER;
+    /* Return to caller */
+    return Status;
 }
 
 /*

Modified: trunk/reactos/ntoskrnl/ps/psmgr.c
--- trunk/reactos/ntoskrnl/ps/psmgr.c	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/ps/psmgr.c	2005-08-07 22:48:07 UTC (rev 17182)
@@ -44,11 +44,7 @@
 PVOID PspSystemDllBase = NULL;
 PVOID PspSystemDllSection = NULL;
 PVOID PspSystemDllEntryPoint = NULL;
-
-VOID
-INIT_FUNCTION
-PsInitClientIDManagment(VOID);
-
+PHANDLE_TABLE PspCidTable = NULL;
 VOID STDCALL PspKillMostProcesses();
 
 /* FUNCTIONS ***************************************************************/
@@ -72,6 +68,14 @@
 
 VOID
 INIT_FUNCTION
+PsInitClientIDManagment(VOID)
+{
+  PspCidTable = ExCreateHandleTable(NULL);
+  ASSERT(PspCidTable);
+}
+
+VOID
+INIT_FUNCTION
 PsInitThreadManagment(VOID)
 /*
  * FUNCTION: Initialize thread managment
@@ -261,7 +265,7 @@
 VOID
 PspPostInitSystemProcess(VOID)
 {
-  NTSTATUS Status;
+  HANDLE_TABLE_ENTRY CidEntry;
 
   /* this routine is called directly after the exectuive handle tables were
      initialized. We'll set up the Client ID handle table and assign the system
@@ -271,10 +275,11 @@
   ObCreateHandleTable(NULL, FALSE, PsInitialSystemProcess);
   ObpKernelHandleTable = PsInitialSystemProcess->ObjectTable;
 
-  Status = PsCreateCidHandle(PsInitialSystemProcess,
-                             PsProcessType,
-                             &PsInitialSystemProcess->UniqueProcessId);
-  if(!NT_SUCCESS(Status))
+  CidEntry.u1.Object = PsInitialSystemProcess;
+  CidEntry.u2.GrantedAccess = 0;
+  PsInitialSystemProcess->UniqueProcessId = ExCreateHandle(PspCidTable, &CidEntry);
+
+  if(!PsInitialSystemProcess->UniqueProcessId)
   {
     DPRINT1("Failed to create CID handle (unique process id) for the system process!\n");
     KEBUGCHECK(0);

Modified: trunk/reactos/ntoskrnl/ps/thread.c
--- trunk/reactos/ntoskrnl/ps/thread.c	2005-08-07 21:48:14 UTC (rev 17181)
+++ trunk/reactos/ntoskrnl/ps/thread.c	2005-08-07 22:48:07 UTC (rev 17182)
@@ -19,6 +19,7 @@
 extern LIST_ENTRY PsActiveProcessHead;
 extern PEPROCESS PsIdleProcess;
 extern PVOID PspSystemDllEntryPoint;
+extern PHANDLE_TABLE PspCidTable;
 
 POBJECT_TYPE EXPORTED PsThreadType = NULL;
 
@@ -114,6 +115,7 @@
     KIRQL OldIrql;
     KPROCESSOR_MODE PreviousMode = ExGetPreviousMode();
     NTSTATUS Status;
+    HANDLE_TABLE_ENTRY CidEntry;
     PVOID KernelStack;
 
     /* Reference the Process by handle or pointer, depending on what we got */
@@ -180,12 +182,15 @@
 
     /* Create Cid Handle */
     DPRINT("Creating Thread Handle (CID)\n");
-    if (!(NT_SUCCESS(PsCreateCidHandle(Thread, PsThreadType, &Thread->Cid.UniqueThread)))) {
+    CidEntry.u1.Object = Thread;
+    CidEntry.u2.GrantedAccess = 0;
+    Thread->Cid.UniqueThread = ExCreateHandle(PspCidTable, &CidEntry);
+    if (!Thread->Cid.UniqueThread) {
 
         DPRINT1("Failed to create Thread Handle (CID)\n");
         ObDereferenceObject(Process);
         ObDereferenceObject(Thread);
-        return Status;
+        return STATUS_INSUFFICIENT_RESOURCES;
     }
 
     /* Initialize Lists */
@@ -360,6 +365,43 @@
 /*
  * @implemented
  */
+NTSTATUS
+STDCALL
+PsLookupThreadByThreadId(IN HANDLE ThreadId,
+                         OUT PETHREAD *Thread)
+{
+    PHANDLE_TABLE_ENTRY CidEntry;
+    PETHREAD FoundThread;
+    NTSTATUS Status = STATUS_INVALID_PARAMETER;
+    PAGED_CODE();
+
+    /* Get the CID Handle Entry */
+    if (!(CidEntry = ExMapHandleToPointer(PspCidTable, 
+                                          HANDLE_TO_EX_HANDLE(ThreadId))))
+    {
+        /* Get the Process */
+        FoundThread = CidEntry->u1.Object;
+
+        /* Make sure it's really a process */
+        if (FoundThread->Tcb.DispatcherHeader.Type == ThreadObject)
+        {
+            /* Reference and return it */
+            ObReferenceObject(FoundThread);
+            *Thread = FoundThread;
+            Status = STATUS_SUCCESS;
+        }
+
+        /* Unlock the Entry */
+        ExUnlockHandleTableEntry(PspCidTable, CidEntry);
+    }
+
+    /* Return to caller */
+    return Status;
+}
+
+/*
+ * @implemented
+ */
 HANDLE
 STDCALL
 PsGetCurrentThreadId(VOID)