[KERNEL32/CSRSRV]: Handle ExitProcess CSRSS message and implement CsrDestroyProcess...
authorAlex Ionescu <aionescu@gmail.com>
Sun, 19 Feb 2012 18:46:05 +0000 (18:46 +0000)
committerAlex Ionescu <aionescu@gmail.com>
Sun, 19 Feb 2012 18:46:05 +0000 (18:46 +0000)
svn path=/trunk/; revision=55717

reactos/dll/win32/kernel32/client/proc.c
reactos/include/reactos/subsys/csrss/csrss.h
reactos/subsystems/win32/csrss/csrsrv/api/process.c
reactos/subsystems/win32/csrss/csrsrv/thredsup.c
reactos/subsystems/win32/csrss/include/api.h

index d9e2b7a..7e19346 100644 (file)
@@ -1753,6 +1753,7 @@ ExitProcess(IN UINT uExitCode)
         LdrShutdownProcess();
 
         /* Notify Base Server of process termination */
+        CsrRequest.Data.TerminateProcessRequest.uExitCode = uExitCode;
         CsrClientCallServer(&CsrRequest,
                             NULL,
                             MAKE_CSR_API(TERMINATE_PROCESS, CSR_NATIVE),
index f50e15f..670e8ab 100644 (file)
@@ -80,7 +80,7 @@ typedef struct
 
 typedef struct
 {
-    ULONG Dummy;
+    UINT uExitCode;
 } CSRSS_TERMINATE_PROCESS, *PCSRSS_TERMINATE_PROCESS;
 
 typedef struct
@@ -713,6 +713,7 @@ typedef struct _CSR_API_MESSAGE
     {
         CSRSS_CREATE_PROCESS CreateProcessRequest;
         CSRSS_CREATE_THREAD CreateThreadRequest;
+        CSRSS_TERMINATE_PROCESS TerminateProcessRequest;
         CSRSS_CONNECT_PROCESS ConnectRequest;
         CSRSS_WRITE_CONSOLE WriteConsoleRequest;
         CSRSS_READ_CONSOLE ReadConsoleRequest;
index d2a44b3..0b2c92c 100644 (file)
@@ -245,26 +245,12 @@ CSR_API(CsrSrvCreateThread)
 
 CSR_API(CsrTerminateProcess)
 {
-   PLIST_ENTRY NextEntry;
-   PCSR_THREAD Thread;
-   
-   LOCK;
-   
-   NextEntry = ProcessData->ThreadList.Flink;
-   while (NextEntry != &ProcessData->ThreadList)
-   {
-        Thread = CONTAINING_RECORD(NextEntry, CSR_THREAD, Link);
-        NextEntry = NextEntry->Flink;
-        
-        ASSERT(ProcessStructureListLocked());
-        CsrThreadRefcountZero(Thread);
-        LOCK;
-        
-   }
-   
-   UNLOCK;
-   ProcessData->Flags |= CsrProcessTerminated;
-   return STATUS_SUCCESS;
+    PCSR_THREAD CsrThread = NtCurrentTeb()->CsrClientThread;
+    ASSERT(CsrThread != NULL);
+    
+    /* Remove the CSR_THREADs and CSR_PROCESS */
+    return CsrDestroyProcess(&CsrThread->ClientId,
+                             (NTSTATUS)Request->Data.TerminateProcessRequest.uExitCode);
 }
 
 CSR_API(CsrConnectProcess)
index 121d39b..2fc67a5 100644 (file)
@@ -310,6 +310,75 @@ CsrThreadRefcountZero(IN PCSR_THREAD CsrThread)
     CsrDereferenceProcess(CsrProcess);
 }
 
+/*++
+ * @name CsrDestroyThread
+ * @implemented NT4
+ *
+ * The CsrDestroyThread routine destroys the CSR Thread corresponding to
+ * a given Thread ID.
+ *
+ * @param Cid
+ *        Pointer to the Client ID Structure corresponding to the CSR
+ *        Thread which is about to be destroyed.
+ *
+ * @return STATUS_SUCCESS in case of success, STATUS_THREAD_IS_TERMINATING
+ *         if the CSR Thread is already terminating.
+ *
+ * @remarks None.
+ *
+ *--*/
+NTSTATUS
+NTAPI
+CsrDestroyThread(IN PCLIENT_ID Cid)
+{
+    CLIENT_ID ClientId = *Cid;
+    PCSR_THREAD CsrThread;
+    PCSR_PROCESS CsrProcess;
+
+    /* Acquire lock */
+    CsrAcquireProcessLock();
+
+    /* Find the thread */
+    CsrThread = CsrLocateThreadByClientId(&CsrProcess,
+                                          &ClientId);
+
+    /* Make sure we got one back, and that it's not already gone */
+    if (!CsrThread || CsrThread->Flags & CsrThreadTerminated)
+    {
+        /* Release the lock and return failure */
+        CsrReleaseProcessLock();
+        return STATUS_THREAD_IS_TERMINATING;
+    }
+
+    /* Set the terminated flag */
+    CsrThread->Flags |= CsrThreadTerminated;
+
+    /* Acquire the Wait Lock */
+    CsrAcquireWaitLock();
+
+    /* Do we have an active wait block? */
+    if (CsrThread->WaitBlock)
+    {
+        /* Notify waiters of termination */
+        CsrNotifyWaitBlock(CsrThread->WaitBlock,
+                           NULL,
+                           NULL,
+                           NULL,
+                           CsrProcessTerminating,
+                           TRUE);
+    }
+
+    /* Release the Wait Lock */
+    CsrReleaseWaitLock();
+
+    /* Dereference the thread */
+    CsrLockedDereferenceThread(CsrThread);
+
+    /* Release the Process Lock and return success */
+    CsrReleaseProcessLock();
+    return STATUS_SUCCESS;
+}
+
 /*++
  * @name CsrLockedDereferenceThread
  *
index 5e922ca..e20af3e 100644 (file)
@@ -420,6 +420,10 @@ NTAPI
 CsrDestroyProcess(IN PCLIENT_ID Cid,
 IN NTSTATUS ExitStatus);
 
+NTSTATUS
+NTAPI
+CsrDestroyThread(IN PCLIENT_ID Cid);
+
 VOID
 NTAPI
 CsrLockedDereferenceThread(IN PCSR_THREAD CsrThread);