2 * Task termination utility
4 * Copyright 2008 Andrew Riedi
5 * Copyright 2010 Andrew Nguyen
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.
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.
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
24 #include <wine/unicode.h>
29 int force_termination;
32 unsigned int task_count;
40 static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
44 WCHAR msg_buffer[8192];
46 wlen = vsprintfW(msg_buffer, msg, va_args);
48 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
54 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
56 msgA = HeapAlloc(GetProcessHeap(), 0, len);
60 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
62 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
63 HeapFree(GetProcessHeap(), 0, msgA);
69 static int taskkill_printfW(const WCHAR *msg, ...)
74 va_start(va_args, msg);
75 len = taskkill_vprintfW(msg, va_args);
81 static int taskkill_message_printfW(int msg, ...)
84 WCHAR msg_buffer[8192];
87 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
88 sizeof(msg_buffer)/sizeof(WCHAR));
90 va_start(va_args, msg);
91 len = taskkill_vprintfW(msg_buffer, va_args);
97 static int taskkill_message(int msg)
99 static const WCHAR formatW[] = {'%','s',0};
100 WCHAR msg_buffer[8192];
102 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
103 sizeof(msg_buffer)/sizeof(WCHAR));
105 return taskkill_printfW(formatW, msg_buffer);
108 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
109 static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
111 struct pid_close_info *info = (struct pid_close_info *)lParam;
114 GetWindowThreadProcessId(hwnd, &hwnd_pid);
116 if (hwnd_pid == info->pid)
118 PostMessageW(hwnd, WM_CLOSE, 0, 0);
125 static DWORD *enumerate_processes(DWORD *list_count)
127 DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
129 pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
137 if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
139 HeapFree(GetProcessHeap(), 0, pid_list);
143 /* EnumProcesses can't signal an insufficient buffer condition, so the
144 * only way to possibly determine whether a larger buffer is required
145 * is to see whether the written number of bytes is the same as the
146 * buffer size. If so, the buffer will be reallocated to twice the
148 if (alloc_bytes != needed_bytes)
152 realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
155 HeapFree(GetProcessHeap(), 0, pid_list);
158 pid_list = realloc_list;
161 *list_count = needed_bytes / sizeof(*pid_list);
165 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
171 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
175 if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
177 CloseHandle(process);
181 if (!GetModuleBaseNameW(process, module, buf, chars))
183 CloseHandle(process);
187 CloseHandle(process);
191 /* The implemented task enumeration and termination behavior does not
192 * exactly match native behavior. On Windows:
194 * In the case of terminating by process name, specifying a particular
195 * process name more times than the number of running instances causes
196 * all instances to be terminated, but termination failure messages to
197 * be printed as many times as the difference between the specification
198 * quantity and the number of running instances.
200 * Successful terminations are all listed first in order, with failing
201 * terminations being listed at the end.
203 * A PID of zero causes taskkill to warn about the inability to terminate
204 * system processes. */
205 static int send_close_messages(void)
207 DWORD *pid_list, pid_list_size;
211 pid_list = enumerate_processes(&pid_list_size);
214 taskkill_message(STRING_ENUM_FAILED);
218 for (i = 0; i < task_count; i++)
220 WCHAR *p = task_list[i];
221 BOOL is_numeric = TRUE;
223 /* Determine whether the string is not numeric. */
235 DWORD pid = atoiW(task_list[i]);
236 struct pid_close_info info = { pid };
238 EnumWindows(pid_enum_proc, (LPARAM)&info);
240 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
243 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
250 BOOL found_process = FALSE;
252 for (index = 0; index < pid_list_size; index++)
254 WCHAR process_name[MAX_PATH];
256 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
257 !strcmpiW(process_name, task_list[i]))
259 struct pid_close_info info = { pid_list[index] };
261 found_process = TRUE;
262 EnumWindows(pid_enum_proc, (LPARAM)&info);
263 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
269 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
275 HeapFree(GetProcessHeap(), 0, pid_list);
279 static int terminate_processes(void)
281 DWORD *pid_list, pid_list_size;
285 pid_list = enumerate_processes(&pid_list_size);
288 taskkill_message(STRING_ENUM_FAILED);
292 for (i = 0; i < task_count; i++)
294 WCHAR *p = task_list[i];
295 BOOL is_numeric = TRUE;
297 /* Determine whether the string is not numeric. */
309 DWORD pid = atoiW(task_list[i]);
312 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
315 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
320 if (!TerminateProcess(process, 0))
322 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
324 CloseHandle(process);
328 taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
329 CloseHandle(process);
334 BOOL found_process = FALSE;
336 for (index = 0; index < pid_list_size; index++)
338 WCHAR process_name[MAX_PATH];
340 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
341 !strcmpiW(process_name, task_list[i]))
345 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
348 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
353 if (!TerminateProcess(process, 0))
355 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
357 CloseHandle(process);
361 found_process = TRUE;
362 taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
363 CloseHandle(process);
369 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
375 HeapFree(GetProcessHeap(), 0, pid_list);
379 static BOOL add_to_task_list(WCHAR *name)
381 static unsigned int list_size = 16;
385 task_list = HeapAlloc(GetProcessHeap(), 0,
386 list_size * sizeof(*task_list));
390 else if (task_count == list_size)
395 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
396 list_size * sizeof(*task_list));
400 task_list = realloc_list;
403 task_list[task_count++] = name;
407 /* FIXME Argument processing does not match behavior observed on Windows.
408 * Stringent argument counting and processing is performed, and unrecognized
409 * options are detected as parameters when placed after options that accept one. */
410 static BOOL process_arguments(int argc, WCHAR *argv[])
412 static const WCHAR slashForceTerminate[] = {'/','f',0};
413 static const WCHAR slashImage[] = {'/','i','m',0};
414 static const WCHAR slashPID[] = {'/','p','i','d',0};
415 static const WCHAR slashHelp[] = {'/','?',0};
420 BOOL has_im = 0, has_pid = 0;
422 /* Only the lone help option is recognized. */
423 if (argc == 2 && !strcmpW(slashHelp, argv[1]))
425 taskkill_message(STRING_USAGE);
429 for (i = 1; i < argc; i++)
431 int got_im = 0, got_pid = 0;
433 if (!strcmpiW(slashForceTerminate, argv[i]))
434 force_termination = 1;
435 /* Options /IM and /PID appear to behave identically, except for
436 * the fact that they cannot be specified at the same time. */
437 else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
438 (got_pid = !strcmpiW(slashPID, argv[i])))
442 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
443 taskkill_message(STRING_USAGE);
447 if (got_im) has_im = 1;
448 if (got_pid) has_pid = 1;
450 if (has_im && has_pid)
452 taskkill_message(STRING_MUTUAL_EXCLUSIVE);
453 taskkill_message(STRING_USAGE);
457 if (!add_to_task_list(argv[i + 1]))
463 taskkill_message(STRING_INVALID_OPTION);
464 taskkill_message(STRING_USAGE);
471 taskkill_message(STRING_MISSING_OPTION);
472 taskkill_message(STRING_USAGE);
479 int wmain(int argc, WCHAR *argv[])
483 if (!process_arguments(argc, argv))
485 HeapFree(GetProcessHeap(), 0, task_list);
489 if (force_termination)
490 status_code = terminate_processes();
492 status_code = send_close_messages();
494 HeapFree(GetProcessHeap(), 0, task_list);