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/unicode.h>
29 static int force_termination
;
31 static WCHAR
**task_list
;
32 static unsigned int task_count
;
40 static int taskkill_vprintfW(const WCHAR
*msg
, __ms_va_list va_args
)
44 WCHAR msg_buffer
[8192];
46 wlen
= FormatMessageW(FORMAT_MESSAGE_FROM_STRING
, msg
, 0, 0, msg_buffer
,
47 sizeof(msg_buffer
)/sizeof(*msg_buffer
), &va_args
);
49 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
);
55 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
56 * back to WriteFile(), assuming the console encoding is still the right
59 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
61 msgA
= HeapAlloc(GetProcessHeap(), 0, len
);
65 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
67 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
68 HeapFree(GetProcessHeap(), 0, msgA
);
74 static int CDECL
taskkill_printfW(const WCHAR
*msg
, ...)
79 __ms_va_start(va_args
, msg
);
80 len
= taskkill_vprintfW(msg
, va_args
);
86 static int CDECL
taskkill_message_printfW(int msg
, ...)
89 WCHAR msg_buffer
[8192];
92 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
93 sizeof(msg_buffer
)/sizeof(WCHAR
));
95 __ms_va_start(va_args
, msg
);
96 len
= taskkill_vprintfW(msg_buffer
, va_args
);
102 static int taskkill_message(int msg
)
104 static const WCHAR formatW
[] = {'%','1',0};
105 WCHAR msg_buffer
[8192];
107 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
108 sizeof(msg_buffer
)/sizeof(WCHAR
));
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
= atoiW(task_list
[i
]);
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 !strcmpiW(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
= atoiW(task_list
[i
]);
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 !strcmpiW(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 slashForceTerminate
[] = {'/','f',0};
448 static const WCHAR slashImage
[] = {'/','i','m',0};
449 static const WCHAR slashPID
[] = {'/','p','i','d',0};
450 static const WCHAR slashHelp
[] = {'/','?',0};
455 BOOL has_im
= 0, has_pid
= 0;
457 /* Only the lone help option is recognized. */
458 if (argc
== 2 && !strcmpW(slashHelp
, argv
[1]))
460 taskkill_message(STRING_USAGE
);
464 for (i
= 1; i
< argc
; i
++)
466 int got_im
= 0, got_pid
= 0;
468 if (!strcmpiW(slashForceTerminate
, argv
[i
]))
469 force_termination
= 1;
470 /* Options /IM and /PID appear to behave identically, except for
471 * the fact that they cannot be specified at the same time. */
472 else if ((got_im
= !strcmpiW(slashImage
, argv
[i
])) ||
473 (got_pid
= !strcmpiW(slashPID
, argv
[i
])))
477 taskkill_message_printfW(STRING_MISSING_PARAM
, argv
[i
]);
478 taskkill_message(STRING_USAGE
);
482 if (got_im
) has_im
= 1;
483 if (got_pid
) has_pid
= 1;
485 if (has_im
&& has_pid
)
487 taskkill_message(STRING_MUTUAL_EXCLUSIVE
);
488 taskkill_message(STRING_USAGE
);
492 if (!add_to_task_list(argv
[i
+ 1]))
498 taskkill_message(STRING_INVALID_OPTION
);
499 taskkill_message(STRING_USAGE
);
506 taskkill_message(STRING_MISSING_OPTION
);
507 taskkill_message(STRING_USAGE
);
514 int wmain(int argc
, WCHAR
*argv
[])
518 if (!process_arguments(argc
, argv
))
520 HeapFree(GetProcessHeap(), 0, task_list
);
524 if (force_termination
)
525 status_code
= terminate_processes();
527 status_code
= send_close_messages();
529 HeapFree(GetProcessHeap(), 0, task_list
);