2010-03-10 06:49:53 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
2012-10-25 20:40:41 +00:00
|
|
|
* PROJECT: ReactOS Client/Server Runtime SubSystem
|
|
|
|
* FILE: subsystems/win32/csrsrv/procsup.c
|
2012-10-20 13:00:41 +00:00
|
|
|
* PURPOSE: CSR Server DLL Process Management
|
2010-03-10 06:49:53 +00:00
|
|
|
* PROGRAMMERS: ReactOS Portable Systems Group
|
2012-10-20 13:00:41 +00:00
|
|
|
* Alex Ionescu (alex@relsoft.net)
|
2010-03-10 06:49:53 +00:00
|
|
|
*/
|
2012-10-25 20:40:41 +00:00
|
|
|
|
2010-03-10 06:49:53 +00:00
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#include <srv.h>
|
|
|
|
|
2014-01-04 10:27:29 +00:00
|
|
|
#include <winuser.h>
|
|
|
|
|
2010-03-10 06:49:53 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/* GLOBALS ********************************************************************/
|
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
RTL_CRITICAL_SECTION CsrProcessLock;
|
2012-10-13 20:32:44 +00:00
|
|
|
PCSR_PROCESS CsrRootProcess = NULL;
|
2010-03-10 06:49:53 +00:00
|
|
|
SECURITY_QUALITY_OF_SERVICE CsrSecurityQos =
|
|
|
|
{
|
|
|
|
sizeof(SECURITY_QUALITY_OF_SERVICE),
|
|
|
|
SecurityImpersonation,
|
|
|
|
SECURITY_STATIC_TRACKING,
|
|
|
|
FALSE
|
|
|
|
};
|
2012-10-13 20:32:44 +00:00
|
|
|
ULONG CsrProcessSequenceCount = 5;
|
2012-10-20 21:03:32 +00:00
|
|
|
extern ULONG CsrTotalPerProcessDataLength;
|
2010-03-10 06:49:53 +00:00
|
|
|
|
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/* PRIVATE FUNCTIONS **********************************************************/
|
|
|
|
|
|
|
|
/*++
|
2012-11-20 19:34:03 +00:00
|
|
|
* @name CsrSetToNormalPriority
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2012-11-20 19:34:03 +00:00
|
|
|
* The CsrSetToNormalPriority routine sets the current NT Process'
|
2012-10-20 13:00:41 +00:00
|
|
|
* priority to the normal priority for CSR Processes.
|
|
|
|
*
|
|
|
|
* @param None.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @remarks The "Normal" Priority corresponds to the Normal Foreground
|
2012-10-20 13:00:41 +00:00
|
|
|
* Priority (9) plus a boost of 4.
|
|
|
|
*
|
|
|
|
*--*/
|
2010-03-10 06:49:53 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2012-11-20 19:34:03 +00:00
|
|
|
CsrSetToNormalPriority(VOID)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
|
|
|
KPRIORITY BasePriority = (8 + 1) + 4;
|
|
|
|
|
|
|
|
/* Set the Priority */
|
|
|
|
NtSetInformationProcess(NtCurrentProcess(),
|
|
|
|
ProcessBasePriority,
|
|
|
|
&BasePriority,
|
2015-08-15 16:34:21 +00:00
|
|
|
sizeof(BasePriority));
|
2010-03-10 06:49:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/*++
|
2012-11-20 19:34:03 +00:00
|
|
|
* @name CsrSetToShutdownPriority
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2012-11-20 19:34:03 +00:00
|
|
|
* The CsrSetToShutdownPriority routine sets the current NT Process'
|
2012-10-20 13:00:41 +00:00
|
|
|
* priority to the boosted priority for CSR Processes doing shutdown.
|
|
|
|
* Additonally, it acquires the Shutdown Privilege required for shutdown.
|
|
|
|
*
|
|
|
|
* @param None.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @remarks The "Shutdown" Priority corresponds to the Normal Foreground
|
2012-10-20 13:00:41 +00:00
|
|
|
* Priority (9) plus a boost of 6.
|
|
|
|
*
|
|
|
|
*--*/
|
2010-03-10 06:49:53 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2012-11-20 19:34:03 +00:00
|
|
|
CsrSetToShutdownPriority(VOID)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2015-08-15 16:34:21 +00:00
|
|
|
KPRIORITY BasePriority = (8 + 1) + 6;
|
2010-03-10 06:49:53 +00:00
|
|
|
BOOLEAN Old;
|
|
|
|
|
|
|
|
/* Get the shutdown privilege */
|
|
|
|
if (NT_SUCCESS(RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE,
|
|
|
|
TRUE,
|
|
|
|
FALSE,
|
|
|
|
&Old)))
|
|
|
|
{
|
|
|
|
/* Set the Priority */
|
|
|
|
NtSetInformationProcess(NtCurrentProcess(),
|
|
|
|
ProcessBasePriority,
|
2015-08-15 16:34:21 +00:00
|
|
|
&BasePriority,
|
|
|
|
sizeof(BasePriority));
|
2010-03-10 06:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/*++
|
|
|
|
* @name CsrProcessRefcountZero
|
|
|
|
*
|
|
|
|
* The CsrProcessRefcountZero routine is executed when a CSR Process has lost
|
|
|
|
* all its active references. It removes and de-allocates the CSR Process.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process 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 Process has lost all
|
|
|
|
* its references.
|
|
|
|
*
|
|
|
|
* This routine is called with the Process Lock held.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrProcessRefcountZero(IN PCSR_PROCESS CsrProcess)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2012-10-20 13:00:41 +00:00
|
|
|
ASSERT(ProcessStructureListLocked());
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Remove the Process from the list */
|
|
|
|
CsrRemoveProcess(CsrProcess);
|
|
|
|
|
|
|
|
/* Check if there's a session */
|
|
|
|
if (CsrProcess->NtSession)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Dereference the Session */
|
|
|
|
CsrDereferenceNtSession(CsrProcess->NtSession, 0);
|
|
|
|
}
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Close the Client Port if there is one */
|
|
|
|
if (CsrProcess->ClientPort) NtClose(CsrProcess->ClientPort);
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Close the process handle */
|
|
|
|
NtClose(CsrProcess->ProcessHandle);
|
|
|
|
|
|
|
|
/* Free the Proces Object */
|
|
|
|
CsrDeallocateProcess(CsrProcess);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrLockedDereferenceProcess
|
|
|
|
*
|
|
|
|
* The CsrLockedDereferenceProcess dereferences a CSR Process while the
|
|
|
|
* Process Lock is already being held.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process to be dereferenced.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks This routine will return with the Process Lock held.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrLockedDereferenceProcess(PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
|
|
|
LONG LockCount;
|
|
|
|
|
|
|
|
/* Decrease reference count */
|
|
|
|
LockCount = --CsrProcess->ReferenceCount;
|
|
|
|
ASSERT(LockCount >= 0);
|
2013-03-10 19:37:33 +00:00
|
|
|
if (LockCount == 0)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
|
|
|
/* Call the generic cleanup code */
|
|
|
|
DPRINT1("Should kill process: %p\n", CsrProcess);
|
2013-01-01 02:59:31 +00:00
|
|
|
CsrProcessRefcountZero(CsrProcess);
|
2021-03-23 17:35:13 +00:00
|
|
|
/* Acquire the lock again, it was released in CsrProcessRefcountZero */
|
2021-03-23 17:15:23 +00:00
|
|
|
CsrAcquireProcessLock();
|
2012-02-16 03:41:18 +00:00
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/*++
|
|
|
|
* @name CsrAllocateProcess
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrAllocateProcess routine allocates a new CSR Process object.
|
|
|
|
*
|
|
|
|
* @return Pointer to the newly allocated CSR Process.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
PCSR_PROCESS
|
|
|
|
NTAPI
|
|
|
|
CsrAllocateProcess(VOID)
|
|
|
|
{
|
|
|
|
PCSR_PROCESS CsrProcess;
|
|
|
|
ULONG TotalSize;
|
|
|
|
|
|
|
|
/* Calculate the amount of memory this should take */
|
|
|
|
TotalSize = sizeof(CSR_PROCESS) +
|
|
|
|
(CSR_SERVER_DLL_MAX * sizeof(PVOID)) +
|
|
|
|
CsrTotalPerProcessDataLength;
|
|
|
|
|
|
|
|
/* Allocate a Process */
|
|
|
|
CsrProcess = RtlAllocateHeap(CsrHeap, HEAP_ZERO_MEMORY, TotalSize);
|
|
|
|
if (!CsrProcess) return NULL;
|
|
|
|
|
|
|
|
/* Handle the Sequence Number and protect against overflow */
|
|
|
|
CsrProcess->SequenceNumber = CsrProcessSequenceCount++;
|
|
|
|
if (CsrProcessSequenceCount < 5) CsrProcessSequenceCount = 5;
|
|
|
|
|
|
|
|
/* Increase the reference count */
|
2013-01-01 02:59:31 +00:00
|
|
|
CsrLockedReferenceProcess(CsrProcess);
|
2012-10-20 13:00:41 +00:00
|
|
|
|
|
|
|
/* Initialize the Thread List */
|
|
|
|
InitializeListHead(&CsrProcess->ThreadList);
|
|
|
|
|
|
|
|
/* Return the Process */
|
|
|
|
return CsrProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrLockedReferenceProcess
|
|
|
|
*
|
|
|
|
* The CsrLockedReferenceProcess references a CSR Process while the
|
|
|
|
* Process Lock is already being held.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process to be referenced.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks This routine will return with the Process Lock held.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrLockedReferenceProcess(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
|
|
|
/* Increment the reference count */
|
|
|
|
++CsrProcess->ReferenceCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrInitializeProcessStructure
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrInitializeProcessStructure routine sets up support for CSR Processes
|
2012-11-05 00:23:58 +00:00
|
|
|
* and CSR Threads by initializing our own CSR Root Process.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
* @param None.
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
CsrInitializeProcessStructure(VOID)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG i;
|
|
|
|
|
|
|
|
/* Initialize the Lock */
|
|
|
|
Status = RtlInitializeCriticalSection(&CsrProcessLock);
|
|
|
|
if (!NT_SUCCESS(Status)) return Status;
|
|
|
|
|
|
|
|
/* Set up the Root Process */
|
|
|
|
CsrRootProcess = CsrAllocateProcess();
|
|
|
|
if (!CsrRootProcess) return STATUS_NO_MEMORY;
|
|
|
|
|
|
|
|
/* Set up the minimal information for it */
|
|
|
|
InitializeListHead(&CsrRootProcess->ListLink);
|
|
|
|
CsrRootProcess->ProcessHandle = (HANDLE)-1;
|
|
|
|
CsrRootProcess->ClientId = NtCurrentTeb()->ClientId;
|
|
|
|
|
|
|
|
/* Initialize the Thread Hash List */
|
2013-07-28 13:54:42 +00:00
|
|
|
for (i = 0; i < NUMBER_THREAD_HASH_BUCKETS; i++) InitializeListHead(&CsrThreadHashTable[i]);
|
2012-10-20 13:00:41 +00:00
|
|
|
|
|
|
|
/* Initialize the Wait Lock */
|
|
|
|
return RtlInitializeCriticalSection(&CsrWaitListsLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrDeallocateProcess
|
|
|
|
*
|
|
|
|
* The CsrDeallocateProcess frees the memory associated with a CSR Process.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process to be freed.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks Do not call this routine. It is reserved for the internal
|
|
|
|
* thread management routines when a CSR Process has been cleanly
|
|
|
|
* dereferenced and killed.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrDeallocateProcess(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
|
|
|
/* Free the process object from the heap */
|
|
|
|
RtlFreeHeap(CsrHeap, 0, CsrProcess);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrRemoveProcess
|
|
|
|
*
|
|
|
|
* The CsrRemoveProcess function undoes a CsrInsertProcess operation and
|
|
|
|
* removes the CSR Process from the Process List and notifies Server DLLs
|
|
|
|
* of this removal.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process to remove.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrRemoveProcess(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
|
|
|
PCSR_SERVER_DLL ServerDll;
|
|
|
|
ULONG i;
|
|
|
|
ASSERT(ProcessStructureListLocked());
|
|
|
|
|
|
|
|
/* Remove us from the Process List */
|
|
|
|
RemoveEntryList(&CsrProcess->ListLink);
|
|
|
|
|
|
|
|
/* Release the lock */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
|
|
|
|
/* Loop every Server DLL */
|
|
|
|
for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Get the Server DLL */
|
|
|
|
ServerDll = CsrLoadedServerDll[i];
|
|
|
|
|
|
|
|
/* Check if it's valid and if it has a Disconnect Callback */
|
2012-11-13 21:08:19 +00:00
|
|
|
if (ServerDll && ServerDll->DisconnectCallback)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Call it */
|
|
|
|
ServerDll->DisconnectCallback(CsrProcess);
|
2010-03-10 06:49:53 +00:00
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/*++
|
|
|
|
* @name CsrInsertProcess
|
|
|
|
*
|
|
|
|
* The CsrInsertProcess routine inserts a CSR Process into the Process List
|
|
|
|
* and notifies Server DLLs of the creation of a new CSR Process.
|
|
|
|
*
|
2013-01-26 21:23:10 +00:00
|
|
|
* @param ParentProcess
|
|
|
|
* Optional pointer to the Parent Process creating this CSR Process.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process which is to be inserted.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2013-01-26 21:23:10 +00:00
|
|
|
CsrInsertProcess(IN PCSR_PROCESS ParentProcess OPTIONAL,
|
|
|
|
IN PCSR_PROCESS CsrProcess)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
|
|
|
PCSR_SERVER_DLL ServerDll;
|
|
|
|
ULONG i;
|
|
|
|
ASSERT(ProcessStructureListLocked());
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Insert it into the Root List */
|
|
|
|
InsertTailList(&CsrRootProcess->ListLink, &CsrProcess->ListLink);
|
|
|
|
|
|
|
|
/* Notify the Server DLLs */
|
|
|
|
for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
|
|
|
|
{
|
|
|
|
/* Get the current Server DLL */
|
|
|
|
ServerDll = CsrLoadedServerDll[i];
|
|
|
|
|
|
|
|
/* Make sure it's valid and that it has callback */
|
2012-11-05 00:23:58 +00:00
|
|
|
if (ServerDll && ServerDll->NewProcessCallback)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2013-01-26 21:23:10 +00:00
|
|
|
ServerDll->NewProcessCallback(ParentProcess, CsrProcess);
|
2010-03-10 06:49:53 +00:00
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* PUBLIC FUNCTIONS ***********************************************************/
|
|
|
|
|
|
|
|
/*++
|
2012-10-22 00:09:51 +00:00
|
|
|
* @name CsrCreateProcess
|
2012-10-20 13:00:41 +00:00
|
|
|
* @implemented NT4
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* The CsrCreateProcess routine creates a CSR Process object for an NT Process.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @param hProcess
|
|
|
|
* Handle to an existing NT Process to which to associate this
|
|
|
|
* CSR Process.
|
|
|
|
*
|
|
|
|
* @param hThread
|
|
|
|
* Handle to an existing NT Thread to which to create its
|
|
|
|
* corresponding CSR Thread for this CSR Process.
|
|
|
|
*
|
|
|
|
* @param ClientId
|
|
|
|
* Pointer to the Client ID structure of the NT Process to associate
|
|
|
|
* with this CSR Process.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @param NtSession
|
|
|
|
* @param Flags
|
|
|
|
* @param DebugCid
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @remarks None.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrCreateProcess(IN HANDLE hProcess,
|
|
|
|
IN HANDLE hThread,
|
|
|
|
IN PCLIENT_ID ClientId,
|
|
|
|
IN PCSR_NT_SESSION NtSession,
|
|
|
|
IN ULONG Flags,
|
|
|
|
IN PCLIENT_ID DebugCid)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
PCSR_THREAD CurrentThread = CsrGetClientThread();
|
|
|
|
CLIENT_ID CurrentCid;
|
|
|
|
PCSR_PROCESS CurrentProcess;
|
2012-11-13 21:08:19 +00:00
|
|
|
PCSR_SERVER_DLL ServerDll;
|
2012-10-22 00:09:51 +00:00
|
|
|
PVOID ProcessData;
|
|
|
|
ULONG i;
|
|
|
|
PCSR_PROCESS CsrProcess;
|
2012-10-20 13:00:41 +00:00
|
|
|
NTSTATUS Status;
|
2012-10-22 00:09:51 +00:00
|
|
|
PCSR_THREAD CsrThread;
|
|
|
|
KERNEL_USER_TIMES KernelTimes;
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Get the current CID and lock Processes */
|
|
|
|
CurrentCid = CurrentThread->ClientId;
|
|
|
|
CsrAcquireProcessLock();
|
|
|
|
|
|
|
|
/* Get the current CSR Thread */
|
|
|
|
CurrentThread = CsrLocateThreadByClientId(&CurrentProcess, &CurrentCid);
|
|
|
|
if (!CurrentThread)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* We've failed to locate the thread */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_THREAD_IS_TERMINATING;
|
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Allocate a new Process Object */
|
|
|
|
CsrProcess = CsrAllocateProcess();
|
|
|
|
if (!CsrProcess)
|
|
|
|
{
|
|
|
|
/* Couldn't allocate Process */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inherit the Process Data */
|
|
|
|
CurrentProcess = CurrentThread->Process;
|
2012-11-13 23:45:02 +00:00
|
|
|
ProcessData = &CsrProcess->ServerData[CSR_SERVER_DLL_MAX];
|
2012-10-22 00:09:51 +00:00
|
|
|
for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
|
|
|
|
{
|
2012-11-13 21:08:19 +00:00
|
|
|
/* Get the current Server */
|
|
|
|
ServerDll = CsrLoadedServerDll[i];
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Check if the DLL is Loaded and has Per Process Data */
|
2012-11-13 21:08:19 +00:00
|
|
|
if (ServerDll && ServerDll->SizeOfProcessData)
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Set the pointer */
|
|
|
|
CsrProcess->ServerData[i] = ProcessData;
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Copy the Data */
|
|
|
|
RtlMoveMemory(ProcessData,
|
|
|
|
CurrentProcess->ServerData[i],
|
2012-11-13 21:08:19 +00:00
|
|
|
ServerDll->SizeOfProcessData);
|
2012-10-22 00:09:51 +00:00
|
|
|
|
|
|
|
/* Update next data pointer */
|
|
|
|
ProcessData = (PVOID)((ULONG_PTR)ProcessData +
|
2012-11-13 21:08:19 +00:00
|
|
|
ServerDll->SizeOfProcessData);
|
2012-10-22 00:09:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No data for this Server */
|
|
|
|
CsrProcess->ServerData[i] = NULL;
|
2010-03-10 06:49:53 +00:00
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2015-08-15 16:34:21 +00:00
|
|
|
/* Set the Exception Port for us */
|
2012-10-22 00:09:51 +00:00
|
|
|
Status = NtSetInformationProcess(hProcess,
|
|
|
|
ProcessExceptionPort,
|
|
|
|
&CsrApiPort,
|
2015-08-15 16:34:21 +00:00
|
|
|
sizeof(CsrApiPort));
|
2012-10-22 00:09:51 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Failed */
|
|
|
|
CsrDeallocateProcess(CsrProcess);
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if CreateProcess got CREATE_NEW_PROCESS_GROUP */
|
2014-04-28 23:09:09 +00:00
|
|
|
if (Flags & CsrProcessCreateNewGroup)
|
2012-10-22 00:09:51 +00:00
|
|
|
{
|
2014-04-28 23:09:09 +00:00
|
|
|
/*
|
|
|
|
* We create the process group leader of a new process group, therefore
|
|
|
|
* its process group ID and sequence number are its own ones.
|
|
|
|
*/
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrProcess->ProcessGroupId = HandleToUlong(ClientId->UniqueProcess);
|
|
|
|
CsrProcess->ProcessGroupSequence = CsrProcess->SequenceNumber;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-28 23:09:09 +00:00
|
|
|
/* Inherit the process group ID and sequence number from the current process */
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrProcess->ProcessGroupId = CurrentProcess->ProcessGroupId;
|
|
|
|
CsrProcess->ProcessGroupSequence = CurrentProcess->ProcessGroupSequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is a console process */
|
|
|
|
if (Flags & CsrProcessIsConsoleApp) CsrProcess->Flags |= CsrProcessIsConsoleApp;
|
|
|
|
|
|
|
|
/* Mask out non-debug flags */
|
|
|
|
Flags &= ~(CsrProcessIsConsoleApp | CsrProcessCreateNewGroup | CsrProcessPriorityFlags);
|
|
|
|
|
|
|
|
/* Check if every process will be debugged */
|
|
|
|
if (!(Flags) && (CurrentProcess->DebugFlags & CsrDebugProcessChildren))
|
|
|
|
{
|
|
|
|
/* Pass it on to the current process */
|
|
|
|
CsrProcess->DebugFlags = CsrDebugProcessChildren;
|
|
|
|
CsrProcess->DebugCid = CurrentProcess->DebugCid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if Debugging was used on this process */
|
|
|
|
if ((Flags & (CsrDebugOnlyThisProcess | CsrDebugProcessChildren)) && (DebugCid))
|
|
|
|
{
|
|
|
|
/* Save the debug flag used */
|
|
|
|
CsrProcess->DebugFlags = Flags;
|
|
|
|
|
|
|
|
/* Save the CID */
|
|
|
|
CsrProcess->DebugCid = *DebugCid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if Debugging is enabled */
|
|
|
|
if (CsrProcess->DebugFlags)
|
|
|
|
{
|
|
|
|
/* Set the Debug Port for us */
|
|
|
|
Status = NtSetInformationProcess(hProcess,
|
|
|
|
ProcessDebugPort,
|
|
|
|
&CsrApiPort,
|
2015-08-15 16:34:21 +00:00
|
|
|
sizeof(CsrApiPort));
|
2012-10-22 00:09:51 +00:00
|
|
|
ASSERT(NT_SUCCESS(Status));
|
2012-10-20 13:00:41 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2010-03-10 06:49:53 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Failed */
|
|
|
|
CsrDeallocateProcess(CsrProcess);
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_NO_MEMORY;
|
2010-03-10 06:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Get the Thread Create Time */
|
|
|
|
Status = NtQueryInformationThread(hThread,
|
|
|
|
ThreadTimes,
|
2015-08-15 16:34:21 +00:00
|
|
|
&KernelTimes,
|
2012-10-22 00:09:51 +00:00
|
|
|
sizeof(KernelTimes),
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Failed */
|
|
|
|
CsrDeallocateProcess(CsrProcess);
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Allocate a CSR Thread Structure */
|
|
|
|
CsrThread = CsrAllocateThread(CsrProcess);
|
|
|
|
if (!CsrThread)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Failed */
|
|
|
|
CsrDeallocateProcess(CsrProcess);
|
|
|
|
CsrReleaseProcessLock();
|
2012-10-20 13:00:41 +00:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Save the data we have */
|
|
|
|
CsrThread->CreateTime = KernelTimes.CreateTime;
|
|
|
|
CsrThread->ClientId = *ClientId;
|
|
|
|
CsrThread->ThreadHandle = hThread;
|
|
|
|
ProtectHandle(hThread);
|
|
|
|
CsrThread->Flags = 0;
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Insert the Thread into the Process */
|
2013-09-12 09:02:48 +00:00
|
|
|
Status = CsrInsertThread(CsrProcess, CsrThread);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Bail out */
|
|
|
|
CsrDeallocateProcess(CsrProcess);
|
|
|
|
CsrDeallocateThread(CsrThread);
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return Status;
|
|
|
|
}
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Reference the session */
|
|
|
|
CsrReferenceNtSession(NtSession);
|
|
|
|
CsrProcess->NtSession = NtSession;
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Setup Process Data */
|
|
|
|
CsrProcess->ClientId = *ClientId;
|
|
|
|
CsrProcess->ProcessHandle = hProcess;
|
|
|
|
CsrProcess->ShutdownLevel = 0x280;
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Set the Priority to Background */
|
|
|
|
CsrSetBackgroundPriority(CsrProcess);
|
|
|
|
|
|
|
|
/* Insert the Process */
|
2013-01-26 21:23:10 +00:00
|
|
|
CsrInsertProcess(CurrentProcess, CsrProcess);
|
2012-10-22 00:09:51 +00:00
|
|
|
|
|
|
|
/* Release lock and return */
|
|
|
|
CsrReleaseProcessLock();
|
2010-03-10 06:49:53 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2012-02-16 21:46:59 +00:00
|
|
|
/*++
|
2012-10-22 00:09:51 +00:00
|
|
|
* @name CsrDebugProcess
|
2012-10-20 13:00:41 +00:00
|
|
|
* @implemented NT4
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* The CsrDebugProcess routine is deprecated in NT 5.1 and higher. It is
|
|
|
|
* exported only for compatibility with older CSR Server DLLs.
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @param CsrProcess
|
|
|
|
* Deprecated.
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @return Deprecated
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @remarks Deprecated.
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
|
|
|
*--*/
|
2012-10-22 00:09:51 +00:00
|
|
|
NTSTATUS
|
2012-02-16 21:46:59 +00:00
|
|
|
NTAPI
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrDebugProcess(IN PCSR_PROCESS CsrProcess)
|
2012-02-16 21:46:59 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* CSR does not handle debugging anymore */
|
2013-10-05 17:24:31 +00:00
|
|
|
DPRINT("CSRSRV: %s(0x%p) called\n", __FUNCTION__, CsrProcess);
|
2012-10-22 00:09:51 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
2012-02-16 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
2012-10-22 00:09:51 +00:00
|
|
|
* @name CsrDebugProcessStop
|
2012-10-20 13:00:41 +00:00
|
|
|
* @implemented NT4
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* The CsrDebugProcessStop routine is deprecated in NT 5.1 and higher. It is
|
|
|
|
* exported only for compatibility with older CSR Server DLLs.
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @param CsrProcess
|
|
|
|
* Deprecated.
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @return Deprecated
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @remarks Deprecated.
|
2012-02-16 21:46:59 +00:00
|
|
|
*
|
|
|
|
*--*/
|
2012-10-22 00:09:51 +00:00
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
CsrDebugProcessStop(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
|
|
|
/* CSR does not handle debugging anymore */
|
2013-10-05 17:24:31 +00:00
|
|
|
DPRINT("CSRSRV: %s(0x%p) called\n", __FUNCTION__, CsrProcess);
|
2012-10-22 00:09:51 +00:00
|
|
|
return STATUS_UNSUCCESSFUL;
|
2012-02-16 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrDereferenceProcess
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrDereferenceProcess routine removes a reference from a CSR Process.
|
|
|
|
*
|
|
|
|
* @param CsrThread
|
|
|
|
* Pointer to the CSR Process to dereference.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks If the reference count has reached zero (ie: the CSR Process has
|
|
|
|
* no more active references), it will be deleted.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrDereferenceProcess(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
|
|
|
LONG LockCount;
|
|
|
|
|
|
|
|
/* Acquire process lock */
|
|
|
|
CsrAcquireProcessLock();
|
|
|
|
|
|
|
|
/* Decrease reference count */
|
|
|
|
LockCount = --CsrProcess->ReferenceCount;
|
|
|
|
ASSERT(LockCount >= 0);
|
2013-03-10 19:37:33 +00:00
|
|
|
if (LockCount == 0)
|
2012-02-16 21:46:59 +00:00
|
|
|
{
|
|
|
|
/* Call the generic cleanup code */
|
|
|
|
CsrProcessRefcountZero(CsrProcess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just release the lock */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-19 18:05:49 +00:00
|
|
|
/*++
|
|
|
|
* @name CsrDestroyProcess
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrDestroyProcess routine destroys the CSR Process corresponding to
|
|
|
|
* a given Client ID.
|
|
|
|
*
|
|
|
|
* @param Cid
|
|
|
|
* Pointer to the Client ID Structure corresponding to the CSR
|
|
|
|
* Process which is about to be destroyed.
|
|
|
|
*
|
|
|
|
* @param ExitStatus
|
|
|
|
* Unused.
|
|
|
|
*
|
|
|
|
* @return STATUS_SUCCESS in case of success, STATUS_THREAD_IS_TERMINATING
|
|
|
|
* if the CSR Process is already terminating.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
CsrDestroyProcess(IN PCLIENT_ID Cid,
|
|
|
|
IN NTSTATUS ExitStatus)
|
|
|
|
{
|
|
|
|
PCSR_THREAD CsrThread;
|
|
|
|
PCSR_PROCESS CsrProcess;
|
|
|
|
CLIENT_ID ClientId = *Cid;
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
|
|
|
|
/* 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) || (CsrProcess->Flags & CsrProcessTerminating))
|
|
|
|
{
|
|
|
|
/* Release the lock and return failure */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_THREAD_IS_TERMINATING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the terminated flag */
|
|
|
|
CsrProcess->Flags |= CsrProcessTerminating;
|
|
|
|
|
|
|
|
/* Get the List Pointers */
|
|
|
|
NextEntry = CsrProcess->ThreadList.Flink;
|
|
|
|
while (NextEntry != &CsrProcess->ThreadList)
|
|
|
|
{
|
|
|
|
/* Get the current thread entry */
|
|
|
|
CsrThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, Link);
|
|
|
|
|
2012-12-11 01:25:57 +00:00
|
|
|
/* Move to the next entry */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
|
2012-02-19 18:05:49 +00:00
|
|
|
/* Make sure the thread isn't already dead */
|
|
|
|
if (CsrThread->Flags & CsrThreadTerminated)
|
|
|
|
{
|
2012-12-11 01:25:57 +00:00
|
|
|
/* Go the the next thread */
|
2012-02-19 18:05:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2012-02-19 11:39:07 +00:00
|
|
|
/*++
|
2012-10-22 00:09:51 +00:00
|
|
|
* @name CsrGetProcessLuid
|
2012-02-19 11:39:07 +00:00
|
|
|
* @implemented NT4
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* The CsrGetProcessLuid routine gets the LUID of the given process.
|
2012-02-19 11:39:07 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @param hProcess
|
|
|
|
* Optional handle to the process whose LUID should be returned.
|
2012-02-19 11:39:07 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @param Luid
|
2013-03-10 19:37:33 +00:00
|
|
|
* Pointer to a LUID Pointer which will receive the CSR Process' LUID.
|
2012-02-19 11:39:07 +00:00
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
|
2012-02-19 11:39:07 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @remarks If hProcess is not supplied, then the current thread's token will
|
|
|
|
* be used. If that too is missing, then the current process' token
|
|
|
|
* will be used.
|
2012-02-19 11:39:07 +00:00
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrGetProcessLuid(IN HANDLE hProcess OPTIONAL,
|
|
|
|
OUT PLUID Luid)
|
2012-02-19 11:39:07 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
HANDLE hToken = NULL;
|
2012-02-19 11:39:07 +00:00
|
|
|
NTSTATUS Status;
|
2012-10-22 00:09:51 +00:00
|
|
|
ULONG Length;
|
|
|
|
PTOKEN_STATISTICS TokenStats;
|
2012-02-19 11:39:07 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Check if we have a handle to a CSR Process */
|
|
|
|
if (!hProcess)
|
2012-02-19 11:39:07 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* We don't, so try opening the Thread's Token */
|
|
|
|
Status = NtOpenThreadToken(NtCurrentThread(),
|
|
|
|
TOKEN_QUERY,
|
|
|
|
FALSE,
|
|
|
|
&hToken);
|
2012-02-19 11:39:07 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Check for success */
|
|
|
|
if (!NT_SUCCESS(Status))
|
2012-02-19 11:39:07 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* If we got some other failure, then return and quit */
|
|
|
|
if (Status != STATUS_NO_TOKEN) return Status;
|
2012-02-19 11:39:07 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* We don't have a Thread Token, use a Process Token */
|
|
|
|
hProcess = NtCurrentProcess();
|
|
|
|
hToken = NULL;
|
2012-02-19 11:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Check if we have a token by now */
|
|
|
|
if (!hToken)
|
2012-02-19 11:39:07 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* No token yet, so open the Process Token */
|
|
|
|
Status = NtOpenProcessToken(hProcess,
|
|
|
|
TOKEN_QUERY,
|
|
|
|
&hToken);
|
2012-02-19 11:39:07 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Still no token, return the error */
|
|
|
|
return Status;
|
2012-02-19 11:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Now get the size we'll need for the Token Information */
|
|
|
|
Status = NtQueryInformationToken(hToken,
|
|
|
|
TokenStatistics,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&Length);
|
2020-05-25 21:52:35 +00:00
|
|
|
if (Status != STATUS_BUFFER_TOO_SMALL)
|
|
|
|
{
|
|
|
|
/* Close the token and fail */
|
|
|
|
NtClose(hToken);
|
|
|
|
return Status;
|
|
|
|
}
|
2012-02-19 11:39:07 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Allocate memory for the Token Info */
|
|
|
|
if (!(TokenStats = RtlAllocateHeap(CsrHeap, 0, Length)))
|
2012-02-19 11:39:07 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Fail and close the token */
|
|
|
|
NtClose(hToken);
|
2012-02-19 11:39:07 +00:00
|
|
|
return STATUS_NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Now query the information */
|
|
|
|
Status = NtQueryInformationToken(hToken,
|
|
|
|
TokenStatistics,
|
|
|
|
TokenStats,
|
|
|
|
Length,
|
|
|
|
&Length);
|
2012-02-19 11:39:07 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Close the handle */
|
|
|
|
NtClose(hToken);
|
2012-02-19 11:39:07 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Check for success */
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Return the LUID */
|
|
|
|
*Luid = TokenStats->AuthenticationId;
|
|
|
|
}
|
2010-03-10 06:49:53 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Free the query information */
|
|
|
|
RtlFreeHeap(CsrHeap, 0, TokenStats);
|
|
|
|
|
|
|
|
/* Return the Status */
|
|
|
|
return Status;
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
2012-10-22 00:09:51 +00:00
|
|
|
* @name CsrImpersonateClient
|
2012-10-20 13:00:41 +00:00
|
|
|
* @implemented NT4
|
2012-02-16 06:45:25 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* The CsrImpersonateClient will impersonate the given CSR Thread.
|
2012-02-16 06:45:25 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @param CsrThread
|
|
|
|
* Pointer to the CSR Thread to impersonate.
|
2012-02-16 06:45:25 +00:00
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return TRUE if impersonation succeeded, FALSE otherwise.
|
2012-02-16 06:45:25 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @remarks Impersonation can be recursive.
|
2012-02-16 06:45:25 +00:00
|
|
|
*
|
|
|
|
*--*/
|
2012-10-22 00:09:51 +00:00
|
|
|
BOOLEAN
|
2012-02-16 06:45:25 +00:00
|
|
|
NTAPI
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrImpersonateClient(IN PCSR_THREAD CsrThread)
|
2012-02-16 06:45:25 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PCSR_THREAD CurrentThread = CsrGetClientThread();
|
2012-02-16 06:45:25 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Use the current thread if none given */
|
|
|
|
if (!CsrThread) CsrThread = CurrentThread;
|
2012-02-16 06:45:25 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Still no thread, something is wrong */
|
|
|
|
if (!CsrThread)
|
|
|
|
{
|
|
|
|
/* Failure */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the call */
|
|
|
|
Status = NtImpersonateThread(NtCurrentThread(),
|
|
|
|
CsrThread->ThreadHandle,
|
|
|
|
&CsrSecurityQos);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* Failure */
|
2020-04-14 20:53:49 +00:00
|
|
|
#ifdef CSR_DBG
|
2012-10-22 00:09:51 +00:00
|
|
|
DPRINT1("CSRSS: Can't impersonate client thread - Status = %lx\n", Status);
|
2013-03-10 19:37:33 +00:00
|
|
|
// if (Status != STATUS_BAD_IMPERSONATION_LEVEL) DbgBreakPoint();
|
2020-04-14 20:53:49 +00:00
|
|
|
#endif
|
2012-10-22 00:09:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increase the impersonation count for the current thread */
|
|
|
|
if (CurrentThread) ++CurrentThread->ImpersonationCount;
|
|
|
|
|
|
|
|
/* Return Success */
|
|
|
|
return TRUE;
|
2012-02-16 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrLockProcessByClientId
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrLockProcessByClientId routine locks the CSR Process corresponding
|
|
|
|
* to the given Process ID and optionally returns it.
|
|
|
|
*
|
|
|
|
* @param Pid
|
|
|
|
* Process ID corresponding to the CSR Process which will be locked.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Optional pointer to a CSR Process pointer which will hold the
|
|
|
|
* CSR Process corresponding to the given Process ID.
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
|
2012-02-16 06:45:25 +00:00
|
|
|
*
|
|
|
|
* @remarks Locking a CSR Process is defined as acquiring an extra
|
|
|
|
* reference to it and returning with the Process Lock held.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
CsrLockProcessByClientId(IN HANDLE Pid,
|
|
|
|
OUT PCSR_PROCESS *CsrProcess)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
PCSR_PROCESS CurrentProcess = NULL;
|
2013-01-12 00:33:55 +00:00
|
|
|
NTSTATUS Status = STATUS_UNSUCCESSFUL;
|
2012-02-16 06:45:25 +00:00
|
|
|
|
|
|
|
/* Acquire the lock */
|
|
|
|
CsrAcquireProcessLock();
|
|
|
|
|
|
|
|
/* Assume failure */
|
|
|
|
ASSERT(CsrProcess != NULL);
|
|
|
|
*CsrProcess = NULL;
|
|
|
|
|
|
|
|
/* Setup the List Pointers */
|
2012-02-18 01:27:50 +00:00
|
|
|
NextEntry = &CsrRootProcess->ListLink;
|
|
|
|
do
|
2012-02-16 06:45:25 +00:00
|
|
|
{
|
|
|
|
/* Get the Process */
|
|
|
|
CurrentProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
|
|
|
|
|
|
|
|
/* Check for PID Match */
|
2012-02-18 01:27:50 +00:00
|
|
|
if (CurrentProcess->ClientId.UniqueProcess == Pid)
|
|
|
|
{
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
break;
|
|
|
|
}
|
2012-02-16 06:45:25 +00:00
|
|
|
|
2012-12-11 01:25:57 +00:00
|
|
|
/* Move to the next entry */
|
2012-02-16 06:45:25 +00:00
|
|
|
NextEntry = NextEntry->Flink;
|
2012-02-18 01:27:50 +00:00
|
|
|
} while (NextEntry != &CsrRootProcess->ListLink);
|
2012-02-16 06:45:25 +00:00
|
|
|
|
|
|
|
/* Check if we didn't find it in the list */
|
2012-02-18 01:27:50 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2012-02-16 06:45:25 +00:00
|
|
|
{
|
|
|
|
/* Nothing found, release the lock */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
}
|
2012-02-18 01:27:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Lock the found process and return it */
|
|
|
|
CsrLockedReferenceProcess(CurrentProcess);
|
|
|
|
*CsrProcess = CurrentProcess;
|
|
|
|
}
|
2012-12-11 01:45:49 +00:00
|
|
|
|
2012-02-18 01:27:50 +00:00
|
|
|
/* Return the result */
|
|
|
|
return Status;
|
2012-02-16 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/*++
|
|
|
|
* @name CsrRevertToSelf
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrRevertToSelf routine will attempt to remove an active impersonation.
|
|
|
|
*
|
|
|
|
* @param None.
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return TRUE if the reversion was succesful, FALSE otherwise.
|
2012-10-22 00:09:51 +00:00
|
|
|
*
|
|
|
|
* @remarks Impersonation can be recursive; as such, the impersonation token
|
|
|
|
* will only be deleted once the CSR Thread's impersonaton count
|
|
|
|
* has reached zero.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
CsrRevertToSelf(VOID)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PCSR_THREAD CurrentThread = CsrGetClientThread();
|
|
|
|
HANDLE ImpersonationToken = NULL;
|
|
|
|
|
|
|
|
/* Check if we have a Current Thread */
|
|
|
|
if (CurrentThread)
|
|
|
|
{
|
|
|
|
/* Make sure impersonation is on */
|
|
|
|
if (!CurrentThread->ImpersonationCount)
|
|
|
|
{
|
2013-03-10 19:37:33 +00:00
|
|
|
DPRINT1("CSRSS: CsrRevertToSelf called while not impersonating\n");
|
2012-10-22 00:09:51 +00:00
|
|
|
// DbgBreakPoint();
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-03-10 19:37:33 +00:00
|
|
|
else if ((--CurrentThread->ImpersonationCount) > 0)
|
2012-10-22 00:09:51 +00:00
|
|
|
{
|
|
|
|
/* Success; impersonation count decreased but still not zero */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Impersonation has been totally removed, revert to ourselves */
|
|
|
|
Status = NtSetInformationThread(NtCurrentThread(),
|
|
|
|
ThreadImpersonationToken,
|
|
|
|
&ImpersonationToken,
|
2015-08-15 16:34:21 +00:00
|
|
|
sizeof(ImpersonationToken));
|
2012-10-22 00:09:51 +00:00
|
|
|
|
|
|
|
/* Return TRUE or FALSE */
|
|
|
|
return NT_SUCCESS(Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrSetBackgroundPriority
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrSetBackgroundPriority routine sets the priority for the given CSR
|
|
|
|
* Process as a Background priority.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process whose priority will be modified.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
2015-08-15 16:34:21 +00:00
|
|
|
PROCESS_FOREGROUND_BACKGROUND ProcessPriority;
|
2012-10-22 00:09:51 +00:00
|
|
|
|
|
|
|
/* Set the Foreground bit off */
|
2015-08-15 16:34:21 +00:00
|
|
|
ProcessPriority.Foreground = FALSE;
|
2012-10-22 00:09:51 +00:00
|
|
|
|
2015-08-15 16:34:21 +00:00
|
|
|
/* Set the new priority */
|
2012-10-22 00:09:51 +00:00
|
|
|
NtSetInformationProcess(CsrProcess->ProcessHandle,
|
2015-08-15 16:34:21 +00:00
|
|
|
ProcessForegroundInformation,
|
|
|
|
&ProcessPriority,
|
|
|
|
sizeof(ProcessPriority));
|
2012-10-22 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
|
|
|
* @name CsrSetForegroundPriority
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrSetForegroundPriority routine sets the priority for the given CSR
|
|
|
|
* Process as a Foreground priority.
|
|
|
|
*
|
|
|
|
* @param CsrProcess
|
|
|
|
* Pointer to the CSR Process whose priority will be modified.
|
|
|
|
*
|
|
|
|
* @return None.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
CsrSetForegroundPriority(IN PCSR_PROCESS CsrProcess)
|
|
|
|
{
|
2015-08-15 16:34:21 +00:00
|
|
|
PROCESS_FOREGROUND_BACKGROUND ProcessPriority;
|
2012-10-22 00:09:51 +00:00
|
|
|
|
|
|
|
/* Set the Foreground bit on */
|
2015-08-15 16:34:21 +00:00
|
|
|
ProcessPriority.Foreground = TRUE;
|
2012-10-22 00:09:51 +00:00
|
|
|
|
2015-08-15 16:34:21 +00:00
|
|
|
/* Set the new priority */
|
2012-10-22 00:09:51 +00:00
|
|
|
NtSetInformationProcess(CsrProcess->ProcessHandle,
|
2015-08-15 16:34:21 +00:00
|
|
|
ProcessForegroundInformation,
|
|
|
|
&ProcessPriority,
|
|
|
|
sizeof(ProcessPriority));
|
2012-10-22 00:09:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-15 19:28:41 +00:00
|
|
|
/*++
|
|
|
|
* @name FindProcessForShutdown
|
|
|
|
*
|
|
|
|
* The FindProcessForShutdown routine returns a CSR Process which is ready
|
|
|
|
* to be shutdown, and sets the appropriate shutdown flags for it.
|
|
|
|
*
|
|
|
|
* @param CallerLuid
|
|
|
|
* Pointer to the LUID of the CSR Process calling this routine.
|
|
|
|
*
|
|
|
|
* @return Pointer to a CSR Process which is ready to be shutdown.
|
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
PCSR_PROCESS
|
|
|
|
NTAPI
|
|
|
|
FindProcessForShutdown(IN PLUID CallerLuid)
|
|
|
|
{
|
|
|
|
PCSR_PROCESS CsrProcess, ReturnCsrProcess = NULL;
|
|
|
|
PCSR_THREAD CsrThread;
|
|
|
|
NTSTATUS Status;
|
|
|
|
ULONG Level = 0;
|
|
|
|
LUID ProcessLuid;
|
|
|
|
LUID SystemLuid = SYSTEM_LUID;
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
|
|
|
|
/* Set the List Pointers */
|
|
|
|
NextEntry = CsrRootProcess->ListLink.Flink;
|
|
|
|
while (NextEntry != &CsrRootProcess->ListLink)
|
|
|
|
{
|
|
|
|
/* Get the process */
|
|
|
|
CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
|
|
|
|
|
|
|
|
/* Move to the next entry */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
|
|
|
|
/* Skip this process if it's already been processed */
|
|
|
|
if (CsrProcess->Flags & CsrProcessSkipShutdown) continue;
|
|
|
|
|
|
|
|
/* Get the LUID of this process */
|
|
|
|
Status = CsrGetProcessLuid(CsrProcess->ProcessHandle, &ProcessLuid);
|
|
|
|
|
|
|
|
/* Check if we didn't get access to the LUID */
|
|
|
|
if (Status == STATUS_ACCESS_DENIED)
|
|
|
|
{
|
|
|
|
/* Check if we have any threads */
|
|
|
|
if (CsrProcess->ThreadCount)
|
|
|
|
{
|
|
|
|
/* Impersonate one of the threads and retry */
|
|
|
|
CsrThread = CONTAINING_RECORD(CsrProcess->ThreadList.Flink,
|
|
|
|
CSR_THREAD,
|
|
|
|
Link);
|
|
|
|
if (CsrImpersonateClient(CsrThread))
|
|
|
|
{
|
|
|
|
Status = CsrGetProcessLuid(NULL, &ProcessLuid);
|
|
|
|
CsrRevertToSelf();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = STATUS_BAD_IMPERSONATION_LEVEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* We didn't have access, so skip it */
|
|
|
|
CsrProcess->Flags |= CsrProcessSkipShutdown;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this is the System LUID */
|
|
|
|
if (RtlEqualLuid(&ProcessLuid, &SystemLuid))
|
|
|
|
{
|
|
|
|
/* Mark this process */
|
|
|
|
CsrProcess->ShutdownFlags |= CsrShutdownSystem;
|
|
|
|
}
|
|
|
|
else if (!RtlEqualLuid(&ProcessLuid, CallerLuid))
|
|
|
|
{
|
|
|
|
/* Our LUID doesn't match with the caller's */
|
|
|
|
CsrProcess->ShutdownFlags |= CsrShutdownOther;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we're past the previous level */
|
|
|
|
if ((CsrProcess->ShutdownLevel > Level) || !ReturnCsrProcess)
|
|
|
|
{
|
|
|
|
/* Update the level */
|
|
|
|
Level = CsrProcess->ShutdownLevel;
|
|
|
|
|
|
|
|
/* Set the final process */
|
|
|
|
ReturnCsrProcess = CsrProcess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we found a process */
|
|
|
|
if (ReturnCsrProcess)
|
|
|
|
{
|
|
|
|
/* Skip this one next time */
|
|
|
|
ReturnCsrProcess->Flags |= CsrProcessSkipShutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReturnCsrProcess;
|
|
|
|
}
|
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
/*++
|
|
|
|
* @name CsrShutdownProcesses
|
|
|
|
* @implemented NT4
|
|
|
|
*
|
|
|
|
* The CsrShutdownProcesses routine shuts down every CSR Process possible
|
|
|
|
* and calls each Server DLL's shutdown notification.
|
|
|
|
*
|
|
|
|
* @param CallerLuid
|
|
|
|
* Pointer to the LUID of the CSR Process that is ordering the
|
|
|
|
* shutdown.
|
|
|
|
*
|
|
|
|
* @param Flags
|
|
|
|
* Flags to send to the shutdown notification routine.
|
|
|
|
*
|
2013-03-10 19:37:33 +00:00
|
|
|
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
* @remarks None.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
|
|
|
CsrShutdownProcesses(IN PLUID CallerLuid,
|
|
|
|
IN ULONG Flags)
|
|
|
|
{
|
|
|
|
PLIST_ENTRY NextEntry;
|
|
|
|
PCSR_PROCESS CsrProcess;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BOOLEAN FirstTry;
|
|
|
|
ULONG i;
|
|
|
|
PCSR_SERVER_DLL ServerDll;
|
2014-06-15 19:28:41 +00:00
|
|
|
ULONG Result = 0;
|
2012-10-20 13:00:41 +00:00
|
|
|
|
|
|
|
/* Acquire process lock */
|
|
|
|
CsrAcquireProcessLock();
|
|
|
|
|
|
|
|
/* Add shutdown flag */
|
|
|
|
CsrRootProcess->ShutdownFlags |= CsrShutdownSystem;
|
|
|
|
|
|
|
|
/* Get the list pointers */
|
|
|
|
NextEntry = CsrRootProcess->ListLink.Flink;
|
|
|
|
while (NextEntry != &CsrRootProcess->ListLink)
|
|
|
|
{
|
|
|
|
/* Get the Process */
|
|
|
|
CsrProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
|
|
|
|
|
2012-12-11 01:25:57 +00:00
|
|
|
/* Move to the next entry */
|
|
|
|
NextEntry = NextEntry->Flink;
|
|
|
|
|
|
|
|
/* Remove the skip flag, set shutdown flags to 0 */
|
2012-10-20 13:00:41 +00:00
|
|
|
CsrProcess->Flags &= ~CsrProcessSkipShutdown;
|
|
|
|
CsrProcess->ShutdownFlags = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-28 21:40:34 +00:00
|
|
|
/* Set shutdown Priority */
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrSetToShutdownPriority();
|
2012-10-20 13:00:41 +00:00
|
|
|
|
|
|
|
/* Start looping */
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
/* Find the next process to shutdown */
|
|
|
|
CsrProcess = FindProcessForShutdown(CallerLuid);
|
|
|
|
if (!CsrProcess) break;
|
|
|
|
|
|
|
|
/* Increase reference to process */
|
2013-01-01 02:59:31 +00:00
|
|
|
CsrLockedReferenceProcess(CsrProcess);
|
2012-10-20 13:00:41 +00:00
|
|
|
|
|
|
|
FirstTry = TRUE;
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
/* Loop all the servers */
|
|
|
|
for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
|
|
|
|
{
|
|
|
|
/* Get the current server */
|
|
|
|
ServerDll = CsrLoadedServerDll[i];
|
2012-11-13 21:08:19 +00:00
|
|
|
|
|
|
|
/* Check if it's valid and if it has a Shutdown Process Callback */
|
|
|
|
if (ServerDll && ServerDll->ShutdownProcessCallback)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
|
|
|
/* Release the lock, make the callback, and acquire it back */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
Result = ServerDll->ShutdownProcessCallback(CsrProcess,
|
|
|
|
Flags,
|
|
|
|
FirstTry);
|
|
|
|
CsrAcquireProcessLock();
|
|
|
|
|
|
|
|
/* Check the result */
|
|
|
|
if (Result == CsrShutdownCsrProcess)
|
|
|
|
{
|
|
|
|
/* The callback unlocked the process */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (Result == CsrShutdownCancelled)
|
|
|
|
{
|
2020-04-14 20:53:49 +00:00
|
|
|
#ifdef CSR_DBG
|
2012-10-20 13:00:41 +00:00
|
|
|
/* Check if this was a forced shutdown */
|
|
|
|
if (Flags & EWX_FORCE)
|
|
|
|
{
|
|
|
|
DPRINT1("Process %x cancelled forced shutdown (Dll = %d)\n",
|
|
|
|
CsrProcess->ClientId.UniqueProcess, i);
|
|
|
|
DbgBreakPoint();
|
|
|
|
}
|
2020-04-14 20:53:49 +00:00
|
|
|
#endif
|
2012-10-20 13:00:41 +00:00
|
|
|
|
|
|
|
/* Shutdown was cancelled, unlock and exit */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
Status = STATUS_CANCELLED;
|
|
|
|
goto Quickie;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No matches during the first try, so loop again */
|
2014-06-15 19:28:41 +00:00
|
|
|
if (FirstTry && (Result == CsrShutdownNonCsrProcess))
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
|
|
|
FirstTry = FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Second try, break out */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've reached the final loop here, so dereference */
|
2020-04-14 20:53:49 +00:00
|
|
|
if (i == CSR_SERVER_DLL_MAX)
|
|
|
|
CsrLockedDereferenceProcess(CsrProcess);
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Success path */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
Quickie:
|
|
|
|
/* Return to normal priority */
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrSetToNormalPriority();
|
|
|
|
|
2012-10-20 13:00:41 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*++
|
2012-10-22 00:09:51 +00:00
|
|
|
* @name CsrUnlockProcess
|
2012-10-20 13:00:41 +00:00
|
|
|
* @implemented NT4
|
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* The CsrUnlockProcess undoes a previous CsrLockProcessByClientId operation.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
* @param CsrProcess
|
2012-10-22 00:09:51 +00:00
|
|
|
* Pointer to a previously locked CSR Process.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @return STATUS_SUCCESS.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
2012-10-22 00:09:51 +00:00
|
|
|
* @remarks This routine must be called with the Process Lock held.
|
2012-10-20 13:00:41 +00:00
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
NTSTATUS
|
|
|
|
NTAPI
|
2012-10-22 00:09:51 +00:00
|
|
|
CsrUnlockProcess(IN PCSR_PROCESS CsrProcess)
|
2012-10-20 13:00:41 +00:00
|
|
|
{
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Dereference the process */
|
|
|
|
CsrLockedDereferenceProcess(CsrProcess);
|
2012-10-20 13:00:41 +00:00
|
|
|
|
2012-10-22 00:09:51 +00:00
|
|
|
/* Release the lock and return */
|
|
|
|
CsrReleaseProcessLock();
|
|
|
|
return STATUS_SUCCESS;
|
2012-10-20 13:00:41 +00:00
|
|
|
}
|
|
|
|
|
2010-03-10 06:49:53 +00:00
|
|
|
/* EOF */
|