From 0181638af577d9297570167a5e30acf72d11b4de Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Sun, 19 Feb 2012 18:46:05 +0000 Subject: [PATCH] [KERNEL32/CSRSRV]: Handle ExitProcess CSRSS message and implement CsrDestroyProcess to do so. Also implement CsrDestroyThread for future purposes. svn path=/trunk/; revision=55717 --- reactos/dll/win32/kernel32/client/proc.c | 1 + reactos/include/reactos/subsys/csrss/csrss.h | 3 +- .../win32/csrss/csrsrv/api/process.c | 26 ++----- .../subsystems/win32/csrss/csrsrv/thredsup.c | 69 +++++++++++++++++++ reactos/subsystems/win32/csrss/include/api.h | 4 ++ 5 files changed, 82 insertions(+), 21 deletions(-) diff --git a/reactos/dll/win32/kernel32/client/proc.c b/reactos/dll/win32/kernel32/client/proc.c index d9e2b7a1bb2..7e19346ef5e 100644 --- a/reactos/dll/win32/kernel32/client/proc.c +++ b/reactos/dll/win32/kernel32/client/proc.c @@ -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), diff --git a/reactos/include/reactos/subsys/csrss/csrss.h b/reactos/include/reactos/subsys/csrss/csrss.h index f50e15f334a..670e8ab85f3 100644 --- a/reactos/include/reactos/subsys/csrss/csrss.h +++ b/reactos/include/reactos/subsys/csrss/csrss.h @@ -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; diff --git a/reactos/subsystems/win32/csrss/csrsrv/api/process.c b/reactos/subsystems/win32/csrss/csrsrv/api/process.c index d2a44b3f921..0b2c92cb0f3 100644 --- a/reactos/subsystems/win32/csrss/csrsrv/api/process.c +++ b/reactos/subsystems/win32/csrss/csrsrv/api/process.c @@ -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) diff --git a/reactos/subsystems/win32/csrss/csrsrv/thredsup.c b/reactos/subsystems/win32/csrss/csrsrv/thredsup.c index 121d39bffd2..2fc67a52c0e 100644 --- a/reactos/subsystems/win32/csrss/csrsrv/thredsup.c +++ b/reactos/subsystems/win32/csrss/csrsrv/thredsup.c @@ -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 * diff --git a/reactos/subsystems/win32/csrss/include/api.h b/reactos/subsystems/win32/csrss/include/api.h index 5e922caf263..e20af3ec809 100644 --- a/reactos/subsystems/win32/csrss/include/api.h +++ b/reactos/subsystems/win32/csrss/include/api.h @@ -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);