winegstreamer: Leave async reader callback_cs on calloc error.
[wine.git] / programs / taskkill / taskkill.c
blob2904e64f40a661573796fd3cdb11bc784d685096
1 /*
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
22 #include <stdlib.h>
23 #include <windows.h>
24 #include <psapi.h>
25 #include <wine/debug.h>
27 #include "taskkill.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
31 static BOOL force_termination = FALSE;
33 static WCHAR **task_list;
34 static unsigned int task_count;
36 struct pid_close_info
38 DWORD pid;
39 BOOL found;
42 static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
44 int wlen;
45 DWORD count;
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))
53 DWORD len;
54 char *msgA;
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,
60 NULL, 0, NULL, NULL);
61 msgA = HeapAlloc(GetProcessHeap(), 0, len);
62 if (!msgA)
63 return 0;
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);
70 return count;
73 static int WINAPIV taskkill_printfW(const WCHAR *msg, ...)
75 va_list va_args;
76 int len;
78 va_start(va_args, msg);
79 len = taskkill_vprintfW(msg, va_args);
80 va_end(va_args);
82 return len;
85 static int WINAPIV taskkill_message_printfW(int msg, ...)
87 va_list va_args;
88 WCHAR msg_buffer[8192];
89 int len;
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);
95 va_end(va_args);
97 return len;
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;
113 DWORD hwnd_pid;
115 GetWindowThreadProcessId(hwnd, &hwnd_pid);
117 if (hwnd_pid == info->pid)
119 PostMessageW(hwnd, WM_CLOSE, 0, 0);
120 info->found = TRUE;
123 return TRUE;
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);
131 if (!pid_list)
132 return NULL;
134 for (;;)
136 DWORD *realloc_list;
138 if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
140 HeapFree(GetProcessHeap(), 0, pid_list);
141 return NULL;
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
148 * size. */
149 if (alloc_bytes != needed_bytes)
150 break;
152 alloc_bytes *= 2;
153 realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
154 if (!realloc_list)
156 HeapFree(GetProcessHeap(), 0, pid_list);
157 return NULL;
159 pid_list = realloc_list;
162 *list_count = needed_bytes / sizeof(*pid_list);
163 return pid_list;
166 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
168 HANDLE process;
169 HMODULE module;
170 DWORD required_size;
172 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
173 if (!process)
174 return FALSE;
176 if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
178 CloseHandle(process);
179 return FALSE;
182 if (!GetModuleBaseNameW(process, module, buf, chars))
184 CloseHandle(process);
185 return FALSE;
188 CloseHandle(process);
189 return TRUE;
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();
210 unsigned int i;
211 int status_code = 0;
213 pid_list = enumerate_processes(&pid_list_size);
214 if (!pid_list)
216 taskkill_message(STRING_ENUM_FAILED);
217 return 1;
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. */
226 while (*p)
228 if (!iswdigit(*p++))
230 is_numeric = FALSE;
231 break;
235 if (is_numeric)
237 DWORD pid = wcstol(task_list[i], NULL, 10);
238 struct pid_close_info info = { pid };
240 if (pid == self_pid)
242 taskkill_message(STRING_SELF_TERMINATION);
243 status_code = 1;
244 continue;
247 EnumWindows(pid_enum_proc, (LPARAM)&info);
248 if (info.found)
249 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
250 else
252 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
253 status_code = 128;
256 else
258 DWORD index;
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);
274 status_code = 1;
275 continue;
278 EnumWindows(pid_enum_proc, (LPARAM)&info);
279 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
283 if (!found_process)
285 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
286 status_code = 128;
291 HeapFree(GetProcessHeap(), 0, pid_list);
292 return status_code;
295 static int terminate_processes(void)
297 DWORD *pid_list, pid_list_size;
298 DWORD self_pid = GetCurrentProcessId();
299 unsigned int i;
300 int status_code = 0;
302 pid_list = enumerate_processes(&pid_list_size);
303 if (!pid_list)
305 taskkill_message(STRING_ENUM_FAILED);
306 return 1;
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. */
315 while (*p)
317 if (!iswdigit(*p++))
319 is_numeric = FALSE;
320 break;
324 if (is_numeric)
326 DWORD pid = wcstol(task_list[i], NULL, 10);
327 HANDLE process;
329 if (pid == self_pid)
331 taskkill_message(STRING_SELF_TERMINATION);
332 status_code = 1;
333 continue;
336 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
337 if (!process)
339 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
340 status_code = 128;
341 continue;
344 if (!TerminateProcess(process, 0))
346 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
347 status_code = 1;
348 CloseHandle(process);
349 continue;
352 taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
353 CloseHandle(process);
355 else
357 DWORD index;
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]))
367 HANDLE process;
369 if (pid_list[index] == self_pid)
371 taskkill_message(STRING_SELF_TERMINATION);
372 status_code = 1;
373 continue;
376 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
377 if (!process)
379 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
380 status_code = 128;
381 continue;
384 if (!TerminateProcess(process, 0))
386 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
387 status_code = 1;
388 CloseHandle(process);
389 continue;
392 found_process = TRUE;
393 taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
394 CloseHandle(process);
398 if (!found_process)
400 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
401 status_code = 128;
406 HeapFree(GetProcessHeap(), 0, pid_list);
407 return status_code;
410 static BOOL add_to_task_list(WCHAR *name)
412 static unsigned int list_size = 16;
414 if (!task_list)
416 task_list = HeapAlloc(GetProcessHeap(), 0,
417 list_size * sizeof(*task_list));
418 if (!task_list)
419 return FALSE;
421 else if (task_count == list_size)
423 void *realloc_list;
425 list_size *= 2;
426 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
427 list_size * sizeof(*task_list));
428 if (!realloc_list)
429 return FALSE;
431 task_list = realloc_list;
434 task_list[task_count++] = name;
435 return TRUE;
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[])
443 if (argc > 1)
445 int i;
446 WCHAR *argdata;
447 BOOL has_im = FALSE, has_pid = FALSE;
449 /* Only the lone help option is recognized. */
450 if (argc == 2)
452 argdata = argv[1];
453 if ((*argdata == '/' || *argdata == '-') && !lstrcmpW(L"?", argdata + 1))
455 taskkill_message(STRING_USAGE);
456 exit(0);
460 for (i = 1; i < argc; i++)
462 BOOL got_im = FALSE, got_pid = FALSE;
464 argdata = argv[i];
465 if (*argdata != '/' && *argdata != '-')
466 goto invalid;
467 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)))
478 if (!argv[i + 1])
480 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
481 taskkill_message(STRING_USAGE);
482 return FALSE;
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);
492 return FALSE;
495 if (!add_to_task_list(argv[i + 1]))
496 return FALSE;
497 i++;
499 else
501 invalid:
502 taskkill_message(STRING_INVALID_OPTION);
503 taskkill_message(STRING_USAGE);
504 return FALSE;
508 else
510 taskkill_message(STRING_MISSING_OPTION);
511 taskkill_message(STRING_USAGE);
512 return FALSE;
515 return TRUE;
518 int __cdecl wmain(int argc, WCHAR *argv[])
520 int status_code = 0;
522 if (!process_arguments(argc, argv))
524 HeapFree(GetProcessHeap(), 0, task_list);
525 return 1;
528 if (force_termination)
529 status_code = terminate_processes();
530 else
531 status_code = send_close_messages();
533 HeapFree(GetProcessHeap(), 0, task_list);
534 return status_code;