taskkill: Add Slovenian translation.
[wine] / programs / taskkill / taskkill.c
1 /*
2  * Task termination utility
3  *
4  * Copyright 2008 Andrew Riedi
5  * Copyright 2010 Andrew Nguyen
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <windows.h>
23 #include <psapi.h>
24 #include <wine/debug.h>
25 #include <wine/unicode.h>
26
27 #include "taskkill.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(taskkill);
30
31 int force_termination;
32
33 WCHAR **task_list;
34 unsigned int task_count;
35
36 struct pid_close_info
37 {
38     DWORD pid;
39     BOOL found;
40 };
41
42 static int taskkill_vprintfW(const WCHAR *msg, va_list va_args)
43 {
44     int wlen;
45     DWORD count, ret;
46     WCHAR msg_buffer[8192];
47
48     wlen = vsprintfW(msg_buffer, msg, va_args);
49
50     ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
51     if (!ret)
52     {
53         DWORD len;
54         char *msgA;
55
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;
61
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);
66     }
67
68     return count;
69 }
70
71 static int taskkill_printfW(const WCHAR *msg, ...)
72 {
73     va_list va_args;
74     int len;
75
76     va_start(va_args, msg);
77     len = taskkill_vprintfW(msg, va_args);
78     va_end(va_args);
79
80     return len;
81 }
82
83 static int taskkill_message_printfW(int msg, ...)
84 {
85     va_list va_args;
86     WCHAR msg_buffer[8192];
87     int len;
88
89     LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
90         sizeof(msg_buffer)/sizeof(WCHAR));
91
92     va_start(va_args, msg);
93     len = taskkill_vprintfW(msg_buffer, va_args);
94     va_end(va_args);
95
96     return len;
97 }
98
99 static int taskkill_message(int msg)
100 {
101     static const WCHAR formatW[] = {'%','s',0};
102     WCHAR msg_buffer[8192];
103
104     LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
105         sizeof(msg_buffer)/sizeof(WCHAR));
106
107     return taskkill_printfW(formatW, msg_buffer);
108 }
109
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)
112 {
113     struct pid_close_info *info = (struct pid_close_info *)lParam;
114     DWORD hwnd_pid;
115
116     GetWindowThreadProcessId(hwnd, &hwnd_pid);
117
118     if (hwnd_pid == info->pid)
119     {
120         PostMessageW(hwnd, WM_CLOSE, 0, 0);
121         info->found = TRUE;
122     }
123
124     return TRUE;
125 }
126
127 static DWORD *enumerate_processes(DWORD *list_count)
128 {
129     DWORD *pid_list, alloc_bytes = 1024 * sizeof(*pid_list), needed_bytes;
130
131     pid_list = HeapAlloc(GetProcessHeap(), 0, alloc_bytes);
132     if (!pid_list)
133         return NULL;
134
135     for (;;)
136     {
137         DWORD *realloc_list;
138
139         if (!EnumProcesses(pid_list, alloc_bytes, &needed_bytes))
140         {
141             HeapFree(GetProcessHeap(), 0, pid_list);
142             return NULL;
143         }
144
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;
152
153         alloc_bytes *= 2;
154         realloc_list = HeapReAlloc(GetProcessHeap(), 0, pid_list, alloc_bytes);
155         if (!realloc_list)
156         {
157             HeapFree(GetProcessHeap(), 0, pid_list);
158             return NULL;
159         }
160         pid_list = realloc_list;
161     }
162
163     *list_count = needed_bytes / sizeof(*pid_list);
164     return pid_list;
165 }
166
167 static BOOL get_process_name_from_pid(DWORD pid, WCHAR *buf, DWORD chars)
168 {
169     HANDLE process;
170     HMODULE module;
171     DWORD required_size;
172
173     process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
174     if (!process)
175         return FALSE;
176
177     if (!EnumProcessModules(process, &module, sizeof(module), &required_size))
178     {
179         CloseHandle(process);
180         return FALSE;
181     }
182
183     if (!GetModuleBaseNameW(process, module, buf, chars))
184     {
185         CloseHandle(process);
186         return FALSE;
187     }
188
189     CloseHandle(process);
190     return TRUE;
191 }
192
193 /* The implemented task enumeration and termination behavior does not
194  * exactly match native behavior. On Windows:
195  *
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.
201  *
202  * Successful terminations are all listed first in order, with failing
203  * terminations being listed at the end.
204  *
205  * A PID of zero causes taskkill to warn about the inability to terminate
206  * system processes. */
207 static int send_close_messages(void)
208 {
209     DWORD *pid_list, pid_list_size;
210     unsigned int i;
211     int status_code = 0;
212
213     pid_list = enumerate_processes(&pid_list_size);
214     if (!pid_list)
215     {
216         taskkill_message(STRING_ENUM_FAILED);
217         return 1;
218     }
219
220     for (i = 0; i < task_count; i++)
221     {
222         WCHAR *p = task_list[i];
223         BOOL is_numeric = TRUE;
224
225         /* Determine whether the string is not numeric. */
226         while (*p)
227         {
228             if (!isdigitW(*p++))
229             {
230                 is_numeric = FALSE;
231                 break;
232             }
233         }
234
235         if (is_numeric)
236         {
237             DWORD pid = atoiW(task_list[i]);
238             struct pid_close_info info = { pid };
239
240             EnumWindows(pid_enum_proc, (LPARAM)&info);
241             if (info.found)
242                 taskkill_message_printfW(STRING_CLOSE_PID_SEARCH, pid);
243             else
244             {
245                 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
246                 status_code = 128;
247             }
248         }
249         else
250         {
251             DWORD index;
252             BOOL found_process = FALSE;
253
254             for (index = 0; index < pid_list_size; index++)
255             {
256                 WCHAR process_name[MAX_PATH];
257
258                 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
259                     !strcmpiW(process_name, task_list[i]))
260                 {
261                     struct pid_close_info info = { pid_list[index] };
262
263                     found_process = TRUE;
264                     EnumWindows(pid_enum_proc, (LPARAM)&info);
265                     taskkill_message_printfW(STRING_CLOSE_PROC_SRCH, process_name, pid_list[index]);
266                 }
267             }
268
269             if (!found_process)
270             {
271                 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
272                 status_code = 128;
273             }
274         }
275     }
276
277     HeapFree(GetProcessHeap(), 0, pid_list);
278     return status_code;
279 }
280
281 static int terminate_processes(void)
282 {
283     DWORD *pid_list, pid_list_size;
284     unsigned int i;
285     int status_code = 0;
286
287     pid_list = enumerate_processes(&pid_list_size);
288     if (!pid_list)
289     {
290         taskkill_message(STRING_ENUM_FAILED);
291         return 1;
292     }
293
294     for (i = 0; i < task_count; i++)
295     {
296         WCHAR *p = task_list[i];
297         BOOL is_numeric = TRUE;
298
299         /* Determine whether the string is not numeric. */
300         while (*p)
301         {
302             if (!isdigitW(*p++))
303             {
304                 is_numeric = FALSE;
305                 break;
306             }
307         }
308
309         if (is_numeric)
310         {
311             DWORD pid = atoiW(task_list[i]);
312             HANDLE process;
313
314             process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
315             if (!process)
316             {
317                 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
318                 status_code = 128;
319                 continue;
320             }
321
322             if (!TerminateProcess(process, 0))
323             {
324                 taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
325                 status_code = 1;
326                 CloseHandle(process);
327                 continue;
328             }
329
330             taskkill_message_printfW(STRING_TERM_PID_SEARCH, pid);
331             CloseHandle(process);
332         }
333         else
334         {
335             DWORD index;
336             BOOL found_process = FALSE;
337
338             for (index = 0; index < pid_list_size; index++)
339             {
340                 WCHAR process_name[MAX_PATH];
341
342                 if (get_process_name_from_pid(pid_list[index], process_name, MAX_PATH) &&
343                     !strcmpiW(process_name, task_list[i]))
344                 {
345                     HANDLE process;
346
347                     process = OpenProcess(PROCESS_TERMINATE, FALSE, pid_list[index]);
348                     if (!process)
349                     {
350                         taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
351                         status_code = 128;
352                         continue;
353                     }
354
355                     if (!TerminateProcess(process, 0))
356                     {
357                         taskkill_message_printfW(STRING_TERMINATE_FAILED, task_list[i]);
358                         status_code = 1;
359                         CloseHandle(process);
360                         continue;
361                     }
362
363                     found_process = TRUE;
364                     taskkill_message_printfW(STRING_TERM_PROC_SEARCH, task_list[i], pid_list[index]);
365                     CloseHandle(process);
366                 }
367             }
368
369             if (!found_process)
370             {
371                 taskkill_message_printfW(STRING_SEARCH_FAILED, task_list[i]);
372                 status_code = 128;
373             }
374         }
375     }
376
377     HeapFree(GetProcessHeap(), 0, pid_list);
378     return status_code;
379 }
380
381 static BOOL add_to_task_list(WCHAR *name)
382 {
383     static unsigned int list_size = 16;
384
385     if (!task_list)
386     {
387         task_list = HeapAlloc(GetProcessHeap(), 0,
388                                    list_size * sizeof(*task_list));
389         if (!task_list)
390             return FALSE;
391     }
392     else if (task_count == list_size)
393     {
394         void *realloc_list;
395
396         list_size *= 2;
397         realloc_list = HeapReAlloc(GetProcessHeap(), 0, task_list,
398                                    list_size * sizeof(*task_list));
399         if (!realloc_list)
400             return FALSE;
401
402         task_list = realloc_list;
403     }
404
405     task_list[task_count++] = name;
406     return TRUE;
407 }
408
409 /* FIXME Argument processing does not match behavior observed on Windows.
410  * Stringent argument counting and processing is performed, and unrecognized
411  * options are detected as parameters when placed after options that accept one. */
412 static BOOL process_arguments(int argc, WCHAR *argv[])
413 {
414     static const WCHAR slashForceTerminate[] = {'/','f',0};
415     static const WCHAR slashImage[] = {'/','i','m',0};
416     static const WCHAR slashPID[] = {'/','p','i','d',0};
417     static const WCHAR slashHelp[] = {'/','?',0};
418
419     if (argc > 1)
420     {
421         int i;
422         BOOL has_im = 0, has_pid = 0;
423
424         /* Only the lone help option is recognized. */
425         if (argc == 2 && !strcmpW(slashHelp, argv[1]))
426         {
427             taskkill_message(STRING_USAGE);
428             exit(0);
429         }
430
431         for (i = 1; i < argc; i++)
432         {
433             int got_im = 0, got_pid = 0;
434
435             if (!strcmpiW(slashForceTerminate, argv[i]))
436                 force_termination = 1;
437             /* Options /IM and /PID appear to behave identically, except for
438              * the fact that they cannot be specified at the same time. */
439             else if ((got_im = !strcmpiW(slashImage, argv[i])) ||
440                      (got_pid = !strcmpiW(slashPID, argv[i])))
441             {
442                 if (!argv[i + 1])
443                 {
444                     taskkill_message_printfW(STRING_MISSING_PARAM, argv[i]);
445                     taskkill_message(STRING_USAGE);
446                     return FALSE;
447                 }
448
449                 if (got_im) has_im = 1;
450                 if (got_pid) has_pid = 1;
451
452                 if (has_im && has_pid)
453                 {
454                     taskkill_message(STRING_MUTUAL_EXCLUSIVE);
455                     taskkill_message(STRING_USAGE);
456                     return FALSE;
457                 }
458
459                 if (!add_to_task_list(argv[i + 1]))
460                     return FALSE;
461                 i++;
462             }
463             else
464             {
465                 taskkill_message(STRING_INVALID_OPTION);
466                 taskkill_message(STRING_USAGE);
467                 return FALSE;
468             }
469         }
470     }
471     else
472     {
473         taskkill_message(STRING_MISSING_OPTION);
474         taskkill_message(STRING_USAGE);
475         return FALSE;
476     }
477
478     return TRUE;
479 }
480
481 int wmain(int argc, WCHAR *argv[])
482 {
483     int status_code = 0;
484
485     if (!process_arguments(argc, argv))
486     {
487         HeapFree(GetProcessHeap(), 0, task_list);
488         return 1;
489     }
490
491     if (force_termination)
492         status_code = terminate_processes();
493     else
494         status_code = send_close_messages();
495
496     HeapFree(GetProcessHeap(), 0, task_list);
497     return status_code;
498 }