- Remove cid.c
authorAlex Ionescu <aionescu@gmail.com>
Sun, 7 Aug 2005 22:48:07 +0000 (22:48 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Sun, 7 Aug 2005 22:48:07 +0000 (22:48 +0000)
- Do direct handle creation/deletion where needed.

svn path=/trunk/; revision=17182

reactos/ntoskrnl/include/internal/ps.h
reactos/ntoskrnl/ntoskrnl.xml
reactos/ntoskrnl/ps/cid.c
reactos/ntoskrnl/ps/kill.c
reactos/ntoskrnl/ps/process.c
reactos/ntoskrnl/ps/psmgr.c
reactos/ntoskrnl/ps/thread.c

index 12f4f13..ccee3e2 100644 (file)
@@ -216,10 +216,6 @@ PspGetSystemDllEntryPoints(VOID);
 
 /* 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);
 
index a3416ec..4b347b8 100644 (file)
                                <file>continue.c</file>
                        </directory>
                </if>
-               <file>cid.c</file>
                <file>debug.c</file>
                <file>idle.c</file>
                <file>job.c</file>
index 935ca8f..dfd3b1d 100644 (file)
 
 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
  */
index ca28ec7..a4f2e7b 100644 (file)
@@ -21,6 +21,7 @@ WORK_QUEUE_ITEM PspReaperWorkItem;
 BOOLEAN PspReaping = FALSE;
 extern LIST_ENTRY PsActiveProcessHead;
 extern FAST_MUTEX PspActiveProcessMutex;
+extern PHANDLE_TABLE PspCidTable;
 
 /* FUNCTIONS *****************************************************************/
 
@@ -153,9 +154,9 @@ PspDeleteProcess(PVOID ObjectBody)
     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 @@ PspDeleteThread(PVOID ObjectBody)
     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 */
index 06de86f..f71343c 100644 (file)
@@ -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 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
     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 @@ PspCreateProcess(OUT PHANDLE ProcessHandle,
 
     /* 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 @@ STDCALL
 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;
 }
 
 /*
index f234c20..a384db6 100644 (file)
@@ -44,11 +44,7 @@ extern PVOID KeRaiseUserExceptionDispatcher;
 PVOID PspSystemDllBase = NULL;
 PVOID PspSystemDllSection = NULL;
 PVOID PspSystemDllEntryPoint = NULL;
-
-VOID
-INIT_FUNCTION
-PsInitClientIDManagment(VOID);
-
+PHANDLE_TABLE PspCidTable = NULL;
 VOID STDCALL PspKillMostProcesses();
 
 /* FUNCTIONS ***************************************************************/
@@ -70,6 +66,14 @@ PiInitProcessManager(VOID)
    PsInitialiseW32Call();
 }
 
+VOID
+INIT_FUNCTION
+PsInitClientIDManagment(VOID)
+{
+  PspCidTable = ExCreateHandleTable(NULL);
+  ASSERT(PspCidTable);
+}
+
 VOID
 INIT_FUNCTION
 PsInitThreadManagment(VOID)
@@ -261,7 +265,7 @@ PsInitProcessManagment(VOID)
 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 @@ PspPostInitSystemProcess(VOID)
   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);
index 3838dfd..04f2fd8 100644 (file)
@@ -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 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
     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 @@ PspCreateThread(OUT PHANDLE ThreadHandle,
 
     /* 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 */
@@ -357,6 +362,43 @@ PsCreateSystemThread(PHANDLE ThreadHandle,
                            StartContext);
 }
 
+/*
+ * @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
  */