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 len
= WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
,
55 msgA
= HeapAlloc(GetProcessHeap(), 0, len
);
59 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer
, wlen
, msgA
, len
,
61 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), msgA
, len
, &count
, FALSE
);
62 HeapFree(GetProcessHeap(), 0, msgA
);
68 static int taskkill_printfW(const WCHAR
*msg
, ...)
73 va_start(va_args
, msg
);
74 len
= taskkill_vprintfW(msg
, va_args
);
80 static int taskkill_message_printfW(int msg
, ...)
83 WCHAR msg_buffer
[8192];
86 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
87 sizeof(msg_buffer
)/sizeof(WCHAR
));
89 va_start(va_args
, msg
);
90 len
= taskkill_vprintfW(msg_buffer
, va_args
);
96 static int taskkill_message(int msg
)
98 static const WCHAR formatW
[] = {'%','s',0};
99 WCHAR msg_buffer
[8192];
101 LoadStringW(GetModuleHandleW(NULL
), msg
, msg_buffer
,
102 sizeof(msg_buffer
)/sizeof(WCHAR
));
104 return taskkill_printfW(formatW
, msg_buffer
);
107 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
108 static BOOL CALLBACK
pid_enum_proc(HWND hwnd
, LPARAM lParam
)
110 struct pid_close_info
*info
= (struct pid_close_info
*)lParam
;
113 GetWindowThreadProcessId(hwnd
, &hwnd_pid
);
115 if (hwnd_pid
== info
->pid
)
117 PostMessageW(hwnd
, WM_CLOSE
, 0, 0);
124 static DWORD
*enumerate_processes(DWORD
*list_count
)
126 DWORD
*pid_list
, alloc_bytes
= 1024 * sizeof(*pid_list
), needed_bytes
;
128 pid_list
= HeapAlloc(GetProcessHeap(), 0, alloc_bytes
);
136 if (!EnumProcesses(pid_list
, alloc_bytes
, &needed_bytes
))
138 HeapFree(GetProcessHeap(), 0, pid_list
);
142 /* EnumProcesses can't signal an insufficient buffer condition, so the
143 * only way to possibly determine whether a larger buffer is required
144 * is to see whether the written number of bytes is the same as the
145 * buffer size. If so, the buffer will be reallocated to twice the
147 if (alloc_bytes
!= needed_bytes
)
151 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, pid_list
, alloc_bytes
);
154 HeapFree(GetProcessHeap(), 0, pid_list
);
157 pid_list
= realloc_list
;
160 *list_count
= needed_bytes
/ sizeof(*pid_list
);
164 static BOOL
get_process_name_from_pid(DWORD pid
, WCHAR
*buf
, DWORD chars
)
170 process
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
, FALSE
, pid
);
174 if (!EnumProcessModules(process
, &module
, sizeof(module
), &required_size
))
176 CloseHandle(process
);
180 if (!GetModuleBaseNameW(process
, module
, buf
, chars
))
182 CloseHandle(process
);
186 CloseHandle(process
);
190 /* The implemented task enumeration and termination behavior does not
191 * exactly match native behavior. On Windows:
193 * In the case of terminating by process name, specifying a particular
194 * process name more times than the number of running instances causes
195 * all instances to be terminated, but termination failure messages to
196 * be printed as many times as the difference between the specification
197 * quantity and the number of running instances.
199 * Successful terminations are all listed first in order, with failing
200 * terminations being listed at the end.
202 * A PID of zero causes taskkill to warn about the inability to terminate
203 * system processes. */
204 static int send_close_messages(void)
206 DWORD
*pid_list
, pid_list_size
;
207 DWORD self_pid
= GetCurrentProcessId();
211 pid_list
= enumerate_processes(&pid_list_size
);
214 taskkill_message(STRING_ENUM_FAILED
);
218 for (i
= 0; i
< task_count
; i
++)
220 WCHAR
*p
= task_list
[i
];
221 BOOL is_numeric
= TRUE
;
223 /* Determine whether the string is not numeric. */
235 DWORD pid
= atoiW(task_list
[i
]);
236 struct pid_close_info info
= { pid
};
240 taskkill_message(STRING_SELF_TERMINATION
);
245 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
247 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH
, pid
);
250 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
257 BOOL found_process
= FALSE
;
259 for (index
= 0; index
< pid_list_size
; index
++)
261 WCHAR process_name
[MAX_PATH
];
263 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
264 !strcmpiW(process_name
, task_list
[i
]))
266 struct pid_close_info info
= { pid_list
[index
] };
268 found_process
= TRUE
;
269 if (pid_list
[index
] == self_pid
)
271 taskkill_message(STRING_SELF_TERMINATION
);
276 EnumWindows(pid_enum_proc
, (LPARAM
)&info
);
277 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH
, process_name
, pid_list
[index
]);
283 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
289 HeapFree(GetProcessHeap(), 0, pid_list
);
293 static int terminate_processes(void)
295 DWORD
*pid_list
, pid_list_size
;
296 DWORD self_pid
= GetCurrentProcessId();
300 pid_list
= enumerate_processes(&pid_list_size
);
303 taskkill_message(STRING_ENUM_FAILED
);
307 for (i
= 0; i
< task_count
; i
++)
309 WCHAR
*p
= task_list
[i
];
310 BOOL is_numeric
= TRUE
;
312 /* Determine whether the string is not numeric. */
324 DWORD pid
= atoiW(task_list
[i
]);
329 taskkill_message(STRING_SELF_TERMINATION
);
334 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid
);
337 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
342 if (!TerminateProcess(process
, 0))
344 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
346 CloseHandle(process
);
350 taskkill_message_printfW(STRING_TERM_PID_SEARCH
, pid
);
351 CloseHandle(process
);
356 BOOL found_process
= FALSE
;
358 for (index
= 0; index
< pid_list_size
; index
++)
360 WCHAR process_name
[MAX_PATH
];
362 if (get_process_name_from_pid(pid_list
[index
], process_name
, MAX_PATH
) &&
363 !strcmpiW(process_name
, task_list
[i
]))
367 if (pid_list
[index
] == self_pid
)
369 taskkill_message(STRING_SELF_TERMINATION
);
374 process
= OpenProcess(PROCESS_TERMINATE
, FALSE
, pid_list
[index
]);
377 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
382 if (!TerminateProcess(process
, 0))
384 taskkill_message_printfW(STRING_TERMINATE_FAILED
, task_list
[i
]);
386 CloseHandle(process
);
390 found_process
= TRUE
;
391 taskkill_message_printfW(STRING_TERM_PROC_SEARCH
, task_list
[i
], pid_list
[index
]);
392 CloseHandle(process
);
398 taskkill_message_printfW(STRING_SEARCH_FAILED
, task_list
[i
]);
404 HeapFree(GetProcessHeap(), 0, pid_list
);
408 static BOOL
add_to_task_list(WCHAR
*name
)
410 static unsigned int list_size
= 16;
414 task_list
= HeapAlloc(GetProcessHeap(), 0,
415 list_size
* sizeof(*task_list
));
419 else if (task_count
== list_size
)
424 realloc_list
= HeapReAlloc(GetProcessHeap(), 0, task_list
,
425 list_size
* sizeof(*task_list
));
429 task_list
= realloc_list
;
432 task_list
[task_count
++] = name
;
436 /* FIXME Argument processing does not match behavior observed on Windows.
437 * Stringent argument counting and processing is performed, and unrecognized
438 * options are detected as parameters when placed after options that accept one. */
439 static BOOL
process_arguments(int argc
, WCHAR
*argv
[])
441 static const WCHAR slashForceTerminate
[] = {'/','f',0};
442 static const WCHAR slashImage
[] = {'/','i','m',0};
443 static const WCHAR slashPID
[] = {'/','p','i','d',0};
444 static const WCHAR slashHelp
[] = {'/','?',0};
449 BOOL has_im
= 0, has_pid
= 0;
451 /* Only the lone help option is recognized. */
452 if (argc
== 2 && !strcmpW(slashHelp
, argv
[1]))
454 taskkill_message(STRING_USAGE
);
458 for (i
= 1; i
< argc
; i
++)
460 int got_im
= 0, got_pid
= 0;
462 if (!strcmpiW(slashForceTerminate
, argv
[i
]))
463 force_termination
= 1;
464 /* Options /IM and /PID appear to behave identically, except for
465 * the fact that they cannot be specified at the same time. */
466 else if ((got_im
= !strcmpiW(slashImage
, argv
[i
])) ||
467 (got_pid
= !strcmpiW(slashPID
, argv
[i
])))
471 taskkill_message_printfW(STRING_MISSING_PARAM
, argv
[i
]);
472 taskkill_message(STRING_USAGE
);
476 if (got_im
) has_im
= 1;
477 if (got_pid
) has_pid
= 1;
479 if (has_im
&& has_pid
)
481 taskkill_message(STRING_MUTUAL_EXCLUSIVE
);
482 taskkill_message(STRING_USAGE
);
486 if (!add_to_task_list(argv
[i
+ 1]))
492 taskkill_message(STRING_INVALID_OPTION
);
493 taskkill_message(STRING_USAGE
);
500 taskkill_message(STRING_MISSING_OPTION
);
501 taskkill_message(STRING_USAGE
);
508 int wmain(int argc
, WCHAR
*argv
[])
512 if (!process_arguments(argc
, argv
))
514 HeapFree(GetProcessHeap(), 0, task_list
);
518 if (force_termination
)
519 status_code
= terminate_processes();
521 status_code
= send_close_messages();
523 HeapFree(GetProcessHeap(), 0, task_list
);