123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- /*
- * Task termination utility
- *
- * Copyright 2008 Andrew Riedi
- * Copyright 2010 Andrew Nguyen
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
- #include <stdlib.h>
- #include <windows.h>
- #include <psapi.h>
- #include <wine/debug.h>
- #include "taskkill.h"
- WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
- static BOOL force_termination = FALSE;
- static WCHAR **task_list;
- static unsigned int task_count;
- struct pid_close_info
- {
- DWORD pid;
- BOOL found;
- };
- static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
- {
- int wlen;
- DWORD count, ret;
- WCHAR msg_buffer[8192];
- wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
- ARRAY_SIZE(msg_buffer), &va_args);
- ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
- if (!ret)
- {
- DWORD len;
- char *msgA;
- /* On Windows WriteConsoleW() fails if the output is redirected. So fall
- * back to WriteFile(), assuming the console encoding is still the right
- * one in that case.
- */
- len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
- NULL, 0, NULL, NULL);
- msgA = HeapAlloc(GetProcessHeap(), 0, len);
- if (!msgA)
- return 0;
- WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
- NULL, NULL);
- WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
- HeapFree(GetProcessHeap(), 0, msgA);
- }
- return count;
- }
- static int WINAPIV taskkill_printfW(const WCHAR *msg, ...)
- {
- va_list va_args;
- int len;
- va_start(va_args, msg);
- len = taskkill_vprintfW(msg, va_args);
- va_end(va_args);
- return len;
- }
- static int WINAPIV taskkill_message_printfW(int msg, ...)
- {
- va_list va_args;
- WCHAR msg_buffer[8192];
- int len;
- LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
- va_start(va_args, msg);
- len = taskkill_vprintfW(msg_buffer, va_args);
- va_end(va_args);
- return len;
- }
- static int taskkill_message(int msg)
- {
- WCHAR msg_buffer[8192];
- LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
- return taskkill_printfW(L"%1", msg_buffer);
- }
- /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
- static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
- {
- struct pid_close_info *info = (struct pid_close_info *)lParam;
- DWORD hwnd_pid;
- GetWindowThreadProcessId(hwnd, &hwnd_pid);
- if (hwnd_pid == info->pid)
- {
- PostMessageW(hwnd, WM_CLOSE, 0, 0);
- info->found = TRUE;
- }
- return TRUE;
- }
- static DWORD *enumerate_processes(DWORD *list_count)
- {
- DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
- pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
- if (!pid_list)
- return NULL;
- for (;;)
- {
- DWORD *realloc_list;
- if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
- {
- HeapFree(GetProcessHeap(), 0, pid_list);
- return NULL;
- }
- /* EnumProcesses can't signal an insufficient buffer condition, so the
- * only way to possibly determine whether a larger buffer is required
- * is to see whether the written number of bytes is the same as the
- * buffer size. If so, the buffer will be reallocated to twice the
- * size. */
- if (alloc_bytes != needed_bytes)
- break;
- alloc_bytes *= 2;
- realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
- if (!realloc_list)
- {
- HeapFree(GetProcessHeap(), 0, pid_list);
- return NULL;
- }
- pid_list = realloc_list;
- }
- *list_count = needed_bytes / sizeof(*pid_list);
- return pid_list;
- }
- static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
- {
- HANDLE process;
- HMODULE module;
- DWORD required_size;
- process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
- if (!process)
- return FALSE;
- if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
- {
- CloseHandle(process);
- return FALSE;
- }
- if (!GetModuleBaseNameW(process, module, buf, chars))
- {
- CloseHandle(process);
- return FALSE;
- }
- CloseHandle(process);
- return TRUE;
- }
- /* The implemented task enumeration and termination behavior does not
- * exactly match native behavior. On Windows:
- *
- * In the case of terminating by process name, specifying a particular
- * process name more times than the number of running instances causes
- * all instances to be terminated, but termination failure messages to
- * be printed as many times as the difference between the specification
- * quantity and the number of running instances.
- *
- * Successful terminations are all listed first in order, with failing
- * terminations being listed at the end.
- *
- * A PID of zero causes taskkill to warn about the inability to terminate
- * system processes. */
- static int send_close_messages(void)
- {
- DWORD *pid_list, pid_list_size;
- DWORD self_pid = GetCurrentProcessId();
- unsigned int i;
- int status_code = 0;
- pid_list = enumerate_processes(&pid_list_size);
- if (!pid_list)
- {
- taskkill_message(STRING_ENUM_FAILED);
- return 1;
- }
- for (i = 0; i < task_count; i++)
- {
- WCHAR *p = task_list[i];
- BOOL is_numeric = TRUE;
- /* Determine whether the string is not numeric. */
- while (*p)
- {
- if (!iswdigit(*p++))
- {
- is_numeric = FALSE;
- break;
- }
- }
- if (is_numeric)
- {
- DWORD pid = wcstol(task_list[i], NULL, 10);
- struct pid_close_info info = { pid };
- if (pid == self_pid)
- {
- taskkill_message(STRING_SELF_TERMINATION);
- status_code = 1;
- continue;
- }
- EnumWindows(pid_enum_proc, (LPARAM)&info);
- if (info.found)
- taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
- else
- {
- taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
- status_code = 128;
- }
- }
- else
- {
- DWORD index;
- BOOL found_process = FALSE;
- for (index = 0; index < pid_list_size; index++)
- {
- WCHAR process_name[MAX_PATH];
- if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
- !wcsicmp(process_name, task_list[i]))
- {
- struct pid_close_info info = { pid_list[index] };
- found_process = TRUE;
- if (pid_list[index] == self_pid)
- {
- taskkill_message(STRING_SELF_TERMINATION);
- status_code = 1;
- continue;
- }
- EnumWindows(pid_enum_proc, (LPARAM)&info);
- taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
- }
- }
- if (!found_process)
- {
- taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
- status_code = 128;
- }
- }
- }
- HeapFree(GetProcessHeap(), 0, pid_list);
- return status_code;
- }
- static int terminate_processes(void)
- {
- DWORD *pid_list, pid_list_size;
- DWORD self_pid = GetCurrentProcessId();
- unsigned int i;
- int status_code = 0;
- pid_list = enumerate_processes(&pid_list_size);
- if (!pid_list)
- {
- taskkill_message(STRING_ENUM_FAILED);
- return 1;
- }
- for (i = 0; i < task_count; i++)
- {
- WCHAR *p = task_list[i];
- BOOL is_numeric = TRUE;
- /* Determine whether the string is not numeric. */
- while (*p)
- {
- if (!iswdigit(*p++))
- {
- is_numeric = FALSE;
- break;
- }
- }
- if (is_numeric)
- {
- DWORD pid = wcstol(task_list[i], NULL, 10);
- HANDLE process;
- if (pid == self_pid)
- {
- taskkill_message(STRING_SELF_TERMINATION);
- status_code = 1;
- continue;
- }
- process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
- if (!process)
- {
- taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
- status_code = 128;
- continue;
- }
- if (!TerminateProcess(process, 0))
- {
- taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
- status_code = 1;
- CloseHandle(process);
- continue;
- }
- taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
- CloseHandle(process);
- }
- else
- {
- DWORD index;
- BOOL found_process = FALSE;
- for (index = 0; index < pid_list_size; index++)
- {
- WCHAR process_name[MAX_PATH];
- if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
- !wcsicmp(process_name, task_list[i]))
- {
- HANDLE process;
- if (pid_list[index] == self_pid)
- {
- taskkill_message(STRING_SELF_TERMINATION);
- status_code = 1;
- continue;
- }
- process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
- if (!process)
- {
- taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
- status_code = 128;
- continue;
- }
- if (!TerminateProcess(process, 0))
- {
- taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
- status_code = 1;
- CloseHandle(process);
- continue;
- }
- found_process = TRUE;
- taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
- CloseHandle(process);
- }
- }
- if (!found_process)
- {
- taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
- status_code = 128;
- }
- }
- }
- HeapFree(GetProcessHeap(), 0, pid_list);
- return status_code;
- }
- static BOOL add_to_task_list(WCHAR *name)
- {
- static unsigned int list_size = 16;
- if (!task_list)
- {
- task_list = HeapAlloc(GetProcessHeap(), 0,
- list_size * sizeof(*task_list));
- if (!task_list)
- return FALSE;
- }
- else if (task_count == list_size)
- {
- void *realloc_list;
- list_size *= 2;
- realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
- list_size * sizeof(*task_list));
- if (!realloc_list)
- return FALSE;
- task_list = realloc_list;
- }
- task_list[task_count++] = name;
- return TRUE;
- }
- /* FIXME Argument processing does not match behavior observed on Windows.
- * Stringent argument counting and processing is performed, and unrecognized
- * options are detected as parameters when placed after options that accept one. */
- static BOOL process_arguments(int argc, WCHAR *argv[])
- {
- if (argc > 1)
- {
- int i;
- WCHAR *argdata;
- BOOL has_im = FALSE, has_pid = FALSE;
- /* Only the lone help option is recognized. */
- if (argc == 2)
- {
- argdata = argv[1];
- if ((*argdata == '/' || *argdata == '-') && !lstrcmpW(L"?", argdata + 1))
- {
- taskkill_message(STRING_USAGE);
- exit(0);
- }
- }
- for (i = 1; i < argc; i++)
- {
- BOOL got_im = FALSE, got_pid = FALSE;
- argdata = argv[i];
- if (*argdata != '/' && *argdata != '-')
- goto invalid;
- argdata++;
- if (!wcsicmp(L"t", argdata))
- WINE_FIXME("argument T not supported\n");
- if (!wcsicmp(L"f", argdata))
- force_termination = TRUE;
- /* Options /IM and /PID appear to behave identically, except for
- * the fact that they cannot be specified at the same time. */
- else if ((got_im = !wcsicmp(L"im", argdata)) ||
- (got_pid = !wcsicmp(L"pid", argdata)))
- {
- if (!argv[i + 1])
- {
- taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
- taskkill_message(STRING_USAGE);
- return FALSE;
- }
- if (got_im) has_im = TRUE;
- if (got_pid) has_pid = TRUE;
- if (has_im && has_pid)
- {
- taskkill_message(STRING_MUTUAL_EXCLUSIVE);
- taskkill_message(STRING_USAGE);
- return FALSE;
- }
- if (!add_to_task_list(argv[i + 1]))
- return FALSE;
- i++;
- }
- else
- {
- invalid:
- taskkill_message(STRING_INVALID_OPTION);
- taskkill_message(STRING_USAGE);
- return FALSE;
- }
- }
- }
- else
- {
- taskkill_message(STRING_MISSING_OPTION);
- taskkill_message(STRING_USAGE);
- return FALSE;
- }
- return TRUE;
- }
- int __cdecl wmain(int argc, WCHAR *argv[])
- {
- int status_code = 0;
- if (!process_arguments(argc, argv))
- {
- HeapFree(GetProcessHeap(), 0, task_list);
- return 1;
- }
- if (force_termination)
- status_code = terminate_processes();
- else
- status_code = send_close_messages();
- HeapFree(GetProcessHeap(), 0, task_list);
- return status_code;
- }
|