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
25 #include <wine/debug.h>
26 #include <wine/unicode.h>
30 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
32 static int force_termination;
34 static WCHAR **task_list;
35 static unsigned int task_count;
43 static int taskkill_vprintfW(const WCHAR *msg, __ms_va_list va_args)
47 WCHAR msg_buffer[8192];
49 wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
50 sizeof(msg_buffer)/sizeof(*msg_buffer), &va_args);
52 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
58 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
59 * back to WriteFile(), assuming the console encoding is still the right
62 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
64 msgA = HeapAlloc(GetProcessHeap(), 0, len);
68 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
70 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
71 HeapFree(GetProcessHeap(), 0, msgA);
77 static int CDECL taskkill_printfW(const WCHAR *msg, ...)
82 __ms_va_start(va_args, msg);
83 len = taskkill_vprintfW(msg, va_args);
89 static int CDECL taskkill_message_printfW(int msg, ...)
92 WCHAR msg_buffer[8192];
95 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
96 sizeof(msg_buffer)/sizeof(WCHAR));
98 __ms_va_start(va_args, msg);
99 len = taskkill_vprintfW(msg_buffer, va_args);
100 __ms_va_end(va_args);
105 static int taskkill_message(int msg)
107 static const WCHAR formatW[] = {'%','1',0};
108 WCHAR msg_buffer[8192];
110 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
111 sizeof(msg_buffer)/sizeof(WCHAR));
113 return taskkill_printfW(formatW, msg_buffer);
116 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
117 static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
119 struct pid_close_info *info = (struct pid_close_info *)lParam;
122 GetWindowThreadProcessId(hwnd, &hwnd_pid);
124 if (hwnd_pid == info->pid)
126 PostMessageW(hwnd, WM_CLOSE, 0, 0);
133 static DWORD *enumerate_processes(DWORD *list_count)
135 DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
137 pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
145 if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
147 HeapFree(GetProcessHeap(), 0, pid_list);
151 /* EnumProcesses can't signal an insufficient buffer condition, so the
152 * only way to possibly determine whether a larger buffer is required
153 * is to see whether the written number of bytes is the same as the
154 * buffer size. If so, the buffer will be reallocated to twice the
156 if (alloc_bytes != needed_bytes)
160 realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
163 HeapFree(GetProcessHeap(), 0, pid_list);
166 pid_list = realloc_list;
169 *list_count = needed_bytes / sizeof(*pid_list);
173 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
179 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
183 if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
185 CloseHandle(process);
189 if (!GetModuleBaseNameW(process, module, buf, chars))
191 CloseHandle(process);
195 CloseHandle(process);
199 /* The implemented task enumeration and termination behavior does not
200 * exactly match native behavior. On Windows:
202 * In the case of terminating by process name, specifying a particular
203 * process name more times than the number of running instances causes
204 * all instances to be terminated, but termination failure messages to
205 * be printed as many times as the difference between the specification
206 * quantity and the number of running instances.
208 * Successful terminations are all listed first in order, with failing
209 * terminations being listed at the end.
211 * A PID of zero causes taskkill to warn about the inability to terminate
212 * system processes. */
213 static int send_close_messages(void)
215 DWORD *pid_list, pid_list_size;
216 DWORD self_pid = GetCurrentProcessId();
220 pid_list = enumerate_processes(&pid_list_size);
223 taskkill_message(STRING_ENUM_FAILED);
227 for (i = 0; i < task_count; i++)
229 WCHAR *p = task_list[i];
230 BOOL is_numeric = TRUE;
232 /* Determine whether the string is not numeric. */
244 DWORD pid = atoiW(task_list[i]);
245 struct pid_close_info info = { pid };
249 taskkill_message(STRING_SELF_TERMINATION);
254 EnumWindows(pid_enum_proc, (LPARAM)&info);
256 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
259 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
266 BOOL found_process = FALSE;
268 for (index = 0; index < pid_list_size; index++)
270 WCHAR process_name[MAX_PATH];
272 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
273 !strcmpiW(process_name, task_list[i]))
275 struct pid_close_info info = { pid_list[index] };
277 found_process = TRUE;
278 if (pid_list[index] == self_pid)
280 taskkill_message(STRING_SELF_TERMINATION);
285 EnumWindows(pid_enum_proc, (LPARAM)&info);
286 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
292 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
298 HeapFree(GetProcessHeap(), 0, pid_list);
302 static int terminate_processes(void)
304 DWORD *pid_list, pid_list_size;
305 DWORD self_pid = GetCurrentProcessId();
309 pid_list = enumerate_processes(&pid_list_size);
312 taskkill_message(STRING_ENUM_FAILED);
316 for (i = 0; i < task_count; i++)
318 WCHAR *p = task_list[i];
319 BOOL is_numeric = TRUE;
321 /* Determine whether the string is not numeric. */
333 DWORD pid = atoiW(task_list[i]);
338 taskkill_message(STRING_SELF_TERMINATION);
343 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
346 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
351 if (!TerminateProcess(process, 0))
353 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
355 CloseHandle(process);
359 taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
360 CloseHandle(process);
365 BOOL found_process = FALSE;
367 for (index = 0; index < pid_list_size; index++)
369 WCHAR process_name[MAX_PATH];
371 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
372 !strcmpiW(process_name, task_list[i]))
376 if (pid_list[index] == self_pid)
378 taskkill_message(STRING_SELF_TERMINATION);
383 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
386 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
391 if (!TerminateProcess(process, 0))
393 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
395 CloseHandle(process);
399 found_process = TRUE;
400 taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
401 CloseHandle(process);
407 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
413 HeapFree(GetProcessHeap(), 0, pid_list);
417 static BOOL add_to_task_list(WCHAR *name)
419 static unsigned int list_size = 16;
423 task_list = HeapAlloc(GetProcessHeap(), 0,
424 list_size * sizeof(*task_list));
428 else if (task_count == list_size)
433 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
434 list_size * sizeof(*task_list));
438 task_list = realloc_list;
441 task_list[task_count++] = name;
445 /* FIXME Argument processing does not match behavior observed on Windows.
446 * Stringent argument counting and processing is performed, and unrecognized
447 * options are detected as parameters when placed after options that accept one. */
448 static BOOL process_arguments(int argc, WCHAR *argv[])
450 static const WCHAR slashForceTerminate[] = {'/','f',0};
451 static const WCHAR slashImage[] = {'/','i','m',0};
452 static const WCHAR slashPID[] = {'/','p','i','d',0};
453 static const WCHAR slashHelp[] = {'/','?',0};
454 static const WCHAR slashTerminateChildren[] = {'/','t',0};
459 BOOL has_im = 0, has_pid = 0;
461 /* Only the lone help option is recognized. */
462 if (argc == 2 && !strcmpW(slashHelp, argv[1]))
464 taskkill_message(STRING_USAGE);
468 for (i = 1; i < argc; i++)
470 int got_im = 0, got_pid = 0;
472 if (!strcmpiW(slashTerminateChildren, argv[i]))
473 WINE_FIXME("/T not supported\n");
474 if (!strcmpiW(slashForceTerminate, argv[i]))
475 force_termination = 1;
476 /* Options /IM and /PID appear to behave identically, except for
477 * the fact that they cannot be specified at the same time. */
478 else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
479 (got_pid = !strcmpiW(slashPID, argv[i])))
483 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
484 taskkill_message(STRING_USAGE);
488 if (got_im) has_im = 1;
489 if (got_pid) has_pid = 1;
491 if (has_im && has_pid)
493 taskkill_message(STRING_MUTUAL_EXCLUSIVE);
494 taskkill_message(STRING_USAGE);
498 if (!add_to_task_list(argv[i + 1]))
504 taskkill_message(STRING_INVALID_OPTION);
505 taskkill_message(STRING_USAGE);
512 taskkill_message(STRING_MISSING_OPTION);
513 taskkill_message(STRING_USAGE);
520 int wmain(int argc, WCHAR *argv[])
524 if (!process_arguments(argc, argv))
526 HeapFree(GetProcessHeap(), 0, task_list);
530 if (force_termination)
531 status_code = terminate_processes();
533 status_code = send_close_messages();
535 HeapFree(GetProcessHeap(), 0, task_list);