2004-01-10 02:14:13 +00:00
|
|
|
/*
|
|
|
|
* ReactOS Task Manager
|
|
|
|
*
|
2010-08-08 03:41:33 +00:00
|
|
|
* endproc.c
|
2004-01-10 02:14:13 +00:00
|
|
|
*
|
2014-12-13 06:35:56 +00:00
|
|
|
* Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
|
|
|
|
* 2005 Klemens Friedl <frik85@reactos.at>
|
|
|
|
* 2014 Ismael Ferreras Morezuelas <swyterzone+ros@gmail.com>
|
2004-01-10 02:14:13 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2009-10-27 10:34:16 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-01-10 02:14:13 +00:00
|
|
|
*/
|
2005-05-08 04:07:56 +00:00
|
|
|
|
2013-01-24 23:00:42 +00:00
|
|
|
#include "precomp.h"
|
2015-03-14 15:19:07 +00:00
|
|
|
|
|
|
|
#define NTOS_MODE_USER
|
|
|
|
#include <ndk/psfuncs.h>
|
2004-01-10 02:14:13 +00:00
|
|
|
|
|
|
|
void ProcessPage_OnEndProcess(void)
|
|
|
|
{
|
2007-10-31 23:26:27 +00:00
|
|
|
DWORD dwProcessId;
|
|
|
|
HANDLE hProcess;
|
2008-02-15 19:22:55 +00:00
|
|
|
WCHAR szTitle[256];
|
2007-10-31 23:26:27 +00:00
|
|
|
WCHAR strErrorText[260];
|
2004-01-10 02:14:13 +00:00
|
|
|
|
2009-06-28 14:22:09 +00:00
|
|
|
dwProcessId = GetSelectedProcessId();
|
2004-01-10 02:14:13 +00:00
|
|
|
|
2009-06-28 14:22:09 +00:00
|
|
|
if (dwProcessId == 0)
|
2004-01-10 02:14:13 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-13 06:35:56 +00:00
|
|
|
hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
|
|
|
|
|
|
|
|
/* forbid killing system processes even if we have privileges -- sigh, windows kludge! */
|
|
|
|
if (hProcess && IsCriticalProcess(hProcess))
|
|
|
|
{
|
|
|
|
LoadStringW(hInst, IDS_MSG_UNABLETERMINATEPRO, szTitle, 256);
|
|
|
|
LoadStringW(hInst, IDS_MSG_CLOSESYSTEMPROCESS, strErrorText, 256);
|
|
|
|
MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONWARNING|MB_TOPMOST);
|
2014-12-13 08:18:09 +00:00
|
|
|
CloseHandle(hProcess);
|
2014-12-13 06:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if this is a standard process just ask for confirmation before doing it */
|
2008-02-15 19:22:55 +00:00
|
|
|
LoadStringW(hInst, IDS_MSG_WARNINGTERMINATING, strErrorText, 256);
|
|
|
|
LoadStringW(hInst, IDS_MSG_TASKMGRWARNING, szTitle, 256);
|
2014-12-13 06:35:56 +00:00
|
|
|
if (MessageBoxW(hMainWnd, strErrorText, szTitle, MB_YESNO|MB_ICONWARNING|MB_TOPMOST) != IDYES)
|
2014-12-13 08:18:09 +00:00
|
|
|
{
|
|
|
|
if (hProcess) CloseHandle(hProcess);
|
2004-01-10 02:14:13 +00:00
|
|
|
return;
|
2014-12-13 08:18:09 +00:00
|
|
|
}
|
2004-01-10 02:14:13 +00:00
|
|
|
|
2014-12-13 06:35:56 +00:00
|
|
|
/* no such process or not enough privileges to open its token */
|
2004-01-10 02:14:13 +00:00
|
|
|
if (!hProcess)
|
|
|
|
{
|
|
|
|
GetLastErrorText(strErrorText, 260);
|
2008-02-15 19:22:55 +00:00
|
|
|
LoadStringW(hInst, IDS_MSG_UNABLETERMINATEPRO, szTitle, 256);
|
2014-12-13 06:35:56 +00:00
|
|
|
MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP|MB_TOPMOST);
|
2004-01-10 02:14:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-13 06:35:56 +00:00
|
|
|
/* try to kill it, and notify the user if didn't work */
|
2011-04-29 21:49:37 +00:00
|
|
|
if (!TerminateProcess(hProcess, 1))
|
2004-01-10 02:14:13 +00:00
|
|
|
{
|
|
|
|
GetLastErrorText(strErrorText, 260);
|
2008-02-15 19:22:55 +00:00
|
|
|
LoadStringW(hInst, IDS_MSG_UNABLETERMINATEPRO, szTitle, 256);
|
2014-12-13 06:35:56 +00:00
|
|
|
MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP|MB_TOPMOST);
|
2004-01-10 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
}
|
|
|
|
|
2014-12-13 06:35:56 +00:00
|
|
|
BOOL IsCriticalProcess(HANDLE hProcess)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
ULONG BreakOnTermination;
|
|
|
|
|
|
|
|
/* return early if the process handle does not exist */
|
|
|
|
if (!hProcess)
|
2014-12-13 08:18:09 +00:00
|
|
|
return FALSE;
|
2014-12-13 06:35:56 +00:00
|
|
|
|
|
|
|
/* the important system processes that we don't want to let the user
|
|
|
|
kill come marked as critical, this simplifies the check greatly.
|
|
|
|
|
|
|
|
a critical process brings the system down when is terminated:
|
|
|
|
<http://www.geoffchappell.com/studies/windows/win32/ntdll/api/rtl/peb/setprocessiscritical.htm> */
|
|
|
|
|
|
|
|
status = NtQueryInformationProcess(hProcess,
|
|
|
|
ProcessBreakOnTermination,
|
|
|
|
&BreakOnTermination,
|
|
|
|
sizeof(ULONG),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (NT_SUCCESS(status) && BreakOnTermination)
|
2014-12-13 08:18:09 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2014-12-13 06:35:56 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:07:05 +00:00
|
|
|
BOOL ShutdownProcessTreeHelper(HANDLE hSnapshot, HANDLE hParentProcess, DWORD dwParentPID)
|
|
|
|
{
|
|
|
|
HANDLE hChildHandle;
|
|
|
|
PROCESSENTRY32W ProcessEntry = {0};
|
|
|
|
ProcessEntry.dwSize = sizeof(ProcessEntry);
|
|
|
|
|
|
|
|
if (Process32FirstW(hSnapshot, &ProcessEntry))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (ProcessEntry.th32ParentProcessID == dwParentPID)
|
|
|
|
{
|
|
|
|
hChildHandle = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION,
|
|
|
|
FALSE,
|
|
|
|
ProcessEntry.th32ProcessID);
|
|
|
|
if (!hChildHandle || IsCriticalProcess(hChildHandle))
|
|
|
|
{
|
|
|
|
if (hChildHandle)
|
|
|
|
{
|
|
|
|
CloseHandle(hChildHandle);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!ShutdownProcessTreeHelper(hSnapshot, hChildHandle, ProcessEntry.th32ProcessID))
|
|
|
|
{
|
|
|
|
CloseHandle(hChildHandle);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
CloseHandle(hChildHandle);
|
|
|
|
}
|
|
|
|
} while (Process32NextW(hSnapshot, &ProcessEntry));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TerminateProcess(hParentProcess, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ShutdownProcessTree(HANDLE hParentProcess, DWORD dwParentPID)
|
|
|
|
{
|
|
|
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
BOOL bResult;
|
|
|
|
|
|
|
|
if (!hSnapshot)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bResult = ShutdownProcessTreeHelper(hSnapshot, hParentProcess, dwParentPID);
|
|
|
|
CloseHandle(hSnapshot);
|
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
2004-01-10 02:14:13 +00:00
|
|
|
void ProcessPage_OnEndProcessTree(void)
|
|
|
|
{
|
2007-10-31 23:26:27 +00:00
|
|
|
DWORD dwProcessId;
|
|
|
|
HANDLE hProcess;
|
2008-02-15 19:22:55 +00:00
|
|
|
WCHAR szTitle[256];
|
2007-10-31 23:26:27 +00:00
|
|
|
WCHAR strErrorText[260];
|
2004-01-10 02:14:13 +00:00
|
|
|
|
2009-06-28 14:22:09 +00:00
|
|
|
dwProcessId = GetSelectedProcessId();
|
2004-01-10 02:14:13 +00:00
|
|
|
|
2009-06-28 14:22:09 +00:00
|
|
|
if (dwProcessId == 0)
|
2004-01-10 02:14:13 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-13 06:35:56 +00:00
|
|
|
hProcess = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
|
|
|
|
|
|
|
|
/* forbid killing system processes even if we have privileges -- sigh, windows kludge! */
|
|
|
|
if (hProcess && IsCriticalProcess(hProcess))
|
|
|
|
{
|
|
|
|
LoadStringW(hInst, IDS_MSG_UNABLETERMINATEPRO, szTitle, 256);
|
|
|
|
LoadStringW(hInst, IDS_MSG_CLOSESYSTEMPROCESS, strErrorText, 256);
|
|
|
|
MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONWARNING|MB_TOPMOST);
|
2014-12-13 08:18:09 +00:00
|
|
|
CloseHandle(hProcess);
|
2014-12-13 06:35:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-15 19:22:55 +00:00
|
|
|
LoadStringW(hInst, IDS_MSG_WARNINGTERMINATING, strErrorText, 256);
|
|
|
|
LoadStringW(hInst, IDS_MSG_TASKMGRWARNING, szTitle, 256);
|
|
|
|
if (MessageBoxW(hMainWnd, strErrorText, szTitle, MB_YESNO|MB_ICONWARNING) != IDYES)
|
2014-12-13 08:18:09 +00:00
|
|
|
{
|
|
|
|
if (hProcess) CloseHandle(hProcess);
|
2004-01-10 02:14:13 +00:00
|
|
|
return;
|
2014-12-13 08:18:09 +00:00
|
|
|
}
|
2004-01-10 02:14:13 +00:00
|
|
|
|
|
|
|
if (!hProcess)
|
|
|
|
{
|
|
|
|
GetLastErrorText(strErrorText, 260);
|
2008-02-15 19:22:55 +00:00
|
|
|
LoadStringW(hInst, IDS_MSG_UNABLETERMINATEPRO, szTitle, 256);
|
|
|
|
MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
|
2004-01-10 02:14:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:07:05 +00:00
|
|
|
if (!ShutdownProcessTree(hProcess, dwProcessId))
|
2004-01-10 02:14:13 +00:00
|
|
|
{
|
|
|
|
GetLastErrorText(strErrorText, 260);
|
2008-02-15 19:22:55 +00:00
|
|
|
LoadStringW(hInst, IDS_MSG_UNABLETERMINATEPRO, szTitle, 256);
|
|
|
|
MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
|
2004-01-10 02:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(hProcess);
|
|
|
|
}
|