taskkill: Implement graceful termination by process name.

This commit is contained in:
Andrew Nguyen 2010-09-21 02:20:58 -05:00 committed by Alexandre Julliard
parent df4359b117
commit 1ef5affbf3
4 changed files with 107 additions and 3 deletions

View File

@ -32,5 +32,7 @@ STRINGTABLE
STRING_MISSING_PARAM, "Error: Option %s expects a command line parameter.\n"
STRING_MUTUAL_EXCLUSIVE, "Error: Options /im and /pid are mutually exclusive.\n"
STRING_CLOSE_PID_SEARCH, "Close message sent to top-level windows of process with PID %u.\n"
STRING_CLOSE_PROC_SRCH, "Close message sent to top-level windows of process \"%s\" with PID %u.\n"
STRING_SEARCH_FAILED, "Error: Could not find process \"%s\".\n"
STRING_ENUM_FAILED, "Error: Unable to enumerate the process list.\n"
}

View File

@ -1,7 +1,7 @@
EXTRADEFS = -DWINE_NO_UNICODE_MACROS
MODULE = taskkill.exe
APPMODE = -mconsole -municode
IMPORTS = user32
IMPORTS = psapi user32
C_SRCS = taskkill.c

View File

@ -20,6 +20,7 @@
*/
#include <windows.h>
#include <psapi.h>
#include <wine/debug.h>
#include <wine/unicode.h>
@ -123,6 +124,72 @@ static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
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:
*
@ -139,9 +206,17 @@ static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
* system processes. */
static int send_close_messages(void)
{
DWORD *pid_list, pid_list_size;
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];
@ -172,9 +247,34 @@ static int send_close_messages(void)
}
}
else
WINE_FIXME("Termination by name is not implemented\n");
{
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) &&
!strcmpiW(process_name, task_list[i]))
{
struct pid_close_info info = { pid_list[index] };
found_process = TRUE;
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;
}

View File

@ -28,4 +28,6 @@
#define STRING_MISSING_PARAM 105
#define STRING_MUTUAL_EXCLUSIVE 106
#define STRING_CLOSE_PID_SEARCH 107
#define STRING_SEARCH_FAILED 108
#define STRING_CLOSE_PROC_SRCH 108
#define STRING_SEARCH_FAILED 109
#define STRING_ENUM_FAILED 110