taskkill: Implement graceful termination by process name.
[wine.git] / programs / taskkill / taskkill.c
blobf24883b2c7537af1e8cfbb0fbdc7bdf93d3e8b97
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 <windows.h>
23 #include <psapi.h>
24 #include <wine/debug.h>
25 #include <wine/unicode.h>
27 #include "taskkill.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
31 int force_termination;
33 WCHAR **task_list;
34 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, ret;
46 WCHAR msg_buffer[8192];
48 wlen = vsprintfW(msg_buffer, msg, va_args);
50 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
51 if (!ret)
53 DWORD len;
54 char *msgA;
56 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
57 NULL, 0, NULL, NULL);
58 msgA = HeapAlloc(GetProcessHeap(), 0, len);
59 if (!msgA)
60 return 0;
62 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
63 NULL, NULL);
64 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
65 HeapFree(GetProcessHeap(), 0, msgA);
68 return count;
71 static int taskkill_printfW(const WCHAR *msg, ...)
73 va_list va_args;
74 int len;
76 va_start(va_args, msg);
77 len = taskkill_vprintfW(msg, va_args);
78 va_end(va_args);
80 return len;
83 static int taskkill_message_printfW(int msg, ...)
85 va_list va_args;
86 WCHAR msg_buffer[8192];
87 int len;
89 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
90 sizeof(msg_buffer)/sizeof(WCHAR));
92 va_start(va_args, msg);
93 len = taskkill_vprintfW(msg_buffer, va_args);
94 va_end(va_args);
96 return len;
99 static int taskkill_message(int msg)
101 static const WCHAR formatW[] = {'%','s',0};
102 WCHAR msg_buffer[8192];
104 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
105 sizeof(msg_buffer)/sizeof(WCHAR));
107 return taskkill_printfW(formatW, msg_buffer);
110 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
111 static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
113 struct pid_close_info *info = (struct pid_close_info *)lParam;
114 DWORD hwnd_pid;
116 GetWindowThreadProcessId(hwnd, &hwnd_pid);
118 if (hwnd_pid == info->pid)
120 PostMessageW(hwnd, WM_CLOSE, 0, 0);
121 info->found = TRUE;
124 return TRUE;
127 static DWORD *enumerate_processes(DWORD *list_count)
129 DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
131 pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
132 if (!pid_list)
133 return NULL;
135 for (;;)
137 DWORD *realloc_list;
139 if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
141 HeapFree(GetProcessHeap(), 0, pid_list);
142 return NULL;
145 /* EnumProcesses can't signal an insufficient buffer condition, so the
146 * only way to possibly determine whether a larger buffer is required
147 * is to see whether the written number of bytes is the same as the
148 * buffer size. If so, the buffer will be reallocated to twice the
149 * size. */
150 if (alloc_bytes != needed_bytes)
151 break;
153 alloc_bytes *= 2;
154 realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
155 if (!realloc_list)
157 HeapFree(GetProcessHeap(), 0, pid_list);
158 return NULL;
160 pid_list = realloc_list;
163 *list_count = needed_bytes / sizeof(*pid_list);
164 return pid_list;
167 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
169 HANDLE process;
170 HMODULE module;
171 DWORD required_size;
173 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
174 if (!process)
175 return FALSE;
177 if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
179 CloseHandle(process);
180 return FALSE;
183 if (!GetModuleBaseNameW(process, module, buf, chars))
185 CloseHandle(process);
186 return FALSE;
189 CloseHandle(process);
190 return TRUE;
193 /* The implemented task enumeration and termination behavior does not
194 * exactly match native behavior. On Windows:
196 * In the case of terminating by process name, specifying a particular
197 * process name more times than the number of running instances causes
198 * all instances to be terminated, but termination failure messages to
199 * be printed as many times as the difference between the specification
200 * quantity and the number of running instances.
202 * Successful terminations are all listed first in order, with failing
203 * terminations being listed at the end.
205 * A PID of zero causes taskkill to warn about the inability to terminate
206 * system processes. */
207 static int send_close_messages(void)
209 DWORD *pid_list, pid_list_size;
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 (!isdigitW(*p++))
230 is_numeric = FALSE;
231 break;
235 if (is_numeric)
237 DWORD pid = atoiW(task_list[i]);
238 struct pid_close_info info = { pid };
240 EnumWindows(pid_enum_proc, (LPARAM)&info);
241 if (info.found)
242 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
243 else
245 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
246 status_code = 128;
249 else
251 DWORD index;
252 BOOL found_process = FALSE;
254 for (index = 0; index < pid_list_size; index++)
256 WCHAR process_name[MAX_PATH];
258 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
259 !strcmpiW(process_name, task_list[i]))
261 struct pid_close_info info = { pid_list[index] };
263 found_process = TRUE;
264 EnumWindows(pid_enum_proc, (LPARAM)&info);
265 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
269 if (!found_process)
271 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
272 status_code = 128;
277 HeapFree(GetProcessHeap(), 0, pid_list);
278 return status_code;
281 static BOOL add_to_task_list(WCHAR *name)
283 static unsigned int list_size = 16;
285 if (!task_list)
287 task_list = HeapAlloc(GetProcessHeap(), 0,
288 list_size * sizeof(*task_list));
289 if (!task_list)
290 return FALSE;
292 else if (task_count == list_size)
294 void *realloc_list;
296 list_size *= 2;
297 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
298 list_size * sizeof(*task_list));
299 if (!realloc_list)
300 return FALSE;
302 task_list = realloc_list;
305 task_list[task_count++] = name;
306 return TRUE;
309 /* FIXME Argument processing does not match behavior observed on Windows.
310 * Stringent argument counting and processing is performed, and unrecognized
311 * options are detected as parameters when placed after options that accept one. */
312 static BOOL process_arguments(int argc, WCHAR *argv[])
314 static const WCHAR slashForceTerminate[] = {'/','f',0};
315 static const WCHAR slashImage[] = {'/','i','m',0};
316 static const WCHAR slashPID[] = {'/','p','i','d',0};
317 static const WCHAR slashHelp[] = {'/','?',0};
319 if (argc > 1)
321 int i;
322 BOOL has_im = 0, has_pid = 0;
324 /* Only the lone help option is recognized. */
325 if (argc == 2 && !strcmpW(slashHelp, argv[1]))
327 taskkill_message(STRING_USAGE);
328 exit(0);
331 for (i = 1; i < argc; i++)
333 int got_im = 0, got_pid = 0;
335 if (!strcmpiW(slashForceTerminate, argv[i]))
336 force_termination = 1;
337 /* Options /IM and /PID appear to behave identically, except for
338 * the fact that they cannot be specified at the same time. */
339 else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
340 (got_pid = !strcmpiW(slashPID, argv[i])))
342 if (!argv[i + 1])
344 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
345 taskkill_message(STRING_USAGE);
346 return FALSE;
349 if (got_im) has_im = 1;
350 if (got_pid) has_pid = 1;
352 if (has_im && has_pid)
354 taskkill_message(STRING_MUTUAL_EXCLUSIVE);
355 taskkill_message(STRING_USAGE);
356 return FALSE;
359 if (!add_to_task_list(argv[i + 1]))
360 return FALSE;
361 i++;
363 else
365 taskkill_message(STRING_INVALID_OPTION);
366 taskkill_message(STRING_USAGE);
367 return FALSE;
371 else
373 taskkill_message(STRING_MISSING_OPTION);
374 taskkill_message(STRING_USAGE);
375 return FALSE;
378 return TRUE;
381 int wmain(int argc, WCHAR *argv[])
383 int status_code = 0;
385 if (!process_arguments(argc, argv))
387 HeapFree(GetProcessHeap(), 0, task_list);
388 return 1;
391 if (force_termination)
392 WINE_FIXME("Forced termination is not implemented\n");
393 else
394 status_code = send_close_messages();
396 HeapFree(GetProcessHeap(), 0, task_list);
397 return status_code;