Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. pscid.c
  5. Abstract:
  6. This module implements the Client ID related services.
  7. Author:
  8. Mark Lucovsky (markl) 25-Apr-1989
  9. Jim Kelly (JimK) 2-August-1990
  10. Revision History:
  11. --*/
  12. #include "psp.h"
  13. #ifdef ALLOC_PRAGMA
  14. #pragma alloc_text(PAGE, PsLookupProcessThreadByCid)
  15. #pragma alloc_text(PAGE, PsLookupProcessByProcessId)
  16. #pragma alloc_text(PAGE, PsLookupThreadByThreadId)
  17. #endif //ALLOC_PRAGMA
  18. NTSTATUS
  19. PsLookupProcessThreadByCid(
  20. IN PCLIENT_ID Cid,
  21. OUT PEPROCESS *Process OPTIONAL,
  22. OUT PETHREAD *Thread
  23. )
  24. /*++
  25. Routine Description:
  26. This function accepts The Client ID of a thread, and returns a
  27. referenced pointer to the thread, and possibly a referenced pointer
  28. to the process.
  29. Arguments:
  30. Cid - Specifies the Client ID of the thread.
  31. Process - If specified, returns a referenced pointer to the process
  32. specified in the Cid.
  33. Thread - Returns a referenced pointer to the thread specified in the
  34. Cid.
  35. Return Value:
  36. STATUS_SUCCESS - A process and thread were located based on the contents
  37. of the Cid.
  38. STATUS_INVALID_CID - The specified Cid is invalid.
  39. --*/
  40. {
  41. PHANDLE_TABLE_ENTRY CidEntry;
  42. PETHREAD lThread;
  43. PETHREAD CurrentThread;
  44. PEPROCESS lProcess;
  45. NTSTATUS Status;
  46. PAGED_CODE();
  47. lThread = NULL;
  48. CurrentThread = PsGetCurrentThread ();
  49. KeEnterCriticalRegionThread (&CurrentThread->Tcb);
  50. CidEntry = ExMapHandleToPointer(PspCidTable, Cid->UniqueThread);
  51. if (CidEntry != NULL) {
  52. lThread = (PETHREAD)CidEntry->Object;
  53. if (!ObReferenceObjectSafe (lThread)) {
  54. lThread = NULL;
  55. }
  56. ExUnlockHandleTableEntry(PspCidTable, CidEntry);
  57. }
  58. KeLeaveCriticalRegionThread (&CurrentThread->Tcb);
  59. Status = STATUS_INVALID_CID;
  60. if (lThread != NULL) {
  61. //
  62. // This could be a thread or a process. Check its a thread.
  63. //
  64. if (lThread->Tcb.Header.Type != ThreadObject ||
  65. lThread->Cid.UniqueProcess != Cid->UniqueProcess ||
  66. lThread->GrantedAccess == 0) {
  67. ObDereferenceObject (lThread);
  68. } else {
  69. *Thread = lThread;
  70. if (ARGUMENT_PRESENT (Process)) {
  71. lProcess = THREAD_TO_PROCESS (lThread);
  72. *Process = lProcess;
  73. //
  74. // Since the thread holds a reference to the process this reference does not have to
  75. // be protected.
  76. //
  77. ObReferenceObject (lProcess);
  78. }
  79. Status = STATUS_SUCCESS;
  80. }
  81. }
  82. return Status;
  83. }
  84. NTSTATUS
  85. PsLookupProcessByProcessId(
  86. IN HANDLE ProcessId,
  87. OUT PEPROCESS *Process
  88. )
  89. /*++
  90. Routine Description:
  91. This function accepts the process id of a process and returns a
  92. referenced pointer to the process.
  93. Arguments:
  94. ProcessId - Specifies the Process ID of the process.
  95. Process - Returns a referenced pointer to the process specified by the
  96. process id.
  97. Return Value:
  98. STATUS_SUCCESS - A process was located based on the contents of
  99. the process id.
  100. STATUS_INVALID_PARAMETER - The process was not found.
  101. --*/
  102. {
  103. PHANDLE_TABLE_ENTRY CidEntry;
  104. PEPROCESS lProcess;
  105. PETHREAD CurrentThread;
  106. NTSTATUS Status;
  107. PAGED_CODE();
  108. CurrentThread = PsGetCurrentThread ();
  109. KeEnterCriticalRegionThread (&CurrentThread->Tcb);
  110. CidEntry = ExMapHandleToPointer(PspCidTable, ProcessId);
  111. Status = STATUS_INVALID_PARAMETER;
  112. if (CidEntry != NULL) {
  113. lProcess = (PEPROCESS)CidEntry->Object;
  114. if (lProcess->Pcb.Header.Type == ProcessObject &&
  115. lProcess->GrantedAccess != 0) {
  116. if (ObReferenceObjectSafe(lProcess)) {
  117. *Process = lProcess;
  118. Status = STATUS_SUCCESS;
  119. }
  120. }
  121. ExUnlockHandleTableEntry(PspCidTable, CidEntry);
  122. }
  123. KeLeaveCriticalRegionThread (&CurrentThread->Tcb);
  124. return Status;
  125. }
  126. NTSTATUS
  127. PsLookupThreadByThreadId(
  128. IN HANDLE ThreadId,
  129. OUT PETHREAD *Thread
  130. )
  131. /*++
  132. Routine Description:
  133. This function accepts the thread id of a thread and returns a
  134. referenced pointer to the thread.
  135. Arguments:
  136. ThreadId - Specifies the Thread ID of the thread.
  137. Thread - Returns a referenced pointer to the thread specified by the
  138. thread id.
  139. Return Value:
  140. STATUS_SUCCESS - A thread was located based on the contents of
  141. the thread id.
  142. STATUS_INVALID_PARAMETER - The thread was not found.
  143. --*/
  144. {
  145. PHANDLE_TABLE_ENTRY CidEntry;
  146. PETHREAD lThread;
  147. PETHREAD CurrentThread;
  148. NTSTATUS Status;
  149. PAGED_CODE();
  150. CurrentThread = PsGetCurrentThread ();
  151. KeEnterCriticalRegionThread (&CurrentThread->Tcb);
  152. CidEntry = ExMapHandleToPointer(PspCidTable, ThreadId);
  153. Status = STATUS_INVALID_PARAMETER;
  154. if (CidEntry != NULL) {
  155. lThread = (PETHREAD)CidEntry->Object;
  156. if (lThread->Tcb.Header.Type == ThreadObject && lThread->GrantedAccess) {
  157. if (ObReferenceObjectSafe(lThread)) {
  158. *Thread = lThread;
  159. Status = STATUS_SUCCESS;
  160. }
  161. }
  162. ExUnlockHandleTableEntry(PspCidTable, CidEntry);
  163. }
  164. KeLeaveCriticalRegionThread (&CurrentThread->Tcb);
  165. return Status;
  166. }