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>
29 WINE_DEFAULT_DEBUG_CHANNEL(taskkill
);
31 static BOOL force_termination
= FALSE
;
33 static WCHAR
**task_list
;
34 static unsigned int task_count
;
42 static int taskkill_vprintfW(const WCHAR
*msg
, __ms_va_list va_args
)
46 WCHAR msg_buffer
[8192];
48 wlen
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, msg
, 0, 0, msg_buffer
,
49 ARRAY_SIZE(msg_buffer
), &va_args
);
51 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
);
57 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
58 * back to WriteFile(), assuming the console encoding is still the right
61 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
63 msgA
= HeapAlloc(GetProcessHeap(), 0, len
);
67 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
69 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
70 HeapFree(GetProcessHeap(), 0, msgA
);
76 static int WINAPIV
taskkill_printfW(const WCHAR
*msg
, ...)
81 __ms_va_start(va_args
, msg
);
82 len
= taskkill_vprintfW(msg
, va_args
);
88 static int WINAPIV
taskkill_message_printfW(int msg
, ...)
91 WCHAR msg_buffer
[8192];
94 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
96 __ms_va_start(va_args
, msg
);
97 len
= taskkill_vprintfW(msg_buffer
, va_args
);
103 static int taskkill_message(int msg
)
105 WCHAR msg_buffer
[8192];
107 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
109 return taskkill_printfW(L
"%1", msg_buffer
);
112 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
113 static BOOL CALLBACK
pid_enum_proc(HWND hwnd
, LPARAM lParam
)
115 struct pid_close_info
*info
= (struct pid_close_info
*)lParam
;
118 GetWindowThreadProcessId(hwnd
, &hwnd_pid
);
120 if (hwnd_pid
== info
->pid
)
122 PostMessageW(hwnd
, WM_CLOSE
, 0, 0);
129 static DWORD
*enumerate_processes(DWORD
*list_count
)
131 DWORD
*pid_list
, alloc_bytes
= 1024 * sizeof(*pid_list
), needed_bytes
;
133 pid_list
= HeapAlloc(GetProcessHeap(), 0, alloc_bytes
);
141 if (!EnumProcesses(pid_list
, alloc_bytes
, &needed_bytes
))
143 HeapFree(GetProcessHeap(), 0, pid_list
);
147 /* EnumProcesses can't signal an insufficient buffer condition, so the
148 * only way to possibly determine whether a larger buffer is required
149 * is to see whether the written number of bytes is the same as the
150 * buffer size. If so, the buffer will be reallocated to twice the
152 if (alloc_bytes
!= needed_bytes
)
156 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, pid_list
, alloc_bytes
);
159 HeapFree(GetProcessHeap(), 0, pid_list
);
162 pid_list
= realloc_list
;
165 *list_count
= needed_bytes
/ sizeof(*pid_list
);
169 static BOOL
get_process_name_from_pid(DWORD pid
, WCHAR
*buf
, DWORD chars
)
175 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
179 if (!EnumProcessModules(process
, &module
, sizeof(module
), &required_size
))
181 CloseHandle(process
);
185 if (!GetModuleBaseNameW(process
, module
, buf
, chars
))
187 CloseHandle(process
);
191 CloseHandle(process
);
195 /* The implemented task enumeration and termination behavior does not
196 * exactly match native behavior. On Windows:
198 * In the case of terminating by process name, specifying a particular
199 * process name more times than the number of running instances causes
200 * all instances to be terminated, but termination failure messages to
201 * be printed as many times as the difference between the specification
202 * quantity and the number of running instances.
204 * Successful terminations are all listed first in order, with failing
205 * terminations being listed at the end.
207 * A PID of zero causes taskkill to warn about the inability to terminate
208 * system processes. */
209 static int send_close_messages(void)
211 DWORD
*pid_list
, pid_list_size
;
212 DWORD self_pid
= GetCurrentProcessId();
216 pid_list
= enumerate_processes(&pid_list_size
);
219 taskkill_message(STRING_ENUM_FAILED
);
223 for (i
= 0; i
< task_count
; i
++)
225 WCHAR
*p
= task_list
[i
];
226 BOOL is_numeric
= TRUE
;
228 /* Determine whether the string is not numeric. */
240 DWORD pid
= wcstol(task_list
[i
], NULL
, 10);
241 struct pid_close_info info
= { pid
};
245 taskkill_message(STRING_SELF_TERMINATION
);
250 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
252 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH
, pid
);
255 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
262 BOOL found_process
= FALSE
;
264 for (index
= 0; index
< pid_list_size
; index
++)
266 WCHAR process_name
[MAX_PATH
];
268 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
269 !wcsicmp(process_name
, task_list
[i
]))
271 struct pid_close_info info
= { pid_list
[index
] };
273 found_process
= TRUE
;
274 if (pid_list
[index
] == self_pid
)
276 taskkill_message(STRING_SELF_TERMINATION
);
281 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
282 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH
, process_name
, pid_list
[index
]);
288 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
294 HeapFree(GetProcessHeap(), 0, pid_list
);
298 static int terminate_processes(void)
300 DWORD
*pid_list
, pid_list_size
;
301 DWORD self_pid
= GetCurrentProcessId();
305 pid_list
= enumerate_processes(&pid_list_size
);
308 taskkill_message(STRING_ENUM_FAILED
);
312 for (i
= 0; i
< task_count
; i
++)
314 WCHAR
*p
= task_list
[i
];
315 BOOL is_numeric
= TRUE
;
317 /* Determine whether the string is not numeric. */
329 DWORD pid
= wcstol(task_list
[i
], NULL
, 10);
334 taskkill_message(STRING_SELF_TERMINATION
);
339 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid
);
342 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
347 if (!TerminateProcess(process
, 0))
349 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
351 CloseHandle(process
);
355 taskkill_message_printfW(STRING_TERM_PID_SEARCH
, pid
);
356 CloseHandle(process
);
361 BOOL found_process
= FALSE
;
363 for (index
= 0; index
< pid_list_size
; index
++)
365 WCHAR process_name
[MAX_PATH
];
367 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
368 !wcsicmp(process_name
, task_list
[i
]))
372 if (pid_list
[index
] == self_pid
)
374 taskkill_message(STRING_SELF_TERMINATION
);
379 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid_list
[index
]);
382 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
387 if (!TerminateProcess(process
, 0))
389 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
391 CloseHandle(process
);
395 found_process
= TRUE
;
396 taskkill_message_printfW(STRING_TERM_PROC_SEARCH
, task_list
[i
], pid_list
[index
]);
397 CloseHandle(process
);
403 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
409 HeapFree(GetProcessHeap(), 0, pid_list
);
413 static BOOL
add_to_task_list(WCHAR
*name
)
415 static unsigned int list_size
= 16;
419 task_list
= HeapAlloc(GetProcessHeap(), 0,
420 list_size
* sizeof(*task_list
));
424 else if (task_count
== list_size
)
429 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, task_list
,
430 list_size
* sizeof(*task_list
));
434 task_list
= realloc_list
;
437 task_list
[task_count
++] = name
;
441 /* FIXME Argument processing does not match behavior observed on Windows.
442 * Stringent argument counting and processing is performed, and unrecognized
443 * options are detected as parameters when placed after options that accept one. */
444 static BOOL
process_arguments(int argc
, WCHAR
*argv
[])
450 BOOL has_im
= FALSE
, has_pid
= FALSE
;
452 /* Only the lone help option is recognized. */
456 if ((*argdata
== '/' || *argdata
== '-') && !lstrcmpW(L
"?", argdata
+ 1))
458 taskkill_message(STRING_USAGE
);
463 for (i
= 1; i
< argc
; i
++)
465 BOOL got_im
= FALSE
, got_pid
= FALSE
;
468 if (*argdata
!= '/' && *argdata
!= '-')
472 if (!wcsicmp(L
"t", argdata
))
473 WINE_FIXME("argument T not supported\n");
474 if (!wcsicmp(L
"f", argdata
))
475 force_termination
= TRUE
;
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
= !wcsicmp(L
"im", argdata
)) ||
479 (got_pid
= !wcsicmp(L
"pid", argdata
)))
483 taskkill_message_printfW(STRING_MISSING_PARAM
, argv
[i
]);
484 taskkill_message(STRING_USAGE
);
488 if (got_im
) has_im
= TRUE
;
489 if (got_pid
) has_pid
= TRUE
;
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]))
505 taskkill_message(STRING_INVALID_OPTION
);
506 taskkill_message(STRING_USAGE
);
513 taskkill_message(STRING_MISSING_OPTION
);
514 taskkill_message(STRING_USAGE
);
521 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
525 if (!process_arguments(argc
, argv
))
527 HeapFree(GetProcessHeap(), 0, task_list
);
531 if (force_termination
)
532 status_code
= terminate_processes();
534 status_code
= send_close_messages();
536 HeapFree(GetProcessHeap(), 0, task_list
);