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 static const WCHAR formatW
[] = {'%','1',0};
106 WCHAR msg_buffer
[8192];
108 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
110 return taskkill_printfW(formatW
, msg_buffer
);
113 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
114 static BOOL CALLBACK
pid_enum_proc(HWND hwnd
, LPARAM lParam
)
116 struct pid_close_info
*info
= (struct pid_close_info
*)lParam
;
119 GetWindowThreadProcessId(hwnd
, &hwnd_pid
);
121 if (hwnd_pid
== info
->pid
)
123 PostMessageW(hwnd
, WM_CLOSE
, 0, 0);
130 static DWORD
*enumerate_processes(DWORD
*list_count
)
132 DWORD
*pid_list
, alloc_bytes
= 1024 * sizeof(*pid_list
), needed_bytes
;
134 pid_list
= HeapAlloc(GetProcessHeap(), 0, alloc_bytes
);
142 if (!EnumProcesses(pid_list
, alloc_bytes
, &needed_bytes
))
144 HeapFree(GetProcessHeap(), 0, pid_list
);
148 /* EnumProcesses can't signal an insufficient buffer condition, so the
149 * only way to possibly determine whether a larger buffer is required
150 * is to see whether the written number of bytes is the same as the
151 * buffer size. If so, the buffer will be reallocated to twice the
153 if (alloc_bytes
!= needed_bytes
)
157 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, pid_list
, alloc_bytes
);
160 HeapFree(GetProcessHeap(), 0, pid_list
);
163 pid_list
= realloc_list
;
166 *list_count
= needed_bytes
/ sizeof(*pid_list
);
170 static BOOL
get_process_name_from_pid(DWORD pid
, WCHAR
*buf
, DWORD chars
)
176 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
180 if (!EnumProcessModules(process
, &module
, sizeof(module
), &required_size
))
182 CloseHandle(process
);
186 if (!GetModuleBaseNameW(process
, module
, buf
, chars
))
188 CloseHandle(process
);
192 CloseHandle(process
);
196 /* The implemented task enumeration and termination behavior does not
197 * exactly match native behavior. On Windows:
199 * In the case of terminating by process name, specifying a particular
200 * process name more times than the number of running instances causes
201 * all instances to be terminated, but termination failure messages to
202 * be printed as many times as the difference between the specification
203 * quantity and the number of running instances.
205 * Successful terminations are all listed first in order, with failing
206 * terminations being listed at the end.
208 * A PID of zero causes taskkill to warn about the inability to terminate
209 * system processes. */
210 static int send_close_messages(void)
212 DWORD
*pid_list
, pid_list_size
;
213 DWORD self_pid
= GetCurrentProcessId();
217 pid_list
= enumerate_processes(&pid_list_size
);
220 taskkill_message(STRING_ENUM_FAILED
);
224 for (i
= 0; i
< task_count
; i
++)
226 WCHAR
*p
= task_list
[i
];
227 BOOL is_numeric
= TRUE
;
229 /* Determine whether the string is not numeric. */
241 DWORD pid
= wcstol(task_list
[i
], NULL
, 10);
242 struct pid_close_info info
= { pid
};
246 taskkill_message(STRING_SELF_TERMINATION
);
251 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
253 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH
, pid
);
256 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
263 BOOL found_process
= FALSE
;
265 for (index
= 0; index
< pid_list_size
; index
++)
267 WCHAR process_name
[MAX_PATH
];
269 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
270 !wcsicmp(process_name
, task_list
[i
]))
272 struct pid_close_info info
= { pid_list
[index
] };
274 found_process
= TRUE
;
275 if (pid_list
[index
] == self_pid
)
277 taskkill_message(STRING_SELF_TERMINATION
);
282 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
283 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH
, process_name
, pid_list
[index
]);
289 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
295 HeapFree(GetProcessHeap(), 0, pid_list
);
299 static int terminate_processes(void)
301 DWORD
*pid_list
, pid_list_size
;
302 DWORD self_pid
= GetCurrentProcessId();
306 pid_list
= enumerate_processes(&pid_list_size
);
309 taskkill_message(STRING_ENUM_FAILED
);
313 for (i
= 0; i
< task_count
; i
++)
315 WCHAR
*p
= task_list
[i
];
316 BOOL is_numeric
= TRUE
;
318 /* Determine whether the string is not numeric. */
330 DWORD pid
= wcstol(task_list
[i
], NULL
, 10);
335 taskkill_message(STRING_SELF_TERMINATION
);
340 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid
);
343 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
348 if (!TerminateProcess(process
, 0))
350 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
352 CloseHandle(process
);
356 taskkill_message_printfW(STRING_TERM_PID_SEARCH
, pid
);
357 CloseHandle(process
);
362 BOOL found_process
= FALSE
;
364 for (index
= 0; index
< pid_list_size
; index
++)
366 WCHAR process_name
[MAX_PATH
];
368 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
369 !wcsicmp(process_name
, task_list
[i
]))
373 if (pid_list
[index
] == self_pid
)
375 taskkill_message(STRING_SELF_TERMINATION
);
380 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid_list
[index
]);
383 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
388 if (!TerminateProcess(process
, 0))
390 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
392 CloseHandle(process
);
396 found_process
= TRUE
;
397 taskkill_message_printfW(STRING_TERM_PROC_SEARCH
, task_list
[i
], pid_list
[index
]);
398 CloseHandle(process
);
404 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
410 HeapFree(GetProcessHeap(), 0, pid_list
);
414 static BOOL
add_to_task_list(WCHAR
*name
)
416 static unsigned int list_size
= 16;
420 task_list
= HeapAlloc(GetProcessHeap(), 0,
421 list_size
* sizeof(*task_list
));
425 else if (task_count
== list_size
)
430 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, task_list
,
431 list_size
* sizeof(*task_list
));
435 task_list
= realloc_list
;
438 task_list
[task_count
++] = name
;
442 /* FIXME Argument processing does not match behavior observed on Windows.
443 * Stringent argument counting and processing is performed, and unrecognized
444 * options are detected as parameters when placed after options that accept one. */
445 static BOOL
process_arguments(int argc
, WCHAR
*argv
[])
447 static const WCHAR opForceTerminate
[] = {'f',0};
448 static const WCHAR opImage
[] = {'i','m',0};
449 static const WCHAR opPID
[] = {'p','i','d',0};
450 static const WCHAR opHelp
[] = {'?',0};
451 static const WCHAR opTerminateChildren
[] = {'t',0};
457 BOOL has_im
= FALSE
, has_pid
= FALSE
;
459 /* Only the lone help option is recognized. */
463 if ((*argdata
== '/' || *argdata
== '-') && !lstrcmpW(opHelp
, argdata
+ 1))
465 taskkill_message(STRING_USAGE
);
470 for (i
= 1; i
< argc
; i
++)
472 BOOL got_im
= FALSE
, got_pid
= FALSE
;
475 if (*argdata
!= '/' && *argdata
!= '-')
479 if (!wcsicmp(opTerminateChildren
, argdata
))
480 WINE_FIXME("argument T not supported\n");
481 if (!wcsicmp(opForceTerminate
, argdata
))
482 force_termination
= TRUE
;
483 /* Options /IM and /PID appear to behave identically, except for
484 * the fact that they cannot be specified at the same time. */
485 else if ((got_im
= !wcsicmp(opImage
, argdata
)) ||
486 (got_pid
= !wcsicmp(opPID
, argdata
)))
490 taskkill_message_printfW(STRING_MISSING_PARAM
, argv
[i
]);
491 taskkill_message(STRING_USAGE
);
495 if (got_im
) has_im
= TRUE
;
496 if (got_pid
) has_pid
= TRUE
;
498 if (has_im
&& has_pid
)
500 taskkill_message(STRING_MUTUAL_EXCLUSIVE
);
501 taskkill_message(STRING_USAGE
);
505 if (!add_to_task_list(argv
[i
+ 1]))
512 taskkill_message(STRING_INVALID_OPTION
);
513 taskkill_message(STRING_USAGE
);
520 taskkill_message(STRING_MISSING_OPTION
);
521 taskkill_message(STRING_USAGE
);
528 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
532 if (!process_arguments(argc
, argv
))
534 HeapFree(GetProcessHeap(), 0, task_list
);
538 if (force_termination
)
539 status_code
= terminate_processes();
541 status_code
= send_close_messages();
543 HeapFree(GetProcessHeap(), 0, task_list
);