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>
28 static int force_termination;
30 static WCHAR **task_list;
31 static unsigned int task_count;
39 static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
43 WCHAR msg_buffer[8192];
45 wlen = vsprintfW(msg_buffer, msg, va_args);
47 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
53 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
55 msgA = HeapAlloc(GetProcessHeap(), 0, len);
59 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
61 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
62 HeapFree(GetProcessHeap(), 0, msgA);
68 static int taskkill_printfW(const WCHAR *msg, ...)
73 va_start(va_args, msg);
74 len = taskkill_vprintfW(msg, va_args);
80 static int taskkill_message_printfW(int msg, ...)
83 WCHAR msg_buffer[8192];
86 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
87 sizeof(msg_buffer)/sizeof(WCHAR));
89 va_start(va_args, msg);
90 len = taskkill_vprintfW(msg_buffer, va_args);
96 static int taskkill_message(int msg)
98 static const WCHAR formatW[] = {'%','s',0};
99 WCHAR msg_buffer[8192];
101 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
102 sizeof(msg_buffer)/sizeof(WCHAR));
104 return taskkill_printfW(formatW, msg_buffer);
107 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
108 static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
110 struct pid_close_info *info = (struct pid_close_info *)lParam;
113 GetWindowThreadProcessId(hwnd, &hwnd_pid);
115 if (hwnd_pid == info->pid)
117 PostMessageW(hwnd, WM_CLOSE, 0, 0);
124 static DWORD *enumerate_processes(DWORD *list_count)
126 DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
128 pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
136 if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
138 HeapFree(GetProcessHeap(), 0, pid_list);
142 /* EnumProcesses can't signal an insufficient buffer condition, so the
143 * only way to possibly determine whether a larger buffer is required
144 * is to see whether the written number of bytes is the same as the
145 * buffer size. If so, the buffer will be reallocated to twice the
147 if (alloc_bytes != needed_bytes)
151 realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
154 HeapFree(GetProcessHeap(), 0, pid_list);
157 pid_list = realloc_list;
160 *list_count = needed_bytes / sizeof(*pid_list);
164 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
170 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
174 if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
176 CloseHandle(process);
180 if (!GetModuleBaseNameW(process, module, buf, chars))
182 CloseHandle(process);
186 CloseHandle(process);
190 /* The implemented task enumeration and termination behavior does not
191 * exactly match native behavior. On Windows:
193 * In the case of terminating by process name, specifying a particular
194 * process name more times than the number of running instances causes
195 * all instances to be terminated, but termination failure messages to
196 * be printed as many times as the difference between the specification
197 * quantity and the number of running instances.
199 * Successful terminations are all listed first in order, with failing
200 * terminations being listed at the end.
202 * A PID of zero causes taskkill to warn about the inability to terminate
203 * system processes. */
204 static int send_close_messages(void)
206 DWORD *pid_list, pid_list_size;
210 pid_list = enumerate_processes(&pid_list_size);
213 taskkill_message(STRING_ENUM_FAILED);
217 for (i = 0; i < task_count; i++)
219 WCHAR *p = task_list[i];
220 BOOL is_numeric = TRUE;
222 /* Determine whether the string is not numeric. */
234 DWORD pid = atoiW(task_list[i]);
235 struct pid_close_info info = { pid };
237 EnumWindows(pid_enum_proc, (LPARAM)&info);
239 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
242 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
249 BOOL found_process = FALSE;
251 for (index = 0; index < pid_list_size; index++)
253 WCHAR process_name[MAX_PATH];
255 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
256 !strcmpiW(process_name, task_list[i]))
258 struct pid_close_info info = { pid_list[index] };
260 found_process = TRUE;
261 EnumWindows(pid_enum_proc, (LPARAM)&info);
262 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
268 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
274 HeapFree(GetProcessHeap(), 0, pid_list);
278 static int terminate_processes(void)
280 DWORD *pid_list, pid_list_size;
284 pid_list = enumerate_processes(&pid_list_size);
287 taskkill_message(STRING_ENUM_FAILED);
291 for (i = 0; i < task_count; i++)
293 WCHAR *p = task_list[i];
294 BOOL is_numeric = TRUE;
296 /* Determine whether the string is not numeric. */
308 DWORD pid = atoiW(task_list[i]);
311 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
314 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
319 if (!TerminateProcess(process, 0))
321 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
323 CloseHandle(process);
327 taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
328 CloseHandle(process);
333 BOOL found_process = FALSE;
335 for (index = 0; index < pid_list_size; index++)
337 WCHAR process_name[MAX_PATH];
339 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
340 !strcmpiW(process_name, task_list[i]))
344 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
347 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
352 if (!TerminateProcess(process, 0))
354 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
356 CloseHandle(process);
360 found_process = TRUE;
361 taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
362 CloseHandle(process);
368 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
374 HeapFree(GetProcessHeap(), 0, pid_list);
378 static BOOL add_to_task_list(WCHAR *name)
380 static unsigned int list_size = 16;
384 task_list = HeapAlloc(GetProcessHeap(), 0,
385 list_size * sizeof(*task_list));
389 else if (task_count == list_size)
394 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
395 list_size * sizeof(*task_list));
399 task_list = realloc_list;
402 task_list[task_count++] = name;
406 /* FIXME Argument processing does not match behavior observed on Windows.
407 * Stringent argument counting and processing is performed, and unrecognized
408 * options are detected as parameters when placed after options that accept one. */
409 static BOOL process_arguments(int argc, WCHAR *argv[])
411 static const WCHAR slashForceTerminate[] = {'/','f',0};
412 static const WCHAR slashImage[] = {'/','i','m',0};
413 static const WCHAR slashPID[] = {'/','p','i','d',0};
414 static const WCHAR slashHelp[] = {'/','?',0};
419 BOOL has_im = 0, has_pid = 0;
421 /* Only the lone help option is recognized. */
422 if (argc == 2 && !strcmpW(slashHelp, argv[1]))
424 taskkill_message(STRING_USAGE);
428 for (i = 1; i < argc; i++)
430 int got_im = 0, got_pid = 0;
432 if (!strcmpiW(slashForceTerminate, argv[i]))
433 force_termination = 1;
434 /* Options /IM and /PID appear to behave identically, except for
435 * the fact that they cannot be specified at the same time. */
436 else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
437 (got_pid = !strcmpiW(slashPID, argv[i])))
441 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
442 taskkill_message(STRING_USAGE);
446 if (got_im) has_im = 1;
447 if (got_pid) has_pid = 1;
449 if (has_im && has_pid)
451 taskkill_message(STRING_MUTUAL_EXCLUSIVE);
452 taskkill_message(STRING_USAGE);
456 if (!add_to_task_list(argv[i + 1]))
462 taskkill_message(STRING_INVALID_OPTION);
463 taskkill_message(STRING_USAGE);
470 taskkill_message(STRING_MISSING_OPTION);
471 taskkill_message(STRING_USAGE);
478 int wmain(int argc, WCHAR *argv[])
482 if (!process_arguments(argc, argv))
484 HeapFree(GetProcessHeap(), 0, task_list);
488 if (force_termination)
489 status_code = terminate_processes();
491 status_code = send_close_messages();
493 HeapFree(GetProcessHeap(), 0, task_list);