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
, 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 if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
))
56 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
57 * back to WriteFile() using OEM code page.
59 len
= WideCharToMultiByte(GetOEMCP(), 0, msg_buffer
, wlen
,
61 msgA
= HeapAlloc(GetProcessHeap(), 0, len
);
65 WideCharToMultiByte(GetOEMCP(), 0, msg_buffer
, wlen
, msgA
, len
, NULL
, NULL
);
66 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
67 HeapFree(GetProcessHeap(), 0, msgA
);
73 static int WINAPIV
taskkill_printfW(const WCHAR
*msg
, ...)
78 va_start(va_args
, msg
);
79 len
= taskkill_vprintfW(msg
, va_args
);
85 static int WINAPIV
taskkill_message_printfW(int msg
, ...)
88 WCHAR msg_buffer
[8192];
91 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
93 va_start(va_args
, msg
);
94 len
= taskkill_vprintfW(msg_buffer
, va_args
);
100 static int taskkill_message(int msg
)
102 WCHAR msg_buffer
[8192];
104 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
, ARRAY_SIZE(msg_buffer
));
106 return taskkill_printfW(L
"%1", msg_buffer
);
109 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
110 static BOOL CALLBACK
pid_enum_proc(HWND hwnd
, LPARAM lParam
)
112 struct pid_close_info
*info
= (struct pid_close_info
*)lParam
;
115 GetWindowThreadProcessId(hwnd
, &hwnd_pid
);
117 if (hwnd_pid
== info
->pid
)
119 PostMessageW(hwnd
, WM_CLOSE
, 0, 0);
126 static DWORD
*enumerate_processes(DWORD
*list_count
)
128 DWORD
*pid_list
, alloc_bytes
= 1024 * sizeof(*pid_list
), needed_bytes
;
130 pid_list
= HeapAlloc(GetProcessHeap(), 0, alloc_bytes
);
138 if (!EnumProcesses(pid_list
, alloc_bytes
, &needed_bytes
))
140 HeapFree(GetProcessHeap(), 0, pid_list
);
144 /* EnumProcesses can't signal an insufficient buffer condition, so the
145 * only way to possibly determine whether a larger buffer is required
146 * is to see whether the written number of bytes is the same as the
147 * buffer size. If so, the buffer will be reallocated to twice the
149 if (alloc_bytes
!= needed_bytes
)
153 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, pid_list
, alloc_bytes
);
156 HeapFree(GetProcessHeap(), 0, pid_list
);
159 pid_list
= realloc_list
;
162 *list_count
= needed_bytes
/ sizeof(*pid_list
);
166 static BOOL
get_process_name_from_pid(DWORD pid
, WCHAR
*buf
, DWORD chars
)
172 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
176 if (!EnumProcessModules(process
, &module
, sizeof(module
), &required_size
))
178 CloseHandle(process
);
182 if (!GetModuleBaseNameW(process
, module
, buf
, chars
))
184 CloseHandle(process
);
188 CloseHandle(process
);
192 /* The implemented task enumeration and termination behavior does not
193 * exactly match native behavior. On Windows:
195 * In the case of terminating by process name, specifying a particular
196 * process name more times than the number of running instances causes
197 * all instances to be terminated, but termination failure messages to
198 * be printed as many times as the difference between the specification
199 * quantity and the number of running instances.
201 * Successful terminations are all listed first in order, with failing
202 * terminations being listed at the end.
204 * A PID of zero causes taskkill to warn about the inability to terminate
205 * system processes. */
206 static int send_close_messages(void)
208 DWORD
*pid_list
, pid_list_size
;
209 DWORD self_pid
= GetCurrentProcessId();
213 pid_list
= enumerate_processes(&pid_list_size
);
216 taskkill_message(STRING_ENUM_FAILED
);
220 for (i
= 0; i
< task_count
; i
++)
222 WCHAR
*p
= task_list
[i
];
223 BOOL is_numeric
= TRUE
;
225 /* Determine whether the string is not numeric. */
237 DWORD pid
= wcstol(task_list
[i
], NULL
, 10);
238 struct pid_close_info info
= { pid
};
242 taskkill_message(STRING_SELF_TERMINATION
);
247 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
249 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH
, pid
);
252 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
259 BOOL found_process
= FALSE
;
261 for (index
= 0; index
< pid_list_size
; index
++)
263 WCHAR process_name
[MAX_PATH
];
265 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
266 !wcsicmp(process_name
, task_list
[i
]))
268 struct pid_close_info info
= { pid_list
[index
] };
270 found_process
= TRUE
;
271 if (pid_list
[index
] == self_pid
)
273 taskkill_message(STRING_SELF_TERMINATION
);
278 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
279 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH
, process_name
, pid_list
[index
]);
285 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
291 HeapFree(GetProcessHeap(), 0, pid_list
);
295 static int terminate_processes(void)
297 DWORD
*pid_list
, pid_list_size
;
298 DWORD self_pid
= GetCurrentProcessId();
302 pid_list
= enumerate_processes(&pid_list_size
);
305 taskkill_message(STRING_ENUM_FAILED
);
309 for (i
= 0; i
< task_count
; i
++)
311 WCHAR
*p
= task_list
[i
];
312 BOOL is_numeric
= TRUE
;
314 /* Determine whether the string is not numeric. */
326 DWORD pid
= wcstol(task_list
[i
], NULL
, 10);
331 taskkill_message(STRING_SELF_TERMINATION
);
336 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid
);
339 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
344 if (!TerminateProcess(process
, 0))
346 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
348 CloseHandle(process
);
352 taskkill_message_printfW(STRING_TERM_PID_SEARCH
, pid
);
353 CloseHandle(process
);
358 BOOL found_process
= FALSE
;
360 for (index
= 0; index
< pid_list_size
; index
++)
362 WCHAR process_name
[MAX_PATH
];
364 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
365 !wcsicmp(process_name
, task_list
[i
]))
369 if (pid_list
[index
] == self_pid
)
371 taskkill_message(STRING_SELF_TERMINATION
);
376 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid_list
[index
]);
379 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
384 if (!TerminateProcess(process
, 0))
386 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
388 CloseHandle(process
);
392 found_process
= TRUE
;
393 taskkill_message_printfW(STRING_TERM_PROC_SEARCH
, task_list
[i
], pid_list
[index
]);
394 CloseHandle(process
);
400 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
406 HeapFree(GetProcessHeap(), 0, pid_list
);
410 static BOOL
add_to_task_list(WCHAR
*name
)
412 static unsigned int list_size
= 16;
416 task_list
= HeapAlloc(GetProcessHeap(), 0,
417 list_size
* sizeof(*task_list
));
421 else if (task_count
== list_size
)
426 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, task_list
,
427 list_size
* sizeof(*task_list
));
431 task_list
= realloc_list
;
434 task_list
[task_count
++] = name
;
438 /* FIXME Argument processing does not match behavior observed on Windows.
439 * Stringent argument counting and processing is performed, and unrecognized
440 * options are detected as parameters when placed after options that accept one. */
441 static BOOL
process_arguments(int argc
, WCHAR
*argv
[])
447 BOOL has_im
= FALSE
, has_pid
= FALSE
;
449 /* Only the lone help option is recognized. */
453 if ((*argdata
== '/' || *argdata
== '-') && !lstrcmpW(L
"?", argdata
+ 1))
455 taskkill_message(STRING_USAGE
);
460 for (i
= 1; i
< argc
; i
++)
462 BOOL got_im
= FALSE
, got_pid
= FALSE
;
465 if (*argdata
!= '/' && *argdata
!= '-')
469 if (!wcsicmp(L
"t", argdata
))
470 WINE_FIXME("argument T not supported\n");
471 if (!wcsicmp(L
"f", argdata
))
472 force_termination
= TRUE
;
473 /* Options /IM and /PID appear to behave identically, except for
474 * the fact that they cannot be specified at the same time. */
475 else if ((got_im
= !wcsicmp(L
"im", argdata
)) ||
476 (got_pid
= !wcsicmp(L
"pid", argdata
)))
480 taskkill_message_printfW(STRING_MISSING_PARAM
, argv
[i
]);
481 taskkill_message(STRING_USAGE
);
485 if (got_im
) has_im
= TRUE
;
486 if (got_pid
) has_pid
= TRUE
;
488 if (has_im
&& has_pid
)
490 taskkill_message(STRING_MUTUAL_EXCLUSIVE
);
491 taskkill_message(STRING_USAGE
);
495 if (!add_to_task_list(argv
[i
+ 1]))
502 taskkill_message(STRING_INVALID_OPTION
);
503 taskkill_message(STRING_USAGE
);
510 taskkill_message(STRING_MISSING_OPTION
);
511 taskkill_message(STRING_USAGE
);
518 int __cdecl
wmain(int argc
, WCHAR
*argv
[])
522 if (!process_arguments(argc
, argv
))
524 HeapFree(GetProcessHeap(), 0, task_list
);
528 if (force_termination
)
529 status_code
= terminate_processes();
531 status_code
= send_close_messages();
533 HeapFree(GetProcessHeap(), 0, task_list
);