📄 thread.c
字号:
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS CSR Sub System
* FILE: subsys/csr/csrsrv/thread.c
* PURPOSE: CSR Server DLL Thread Implementation
* PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
*/
/* INCLUDES ******************************************************************/
#include "srv.h"
#define NDEBUG
#include <debug.h>
/* DATA **********************************************************************/
LIST_ENTRY CsrThreadHashTable[256];
SECURITY_QUALITY_OF_SERVICE CsrSecurityQos =
{
sizeof(SECURITY_QUALITY_OF_SERVICE),
SecurityImpersonation,
SECURITY_STATIC_TRACKING,
FALSE
};
/* PRIVATE FUNCTIONS *********************************************************/
/*++
* @name CsrAllocateThread
*
* The CsrAllocateThread routine allocates a new CSR Thread object.
*
* @param CsrProcess
* Pointer to the CSR Process which will contain this CSR Thread.
*
* @return Pointer to the newly allocated CSR Thread.
*
* @remarks None.
*
*--*/
PCSR_THREAD
NTAPI
CsrAllocateThread(IN PCSR_PROCESS CsrProcess)
{
PCSR_THREAD CsrThread;
/* Allocate the structure */
CsrThread = RtlAllocateHeap(CsrHeap, HEAP_ZERO_MEMORY, sizeof(CSR_THREAD));
if (!CsrThread) return(NULL);
/* Reference the Thread and Process */
CsrThread->ReferenceCount++;
CsrProcess->ReferenceCount++;
/* Set the Parent Process */
CsrThread->Process = CsrProcess;
/* Return Thread */
return CsrThread;
}
/*++
* @name CsrLocateThreadByClientId
*
* The CsrLocateThreadByClientId routine locates the CSR Thread and,
* optionally, its parent CSR Process, corresponding to a Client ID.
*
* @param Process
* Optional pointer to a CSR Process pointer which will contain
* the CSR Thread's parent.
*
* @param ClientId
* Pointer to a Client ID structure containing the Unique Thread ID
* to look up.
*
* @return Pointer to the CSR Thread corresponding to this CID, or NULL if
* none was found.
*
* @remarks None.
*
*--*/
PCSR_THREAD
NTAPI
CsrLocateThreadByClientId(OUT PCSR_PROCESS *Process OPTIONAL,
IN PCLIENT_ID ClientId)
{
ULONG i;
PLIST_ENTRY ListHead, NextEntry;
PCSR_THREAD FoundThread;
/* Hash the Thread */
i = CsrHashThread(ClientId->UniqueThread);
/* Set the list pointers */
ListHead = &CsrThreadHashTable[i];
NextEntry = ListHead->Flink;
/* Star the loop */
while (NextEntry != ListHead)
{
/* Get the thread */
FoundThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, HashLinks);
/* Compare the CID */
if (FoundThread->ClientId.UniqueThread == ClientId->UniqueThread)
{
/* Match found, return the process */
*Process = FoundThread->Process;
/* Return thread too */
return FoundThread;
}
/* Next */
NextEntry = NextEntry->Flink;
}
/* Nothing found */
return NULL;
}
/*++
* @name CsrLocateThreadInProcess
*
* The CsrLocateThreadInProcess routine locates the CSR Thread
* corresponding to a Client ID inside a specific CSR Process.
*
* @param Process
* Optional pointer to the CSR Process which contains the CSR Thread
* that will be looked up.
*
* @param ClientId
* Pointer to a Client ID structure containing the Unique Thread ID
* to look up.
*
* @return Pointer to the CSR Thread corresponding to this CID, or NULL if
* none was found.
*
* @remarks If the CsrProcess argument is NULL, the lookup will be done inside
* CsrRootProcess.
*
*--*/
PCSR_THREAD
NTAPI
CsrLocateThreadInProcess(IN PCSR_PROCESS CsrProcess OPTIONAL,
IN PCLIENT_ID Cid)
{
PLIST_ENTRY ListHead, NextEntry;
PCSR_THREAD FoundThread = NULL;
/* Use the Root Process if none was specified */
if (!CsrProcess) CsrProcess = CsrRootProcess;
/* Save the List pointers */
ListHead = &CsrProcess->ThreadList;
NextEntry = ListHead->Flink;
/* Start the Loop */
while (NextEntry != NextEntry)
{
/* Get Thread Entry */
FoundThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, Link);
/* Check for TID Match */
if (FoundThread->ClientId.UniqueThread == Cid->UniqueThread) break;
/* Next entry */
NextEntry = NextEntry->Flink;
}
/* Return what we found */
return FoundThread;
}
/*++
* @name CsrInsertThread
*
* The CsrInsertThread routine inserts a CSR Thread into its parent's
* Thread List and into the Thread Hash Table.
*
* @param Process
* Pointer to the CSR Process containing this CSR Thread.
*
* @param Thread
* Pointer to the CSR Thread to be inserted.
*
* @return None.
*
* @remarks None.
*
*--*/
VOID
NTAPI
CsrInsertThread(IN PCSR_PROCESS Process,
IN PCSR_THREAD Thread)
{
ULONG i;
/* Insert it into the Regular List */
InsertTailList(&Process->ThreadList, &Thread->Link);
/* Increase Thread Count */
Process->ThreadCount++;
/* Hash the Thread */
i = CsrHashThread(Thread->ClientId.UniqueThread);
/* Insert it there too */
InsertHeadList(&CsrThreadHashTable[i], &Thread->HashLinks);
}
/*++
* @name CsrDeallocateThread
*
* The CsrDeallocateThread frees the memory associated with a CSR Thread.
*
* @param CsrThread
* Pointer to the CSR Thread to be freed.
*
* @return None.
*
* @remarks Do not call this routine. It is reserved for the internal
* thread management routines when a CSR Thread has been cleanly
* dereferenced and killed.
*
*--*/
VOID
NTAPI
CsrDeallocateThread(IN PCSR_THREAD CsrThread)
{
/* Free the process object from the heap */
RtlFreeHeap(CsrHeap, 0, CsrThread);
}
/*++
* @name CsrLockedDereferenceThread
*
* The CsrLockedDereferenceThread derefences a CSR Thread while the
* Process Lock is already being held.
*
* @param CsrThread
* Pointer to the CSR Thread to be dereferenced.
*
* @return None.
*
* @remarks This routine will return with the Process Lock held.
*
*--*/
VOID
NTAPI
CsrLockedDereferenceThread(PCSR_THREAD CsrThread)
{
/* Decrease reference count */
if (!(--CsrThread->ReferenceCount))
{
/* Call the generic cleanup code */
CsrThreadRefcountZero(CsrThread);
CsrAcquireProcessLock();
}
}
/*++
* @name CsrRemoveThread
*
* The CsrRemoveThread function undoes a CsrInsertThread operation and
* removes the CSR Thread from the the Hash Table and Thread List.
*
* @param CsrThread
* Pointer to the CSR Thread to remove.
*
* @return None.
*
* @remarks If this CSR Thread is the last one inside a CSR Process, the
* parent will be dereferenced and the CsrProcessLastThreadTerminated
* flag will be set.
*
* After executing this routine, the CSR Thread will have the
* CsrThreadInTermination flag set.
*
*--*/
VOID
NTAPI
CsrRemoveThread(IN PCSR_THREAD CsrThread)
{
/* Remove it from the List */
RemoveEntryList(&CsrThread->Link);
/* Decreate the thread count of the process */
CsrThread->Process->ThreadCount--;
/* Remove it from the Hash List as well */
if (CsrThread->HashLinks.Flink) RemoveEntryList(&CsrThread->HashLinks);
/* Check if this is the last Thread */
if (!CsrThread->Process->ThreadCount)
{
/* Check if it's not already been marked for deletion */
if (!(CsrThread->Process->Flags & CsrProcessLastThreadTerminated))
{
/* Let everyone know this process is about to lose the thread */
CsrThread->Process->Flags |= CsrProcessLastThreadTerminated;
/* Reference the Process */
CsrLockedDereferenceProcess(CsrThread->Process);
}
}
/* Mark the thread for deletion */
CsrThread->Flags |= CsrThreadInTermination;
}
/*++
* @name CsrThreadRefcountZero
*
* The CsrThreadRefcountZero routine is executed when a CSR Thread has lost
* all its active references. It removes and de-allocates the CSR Thread.
*
* @param CsrThread
* Pointer to the CSR Thread that is to be deleted.
*
* @return None.
*
* @remarks Do not call this routine. It is reserved for the internal
* thread management routines when a CSR Thread has lost all
* its references.
*
* This routine is called with the Process Lock held.
*
*--*/
VOID
NTAPI
CsrThreadRefcountZero(IN PCSR_THREAD CsrThread)
{
PCSR_PROCESS CsrProcess = CsrThread->Process;
/* Remove this thread */
CsrRemoveThread(CsrThread);
/* Release the Process Lock */
CsrReleaseProcessLock();
/* Close the NT Thread Handle */
NtClose(CsrThread->ThreadHandle);
/* De-allocate the CSR Thread Object */
CsrDeallocateThread(CsrThread);
/* Remove a reference from the process */
CsrDereferenceProcess(CsrProcess);
}
/* PUBLIC FUNCTIONS ***********************************************************/
/*++
* @name CsrAddStaticServerThread
* @implemented NT4
*
* The CsrAddStaticServerThread routine adds a new CSR Thread to the
* CSR Server Process (CsrRootProcess).
*
* @param hThread
* Handle to an existing NT Thread to which to associate this
* CSR Thread.
*
* @param ClientId
* Pointer to the Client ID structure of the NT Thread to associate
* with this CSR Thread.
*
* @param ThreadFlags
* Initial CSR Thread Flags to associate to this CSR Thread. Usually
* CsrThreadIsServerThread.
*
* @return Pointer to the newly allocated CSR Thread.
*
* @remarks None.
*
*--*/
PCSR_THREAD
NTAPI
CsrAddStaticServerThread(IN HANDLE hThread,
IN PCLIENT_ID ClientId,
IN ULONG ThreadFlags)
{
PCSR_THREAD CsrThread;
/* Get the Lock */
CsrAcquireProcessLock();
/* Allocate the Server Thread */
if ((CsrThread = CsrAllocateThread(CsrRootProcess)))
{
/* Setup the Object */
CsrThread->ThreadHandle = hThread;
CsrThread->ClientId = *ClientId;
CsrThread->Flags = ThreadFlags;
/* Insert it into the Thread List */
InsertTailList(&CsrRootProcess->ThreadList, &CsrThread->Link);
/* Increment the thread count */
CsrRootProcess->ThreadCount++;
}
/* Release the Process Lock and return */
CsrReleaseProcessLock();
return CsrThread;
}
/*++
* @name CsrCreateRemoteThread
* @implemented NT4
*
* The CsrCreateRemoteThread routine creates a CSR Thread object for
* an NT Thread which is not part of the current NT Process.
*
* @param hThread
* Handle to an existing NT Thread to which to associate this
* CSR Thread.
*
* @param ClientId
* Pointer to the Client ID structure of the NT Thread to associate
* with this CSR Thread.
*
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
* othwerwise.
*
* @remarks None.
*
*--*/
NTSTATUS
NTAPI
CsrCreateRemoteThread(IN HANDLE hThread,
IN PCLIENT_ID ClientId)
{
NTSTATUS Status;
HANDLE ThreadHandle;
PCSR_THREAD CsrThread;
PCSR_PROCESS CsrProcess;
KERNEL_USER_TIMES KernelTimes;
DPRINT("CSRSRV: %s called\n", __FUNCTION__);
/* Get the Thread Create Time */
Status = NtQueryInformationThread(hThread,
ThreadTimes,
(PVOID)&KernelTimes,
sizeof(KernelTimes),
NULL);
/* Lock the Owner Process */
Status = CsrLockProcessByClientId(&ClientId->UniqueProcess,
&CsrProcess);
/* Make sure the thread didn't terminate */
if (KernelTimes.ExitTime.QuadPart)
{
/* Unlock the process and return */
CsrUnlockProcess(CsrProcess);
return STATUS_THREAD_IS_TERMINATING;
}
/* Allocate a CSR Thread Structure */
if (!(CsrThread = CsrAllocateThread(CsrProcess)))
{
DPRINT1("CSRSRV:%s: out of memory!\n", __FUNCTION__);
CsrUnlockProcess(CsrProcess);
return STATUS_NO_MEMORY;
}
/* Duplicate the Thread Handle */
Status = NtDuplicateObject(NtCurrentProcess(),
hThread,
NtCurrentProcess(),
&ThreadHandle,
0,
0,
DUPLICATE_SAME_ACCESS);
/* Allow failure */
if (!NT_SUCCESS(Status)) ThreadHandle = hThread;
/* Save the data we have */
CsrThread->CreateTime = KernelTimes.CreateTime;
CsrThread->ClientId = *ClientId;
CsrThread->ThreadHandle = ThreadHandle;
CsrThread->Flags = 0;
/* Insert the Thread into the Process */
CsrInsertThread(CsrProcess, CsrThread);
/* Release the lock and return */
CsrUnlockProcess(CsrProcess);
return STATUS_SUCCESS;
}
/*++
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -