[CSRSRV]: Move the newly ported process APIs to procsup.c where they belong more. No functional change.

svn path=/trunk/; revision=55625
This commit is contained in:
Alex Ionescu 2012-02-16 06:45:25 +00:00
parent 51bc2e4f04
commit 0f20fb2587
4 changed files with 369 additions and 322 deletions

View file

@ -12,320 +12,14 @@
#define NDEBUG
#include <debug.h>
#define CSR_SERVER_DLL_MAX 4
#define LOCK RtlEnterCriticalSection(&ProcessDataLock)
#define UNLOCK RtlLeaveCriticalSection(&ProcessDataLock)
#define CsrAcquireProcessLock() LOCK
#define CsrReleaseProcessLock() UNLOCK
#define ProcessStructureListLocked() \
(ProcessDataLock.OwningThread == NtCurrentTeb()->ClientId.UniqueThread)
extern NTSTATUS CallProcessInherit(PCSR_PROCESS, PCSR_PROCESS);
extern NTSTATUS CallProcessDeleted(PCSR_PROCESS);
/* GLOBALS *******************************************************************/
RTL_CRITICAL_SECTION ProcessDataLock, CsrWaitListsLock;
extern PCSR_PROCESS CsrRootProcess;
extern LIST_ENTRY CsrThreadHashTable[256];
extern ULONG CsrTotalPerProcessDataLength;
LONG CsrProcessSequenceCount = 5;
/* FUNCTIONS *****************************************************************/
/*++
* @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 */
CsrProcess->ReferenceCount++;
/* Initialize the Thread List */
InitializeListHead(&CsrProcess->ThreadList);
/* Return the Process */
return CsrProcess;
}
/*++
* @name CsrLockedReferenceProcess
*
* The CsrLockedReferenceProcess refences 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 CsrServerInitialization
* @implemented NT4
*
* The CsrInitializeProcessStructure routine sets up support for CSR Processes
* and CSR Threads.
*
* @param None.
*
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
* othwerwise.
*
* @remarks None.
*
*--*/
NTSTATUS
NTAPI
CsrInitializeProcessStructure(VOID)
{
NTSTATUS Status;
ULONG i;
/* Initialize the Lock */
Status = RtlInitializeCriticalSection(&ProcessDataLock);
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 */
for (i = 0; i < 256; i++) InitializeListHead(&CsrThreadHashTable[i]);
/* 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)
{
#if 0
PCSR_SERVER_DLL ServerDll;
ULONG i;
#endif
ASSERT(ProcessStructureListLocked());
/* Remove us from the Process List */
RemoveEntryList(&CsrProcess->ListLink);
/* Release the lock */
CsrReleaseProcessLock();
#if 0
/* Loop every Server DLL */
for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
{
/* Get the Server DLL */
ServerDll = CsrLoadedServerDll[i];
/* Check if it's valid and if it has a Disconnect Callback */
if (ServerDll && ServerDll->DisconnectCallback)
{
/* Call it */
(ServerDll->DisconnectCallback)(CsrProcess);
}
}
#endif
}
/*++
* @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.
*
* @param Parent
* Optional pointer to the CSR Process creating this CSR Process.
*
* @param CurrentProcess
* Optional pointer to the current CSR Process.
*
* @param CsrProcess
* Pointer to the CSR Process which is to be inserted.
*
* @return None.
*
* @remarks None.
*
*--*/
VOID
NTAPI
CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL,
IN PCSR_PROCESS CurrentProcess OPTIONAL,
IN PCSR_PROCESS CsrProcess)
{
#if 0
PCSR_SERVER_DLL ServerDll;
ULONG i;
#endif
ASSERT(ProcessStructureListLocked());
/* Set the parent */
CsrProcess->Parent = Parent;
/* Insert it into the Root List */
InsertTailList(&CsrRootProcess->ListLink, &CsrProcess->ListLink);
#if 0
/* 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 */
if ((ServerDll) && (ServerDll->NewProcessCallback))
{
ServerDll->NewProcessCallback(CurrentProcess, CsrProcess);
}
}
#endif
}
/*++
* @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.
*
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
* othwerwise.
*
* @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;
/* Acquire the lock */
CsrAcquireProcessLock();
/* Assume failure */
ASSERT(CsrProcess != NULL);
*CsrProcess = NULL;
/* Setup the List Pointers */
NextEntry = CsrRootProcess->ListLink.Flink;
while (NextEntry != &CsrRootProcess->ListLink)
{
/* Get the Process */
CurrentProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
/* Check for PID Match */
if (CurrentProcess->ClientId.UniqueProcess == Pid) break;
/* Next entry */
NextEntry = NextEntry->Flink;
}
/* Check if we didn't find it in the list */
if (NextEntry == &CsrRootProcess->ListLink)
{
/* Nothing found, release the lock */
CsrReleaseProcessLock();
return STATUS_UNSUCCESSFUL;
}
/* Lock the found process and return it */
CsrLockedReferenceProcess(CurrentProcess);
*CsrProcess = CurrentProcess;
return STATUS_SUCCESS;
}
PCSR_PROCESS WINAPI CsrGetProcessData(HANDLE ProcessId)
{
PCSR_PROCESS CsrProcess;

View file

@ -14,15 +14,9 @@
#define NDEBUG
#include <debug.h>
#define LOCK RtlEnterCriticalSection(&ProcessDataLock)
#define UNLOCK RtlLeaveCriticalSection(&ProcessDataLock)
#define CsrAcquireProcessLock() LOCK
#define CsrReleaseProcessLock() UNLOCK
/* GLOBALS ********************************************************************/
extern RTL_CRITICAL_SECTION ProcessDataLock;
RTL_CRITICAL_SECTION ProcessDataLock, CsrWaitListsLock;
PCSR_PROCESS CsrRootProcess;
SECURITY_QUALITY_OF_SERVICE CsrSecurityQos =
{
@ -31,6 +25,8 @@ SECURITY_QUALITY_OF_SERVICE CsrSecurityQos =
SECURITY_STATIC_TRACKING,
FALSE
};
LONG CsrProcessSequenceCount = 5;
extern ULONG CsrTotalPerProcessDataLength;
/* FUNCTIONS ******************************************************************/
@ -398,4 +394,327 @@ CsrUnlockProcess(IN PCSR_PROCESS CsrProcess)
return STATUS_SUCCESS;
}
/*++
* @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)
{
PROCESS_PRIORITY_CLASS PriorityClass;
/* Set the Foreground bit off */
PriorityClass.Foreground = FALSE;
/* Set the new Priority */
NtSetInformationProcess(CsrProcess->ProcessHandle,
ProcessPriorityClass,
&PriorityClass,
sizeof(PriorityClass));
}
/*++
* @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 */
CsrProcess->ReferenceCount++;
/* Initialize the Thread List */
InitializeListHead(&CsrProcess->ThreadList);
/* Return the Process */
return CsrProcess;
}
/*++
* @name CsrLockedReferenceProcess
*
* The CsrLockedReferenceProcess refences 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 CsrServerInitialization
* @implemented NT4
*
* The CsrInitializeProcessStructure routine sets up support for CSR Processes
* and CSR Threads.
*
* @param None.
*
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
* othwerwise.
*
* @remarks None.
*
*--*/
NTSTATUS
NTAPI
CsrInitializeProcessStructure(VOID)
{
NTSTATUS Status;
ULONG i;
/* Initialize the Lock */
Status = RtlInitializeCriticalSection(&ProcessDataLock);
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 */
for (i = 0; i < 256; i++) InitializeListHead(&CsrThreadHashTable[i]);
/* 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)
{
#if 0
PCSR_SERVER_DLL ServerDll;
ULONG i;
#endif
ASSERT(ProcessStructureListLocked());
/* Remove us from the Process List */
RemoveEntryList(&CsrProcess->ListLink);
/* Release the lock */
CsrReleaseProcessLock();
#if 0
/* Loop every Server DLL */
for (i = 0; i < CSR_SERVER_DLL_MAX; i++)
{
/* Get the Server DLL */
ServerDll = CsrLoadedServerDll[i];
/* Check if it's valid and if it has a Disconnect Callback */
if (ServerDll && ServerDll->DisconnectCallback)
{
/* Call it */
(ServerDll->DisconnectCallback)(CsrProcess);
}
}
#endif
}
/*++
* @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.
*
* @param Parent
* Optional pointer to the CSR Process creating this CSR Process.
*
* @param CurrentProcess
* Optional pointer to the current CSR Process.
*
* @param CsrProcess
* Pointer to the CSR Process which is to be inserted.
*
* @return None.
*
* @remarks None.
*
*--*/
VOID
NTAPI
CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL,
IN PCSR_PROCESS CurrentProcess OPTIONAL,
IN PCSR_PROCESS CsrProcess)
{
#if 0
PCSR_SERVER_DLL ServerDll;
ULONG i;
#endif
ASSERT(ProcessStructureListLocked());
/* Set the parent */
CsrProcess->Parent = Parent;
/* Insert it into the Root List */
InsertTailList(&CsrRootProcess->ListLink, &CsrProcess->ListLink);
#if 0
/* 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 */
if ((ServerDll) && (ServerDll->NewProcessCallback))
{
ServerDll->NewProcessCallback(CurrentProcess, CsrProcess);
}
}
#endif
}
/*++
* @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.
*
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
* othwerwise.
*
* @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;
/* Acquire the lock */
CsrAcquireProcessLock();
/* Assume failure */
ASSERT(CsrProcess != NULL);
*CsrProcess = NULL;
/* Setup the List Pointers */
NextEntry = CsrRootProcess->ListLink.Flink;
while (NextEntry != &CsrRootProcess->ListLink)
{
/* Get the Process */
CurrentProcess = CONTAINING_RECORD(NextEntry, CSR_PROCESS, ListLink);
/* Check for PID Match */
if (CurrentProcess->ClientId.UniqueProcess == Pid) break;
/* Next entry */
NextEntry = NextEntry->Flink;
}
/* Check if we didn't find it in the list */
if (NextEntry == &CsrRootProcess->ListLink)
{
/* Nothing found, release the lock */
CsrReleaseProcessLock();
return STATUS_UNSUCCESSFUL;
}
/* Lock the found process and return it */
CsrLockedReferenceProcess(CurrentProcess);
*CsrProcess = CurrentProcess;
return STATUS_SUCCESS;
}
/* EOF */

View file

@ -14,21 +14,12 @@
#define NDEBUG
#include <debug.h>
#define LOCK RtlEnterCriticalSection(&ProcessDataLock)
#define UNLOCK RtlLeaveCriticalSection(&ProcessDataLock)
#define CsrHeap RtlGetProcessHeap()
#define CsrHashThread(t) \
(HandleToUlong(t)&(256 - 1))
#define CsrAcquireProcessLock() LOCK
#define CsrReleaseProcessLock() UNLOCK
/* GLOBALS ********************************************************************/
LIST_ENTRY CsrThreadHashTable[256];
extern PCSR_PROCESS CsrRootProcess;
extern RTL_CRITICAL_SECTION ProcessDataLock;
extern PCSR_PROCESS ProcessData[256];
/* FUNCTIONS ******************************************************************/

View file

@ -14,6 +14,14 @@
#include <csrss/csrss.h>
#define CSR_SERVER_DLL_MAX 4
#define LOCK RtlEnterCriticalSection(&ProcessDataLock)
#define UNLOCK RtlLeaveCriticalSection(&ProcessDataLock)
#define CsrAcquireProcessLock() LOCK
#define CsrReleaseProcessLock() UNLOCK
#define ProcessStructureListLocked() \
(ProcessDataLock.OwningThread == NtCurrentTeb()->ClientId.UniqueThread)
typedef enum _CSR_THREAD_FLAGS
{
CsrThreadAltertable = 0x1,
@ -137,10 +145,33 @@ CSR_API(CsrSrvCreateThread);
CSR_API(CsrGetShutdownParameters);
CSR_API(CsrSetShutdownParameters);
VOID
NTAPI
CsrSetBackgroundPriority(IN PCSR_PROCESS CsrProcess);
PCSR_THREAD
NTAPI
CsrAllocateThread(IN PCSR_PROCESS CsrProcess);
PCSR_PROCESS
NTAPI
CsrAllocateProcess(VOID);
VOID
NTAPI
CsrDeallocateProcess(IN PCSR_PROCESS CsrProcess);
VOID
NTAPI
CsrRemoveProcess(IN PCSR_PROCESS CsrProcess);
VOID
NTAPI
CsrInsertProcess(IN PCSR_PROCESS Parent OPTIONAL,
IN PCSR_PROCESS CurrentProcess OPTIONAL,
IN PCSR_PROCESS CsrProcess);
/* api/wapi.c */
NTSTATUS FASTCALL CsrApiRegisterDefinitions(PCSRSS_API_DEFINITION NewDefinitions);
VOID FASTCALL CsrApiCallHandler(PCSR_PROCESS ProcessData,
@ -149,6 +180,18 @@ VOID WINAPI CsrSbApiRequestThread (PVOID PortHandle);
VOID NTAPI ClientConnectionThread(HANDLE ServerPort);
extern HANDLE CsrSbApiPort;
extern LIST_ENTRY CsrThreadHashTable[256];
extern PCSR_PROCESS CsrRootProcess;
extern RTL_CRITICAL_SECTION ProcessDataLock, CsrWaitListsLock;
BOOLEAN
NTAPI
ProtectHandle(IN HANDLE ObjectHandle);
VOID
NTAPI
CsrInsertThread(IN PCSR_PROCESS Process,
IN PCSR_THREAD Thread);
/* api/process.c */
typedef NTSTATUS (WINAPI *CSRSS_ENUM_PROCESS_PROC)(PCSR_PROCESS ProcessData,