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
24 #include <wine/unicode.h>
28 static int force_termination
;
30 static WCHAR
**task_list
;
31 static unsigned int task_count
;
39 static int taskkill_vprintfW(const WCHAR
*msg
, va_list va_args
)
43 WCHAR msg_buffer
[8192];
45 wlen
= vsprintfW(msg_buffer
, msg
, va_args
);
47 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), msg_buffer
, wlen
, &count
, NULL
);
53 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
54 * back to WriteFile(), assuming the console encoding is still the right
57 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
59 msgA
= HeapAlloc(GetProcessHeap(), 0, len
);
63 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
65 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
66 HeapFree(GetProcessHeap(), 0, msgA
);
72 static int taskkill_printfW(const WCHAR
*msg
, ...)
77 va_start(va_args
, msg
);
78 len
= taskkill_vprintfW(msg
, va_args
);
84 static int taskkill_message_printfW(int msg
, ...)
87 WCHAR msg_buffer
[8192];
90 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
91 sizeof(msg_buffer
)/sizeof(WCHAR
));
93 va_start(va_args
, msg
);
94 len
= taskkill_vprintfW(msg_buffer
, va_args
);
100 static int taskkill_message(int msg
)
102 static const WCHAR formatW
[] = {'%','s',0};
103 WCHAR msg_buffer
[8192];
105 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
106 sizeof(msg_buffer
)/sizeof(WCHAR
));
108 return taskkill_printfW(formatW
, msg_buffer
);
111 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
112 static BOOL CALLBACK
pid_enum_proc(HWND hwnd
, LPARAM lParam
)
114 struct pid_close_info
*info
= (struct pid_close_info
*)lParam
;
117 GetWindowThreadProcessId(hwnd
, &hwnd_pid
);
119 if (hwnd_pid
== info
->pid
)
121 PostMessageW(hwnd
, WM_CLOSE
, 0, 0);
128 static DWORD
*enumerate_processes(DWORD
*list_count
)
130 DWORD
*pid_list
, alloc_bytes
= 1024 * sizeof(*pid_list
), needed_bytes
;
132 pid_list
= HeapAlloc(GetProcessHeap(), 0, alloc_bytes
);
140 if (!EnumProcesses(pid_list
, alloc_bytes
, &needed_bytes
))
142 HeapFree(GetProcessHeap(), 0, pid_list
);
146 /* EnumProcesses can't signal an insufficient buffer condition, so the
147 * only way to possibly determine whether a larger buffer is required
148 * is to see whether the written number of bytes is the same as the
149 * buffer size. If so, the buffer will be reallocated to twice the
151 if (alloc_bytes
!= needed_bytes
)
155 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, pid_list
, alloc_bytes
);
158 HeapFree(GetProcessHeap(), 0, pid_list
);
161 pid_list
= realloc_list
;
164 *list_count
= needed_bytes
/ sizeof(*pid_list
);
168 static BOOL
get_process_name_from_pid(DWORD pid
, WCHAR
*buf
, DWORD chars
)
174 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
178 if (!EnumProcessModules(process
, &module
, sizeof(module
), &required_size
))
180 CloseHandle(process
);
184 if (!GetModuleBaseNameW(process
, module
, buf
, chars
))
186 CloseHandle(process
);
190 CloseHandle(process
);
194 /* The implemented task enumeration and termination behavior does not
195 * exactly match native behavior. On Windows:
197 * In the case of terminating by process name, specifying a particular
198 * process name more times than the number of running instances causes
199 * all instances to be terminated, but termination failure messages to
200 * be printed as many times as the difference between the specification
201 * quantity and the number of running instances.
203 * Successful terminations are all listed first in order, with failing
204 * terminations being listed at the end.
206 * A PID of zero causes taskkill to warn about the inability to terminate
207 * system processes. */
208 static int send_close_messages(void)
210 DWORD
*pid_list
, pid_list_size
;
211 DWORD self_pid
= GetCurrentProcessId();
215 pid_list
= enumerate_processes(&pid_list_size
);
218 taskkill_message(STRING_ENUM_FAILED
);
222 for (i
= 0; i
< task_count
; i
++)
224 WCHAR
*p
= task_list
[i
];
225 BOOL is_numeric
= TRUE
;
227 /* Determine whether the string is not numeric. */
239 DWORD pid
= atoiW(task_list
[i
]);
240 struct pid_close_info info
= { pid
};
244 taskkill_message(STRING_SELF_TERMINATION
);
249 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
251 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH
, pid
);
254 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
261 BOOL found_process
= FALSE
;
263 for (index
= 0; index
< pid_list_size
; index
++)
265 WCHAR process_name
[MAX_PATH
];
267 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
268 !strcmpiW(process_name
, task_list
[i
]))
270 struct pid_close_info info
= { pid_list
[index
] };
272 found_process
= TRUE
;
273 if (pid_list
[index
] == self_pid
)
275 taskkill_message(STRING_SELF_TERMINATION
);
280 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
281 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH
, process_name
, pid_list
[index
]);
287 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
293 HeapFree(GetProcessHeap(), 0, pid_list
);
297 static int terminate_processes(void)
299 DWORD
*pid_list
, pid_list_size
;
300 DWORD self_pid
= GetCurrentProcessId();
304 pid_list
= enumerate_processes(&pid_list_size
);
307 taskkill_message(STRING_ENUM_FAILED
);
311 for (i
= 0; i
< task_count
; i
++)
313 WCHAR
*p
= task_list
[i
];
314 BOOL is_numeric
= TRUE
;
316 /* Determine whether the string is not numeric. */
328 DWORD pid
= atoiW(task_list
[i
]);
333 taskkill_message(STRING_SELF_TERMINATION
);
338 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid
);
341 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
346 if (!TerminateProcess(process
, 0))
348 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
350 CloseHandle(process
);
354 taskkill_message_printfW(STRING_TERM_PID_SEARCH
, pid
);
355 CloseHandle(process
);
360 BOOL found_process
= FALSE
;
362 for (index
= 0; index
< pid_list_size
; index
++)
364 WCHAR process_name
[MAX_PATH
];
366 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
367 !strcmpiW(process_name
, task_list
[i
]))
371 if (pid_list
[index
] == self_pid
)
373 taskkill_message(STRING_SELF_TERMINATION
);
378 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid_list
[index
]);
381 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
386 if (!TerminateProcess(process
, 0))
388 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
390 CloseHandle(process
);
394 found_process
= TRUE
;
395 taskkill_message_printfW(STRING_TERM_PROC_SEARCH
, task_list
[i
], pid_list
[index
]);
396 CloseHandle(process
);
402 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
408 HeapFree(GetProcessHeap(), 0, pid_list
);
412 static BOOL
add_to_task_list(WCHAR
*name
)
414 static unsigned int list_size
= 16;
418 task_list
= HeapAlloc(GetProcessHeap(), 0,
419 list_size
* sizeof(*task_list
));
423 else if (task_count
== list_size
)
428 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, task_list
,
429 list_size
* sizeof(*task_list
));
433 task_list
= realloc_list
;
436 task_list
[task_count
++] = name
;
440 /* FIXME Argument processing does not match behavior observed on Windows.
441 * Stringent argument counting and processing is performed, and unrecognized
442 * options are detected as parameters when placed after options that accept one. */
443 static BOOL
process_arguments(int argc
, WCHAR
*argv
[])
445 static const WCHAR slashForceTerminate
[] = {'/','f',0};
446 static const WCHAR slashImage
[] = {'/','i','m',0};
447 static const WCHAR slashPID
[] = {'/','p','i','d',0};
448 static const WCHAR slashHelp
[] = {'/','?',0};
453 BOOL has_im
= 0, has_pid
= 0;
455 /* Only the lone help option is recognized. */
456 if (argc
== 2 && !strcmpW(slashHelp
, argv
[1]))
458 taskkill_message(STRING_USAGE
);
462 for (i
= 1; i
< argc
; i
++)
464 int got_im
= 0, got_pid
= 0;
466 if (!strcmpiW(slashForceTerminate
, argv
[i
]))
467 force_termination
= 1;
468 /* Options /IM and /PID appear to behave identically, except for
469 * the fact that they cannot be specified at the same time. */
470 else if ((got_im
= !strcmpiW(slashImage
, argv
[i
])) ||
471 (got_pid
= !strcmpiW(slashPID
, argv
[i
])))
475 taskkill_message_printfW(STRING_MISSING_PARAM
, argv
[i
]);
476 taskkill_message(STRING_USAGE
);
480 if (got_im
) has_im
= 1;
481 if (got_pid
) has_pid
= 1;
483 if (has_im
&& has_pid
)
485 taskkill_message(STRING_MUTUAL_EXCLUSIVE
);
486 taskkill_message(STRING_USAGE
);
490 if (!add_to_task_list(argv
[i
+ 1]))
496 taskkill_message(STRING_INVALID_OPTION
);
497 taskkill_message(STRING_USAGE
);
504 taskkill_message(STRING_MISSING_OPTION
);
505 taskkill_message(STRING_USAGE
);
512 int wmain(int argc
, WCHAR
*argv
[])
516 if (!process_arguments(argc
, argv
))
518 HeapFree(GetProcessHeap(), 0, task_list
);
522 if (force_termination
)
523 status_code
= terminate_processes();
525 status_code
= send_close_messages();
527 HeapFree(GetProcessHeap(), 0, task_list
);