taskkill.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Task termination utility
  3. *
  4. * Copyright 2008 Andrew Riedi
  5. * Copyright 2010 Andrew Nguyen
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include <stdlib.h>
  22. #include <windows.h>
  23. #include <psapi.h>
  24. #include <wine/debug.h>
  25. #include "taskkill.h"
  26. WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
  27. static BOOL force_termination = FALSE;
  28. static WCHAR **task_list;
  29. static unsigned int task_count;
  30. struct pid_close_info
  31. {
  32. DWORD pid;
  33. BOOL found;
  34. };
  35. static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
  36. {
  37. int wlen;
  38. DWORD count, ret;
  39. WCHAR msg_buffer[8192];
  40. wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
  41. ARRAY_SIZE(msg_buffer), &va_args);
  42. ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
  43. if (!ret)
  44. {
  45. DWORD len;
  46. char *msgA;
  47. /* On Windows WriteConsoleW() fails if the output is redirected. So fall
  48. * back to WriteFile(), assuming the console encoding is still the right
  49. * one in that case.
  50. */
  51. len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
  52. NULL, 0, NULL, NULL);
  53. msgA = HeapAlloc(GetProcessHeap(), 0, len);
  54. if (!msgA)
  55. return 0;
  56. WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
  57. NULL, NULL);
  58. WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
  59. HeapFree(GetProcessHeap(), 0, msgA);
  60. }
  61. return count;
  62. }
  63. static int WINAPIV taskkill_printfW(const WCHAR *msg, ...)
  64. {
  65. va_list va_args;
  66. int len;
  67. va_start(va_args, msg);
  68. len = taskkill_vprintfW(msg, va_args);
  69. va_end(va_args);
  70. return len;
  71. }
  72. static int WINAPIV taskkill_message_printfW(int msg, ...)
  73. {
  74. va_list va_args;
  75. WCHAR msg_buffer[8192];
  76. int len;
  77. LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
  78. va_start(va_args, msg);
  79. len = taskkill_vprintfW(msg_buffer, va_args);
  80. va_end(va_args);
  81. return len;
  82. }
  83. static int taskkill_message(int msg)
  84. {
  85. WCHAR msg_buffer[8192];
  86. LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
  87. return taskkill_printfW(L"%1", msg_buffer);
  88. }
  89. /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
  90. static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
  91. {
  92. struct pid_close_info *info = (struct pid_close_info *)lParam;
  93. DWORD hwnd_pid;
  94. GetWindowThreadProcessId(hwnd, &hwnd_pid);
  95. if (hwnd_pid == info->pid)
  96. {
  97. PostMessageW(hwnd, WM_CLOSE, 0, 0);
  98. info->found = TRUE;
  99. }
  100. return TRUE;
  101. }
  102. static DWORD *enumerate_processes(DWORD *list_count)
  103. {
  104. DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
  105. pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
  106. if (!pid_list)
  107. return NULL;
  108. for (;;)
  109. {
  110. DWORD *realloc_list;
  111. if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
  112. {
  113. HeapFree(GetProcessHeap(), 0, pid_list);
  114. return NULL;
  115. }
  116. /* EnumProcesses can't signal an insufficient buffer condition, so the
  117. * only way to possibly determine whether a larger buffer is required
  118. * is to see whether the written number of bytes is the same as the
  119. * buffer size. If so, the buffer will be reallocated to twice the
  120. * size. */
  121. if (alloc_bytes != needed_bytes)
  122. break;
  123. alloc_bytes *= 2;
  124. realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
  125. if (!realloc_list)
  126. {
  127. HeapFree(GetProcessHeap(), 0, pid_list);
  128. return NULL;
  129. }
  130. pid_list = realloc_list;
  131. }
  132. *list_count = needed_bytes / sizeof(*pid_list);
  133. return pid_list;
  134. }
  135. static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
  136. {
  137. HANDLE process;
  138. HMODULE module;
  139. DWORD required_size;
  140. process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  141. if (!process)
  142. return FALSE;
  143. if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
  144. {
  145. CloseHandle(process);
  146. return FALSE;
  147. }
  148. if (!GetModuleBaseNameW(process, module, buf, chars))
  149. {
  150. CloseHandle(process);
  151. return FALSE;
  152. }
  153. CloseHandle(process);
  154. return TRUE;
  155. }
  156. /* The implemented task enumeration and termination behavior does not
  157. * exactly match native behavior. On Windows:
  158. *
  159. * In the case of terminating by process name, specifying a particular
  160. * process name more times than the number of running instances causes
  161. * all instances to be terminated, but termination failure messages to
  162. * be printed as many times as the difference between the specification
  163. * quantity and the number of running instances.
  164. *
  165. * Successful terminations are all listed first in order, with failing
  166. * terminations being listed at the end.
  167. *
  168. * A PID of zero causes taskkill to warn about the inability to terminate
  169. * system processes. */
  170. static int send_close_messages(void)
  171. {
  172. DWORD *pid_list, pid_list_size;
  173. DWORD self_pid = GetCurrentProcessId();
  174. unsigned int i;
  175. int status_code = 0;
  176. pid_list = enumerate_processes(&pid_list_size);
  177. if (!pid_list)
  178. {
  179. taskkill_message(STRING_ENUM_FAILED);
  180. return 1;
  181. }
  182. for (i = 0; i < task_count; i++)
  183. {
  184. WCHAR *p = task_list[i];
  185. BOOL is_numeric = TRUE;
  186. /* Determine whether the string is not numeric. */
  187. while (*p)
  188. {
  189. if (!iswdigit(*p++))
  190. {
  191. is_numeric = FALSE;
  192. break;
  193. }
  194. }
  195. if (is_numeric)
  196. {
  197. DWORD pid = wcstol(task_list[i], NULL, 10);
  198. struct pid_close_info info = { pid };
  199. if (pid == self_pid)
  200. {
  201. taskkill_message(STRING_SELF_TERMINATION);
  202. status_code = 1;
  203. continue;
  204. }
  205. EnumWindows(pid_enum_proc, (LPARAM)&info);
  206. if (info.found)
  207. taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
  208. else
  209. {
  210. taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
  211. status_code = 128;
  212. }
  213. }
  214. else
  215. {
  216. DWORD index;
  217. BOOL found_process = FALSE;
  218. for (index = 0; index < pid_list_size; index++)
  219. {
  220. WCHAR process_name[MAX_PATH];
  221. if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
  222. !wcsicmp(process_name, task_list[i]))
  223. {
  224. struct pid_close_info info = { pid_list[index] };
  225. found_process = TRUE;
  226. if (pid_list[index] == self_pid)
  227. {
  228. taskkill_message(STRING_SELF_TERMINATION);
  229. status_code = 1;
  230. continue;
  231. }
  232. EnumWindows(pid_enum_proc, (LPARAM)&info);
  233. taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
  234. }
  235. }
  236. if (!found_process)
  237. {
  238. taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
  239. status_code = 128;
  240. }
  241. }
  242. }
  243. HeapFree(GetProcessHeap(), 0, pid_list);
  244. return status_code;
  245. }
  246. static int terminate_processes(void)
  247. {
  248. DWORD *pid_list, pid_list_size;
  249. DWORD self_pid = GetCurrentProcessId();
  250. unsigned int i;
  251. int status_code = 0;
  252. pid_list = enumerate_processes(&pid_list_size);
  253. if (!pid_list)
  254. {
  255. taskkill_message(STRING_ENUM_FAILED);
  256. return 1;
  257. }
  258. for (i = 0; i < task_count; i++)
  259. {
  260. WCHAR *p = task_list[i];
  261. BOOL is_numeric = TRUE;
  262. /* Determine whether the string is not numeric. */
  263. while (*p)
  264. {
  265. if (!iswdigit(*p++))
  266. {
  267. is_numeric = FALSE;
  268. break;
  269. }
  270. }
  271. if (is_numeric)
  272. {
  273. DWORD pid = wcstol(task_list[i], NULL, 10);
  274. HANDLE process;
  275. if (pid == self_pid)
  276. {
  277. taskkill_message(STRING_SELF_TERMINATION);
  278. status_code = 1;
  279. continue;
  280. }
  281. process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  282. if (!process)
  283. {
  284. taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
  285. status_code = 128;
  286. continue;
  287. }
  288. if (!TerminateProcess(process, 0))
  289. {
  290. taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
  291. status_code = 1;
  292. CloseHandle(process);
  293. continue;
  294. }
  295. taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
  296. CloseHandle(process);
  297. }
  298. else
  299. {
  300. DWORD index;
  301. BOOL found_process = FALSE;
  302. for (index = 0; index < pid_list_size; index++)
  303. {
  304. WCHAR process_name[MAX_PATH];
  305. if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
  306. !wcsicmp(process_name, task_list[i]))
  307. {
  308. HANDLE process;
  309. if (pid_list[index] == self_pid)
  310. {
  311. taskkill_message(STRING_SELF_TERMINATION);
  312. status_code = 1;
  313. continue;
  314. }
  315. process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
  316. if (!process)
  317. {
  318. taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
  319. status_code = 128;
  320. continue;
  321. }
  322. if (!TerminateProcess(process, 0))
  323. {
  324. taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
  325. status_code = 1;
  326. CloseHandle(process);
  327. continue;
  328. }
  329. found_process = TRUE;
  330. taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
  331. CloseHandle(process);
  332. }
  333. }
  334. if (!found_process)
  335. {
  336. taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
  337. status_code = 128;
  338. }
  339. }
  340. }
  341. HeapFree(GetProcessHeap(), 0, pid_list);
  342. return status_code;
  343. }
  344. static BOOL add_to_task_list(WCHAR *name)
  345. {
  346. static unsigned int list_size = 16;
  347. if (!task_list)
  348. {
  349. task_list = HeapAlloc(GetProcessHeap(), 0,
  350. list_size * sizeof(*task_list));
  351. if (!task_list)
  352. return FALSE;
  353. }
  354. else if (task_count == list_size)
  355. {
  356. void *realloc_list;
  357. list_size *= 2;
  358. realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
  359. list_size * sizeof(*task_list));
  360. if (!realloc_list)
  361. return FALSE;
  362. task_list = realloc_list;
  363. }
  364. task_list[task_count++] = name;
  365. return TRUE;
  366. }
  367. /* FIXME Argument processing does not match behavior observed on Windows.
  368. * Stringent argument counting and processing is performed, and unrecognized
  369. * options are detected as parameters when placed after options that accept one. */
  370. static BOOL process_arguments(int argc, WCHAR *argv[])
  371. {
  372. if (argc > 1)
  373. {
  374. int i;
  375. WCHAR *argdata;
  376. BOOL has_im = FALSE, has_pid = FALSE;
  377. /* Only the lone help option is recognized. */
  378. if (argc == 2)
  379. {
  380. argdata = argv[1];
  381. if ((*argdata == '/' || *argdata == '-') && !lstrcmpW(L"?", argdata + 1))
  382. {
  383. taskkill_message(STRING_USAGE);
  384. exit(0);
  385. }
  386. }
  387. for (i = 1; i < argc; i++)
  388. {
  389. BOOL got_im = FALSE, got_pid = FALSE;
  390. argdata = argv[i];
  391. if (*argdata != '/' && *argdata != '-')
  392. goto invalid;
  393. argdata++;
  394. if (!wcsicmp(L"t", argdata))
  395. WINE_FIXME("argument T not supported\n");
  396. if (!wcsicmp(L"f", argdata))
  397. force_termination = TRUE;
  398. /* Options /IM and /PID appear to behave identically, except for
  399. * the fact that they cannot be specified at the same time. */
  400. else if ((got_im = !wcsicmp(L"im", argdata)) ||
  401. (got_pid = !wcsicmp(L"pid", argdata)))
  402. {
  403. if (!argv[i + 1])
  404. {
  405. taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
  406. taskkill_message(STRING_USAGE);
  407. return FALSE;
  408. }
  409. if (got_im) has_im = TRUE;
  410. if (got_pid) has_pid = TRUE;
  411. if (has_im && has_pid)
  412. {
  413. taskkill_message(STRING_MUTUAL_EXCLUSIVE);
  414. taskkill_message(STRING_USAGE);
  415. return FALSE;
  416. }
  417. if (!add_to_task_list(argv[i + 1]))
  418. return FALSE;
  419. i++;
  420. }
  421. else
  422. {
  423. invalid:
  424. taskkill_message(STRING_INVALID_OPTION);
  425. taskkill_message(STRING_USAGE);
  426. return FALSE;
  427. }
  428. }
  429. }
  430. else
  431. {
  432. taskkill_message(STRING_MISSING_OPTION);
  433. taskkill_message(STRING_USAGE);
  434. return FALSE;
  435. }
  436. return TRUE;
  437. }
  438. int __cdecl wmain(int argc, WCHAR *argv[])
  439. {
  440. int status_code = 0;
  441. if (!process_arguments(argc, argv))
  442. {
  443. HeapFree(GetProcessHeap(), 0, task_list);
  444. return 1;
  445. }
  446. if (force_termination)
  447. status_code = terminate_processes();
  448. else
  449. status_code = send_close_messages();
  450. HeapFree(GetProcessHeap(), 0, task_list);
  451. return status_code;
  452. }