2010-09-21 07:17:09 +00:00
|
|
|
/*
|
|
|
|
* Task termination utility
|
|
|
|
*
|
2010-09-21 07:17:26 +00:00
|
|
|
* Copyright 2008 Andrew Riedi
|
2010-09-21 07:17:09 +00:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-01-23 11:16:36 +00:00
|
|
|
#include <stdlib.h>
|
2010-09-21 07:17:09 +00:00
|
|
|
#include <windows.h>
|
2023-03-28 20:32:31 +00:00
|
|
|
#include <tlhelp32.h>
|
2012-05-18 08:48:01 +00:00
|
|
|
#include <wine/debug.h>
|
2010-09-21 07:17:09 +00:00
|
|
|
|
2010-09-21 07:17:26 +00:00
|
|
|
#include "taskkill.h"
|
|
|
|
|
2012-05-18 08:48:01 +00:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
|
|
|
|
|
2013-10-01 21:33:54 +00:00
|
|
|
static BOOL force_termination = FALSE;
|
2023-03-28 14:19:45 +00:00
|
|
|
static BOOL kill_child_processes;
|
2010-09-21 07:17:09 +00:00
|
|
|
|
2010-10-01 11:38:41 +00:00
|
|
|
static WCHAR **task_list;
|
|
|
|
static unsigned int task_count;
|
2010-09-21 07:17:26 +00:00
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
PROCESSENTRY32W p;
|
|
|
|
BOOL matched;
|
2023-03-28 21:14:17 +00:00
|
|
|
BOOL is_numeric;
|
2023-03-28 20:32:31 +00:00
|
|
|
}
|
|
|
|
*process_list;
|
|
|
|
static unsigned int process_count;
|
2023-03-28 16:09:11 +00:00
|
|
|
|
2010-09-21 07:18:41 +00:00
|
|
|
struct pid_close_info
|
|
|
|
{
|
|
|
|
DWORD pid;
|
|
|
|
BOOL found;
|
|
|
|
};
|
|
|
|
|
2021-10-25 09:02:04 +00:00
|
|
|
static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
|
2010-09-21 07:17:26 +00:00
|
|
|
{
|
|
|
|
int wlen;
|
2022-04-28 09:22:40 +00:00
|
|
|
DWORD count;
|
2010-09-21 07:17:26 +00:00
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
|
2011-11-19 14:10:07 +00:00
|
|
|
wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
|
2018-07-23 22:01:28 +00:00
|
|
|
ARRAY_SIZE(msg_buffer), &va_args);
|
2010-09-21 07:17:26 +00:00
|
|
|
|
2022-04-28 09:22:40 +00:00
|
|
|
if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL))
|
2010-09-21 07:17:26 +00:00
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *msgA;
|
|
|
|
|
2011-08-24 12:29:31 +00:00
|
|
|
/* On Windows WriteConsoleW() fails if the output is redirected. So fall
|
2022-04-28 09:22:40 +00:00
|
|
|
* back to WriteFile() using OEM code page.
|
2011-08-24 12:29:31 +00:00
|
|
|
*/
|
2022-04-28 09:22:40 +00:00
|
|
|
len = WideCharToMultiByte(GetOEMCP(), 0, msg_buffer, wlen,
|
2010-09-21 07:17:26 +00:00
|
|
|
NULL, 0, NULL, NULL);
|
2023-03-28 15:34:12 +00:00
|
|
|
msgA = malloc(len);
|
2010-09-21 07:17:26 +00:00
|
|
|
if (!msgA)
|
|
|
|
return 0;
|
|
|
|
|
2022-04-28 09:22:40 +00:00
|
|
|
WideCharToMultiByte(GetOEMCP(), 0, msg_buffer, wlen, msgA, len, NULL, NULL);
|
2010-09-21 07:17:26 +00:00
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
|
2023-03-28 15:34:12 +00:00
|
|
|
free(msgA);
|
2010-09-21 07:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-11-02 09:22:10 +00:00
|
|
|
static int WINAPIV taskkill_printfW(const WCHAR *msg, ...)
|
2010-09-21 07:17:26 +00:00
|
|
|
{
|
2021-10-25 09:02:04 +00:00
|
|
|
va_list va_args;
|
2010-09-21 07:17:26 +00:00
|
|
|
int len;
|
|
|
|
|
2021-10-25 09:02:04 +00:00
|
|
|
va_start(va_args, msg);
|
2010-09-21 07:17:26 +00:00
|
|
|
len = taskkill_vprintfW(msg, va_args);
|
2021-10-25 09:02:04 +00:00
|
|
|
va_end(va_args);
|
2010-09-21 07:17:26 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-11-02 09:22:10 +00:00
|
|
|
static int WINAPIV taskkill_message_printfW(int msg, ...)
|
2010-09-21 07:17:26 +00:00
|
|
|
{
|
2021-10-25 09:02:04 +00:00
|
|
|
va_list va_args;
|
2010-09-21 07:17:26 +00:00
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
int len;
|
|
|
|
|
2018-07-23 22:01:28 +00:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
|
2010-09-21 07:17:26 +00:00
|
|
|
|
2021-10-25 09:02:04 +00:00
|
|
|
va_start(va_args, msg);
|
2010-09-21 07:17:26 +00:00
|
|
|
len = taskkill_vprintfW(msg_buffer, va_args);
|
2021-10-25 09:02:04 +00:00
|
|
|
va_end(va_args);
|
2010-09-21 07:17:26 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int taskkill_message(int msg)
|
|
|
|
{
|
|
|
|
WCHAR msg_buffer[8192];
|
|
|
|
|
2018-07-23 22:01:28 +00:00
|
|
|
LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
|
2010-09-21 07:17:26 +00:00
|
|
|
|
2020-10-06 22:51:53 +00:00
|
|
|
return taskkill_printfW(L"%1", msg_buffer);
|
2010-09-21 07:17:26 +00:00
|
|
|
}
|
|
|
|
|
2010-09-21 07:18:41 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2023-03-28 16:09:11 +00:00
|
|
|
static BOOL enumerate_processes(void)
|
2010-09-21 07:20:58 +00:00
|
|
|
{
|
2023-03-28 20:32:31 +00:00
|
|
|
unsigned int alloc_count = 128;
|
|
|
|
void *realloc_list;
|
|
|
|
HANDLE snapshot;
|
2010-09-21 07:20:58 +00:00
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (snapshot == INVALID_HANDLE_VALUE)
|
2023-03-28 16:09:11 +00:00
|
|
|
return FALSE;
|
2010-09-21 07:20:58 +00:00
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
process_list = malloc(alloc_count * sizeof(*process_list));
|
|
|
|
if (!process_list)
|
2010-09-21 07:20:58 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
process_list[0].p.dwSize = sizeof(process_list[0].p);
|
|
|
|
if (!Process32FirstW(snapshot, &process_list[0].p))
|
2010-09-21 07:20:58 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
do
|
2010-09-21 07:20:58 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
process_list[process_count].is_numeric = FALSE;
|
2023-03-28 20:32:31 +00:00
|
|
|
process_list[process_count++].matched = FALSE;
|
|
|
|
if (process_count == alloc_count)
|
|
|
|
{
|
|
|
|
alloc_count *= 2;
|
|
|
|
realloc_list = realloc(process_list, alloc_count * sizeof(*process_list));
|
|
|
|
if (!realloc_list)
|
|
|
|
return FALSE;
|
|
|
|
process_list = realloc_list;
|
|
|
|
}
|
|
|
|
process_list[process_count].p.dwSize = sizeof(process_list[process_count].p);
|
|
|
|
} while (Process32NextW(snapshot, &process_list[process_count].p));
|
|
|
|
CloseHandle(snapshot);
|
2010-09-21 07:20:58 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2023-03-28 21:14:17 +00:00
|
|
|
static void mark_task_process(const WCHAR *str, int *status_code)
|
2023-03-28 19:21:13 +00:00
|
|
|
{
|
|
|
|
DWORD self_pid = GetCurrentProcessId();
|
|
|
|
const WCHAR *p = str;
|
2023-03-28 21:14:17 +00:00
|
|
|
BOOL is_numeric;
|
2023-03-28 19:21:13 +00:00
|
|
|
unsigned int i;
|
2023-03-28 21:14:17 +00:00
|
|
|
DWORD pid;
|
2023-03-28 19:21:13 +00:00
|
|
|
|
2023-03-28 21:14:17 +00:00
|
|
|
is_numeric = TRUE;
|
2023-03-28 19:21:13 +00:00
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (!iswdigit(*p++))
|
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
is_numeric = FALSE;
|
2023-03-28 19:21:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 21:14:17 +00:00
|
|
|
if (is_numeric)
|
2023-03-28 19:21:13 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
pid = wcstol(str, NULL, 10);
|
2023-03-28 20:32:31 +00:00
|
|
|
for (i = 0; i < process_count; ++i)
|
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
if (process_list[i].p.th32ProcessID == pid)
|
2023-03-28 20:32:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == process_count || process_list[i].matched)
|
|
|
|
goto not_found;
|
|
|
|
process_list[i].matched = TRUE;
|
2023-03-28 21:14:17 +00:00
|
|
|
process_list[i].is_numeric = TRUE;
|
|
|
|
if (pid == self_pid)
|
2023-03-28 19:21:13 +00:00
|
|
|
{
|
|
|
|
taskkill_message(STRING_SELF_TERMINATION);
|
|
|
|
*status_code = 1;
|
|
|
|
}
|
2023-03-28 21:14:17 +00:00
|
|
|
return;
|
2023-03-28 19:21:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
for (i = 0; i < process_count; ++i)
|
2023-03-28 19:21:13 +00:00
|
|
|
{
|
2023-03-28 20:32:31 +00:00
|
|
|
if (!wcsicmp(process_list[i].p.szExeFile, str) && !process_list[i].matched)
|
2023-03-28 19:21:13 +00:00
|
|
|
{
|
2023-03-28 20:32:31 +00:00
|
|
|
process_list[i].matched = TRUE;
|
|
|
|
if (process_list[i].p.th32ProcessID == self_pid)
|
2023-03-28 19:21:13 +00:00
|
|
|
{
|
|
|
|
taskkill_message(STRING_SELF_TERMINATION);
|
|
|
|
*status_code = 1;
|
|
|
|
}
|
2023-03-28 21:14:17 +00:00
|
|
|
return;
|
2023-03-28 19:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:32:31 +00:00
|
|
|
not_found:
|
2023-03-28 19:21:13 +00:00
|
|
|
taskkill_message_printfW(STRING_SEARCH_FAILED, str);
|
|
|
|
*status_code = 128;
|
2023-03-28 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void taskkill_message_print_process(int msg, unsigned int index)
|
|
|
|
{
|
|
|
|
WCHAR pid_str[16];
|
|
|
|
|
|
|
|
if (!process_list[index].is_numeric)
|
|
|
|
{
|
|
|
|
taskkill_message_printfW(msg, process_list[index].p.szExeFile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wsprintfW(pid_str, L"%lu", process_list[index].p.th32ProcessID);
|
|
|
|
taskkill_message_printfW(msg, pid_str);
|
2023-03-28 19:21:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 14:19:45 +00:00
|
|
|
static BOOL find_parent(unsigned int process_index, unsigned int *parent_index)
|
|
|
|
{
|
|
|
|
DWORD parent_id = process_list[process_index].p.th32ParentProcessID;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!parent_id)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < process_count; ++i)
|
|
|
|
{
|
|
|
|
if (process_list[i].p.th32ProcessID == parent_id)
|
|
|
|
{
|
|
|
|
*parent_index = i;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mark_child_processes(void)
|
|
|
|
{
|
|
|
|
unsigned int i, parent;
|
|
|
|
|
|
|
|
for (i = 0; i < process_count; ++i)
|
|
|
|
{
|
|
|
|
if (process_list[i].matched)
|
|
|
|
continue;
|
|
|
|
parent = i;
|
|
|
|
while (find_parent(parent, &parent))
|
|
|
|
{
|
|
|
|
if (process_list[parent].matched)
|
|
|
|
{
|
|
|
|
WINE_TRACE("Adding child %04lx.\n", process_list[i].p.th32ProcessID);
|
|
|
|
process_list[i].matched = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-21 07:18:41 +00:00
|
|
|
/* 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)
|
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
const WCHAR *process_name;
|
2023-03-28 19:21:13 +00:00
|
|
|
struct pid_close_info info;
|
2010-09-21 07:18:41 +00:00
|
|
|
unsigned int i;
|
|
|
|
int status_code = 0;
|
|
|
|
|
2023-03-28 14:19:45 +00:00
|
|
|
for (i = 0; i < process_count; i++)
|
2010-09-21 07:18:41 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
if (!process_list[i].matched)
|
2023-03-28 19:21:13 +00:00
|
|
|
continue;
|
2023-03-28 21:14:17 +00:00
|
|
|
info.pid = process_list[i].p.th32ProcessID;
|
|
|
|
process_name = process_list[i].p.szExeFile;
|
2023-03-28 19:21:13 +00:00
|
|
|
info.found = FALSE;
|
2023-03-28 14:19:45 +00:00
|
|
|
WINE_TRACE("Terminating pid %04lx.\n", info.pid);
|
2023-03-28 19:21:13 +00:00
|
|
|
EnumWindows(pid_enum_proc, (LPARAM)&info);
|
|
|
|
if (info.found)
|
2010-09-21 07:18:41 +00:00
|
|
|
{
|
2023-03-28 14:19:45 +00:00
|
|
|
if (kill_child_processes)
|
|
|
|
taskkill_message_printfW(STRING_CLOSE_CHILD, info.pid, process_list[i].p.th32ParentProcessID);
|
|
|
|
else if (process_list[i].is_numeric)
|
2023-03-28 19:21:13 +00:00
|
|
|
taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, info.pid);
|
2010-09-21 07:18:41 +00:00
|
|
|
else
|
2023-03-28 19:21:13 +00:00
|
|
|
taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, info.pid);
|
|
|
|
continue;
|
2010-09-21 07:20:58 +00:00
|
|
|
}
|
2023-03-28 21:14:17 +00:00
|
|
|
taskkill_message_print_process(STRING_SEARCH_FAILED, i);
|
2023-03-28 19:21:13 +00:00
|
|
|
status_code = 128;
|
2010-09-21 07:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status_code;
|
|
|
|
}
|
|
|
|
|
2010-09-21 07:21:14 +00:00
|
|
|
static int terminate_processes(void)
|
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
const WCHAR *process_name;
|
2010-09-21 07:21:14 +00:00
|
|
|
unsigned int i;
|
|
|
|
int status_code = 0;
|
2023-03-28 19:21:13 +00:00
|
|
|
HANDLE process;
|
|
|
|
DWORD pid;
|
2010-09-21 07:21:14 +00:00
|
|
|
|
2023-03-28 21:14:17 +00:00
|
|
|
for (i = 0; i < process_count; i++)
|
2010-09-21 07:21:14 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
if (!process_list[i].matched)
|
2023-03-28 19:21:13 +00:00
|
|
|
continue;
|
2010-09-21 07:21:14 +00:00
|
|
|
|
2023-03-28 21:14:17 +00:00
|
|
|
pid = process_list[i].p.th32ProcessID;
|
|
|
|
process_name = process_list[i].p.szExeFile;
|
2023-03-28 19:21:13 +00:00
|
|
|
process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
|
|
|
if (!process)
|
2010-09-21 07:21:14 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
taskkill_message_print_process(STRING_SEARCH_FAILED, i);
|
2023-03-28 19:21:13 +00:00
|
|
|
status_code = 128;
|
|
|
|
continue;
|
2010-09-21 07:21:14 +00:00
|
|
|
}
|
2023-03-28 19:21:13 +00:00
|
|
|
if (!TerminateProcess(process, 1))
|
2010-09-21 07:21:14 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
taskkill_message_print_process(STRING_TERMINATE_FAILED, i);
|
2023-03-28 19:21:13 +00:00
|
|
|
status_code = 1;
|
2010-09-21 07:21:14 +00:00
|
|
|
CloseHandle(process);
|
2023-03-28 19:21:13 +00:00
|
|
|
continue;
|
2010-09-21 07:21:14 +00:00
|
|
|
}
|
2023-03-28 14:19:45 +00:00
|
|
|
if (kill_child_processes)
|
|
|
|
taskkill_message_printfW(STRING_TERM_CHILD, pid, process_list[i].p.th32ParentProcessID);
|
|
|
|
else if (process_list[i].is_numeric)
|
2023-03-28 19:21:13 +00:00
|
|
|
taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
|
2010-09-21 07:21:14 +00:00
|
|
|
else
|
2023-03-28 21:14:17 +00:00
|
|
|
taskkill_message_printfW(STRING_TERM_PROC_SEARCH, process_name, pid);
|
2023-03-28 19:21:13 +00:00
|
|
|
CloseHandle(process);
|
2010-09-21 07:21:14 +00:00
|
|
|
}
|
|
|
|
return status_code;
|
|
|
|
}
|
|
|
|
|
2010-09-21 07:17:26 +00:00
|
|
|
static BOOL add_to_task_list(WCHAR *name)
|
|
|
|
{
|
|
|
|
static unsigned int list_size = 16;
|
|
|
|
|
|
|
|
if (!task_list)
|
|
|
|
{
|
2023-03-28 15:34:12 +00:00
|
|
|
task_list = malloc(list_size * sizeof(*task_list));
|
2010-09-21 07:17:26 +00:00
|
|
|
if (!task_list)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (task_count == list_size)
|
|
|
|
{
|
|
|
|
void *realloc_list;
|
|
|
|
|
|
|
|
list_size *= 2;
|
2023-03-28 15:34:12 +00:00
|
|
|
realloc_list = realloc(task_list, list_size * sizeof(*task_list));
|
2010-09-21 07:17:26 +00:00
|
|
|
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;
|
2014-01-21 23:24:54 +00:00
|
|
|
WCHAR *argdata;
|
2013-10-01 21:33:54 +00:00
|
|
|
BOOL has_im = FALSE, has_pid = FALSE;
|
2010-09-21 07:17:26 +00:00
|
|
|
|
|
|
|
/* Only the lone help option is recognized. */
|
2014-01-21 23:24:54 +00:00
|
|
|
if (argc == 2)
|
2010-09-21 07:17:26 +00:00
|
|
|
{
|
2014-01-21 23:24:54 +00:00
|
|
|
argdata = argv[1];
|
2020-10-06 22:51:53 +00:00
|
|
|
if ((*argdata == '/' || *argdata == '-') && !lstrcmpW(L"?", argdata + 1))
|
2014-01-21 23:24:54 +00:00
|
|
|
{
|
|
|
|
taskkill_message(STRING_USAGE);
|
|
|
|
exit(0);
|
|
|
|
}
|
2010-09-21 07:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
2014-02-22 11:22:34 +00:00
|
|
|
BOOL got_im = FALSE, got_pid = FALSE;
|
2010-09-21 07:17:26 +00:00
|
|
|
|
2014-01-21 23:24:54 +00:00
|
|
|
argdata = argv[i];
|
|
|
|
if (*argdata != '/' && *argdata != '-')
|
|
|
|
goto invalid;
|
|
|
|
argdata++;
|
|
|
|
|
2020-10-06 22:51:53 +00:00
|
|
|
if (!wcsicmp(L"t", argdata))
|
2023-03-28 14:19:45 +00:00
|
|
|
kill_child_processes = TRUE;
|
|
|
|
else if (!wcsicmp(L"f", argdata))
|
2013-10-01 21:33:54 +00:00
|
|
|
force_termination = TRUE;
|
2010-09-21 07:17:26 +00:00
|
|
|
/* Options /IM and /PID appear to behave identically, except for
|
|
|
|
* the fact that they cannot be specified at the same time. */
|
2020-10-06 22:51:53 +00:00
|
|
|
else if ((got_im = !wcsicmp(L"im", argdata)) ||
|
|
|
|
(got_pid = !wcsicmp(L"pid", argdata)))
|
2010-09-21 07:17:26 +00:00
|
|
|
{
|
|
|
|
if (!argv[i + 1])
|
|
|
|
{
|
|
|
|
taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
|
|
|
|
taskkill_message(STRING_USAGE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-01 21:33:54 +00:00
|
|
|
if (got_im) has_im = TRUE;
|
|
|
|
if (got_pid) has_pid = TRUE;
|
2010-09-21 07:17:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2014-01-21 23:24:54 +00:00
|
|
|
invalid:
|
2010-09-21 07:17:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:05:47 +00:00
|
|
|
int __cdecl wmain(int argc, WCHAR *argv[])
|
2010-09-21 07:17:09 +00:00
|
|
|
{
|
2023-03-28 21:14:17 +00:00
|
|
|
int search_status = 0, terminate_status;
|
|
|
|
unsigned int i;
|
|
|
|
|
2010-09-21 07:17:26 +00:00
|
|
|
if (!process_arguments(argc, argv))
|
|
|
|
return 1;
|
|
|
|
|
2023-03-28 16:09:11 +00:00
|
|
|
if (!enumerate_processes())
|
|
|
|
{
|
|
|
|
taskkill_message(STRING_ENUM_FAILED);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-28 21:14:17 +00:00
|
|
|
for (i = 0; i < task_count; ++i)
|
|
|
|
mark_task_process(task_list[i], &search_status);
|
2023-03-28 14:19:45 +00:00
|
|
|
if (kill_child_processes)
|
|
|
|
mark_child_processes();
|
2010-09-21 07:18:41 +00:00
|
|
|
if (force_termination)
|
2023-03-28 21:14:17 +00:00
|
|
|
terminate_status = terminate_processes();
|
|
|
|
else
|
|
|
|
terminate_status = send_close_messages();
|
|
|
|
return search_status ? search_status : terminate_status;
|
2010-09-21 07:17:09 +00:00
|
|
|
}
|