Leaked source code of windows server 2003
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.

273 lines
6.7 KiB

  1. /*++
  2. Copyright(c) 1995 Microsoft Corporation
  3. MODULE NAME
  4. process.c
  5. ABSTRACT
  6. NT process routines for the automatic connection system service.
  7. AUTHOR
  8. Anthony Discolo (adiscolo) 12-Aug-1995
  9. REVISION HISTORY
  10. --*/
  11. #define UNICODE
  12. #define _UNICODE
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <stdlib.h>
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <npapi.h>
  20. #include <debug.h>
  21. #include "radebug.h"
  22. PSYSTEM_PROCESS_INFORMATION
  23. GetSystemProcessInfo()
  24. /*++
  25. DESCRIPTION
  26. Return a block containing information about all processes
  27. currently running in the system.
  28. ARGUMENTS
  29. None.
  30. RETURN VALUE
  31. A pointer to the system process information or NULL if it could
  32. not be allocated or retrieved.
  33. --*/
  34. {
  35. NTSTATUS status;
  36. PUCHAR pLargeBuffer;
  37. ULONG ulcbLargeBuffer = 64 * 1024;
  38. //
  39. // Get the process list.
  40. //
  41. for (;;) {
  42. pLargeBuffer = VirtualAlloc(
  43. NULL,
  44. ulcbLargeBuffer, MEM_COMMIT, PAGE_READWRITE);
  45. if (pLargeBuffer == NULL) {
  46. RASAUTO_TRACE1(
  47. "GetSystemProcessInfo: VirtualAlloc failed (status=0x%x)",
  48. GetLastError());
  49. return NULL;
  50. }
  51. status = NtQuerySystemInformation(
  52. SystemProcessInformation,
  53. pLargeBuffer,
  54. ulcbLargeBuffer,
  55. NULL);
  56. if (status == STATUS_SUCCESS) break;
  57. if (status == STATUS_INFO_LENGTH_MISMATCH) {
  58. VirtualFree(pLargeBuffer, 0, MEM_RELEASE);
  59. ulcbLargeBuffer += 8192;
  60. RASAUTO_TRACE1(
  61. "GetSystemProcesInfo: enlarging buffer to %d",
  62. ulcbLargeBuffer);
  63. }
  64. }
  65. return (PSYSTEM_PROCESS_INFORMATION)pLargeBuffer;
  66. } // GetSystemProcessInfo
  67. PSYSTEM_PROCESS_INFORMATION
  68. FindProcessByNameList(
  69. IN PSYSTEM_PROCESS_INFORMATION pProcessInfo,
  70. IN LPTSTR *lpExeNameList,
  71. IN DWORD dwcExeNameList,
  72. IN DWORD dwPid,
  73. IN BOOL fRequireSessionMatch,
  74. IN DWORD dwSessionId
  75. )
  76. /*++
  77. DESCRIPTION
  78. Given a pointer returned by GetSystemProcessInfo(), find
  79. a process by name.
  80. ARGUMENTS
  81. pProcessInfo: a pointer returned by GetSystemProcessInfo().
  82. lpExeNameList: a pointer to a list of Unicode strings containing the
  83. process to be found.
  84. dwcExeNameList: the number of strings in lpExeNameList
  85. RETURN VALUE
  86. A pointer to the process information for the supplied
  87. process or NULL if it could not be found.
  88. --*/
  89. {
  90. PUCHAR pLargeBuffer = (PUCHAR)pProcessInfo;
  91. DWORD i = 0;
  92. ULONG ulTotalOffset = 0;
  93. BOOL fValid = ((0 == dwPid) ? TRUE : FALSE);
  94. //
  95. // Look in the process list for lpExeName.
  96. //
  97. for (;;) {
  98. if (pProcessInfo->ImageName.Buffer != NULL)
  99. {
  100. RASAUTO_TRACE3(
  101. "FindProcessByName: process: %S (%d) (%d)",
  102. pProcessInfo->ImageName.Buffer,
  103. pProcessInfo->UniqueProcessId,
  104. pProcessInfo->SessionId);
  105. for (i = 0; i < dwcExeNameList; i++)
  106. {
  107. if (!_wcsicmp(pProcessInfo->ImageName.Buffer, lpExeNameList[i]))
  108. {
  109. // return pProcessInfo;
  110. break;
  111. }
  112. }
  113. }
  114. if ( (NULL != pProcessInfo->ImageName.Buffer)
  115. && (i < dwcExeNameList))
  116. {
  117. if(fValid)
  118. {
  119. // XP 353082
  120. //
  121. // If we know the id of the session currently attached to the
  122. // console, then require our process to match that session id.
  123. //
  124. if (fRequireSessionMatch)
  125. {
  126. if (pProcessInfo->SessionId == dwSessionId)
  127. {
  128. RASAUTO_TRACE1(
  129. "FindProcess...: Success (==) pid=%d",
  130. pProcessInfo->UniqueProcessId);
  131. return pProcessInfo;
  132. }
  133. else
  134. {
  135. RASAUTO_TRACE1(
  136. "FindProcess...: %d name match, but not sessionid",
  137. pProcessInfo->UniqueProcessId);
  138. }
  139. }
  140. else
  141. {
  142. RASAUTO_TRACE1(
  143. "FindProcess...: Success (any) pid=%d",
  144. pProcessInfo->UniqueProcessId);
  145. return pProcessInfo;
  146. }
  147. }
  148. else
  149. {
  150. RASAUTO_TRACE1(
  151. "Looking for other instances of %ws",
  152. lpExeNameList[i]);
  153. if (PtrToUlong(pProcessInfo->UniqueProcessId) == dwPid)
  154. {
  155. fValid = TRUE;
  156. }
  157. }
  158. }
  159. //
  160. // Increment offset to next process information block.
  161. //
  162. if (!pProcessInfo->NextEntryOffset)
  163. break;
  164. ulTotalOffset += pProcessInfo->NextEntryOffset;
  165. pProcessInfo = (PSYSTEM_PROCESS_INFORMATION)&pLargeBuffer[ulTotalOffset];
  166. }
  167. RASAUTO_TRACE1("No more instances of %ws found",
  168. pProcessInfo->ImageName.Buffer);
  169. return NULL;
  170. } // FindProcessByNameList
  171. PSYSTEM_PROCESS_INFORMATION
  172. FindProcessByName(
  173. IN PSYSTEM_PROCESS_INFORMATION pProcessInfo,
  174. IN LPTSTR lpExeName
  175. )
  176. /*++
  177. DESCRIPTION
  178. Given a pointer returned by GetSystemProcessInfo(), find
  179. a process by name.
  180. ARGUMENTS
  181. pProcessInfo: a pointer returned by GetSystemProcessInfo().
  182. lpExeName: a pointer to a Unicode string containing the
  183. process to be found.
  184. RETURN VALUE
  185. A pointer to the process information for the supplied
  186. process or NULL if it could not be found.
  187. --*/
  188. {
  189. LPTSTR lpExeNameList[1];
  190. lpExeNameList[0] = lpExeName;
  191. return FindProcessByNameList(
  192. pProcessInfo,
  193. lpExeNameList,
  194. 1,
  195. 0,
  196. FALSE,
  197. 0);
  198. } // FindProcessByName
  199. VOID
  200. FreeSystemProcessInfo(
  201. IN PSYSTEM_PROCESS_INFORMATION pProcessInfo
  202. )
  203. /*++
  204. DESCRIPTION
  205. Free a buffer returned by GetSystemProcessInfo().
  206. ARGUMENTS
  207. pProcessInfo: the pointer returned by GetSystemProcessInfo().
  208. RETURN VALUE
  209. None.
  210. --*/
  211. {
  212. VirtualFree((PUCHAR)pProcessInfo, 0, MEM_RELEASE);
  213. } // FreeSystemProcessInfo