ReactOS 0.4.15-dev-8127-g6338913
endproc.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Task Manager
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Process Termination.
5 * COPYRIGHT: Copyright 1999-2001 Brian Palmer <brianp@reactos.org>
6 * Copyright 2005 Klemens Friedl <frik85@reactos.at>
7 * Copyright 2014 Ismael Ferreras Morezuelas <swyterzone+ros@gmail.com>
8 */
9
10#include "precomp.h"
11
12#define NTOS_MODE_USER
13#include <ndk/psfuncs.h>
14
16{
17 DWORD dwProcessId;
19 WCHAR szTitle[256];
20 WCHAR strErrorText[260];
21
22 dwProcessId = GetSelectedProcessId();
23
24 if (dwProcessId == 0)
25 return;
26
28
29 /* forbid killing system processes even if we have privileges -- sigh, windows kludge! */
31 {
33 LoadStringW(hInst, IDS_MSG_CLOSESYSTEMPROCESS, strErrorText, 256);
36 return;
37 }
38
39 /* if this is a standard process just ask for confirmation before doing it */
40 LoadStringW(hInst, IDS_MSG_WARNINGTERMINATING, strErrorText, 256);
43 {
45 return;
46 }
47
48 /* no such process or not enough privileges to open its token */
49 if (!hProcess)
50 {
51 GetLastErrorText(strErrorText, 260);
54 return;
55 }
56
57 /* try to kill it, and notify the user if didn't work */
59 {
60 GetLastErrorText(strErrorText, 260);
63 }
64
66}
67
69{
71 ULONG BreakOnTermination;
72
73 /* return early if the process handle does not exist */
74 if (!hProcess)
75 return FALSE;
76
77 /* the important system processes that we don't want to let the user
78 kill come marked as critical, this simplifies the check greatly.
79
80 a critical process brings the system down when is terminated:
81 <http://www.geoffchappell.com/studies/windows/win32/ntdll/api/rtl/peb/setprocessiscritical.htm> */
82
85 &BreakOnTermination,
86 sizeof(ULONG),
87 NULL);
88
89 if (NT_SUCCESS(status) && BreakOnTermination)
90 return TRUE;
91
92 return FALSE;
93}
94
95BOOL ShutdownProcessTreeHelper(HANDLE hSnapshot, HANDLE hParentProcess, DWORD dwParentPID)
96{
97 HANDLE hChildHandle;
98 PROCESSENTRY32W ProcessEntry = {0};
99 ProcessEntry.dwSize = sizeof(ProcessEntry);
100
101 if (Process32FirstW(hSnapshot, &ProcessEntry))
102 {
103 do
104 {
105 if (ProcessEntry.th32ParentProcessID == dwParentPID)
106 {
108 FALSE,
109 ProcessEntry.th32ProcessID);
110 if (!hChildHandle || IsCriticalProcess(hChildHandle))
111 {
112 if (hChildHandle)
113 {
114 CloseHandle(hChildHandle);
115 }
116 continue;
117 }
118 if (!ShutdownProcessTreeHelper(hSnapshot, hChildHandle, ProcessEntry.th32ProcessID))
119 {
120 CloseHandle(hChildHandle);
121 return FALSE;
122 }
123 CloseHandle(hChildHandle);
124 }
125 } while (Process32NextW(hSnapshot, &ProcessEntry));
126 }
127
128 return TerminateProcess(hParentProcess, 0);
129}
130
131BOOL ShutdownProcessTree(HANDLE hParentProcess, DWORD dwParentPID)
132{
134 BOOL bResult;
135
136 if (!hSnapshot)
137 {
138 return FALSE;
139 }
140
141 bResult = ShutdownProcessTreeHelper(hSnapshot, hParentProcess, dwParentPID);
142 CloseHandle(hSnapshot);
143 return bResult;
144}
145
147{
148 DWORD dwProcessId;
150 WCHAR szTitle[256];
151 WCHAR strErrorText[260];
152
153 dwProcessId = GetSelectedProcessId();
154
155 if (dwProcessId == 0)
156 return;
157
159
160 /* forbid killing system processes even if we have privileges -- sigh, windows kludge! */
162 {
164 LoadStringW(hInst, IDS_MSG_CLOSESYSTEMPROCESS, strErrorText, 256);
167 return;
168 }
169
170 LoadStringW(hInst, IDS_MSG_WARNINGTERMINATING, strErrorText, 256);
173 {
175 return;
176 }
177
178 if (!hProcess)
179 {
180 GetLastErrorText(strErrorText, 260);
183 return;
184 }
185
186 if (!ShutdownProcessTree(hProcess, dwProcessId))
187 {
188 GetLastErrorText(strErrorText, 260);
191 }
192
194}
LONG NTSTATUS
Definition: precomp.h:26
#define IDS_MSG_CLOSESYSTEMPROCESS
Definition: resource.h:258
#define IDS_MSG_WARNINGTERMINATING
Definition: resource.h:253
#define IDS_MSG_TASKMGRWARNING
Definition: resource.h:252
#define IDS_MSG_UNABLETERMINATEPRO
Definition: resource.h:254
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define CloseHandle
Definition: compat.h:739
BOOL WINAPI TerminateProcess(IN HANDLE hProcess, IN UINT uExitCode)
Definition: proc.c:1532
HANDLE WINAPI OpenProcess(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwProcessId)
Definition: proc.c:1227
BOOL WINAPI Process32NextW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
Definition: toolhelp.c:1073
HANDLE WINAPI CreateToolhelp32Snapshot(DWORD dwFlags, DWORD th32ProcessID)
Definition: toolhelp.c:1255
BOOL WINAPI Process32FirstW(HANDLE hSnapshot, LPPROCESSENTRY32W lppe)
Definition: toolhelp.c:984
HINSTANCE hInst
Definition: dxdiag.c:13
BOOL ShutdownProcessTreeHelper(HANDLE hSnapshot, HANDLE hParentProcess, DWORD dwParentPID)
Definition: endproc.c:95
void ProcessPage_OnEndProcessTree(void)
Definition: endproc.c:146
BOOL IsCriticalProcess(HANDLE hProcess)
Definition: endproc.c:68
void ProcessPage_OnEndProcess(void)
Definition: endproc.c:15
BOOL ShutdownProcessTree(HANDLE hParentProcess, DWORD dwParentPID)
Definition: endproc.c:131
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
#define PROCESS_TERMINATE
Definition: pstypes.h:157
#define PROCESS_QUERY_INFORMATION
Definition: pstypes.h:166
@ ProcessBreakOnTermination
Definition: winternl.h:398
TCHAR szTitle[MAX_LOADSTRING]
Definition: magnifier.c:35
HWND hMainWnd
Definition: magnifier.c:32
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:71
NTSTATUS NTAPI NtQueryInformationProcess(_In_ HANDLE ProcessHandle, _In_ PROCESSINFOCLASS ProcessInformationClass, _Out_ PVOID ProcessInformation, _In_ ULONG ProcessInformationLength, _Out_opt_ PULONG ReturnLength)
Definition: query.c:59
DWORD GetSelectedProcessId(void)
Definition: procpage.c:88
Definition: ps.c:97
DWORD th32ParentProcessID
Definition: tlhelp32.h:55
DWORD th32ProcessID
Definition: tlhelp32.h:51
BOOL ConfirmMessageBox(HWND hWnd, LPCWSTR Text, LPCWSTR Title, UINT Type)
Definition: taskmgr.c:1059
LPWSTR GetLastErrorText(LPWSTR lpszBuf, DWORD dwSize)
Definition: taskmgr.c:1083
#define TH32CS_SNAPPROCESS
Definition: tlhelp32.h:26
uint32_t ULONG
Definition: typedefs.h:59
#define MB_TOPMOST
Definition: winuser.h:825
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define MB_YESNO
Definition: winuser.h:817
int WINAPI MessageBoxW(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
#define MB_OK
Definition: winuser.h:790
#define MB_ICONWARNING
Definition: winuser.h:786
#define MB_ICONSTOP
Definition: winuser.h:803
__wchar_t WCHAR
Definition: xmlstorage.h:180