cryptui: Implement CryptUIDlgSelectCertificateFromStore.
[wine.git] / programs / taskkill / taskkill.c
blob59ce98dc6dc6a6e92b1a82f8ebc121fa05194bb4
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>
26 #include <wine/unicode.h>
28 #include "taskkill.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
32 static BOOL force_termination = FALSE;
34 static WCHAR **task_list;
35 static unsigned int task_count;
37 struct pid_close_info
39 DWORD pid;
40 BOOL found;
43 static int taskkill_vprintfW(const WCHAR *msg, __ms_va_list va_args)
45 int wlen;
46 DWORD count, ret;
47 WCHAR msg_buffer[8192];
49 wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
50 ARRAY_SIZE(msg_buffer), &va_args);
52 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
53 if (!ret)
55 DWORD len;
56 char *msgA;
58 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
59 * back to WriteFile(), assuming the console encoding is still the right
60 * one in that case.
62 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
63 NULL, 0, NULL, NULL);
64 msgA = HeapAlloc(GetProcessHeap(), 0, len);
65 if (!msgA)
66 return 0;
68 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
69 NULL, NULL);
70 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
71 HeapFree(GetProcessHeap(), 0, msgA);
74 return count;
77 static int WINAPIV taskkill_printfW(const WCHAR *msg, ...)
79 __ms_va_list va_args;
80 int len;
82 __ms_va_start(va_args, msg);
83 len = taskkill_vprintfW(msg, va_args);
84 __ms_va_end(va_args);
86 return len;
89 static int WINAPIV taskkill_message_printfW(int msg, ...)
91 __ms_va_list va_args;
92 WCHAR msg_buffer[8192];
93 int len;
95 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
97 __ms_va_start(va_args, msg);
98 len = taskkill_vprintfW(msg_buffer, va_args);
99 __ms_va_end(va_args);
101 return len;
104 static int taskkill_message(int msg)
106 static const WCHAR formatW[] = {'%','1',0};
107 WCHAR msg_buffer[8192];
109 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
111 return taskkill_printfW(formatW, msg_buffer);
114 /* Post WM_CLOSE to all top-level windows belonging to the process with specified PID. */
115 static BOOL CALLBACK pid_enum_proc(HWND hwnd, LPARAM lParam)
117 struct pid_close_info *info = (struct pid_close_info *)lParam;
118 DWORD hwnd_pid;
120 GetWindowThreadProcessId(hwnd, &hwnd_pid);
122 if (hwnd_pid == info->pid)
124 PostMessageW(hwnd, WM_CLOSE, 0, 0);
125 info->found = TRUE;
128 return TRUE;
131 static DWORD *enumerate_processes(DWORD *list_count)
133 DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
135 pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
136 if (!pid_list)
137 return NULL;
139 for (;;)
141 DWORD *realloc_list;
143 if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
145 HeapFree(GetProcessHeap(), 0, pid_list);
146 return NULL;
149 /* EnumProcesses can't signal an insufficient buffer condition, so the
150 * only way to possibly determine whether a larger buffer is required
151 * is to see whether the written number of bytes is the same as the
152 * buffer size. If so, the buffer will be reallocated to twice the
153 * size. */
154 if (alloc_bytes != needed_bytes)
155 break;
157 alloc_bytes *= 2;
158 realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
159 if (!realloc_list)
161 HeapFree(GetProcessHeap(), 0, pid_list);
162 return NULL;
164 pid_list = realloc_list;
167 *list_count = needed_bytes / sizeof(*pid_list);
168 return pid_list;
171 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
173 HANDLE process;
174 HMODULE module;
175 DWORD required_size;
177 process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
178 if (!process)
179 return FALSE;
181 if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
183 CloseHandle(process);
184 return FALSE;
187 if (!GetModuleBaseNameW(process, module, buf, chars))
189 CloseHandle(process);
190 return FALSE;
193 CloseHandle(process);
194 return TRUE;
197 /* The implemented task enumeration and termination behavior does not
198 * exactly match native behavior. On Windows:
200 * In the case of terminating by process name, specifying a particular
201 * process name more times than the number of running instances causes
202 * all instances to be terminated, but termination failure messages to
203 * be printed as many times as the difference between the specification
204 * quantity and the number of running instances.
206 * Successful terminations are all listed first in order, with failing
207 * terminations being listed at the end.
209 * A PID of zero causes taskkill to warn about the inability to terminate
210 * system processes. */
211 static int send_close_messages(void)
213 DWORD *pid_list, pid_list_size;
214 DWORD self_pid = GetCurrentProcessId();
215 unsigned int i;
216 int status_code = 0;
218 pid_list = enumerate_processes(&pid_list_size);
219 if (!pid_list)
221 taskkill_message(STRING_ENUM_FAILED);
222 return 1;
225 for (i = 0; i < task_count; i++)
227 WCHAR *p = task_list[i];
228 BOOL is_numeric = TRUE;
230 /* Determine whether the string is not numeric. */
231 while (*p)
233 if (!isdigitW(*p++))
235 is_numeric = FALSE;
236 break;
240 if (is_numeric)
242 DWORD pid = atoiW(task_list[i]);
243 struct pid_close_info info = { pid };
245 if (pid == self_pid)
247 taskkill_message(STRING_SELF_TERMINATION);
248 status_code = 1;
249 continue;
252 EnumWindows(pid_enum_proc, (LPARAM)&info);
253 if (info.found)
254 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
255 else
257 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
258 status_code = 128;
261 else
263 DWORD index;
264 BOOL found_process = FALSE;
266 for (index = 0; index < pid_list_size; index++)
268 WCHAR process_name[MAX_PATH];
270 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
271 !strcmpiW(process_name, task_list[i]))
273 struct pid_close_info info = { pid_list[index] };
275 found_process = TRUE;
276 if (pid_list[index] == self_pid)
278 taskkill_message(STRING_SELF_TERMINATION);
279 status_code = 1;
280 continue;
283 EnumWindows(pid_enum_proc, (LPARAM)&info);
284 taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
288 if (!found_process)
290 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
291 status_code = 128;
296 HeapFree(GetProcessHeap(), 0, pid_list);
297 return status_code;
300 static int terminate_processes(void)
302 DWORD *pid_list, pid_list_size;
303 DWORD self_pid = GetCurrentProcessId();
304 unsigned int i;
305 int status_code = 0;
307 pid_list = enumerate_processes(&pid_list_size);
308 if (!pid_list)
310 taskkill_message(STRING_ENUM_FAILED);
311 return 1;
314 for (i = 0; i < task_count; i++)
316 WCHAR *p = task_list[i];
317 BOOL is_numeric = TRUE;
319 /* Determine whether the string is not numeric. */
320 while (*p)
322 if (!isdigitW(*p++))
324 is_numeric = FALSE;
325 break;
329 if (is_numeric)
331 DWORD pid = atoiW(task_list[i]);
332 HANDLE process;
334 if (pid == self_pid)
336 taskkill_message(STRING_SELF_TERMINATION);
337 status_code = 1;
338 continue;
341 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
342 if (!process)
344 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
345 status_code = 128;
346 continue;
349 if (!TerminateProcess(process, 0))
351 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
352 status_code = 1;
353 CloseHandle(process);
354 continue;
357 taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
358 CloseHandle(process);
360 else
362 DWORD index;
363 BOOL found_process = FALSE;
365 for (index = 0; index < pid_list_size; index++)
367 WCHAR process_name[MAX_PATH];
369 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
370 !strcmpiW(process_name, task_list[i]))
372 HANDLE process;
374 if (pid_list[index] == self_pid)
376 taskkill_message(STRING_SELF_TERMINATION);
377 status_code = 1;
378 continue;
381 process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
382 if (!process)
384 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
385 status_code = 128;
386 continue;
389 if (!TerminateProcess(process, 0))
391 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
392 status_code = 1;
393 CloseHandle(process);
394 continue;
397 found_process = TRUE;
398 taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
399 CloseHandle(process);
403 if (!found_process)
405 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
406 status_code = 128;
411 HeapFree(GetProcessHeap(), 0, pid_list);
412 return status_code;
415 static BOOL add_to_task_list(WCHAR *name)
417 static unsigned int list_size = 16;
419 if (!task_list)
421 task_list = HeapAlloc(GetProcessHeap(), 0,
422 list_size * sizeof(*task_list));
423 if (!task_list)
424 return FALSE;
426 else if (task_count == list_size)
428 void *realloc_list;
430 list_size *= 2;
431 realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
432 list_size * sizeof(*task_list));
433 if (!realloc_list)
434 return FALSE;
436 task_list = realloc_list;
439 task_list[task_count++] = name;
440 return TRUE;
443 /* FIXME Argument processing does not match behavior observed on Windows.
444 * Stringent argument counting and processing is performed, and unrecognized
445 * options are detected as parameters when placed after options that accept one. */
446 static BOOL process_arguments(int argc, WCHAR *argv[])
448 static const WCHAR opForceTerminate[] = {'f',0};
449 static const WCHAR opImage[] = {'i','m',0};
450 static const WCHAR opPID[] = {'p','i','d',0};
451 static const WCHAR opHelp[] = {'?',0};
452 static const WCHAR opTerminateChildren[] = {'t',0};
454 if (argc > 1)
456 int i;
457 WCHAR *argdata;
458 BOOL has_im = FALSE, has_pid = FALSE;
460 /* Only the lone help option is recognized. */
461 if (argc == 2)
463 argdata = argv[1];
464 if ((*argdata == '/' || *argdata == '-') && !strcmpW(opHelp, argdata + 1))
466 taskkill_message(STRING_USAGE);
467 exit(0);
471 for (i = 1; i < argc; i++)
473 BOOL got_im = FALSE, got_pid = FALSE;
475 argdata = argv[i];
476 if (*argdata != '/' && *argdata != '-')
477 goto invalid;
478 argdata++;
480 if (!strcmpiW(opTerminateChildren, argdata))
481 WINE_FIXME("argument T not supported\n");
482 if (!strcmpiW(opForceTerminate, argdata))
483 force_termination = TRUE;
484 /* Options /IM and /PID appear to behave identically, except for
485 * the fact that they cannot be specified at the same time. */
486 else if ((got_im = !strcmpiW(opImage, argdata)) ||
487 (got_pid = !strcmpiW(opPID, argdata)))
489 if (!argv[i + 1])
491 taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
492 taskkill_message(STRING_USAGE);
493 return FALSE;
496 if (got_im) has_im = TRUE;
497 if (got_pid) has_pid = TRUE;
499 if (has_im && has_pid)
501 taskkill_message(STRING_MUTUAL_EXCLUSIVE);
502 taskkill_message(STRING_USAGE);
503 return FALSE;
506 if (!add_to_task_list(argv[i + 1]))
507 return FALSE;
508 i++;
510 else
512 invalid:
513 taskkill_message(STRING_INVALID_OPTION);
514 taskkill_message(STRING_USAGE);
515 return FALSE;
519 else
521 taskkill_message(STRING_MISSING_OPTION);
522 taskkill_message(STRING_USAGE);
523 return FALSE;
526 return TRUE;
529 int wmain(int argc, WCHAR *argv[])
531 int status_code = 0;
533 if (!process_arguments(argc, argv))
535 HeapFree(GetProcessHeap(), 0, task_list);
536 return 1;
539 if (force_termination)
540 status_code = terminate_processes();
541 else
542 status_code = send_close_messages();
544 HeapFree(GetProcessHeap(), 0, task_list);
545 return status_code;