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.

499 lines
15 KiB

  1. // *********************************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // Module Name:
  6. //
  7. // TaskKill.h
  8. //
  9. // Abstract:
  10. //
  11. // macros and function prototypes of TaskKill.cpp
  12. //
  13. // Author:
  14. //
  15. // Sunil G.V.N. Murali ([email protected]) 26-Nov-2000
  16. //
  17. // Revision History:
  18. //
  19. // Sunil G.V.N. Murali ([email protected]) 26-Nov-2000 : Created It.
  20. //
  21. // *********************************************************************************
  22. #ifndef _TASKKILL_H
  23. #define _TASKKILL_H
  24. // resource header file
  25. #include "resource.h"
  26. //
  27. // NOTE: THIS MODULE WILL WRITTEN IN SUCH A FASHION THAT IT WORKS ONLY
  28. // IN UNICODE BUILD COMPILATION
  29. //
  30. #ifndef UNICODE
  31. #error Must compile only in unicode build environment
  32. #endif
  33. //
  34. // general purpose macros
  35. //
  36. #define EXIT_PROCESS( exitcode ) \
  37. ReleaseGlobals(); \
  38. return (exitcode); \
  39. 1
  40. #define HEAP_FREE( pointer ) \
  41. if ( (pointer) != NULL ) \
  42. { \
  43. HeapFree( GetProcessHeap(), 0, (pointer) ); \
  44. (pointer) = NULL; \
  45. } \
  46. 1
  47. #define RELEASE_MEMORY( block ) \
  48. if ( (block) != NULL ) \
  49. { \
  50. delete (block); \
  51. (block) = NULL; \
  52. } \
  53. 1
  54. #define RELEASE_MEMORY_EX( block ) \
  55. if ( (block) != NULL ) \
  56. { \
  57. delete [] (block); \
  58. (block) = NULL; \
  59. } \
  60. 1
  61. #define DESTROY_ARRAY( array ) \
  62. if ( (array) != NULL ) \
  63. { \
  64. DestroyDynamicArray( &(array) ); \
  65. (array) = NULL; \
  66. } \
  67. 1
  68. //
  69. // winstation related structures ( extract from winsta.h - internal file )
  70. //
  71. //
  72. // structures
  73. typedef struct _CITRIX_PROCESS_INFORMATION {
  74. ULONG MagicNumber;
  75. ULONG LogonId;
  76. PVOID ProcessSid;
  77. ULONG Pad;
  78. } CITRIX_PROCESS_INFORMATION, * PCITRIX_PROCESS_INFORMATION;
  79. // ...
  80. typedef struct _TS_UNICODE_STRING {
  81. USHORT Length;
  82. USHORT MaximumLength;
  83. PWSTR Buffer;
  84. } TS_UNICODE_STRING;
  85. // CAUTION:
  86. // TS_SYSTEM_PROCESS_INFORMATION is duplicated from ntexapi.h, and slightly modified.
  87. // (not nice, but necessary because the Midl compiler doesn't like PVOID !)
  88. typedef struct _TS_SYSTEM_PROCESS_INFORMATION {
  89. ULONG NextEntryOffset;
  90. ULONG NumberOfThreads;
  91. LARGE_INTEGER SpareLi1;
  92. LARGE_INTEGER SpareLi2;
  93. LARGE_INTEGER SpareLi3;
  94. LARGE_INTEGER CreateTime;
  95. LARGE_INTEGER UserTime;
  96. LARGE_INTEGER KernelTime;
  97. TS_UNICODE_STRING ImageName;
  98. LONG BasePriority; // KPRIORITY in ntexapi.h
  99. DWORD UniqueProcessId; // HANDLE in ntexapi.h
  100. DWORD InheritedFromUniqueProcessId; // HANDLE in ntexapi.h
  101. ULONG HandleCount;
  102. ULONG SessionId;
  103. ULONG SpareUl3;
  104. SIZE_T PeakVirtualSize;
  105. SIZE_T VirtualSize;
  106. ULONG PageFaultCount;
  107. ULONG PeakWorkingSetSize;
  108. ULONG WorkingSetSize;
  109. SIZE_T QuotaPeakPagedPoolUsage;
  110. SIZE_T QuotaPagedPoolUsage;
  111. SIZE_T QuotaPeakNonPagedPoolUsage;
  112. SIZE_T QuotaNonPagedPoolUsage;
  113. SIZE_T PagefileUsage;
  114. SIZE_T PeakPagefileUsage;
  115. SIZE_T PrivatePageCount;
  116. } TS_SYSTEM_PROCESS_INFORMATION, *PTS_SYSTEM_PROCESS_INFORMATION;
  117. // ...
  118. typedef struct _TS_ALL_PROCESSES_INFO {
  119. PTS_SYSTEM_PROCESS_INFORMATION pspiProcessInfo;
  120. DWORD SizeOfSid;
  121. PBYTE pSid;
  122. } TS_ALL_PROCESSES_INFO, *PTS_ALL_PROCESSES_INFO;
  123. // defines
  124. #define SERVERNAME_CURRENT ((HANDLE)NULL)
  125. #define GAP_LEVEL_BASIC 0
  126. #define CITRIX_PROCESS_INFO_MAGIC 0x23495452
  127. #define WINSTA_DLLNAME L"Winsta.dll"
  128. #define FUNCNAME_WinStationFreeMemory "WinStationFreeMemory"
  129. #define FUNCNAME_WinStationCloseServer "WinStationCloseServer"
  130. #define FUNCNAME_WinStationOpenServerW "WinStationOpenServerW"
  131. #define FUNCNAME_WinStationEnumerateProcesses "WinStationEnumerateProcesses"
  132. #define FUNCNAME_WinStationFreeGAPMemory "WinStationFreeGAPMemory"
  133. #define FUNCNAME_WinStationGetAllProcesses "WinStationGetAllProcesses"
  134. #define SIZEOF_SYSTEM_THREAD_INFORMATION sizeof( struct SYSTEM_THREAD_INFORMATION )
  135. #define SIZEOF_SYSTEM_PROCESS_INFORMATION sizeof( struct SYSTEM_PROCESS_INFORMATION )
  136. //
  137. // function prototypes
  138. typedef BOOLEAN (WINAPI * FUNC_WinStationFreeMemory)( PVOID pBuffer );
  139. typedef BOOLEAN (WINAPI * FUNC_WinStationCloseServer)( HANDLE hServer );
  140. typedef HANDLE (WINAPI * FUNC_WinStationOpenServerW)( LPWSTR pwszServerName );
  141. typedef BOOLEAN (WINAPI * FUNC_WinStationEnumerateProcesses)( HANDLE hServer, PVOID *ppProcessBuffer );
  142. typedef BOOLEAN (WINAPI * FUNC_WinStationFreeGAPMemory)( ULONG Level,
  143. PVOID ProcessArray, ULONG ulCount );
  144. typedef BOOLEAN (WINAPI * FUNC_WinStationGetAllProcesses)( HANDLE hServer,
  145. ULONG Level, ULONG *pNumberOfProcesses,
  146. PVOID *ppProcessArray );
  147. //
  148. // constants / defines / enumerations
  149. //
  150. //
  151. // WMI related stuff
  152. // class name
  153. #define CLASS_PROCESS L"Win32_Process"
  154. // wmi query
  155. #define WMI_QUERY_TYPE L"WQL"
  156. #define WMI_SERVICE_QUERY L"SELECT Name FROM Win32_Service WHERE ProcessId = %d and State=\"Running\""
  157. #define WMI_MODULES_QUERY L"ASSOCIATORS OF {%s} WHERE ResultClass = CIM_DataFile"
  158. #define WMI_PROCESS_QUERY \
  159. L"SELECT " \
  160. L"__PATH, ProcessId, CSName, Caption, SessionId, ThreadCount, " \
  161. L"WorkingSetSize, KernelModeTime, UserModeTime, ParentProcessId " \
  162. L"FROM Win32_Process"
  163. // wmi query operators etc
  164. #define WMI_QUERY_FIRST_CLAUSE L"WHERE ("
  165. #define WMI_QUERY_SECOND_CLAUSE L"AND"
  166. // Win32_Process class properties
  167. #define WIN32_PROCESS_SYSPROPERTY_PATH L"__PATH"
  168. #define WIN32_PROCESS_PROPERTY_HANDLE L"Handle"
  169. #define WIN32_PROCESS_PROPERTY_COMPUTER L"CSName"
  170. #define WIN32_PROCESS_PROPERTY_IMAGENAME L"Caption"
  171. #define WIN32_PROCESS_PROPERTY_PROCESSID L"ProcessId"
  172. #define WIN32_PROCESS_PROPERTY_SESSION L"SessionId"
  173. #define WIN32_PROCESS_PROPERTY_THREADS L"ThreadCount"
  174. #define WIN32_PROCESS_PROPERTY_USERMODETIME L"UserModeTime"
  175. #define WIN32_PROCESS_PROPERTY_MEMUSAGE L"WorkingSetSize"
  176. #define WIN32_PROCESS_PROPERTY_KERNELMODETIME L"KernelModeTime"
  177. #define WIN32_PROCESS_PROPERTY_PARENTPROCESSID L"ParentProcessId"
  178. // Win32_Process class method(s)
  179. #define WIN32_PROCESS_METHOD_GETOWNER L"GetOwner"
  180. #define WIN32_PROCESS_METHOD_TERMINATE L"Terminate"
  181. // GetOwner method's return values
  182. #define GETOWNER_RETURNVALUE_USER L"User"
  183. #define GETOWNER_RETURNVALUE_DOMAIN L"Domain"
  184. // Terminate input values
  185. #define TERMINATE_INPARAM_REASON L"Reason"
  186. // function default return value
  187. #define WMI_RETURNVALUE L"ReturnValue"
  188. // Win32_Service related stuff
  189. #define WIN32_SERVICE_PROPERTY_NAME L"Name"
  190. // CIM_DataFile related stuff
  191. #define CIM_DATAFILE_PROPERTY_FILENAME L"FileName"
  192. #define CIM_DATAFILE_PROPERTY_EXTENSION L"Extension"
  193. //
  194. // other stuff
  195. #define VALUE_RUNNING GetResString( IDS_VALUE_RUNNING )
  196. #define VALUE_NOTRESPONDING GetResString( IDS_VALUE_NOTRESPONDING )
  197. #define PID_0_DOMAIN GetResString( IDS_PID_0_DOMAIN )
  198. #define PID_0_USERNAME GetResString( IDS_PID_0_USERNAME )
  199. // messages
  200. #define MSG_KILL_SUCCESS GetResString( IDS_MSG_KILL_SUCCESS )
  201. #define MSG_KILL_SUCCESS_QUEUED GetResString( IDS_MSG_KILL_SUCCESS_QUEUED )
  202. #define MSG_KILL_SUCCESS_EX GetResString( IDS_MSG_KILL_SUCCESS_EX )
  203. #define MSG_KILL_SUCCESS_QUEUED_EX GetResString( IDS_MSG_KILL_SUCCESS_QUEUED_EX )
  204. #define MSG_TREE_KILL_SUCCESS GetResString( IDS_MSG_TREE_KILL_SUCCESS )
  205. #define ERROR_TREE_KILL_FAILED GetResString( IDS_ERROR_TREE_KILL_FAILED )
  206. #define ERROR_TASK_HAS_CHILDS GetResString( IDS_ERROR_TASK_HAS_CHILDS )
  207. #define ERROR_KILL_FAILED GetResString( IDS_ERROR_KILL_FAILED )
  208. #define ERROR_KILL_FAILED_EX GetResString( IDS_ERROR_KILL_FAILED_EX )
  209. #define ERROR_PROCESS_NOTFOUND GetResString( IDS_ERROR_PROCESS_NOTFOUND )
  210. #define ERROR_NO_PROCESSES GetResString( IDS_ERROR_NO_PROCESSES )
  211. #define ERROR_UNABLE_TO_TERMINATE GetResString( IDS_ERROR_UNABLE_TO_TERMINATE )
  212. #define ERROR_CRITICAL_SYSTEM_PROCESS GetResString( IDS_ERROR_CRITICAL_SYSTEM_PROCESS )
  213. #define ERROR_CANNOT_KILL_SILENTLY GetResString( IDS_ERROR_CANNOT_KILL_SILENTLY )
  214. #define ERROR_CANNOT_KILL_ITSELF GetResString( IDS_ERROR_CANNOT_KILL_ITSELF )
  215. #define ERROR_COM_ERROR GetResString( IDS_ERROR_COM_ERROR )
  216. #define ERROR_USERNAME_BUT_NOMACHINE GetResString( IDS_ERROR_USERNAME_BUT_NOMACHINE )
  217. #define ERROR_PASSWORD_BUT_NOUSERNAME GetResString( IDS_ERROR_PASSWORD_BUT_NOUSERNAME )
  218. #define ERROR_USERNAME_EMPTY GetResString( IDS_ERROR_USERNAME_EMPTY )
  219. #define ERROR_SERVER_EMPTY GetResString( IDS_ERROR_SERVER_EMPTY )
  220. #define ERROR_WILDCARD_WITHOUT_FILTERS GetResString( IDS_ERROR_WILDCARD_WITHOUT_FILTERS )
  221. #define ERROR_PID_OR_IM_ONLY GetResString( IDS_ERROR_PID_OR_IM_ONLY )
  222. #define ERROR_NO_PID_AND_IM GetResString( IDS_ERROR_NO_PID_AND_IM )
  223. #define ERROR_STRING_FOR_PID GetResString( IDS_ERROR_STRING_FOR_PID )
  224. #define ERROR_INVALID_USAGE_REQUEST GetResString( IDS_ERROR_INVALID_USAGE_REQUEST )
  225. #define ERROR_PLATFORM_SHOULD_BE_X86 GetResString( IDS_ERROR_PLATFORM_SHOULD_BE_X86 )
  226. // progress messages
  227. #define MSG_MODULESINFO GetResString( IDS_MSG_MODULESINFO )
  228. #define MSG_SERVICESINFO GetResString( IDS_MSG_SERVICESINFO )
  229. #define MSG_TASKSINFO GetResString( IDS_MSG_TASKSINFO )
  230. #define MSG_MODULESINFO_EX GetResString( IDS_MSG_MODULESINFO_EX )
  231. #define MSG_FORMINGTREE GetResString( IDS_MSG_FORMINGTREE )
  232. //
  233. // command line options and their indexes in the array
  234. #define MAX_OPTIONS 9
  235. // supported options ( do not localize )
  236. #define OPTION_USAGE L"?"
  237. #define OPTION_SERVER L"s"
  238. #define OPTION_USERNAME L"u"
  239. #define OPTION_PASSWORD L"p"
  240. #define OPTION_FORCE L"f"
  241. #define OPTION_FILTER L"fi"
  242. #define OPTION_PID L"pid"
  243. #define OPTION_IMAGENAME L"im"
  244. #define OPTION_TREE L"t"
  245. // indexes
  246. #define OI_USAGE 0
  247. #define OI_SERVER 1
  248. #define OI_USERNAME 2
  249. #define OI_PASSWORD 3
  250. #define OI_FORCE 4
  251. #define OI_FILTER 5
  252. #define OI_PID 6
  253. #define OI_IMAGENAME 7
  254. #define OI_TREE 8
  255. //
  256. // filter details
  257. #define MAX_FILTERS 10
  258. // supported filters
  259. #define FILTER_STATUS GetResString( IDS_FILTER_STATUS )
  260. #define FILTER_IMAGENAME GetResString( IDS_FILTER_IMAGENAME )
  261. #define FILTER_PID GetResString( IDS_FILTER_PID )
  262. #define FILTER_SESSION GetResString( IDS_FILTER_SESSION )
  263. #define FILTER_CPUTIME GetResString( IDS_FILTER_CPUTIME )
  264. #define FILTER_MEMUSAGE GetResString( IDS_FILTER_MEMUSAGE )
  265. #define FILTER_USERNAME GetResString( IDS_FILTER_USERNAME )
  266. #define FILTER_SERVICES GetResString( IDS_FILTER_SERVICES )
  267. #define FILTER_WINDOWTITLE GetResString( IDS_FILTER_WINDOWNAME )
  268. #define FILTER_MODULES GetResString( IDS_FILTER_MODULES )
  269. #define FI_STATUS 0
  270. #define FI_IMAGENAME 1
  271. #define FI_PID 2
  272. #define FI_SESSION 3
  273. #define FI_CPUTIME 4
  274. #define FI_MEMUSAGE 5
  275. #define FI_USERNAME 6
  276. #define FI_SERVICES 7
  277. #define FI_WINDOWTITLE 8
  278. #define FI_MODULES 9
  279. // values supported by 'status' filter
  280. #define FVALUES_STATUS GetResString( IDS_FVALUES_STATUS )
  281. // operators supported
  282. #define OPERATORS_STRING GetResString( IDS_OPERATORS_STRING )
  283. #define OPERATORS_NUMERIC GetResString( IDS_OPERATORS_NUMERIC )
  284. // max. columns ( information ) to be stored for one task
  285. #define MAX_TASKSINFO 18
  286. // task info indexes
  287. #define TASK_HOSTNAME 0
  288. #define TASK_IMAGENAME 1
  289. #define TASK_PID 2
  290. #define TASK_SESSIONNAME 3
  291. #define TASK_SESSION 4
  292. #define TASK_MEMUSAGE 5
  293. #define TASK_STATUS 6
  294. #define TASK_USERNAME 7
  295. #define TASK_CPUTIME 8
  296. #define TASK_WINDOWTITLE 9
  297. #define TASK_SERVICES 10
  298. #define TASK_MODULES 11
  299. // always hidden
  300. #define TASK_HWND 12
  301. #define TASK_WINSTA 13
  302. #define TASK_DESK 14
  303. #define TASK_CREATINGPROCESSID 15
  304. #define TASK_OBJPATH 16
  305. #define TASK_RANK 17
  306. //
  307. // CTaskKill
  308. //
  309. class CTaskKill
  310. {
  311. public:
  312. // enumerators
  313. enum
  314. {
  315. twiProcessId = 0,
  316. twiWinSta = 1,
  317. twiDesktop = 2,
  318. twiHandle = 3,
  319. twiTitle = 4,
  320. twiCOUNT,
  321. };
  322. // constructor / destructor
  323. public:
  324. CTaskKill();
  325. ~CTaskKill();
  326. // data memebers
  327. private:
  328. // input arguments
  329. BOOL m_bTree; // -tr
  330. BOOL m_bForce; // -fo
  331. CHString m_strServer; // -s
  332. CHString m_strUserName; // -u
  333. CHString m_strPassword; // -p
  334. TARRAY m_arrFilters; // -fi
  335. TARRAY m_arrTasksToKill; // ( defaults = -im and -pid )
  336. // WMI Query
  337. CHString m_strQuery;
  338. // other(s)
  339. DWORD m_dwCurrentPid;
  340. BOOL m_bNeedPassword;
  341. BOOL m_bNeedModulesInfo;
  342. TARRAY m_arrFiltersEx; // parsed filters info
  343. TARRAY m_arrWindowTitles; // window titles
  344. BOOL m_bNeedServicesInfo; // determines whether services info has to gathered or not
  345. BOOL m_bNeedUserContextInfo; // determines whether userinfo has to gathered or not
  346. PTFILTERCONFIG m_pfilterConfigs; // filters config information
  347. // WMI / COM interfaces
  348. IWbemLocator* m_pWbemLocator;
  349. IWbemServices* m_pWbemServices;
  350. IEnumWbemClassObject* m_pWbemEnumObjects;
  351. IWbemClassObject* m_pWbemTerminateInParams;
  352. // WMI connectivity
  353. COAUTHIDENTITY* m_pAuthIdentity;
  354. // output data
  355. TARRAY m_arrRecord;
  356. DWORD m_dwProcessId;
  357. CHString m_strImageName;
  358. BOOL m_bTasksOptimized;
  359. BOOL m_bFiltersOptimized;
  360. // winstation related stuff
  361. CHString m_strUNCServer; // server name
  362. BOOL m_bIsHydra;
  363. HMODULE m_hWinstaLib;
  364. PBYTE m_pProcessInfo;
  365. ULONG m_ulNumberOfProcesses;
  366. BOOL m_bCloseConnection;
  367. // services related stuff
  368. DWORD m_dwServicesCount;
  369. LPENUM_SERVICE_STATUS_PROCESS m_pServicesInfo;
  370. // modules related stuff ( remote only )
  371. BOOL m_bUseRemote;
  372. PPERF_DATA_BLOCK m_pdb;
  373. // progress message related
  374. HANDLE m_hOutput;
  375. CONSOLE_SCREEN_BUFFER_INFO m_csbi;
  376. //
  377. // functions
  378. FUNC_WinStationFreeMemory m_pfnWinStationFreeMemory;
  379. FUNC_WinStationOpenServerW m_pfnWinStationOpenServerW;
  380. FUNC_WinStationCloseServer m_pfnWinStationCloseServer;
  381. FUNC_WinStationFreeGAPMemory m_pfnWinStationFreeGAPMemory;
  382. FUNC_WinStationGetAllProcesses m_pfnWinStationGetAllProcesses;
  383. FUNC_WinStationEnumerateProcesses m_pfnWinStationEnumerateProcesses;
  384. public:
  385. BOOL m_bUsage; // -?
  386. BOOL m_bLocalSystem;
  387. // functions
  388. private:
  389. BOOL CanTerminate();
  390. BOOL Kill( BOOL& bQueued );
  391. BOOL KillProcessOnLocalSystem( BOOL& bQueued );
  392. BOOL ForciblyKillProcessOnLocalSystem();
  393. BOOL ForciblyKillProcessOnRemoteSystem();
  394. LONG MatchTaskToKill( DWORD& dwMatchedIndex );
  395. // helpers
  396. VOID DoOptimization();
  397. VOID SaveData( IWbemClassObject* pWmiObject );
  398. VOID SetStatus( IWbemClassObject* pWmiObject );
  399. VOID SetMemUsage( IWbemClassObject* pWmiObject );
  400. VOID SetCPUTime( IWbemClassObject* pWmiObject );
  401. VOID SetUserContext( IWbemClassObject* pWmiObject );
  402. VOID SetWindowTitle( IWbemClassObject* pWmiObject );
  403. VOID SetServicesInfo( IWbemClassObject* pWmiObject );
  404. BOOL SetModulesInfo( IWbemClassObject* pWmiObject );
  405. // ...
  406. BOOL LoadTasksEx();
  407. BOOL LoadModulesInfo();
  408. BOOL LoadServicesInfo();
  409. BOOL GetModulesOnRemote( TARRAY arrModules );
  410. BOOL GetModulesOnRemoteEx( TARRAY arrModules );
  411. BOOL LoadModulesOnLocal( TARRAY arrModules );
  412. BOOL LoadUserNameFromWinsta( CHString& strDomain, CHString& strUserName );
  413. // winsta functions
  414. BOOLEAN WinStationFreeMemory( PVOID pBuffer );
  415. BOOLEAN WinStationCloseServer( HANDLE hServer );
  416. HANDLE WinStationOpenServerW( LPWSTR pwszServerName );
  417. BOOLEAN WinStationEnumerateProcesses( HANDLE hServer, PVOID *ppProcessBuffer );
  418. BOOLEAN WinStationFreeGAPMemory( ULONG Level, PVOID ProcessArray, ULONG ulCount );
  419. BOOLEAN WinStationGetAllProcesses( HANDLE hServer, ULONG Level,
  420. ULONG *pNumberOfProcesses, PVOID *ppProcessArray );
  421. public:
  422. VOID Usage();
  423. BOOL Initialize();
  424. VOID PrepareColumns();
  425. BOOL ValidateFilters();
  426. BOOL ProcessOptions( DWORD argc, LPCTSTR argv[] );
  427. // functionality related
  428. BOOL Connect();
  429. BOOL LoadTasks();
  430. BOOL EnableDebugPriv();
  431. BOOL DoTerminate( DWORD& dwTerminate );
  432. };
  433. //
  434. // public functions
  435. //
  436. VOID PrintProgressMsg( HANDLE hOutput, LPCWSTR pwszMsg, const CONSOLE_SCREEN_BUFFER_INFO& csbi );
  437. #endif // _TASKLIST_H