2014-08-26 18:57:20 +00:00
|
|
|
|
/*
|
|
|
|
|
* ReactOS Task Manager
|
|
|
|
|
*
|
|
|
|
|
* shutdown.c
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
|
|
|
|
|
* 2011 M<EFBFBD>rio Kacm<EFBFBD>r /Mario Kacmar/ aka Kario (kario@szm.sk)
|
2014-08-27 12:46:22 +00:00
|
|
|
|
* 2014 Robert Naumann <gonzomdx@gmail.com>
|
2014-08-26 18:57:20 +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
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
|
#include <ndk/exfuncs.h>
|
|
|
|
|
#include <ndk/pofuncs.h>
|
2014-08-27 12:46:22 +00:00
|
|
|
|
#include <ndk/rtlfuncs.h>
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
|
|
|
|
// Uncomment when NtInitiatePowerAction() is implemented
|
|
|
|
|
// #define NT_INITIATE_POWERACTION_IMPLEMENTED
|
|
|
|
|
|
|
|
|
|
static BOOL
|
|
|
|
|
EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
|
|
|
|
|
{
|
|
|
|
|
BOOL Success;
|
|
|
|
|
HANDLE hToken;
|
2014-08-27 12:46:22 +00:00
|
|
|
|
TOKEN_PRIVILEGES tp;
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
|
|
|
|
Success = OpenProcessToken(GetCurrentProcess(),
|
|
|
|
|
TOKEN_ADJUST_PRIVILEGES,
|
|
|
|
|
&hToken);
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!Success) return Success;
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Success = LookupPrivilegeValueW(NULL,
|
|
|
|
|
lpszPrivilegeName,
|
|
|
|
|
&tp.Privileges[0].Luid);
|
|
|
|
|
if (!Success) goto Quit;
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
tp.PrivilegeCount = 1;
|
|
|
|
|
tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Quit:
|
|
|
|
|
CloseHandle(hToken);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
return Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
2014-08-27 12:46:22 +00:00
|
|
|
|
ShutDown_StandBy(VOID)
|
2014-08-26 18:57:20 +00:00
|
|
|
|
{
|
2014-08-27 12:46:22 +00:00
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
|
|
if (!EnablePrivilege(SE_SHUTDOWN_NAME, TRUE))
|
|
|
|
|
{
|
|
|
|
|
ShowWin32Error(GetLastError());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
|
|
|
|
#ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Status = NtInitiatePowerAction(PowerActionSleep,
|
|
|
|
|
PowerSystemSleeping1,
|
|
|
|
|
0, FALSE);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
#else
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Status = NtSetSystemPowerState(PowerActionSleep,
|
|
|
|
|
PowerSystemSleeping1,
|
|
|
|
|
0);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
ShowWin32Error(RtlNtStatusToDosError(Status));
|
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
EnablePrivilege(SE_SHUTDOWN_NAME, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
2014-08-27 12:46:22 +00:00
|
|
|
|
ShutDown_Hibernate(VOID)
|
2014-08-26 18:57:20 +00:00
|
|
|
|
{
|
2014-08-27 12:46:22 +00:00
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
|
|
if (!EnablePrivilege(SE_SHUTDOWN_NAME, TRUE))
|
|
|
|
|
{
|
|
|
|
|
ShowWin32Error(GetLastError());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-26 18:57:20 +00:00
|
|
|
|
|
|
|
|
|
#ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Status = NtInitiatePowerAction(PowerActionHibernate,
|
|
|
|
|
PowerSystemHibernate,
|
|
|
|
|
0, FALSE);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
#else
|
2014-08-27 12:46:22 +00:00
|
|
|
|
Status = NtSetSystemPowerState(PowerActionHibernate,
|
|
|
|
|
PowerSystemHibernate,
|
|
|
|
|
0);
|
2014-08-26 18:57:20 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
ShowWin32Error(RtlNtStatusToDosError(Status));
|
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
EnablePrivilege(SE_SHUTDOWN_NAME, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
|
|
|
|
ShutDown_PowerOff(VOID)
|
|
|
|
|
{
|
|
|
|
|
/* Trick: on Windows, pressing the CTRL key forces shutdown via NT API */
|
|
|
|
|
BOOL ForceShutdown = !!(GetKeyState(VK_CONTROL) & 0x8000);
|
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!EnablePrivilege(SE_SHUTDOWN_NAME, TRUE))
|
|
|
|
|
{
|
|
|
|
|
ShowWin32Error(GetLastError());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
if (ForceShutdown)
|
|
|
|
|
{
|
2014-08-27 12:46:22 +00:00
|
|
|
|
NTSTATUS Status = NtShutdownSystem(ShutdownPowerOff);
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
ShowWin32Error(RtlNtStatusToDosError(Status));
|
2014-08-26 18:57:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The choice of EWX_SHUTDOWN or EWX_POWEROFF may be done with NtPowerInformation
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!ExitWindowsEx(EWX_POWEROFF /* EWX_SHUTDOWN */, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
|
|
|
|
|
ShowWin32Error(GetLastError());
|
2014-08-26 18:57:20 +00:00
|
|
|
|
}
|
2014-08-27 12:46:22 +00:00
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
EnablePrivilege(SE_SHUTDOWN_NAME, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
2014-08-27 12:46:22 +00:00
|
|
|
|
ShutDown_Reboot(VOID)
|
2014-08-26 18:57:20 +00:00
|
|
|
|
{
|
|
|
|
|
/* Trick: on Windows, pressing the CTRL key forces reboot via NT API */
|
|
|
|
|
BOOL ForceReboot = !!(GetKeyState(VK_CONTROL) & 0x8000);
|
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!EnablePrivilege(SE_SHUTDOWN_NAME, TRUE))
|
|
|
|
|
{
|
|
|
|
|
ShowWin32Error(GetLastError());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
if (ForceReboot)
|
|
|
|
|
{
|
2014-08-27 12:46:22 +00:00
|
|
|
|
NTSTATUS Status = NtShutdownSystem(ShutdownReboot);
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
|
ShowWin32Error(RtlNtStatusToDosError(Status));
|
2014-08-26 18:57:20 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!ExitWindowsEx(EWX_REBOOT, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
|
|
|
|
|
ShowWin32Error(GetLastError());
|
2014-08-26 18:57:20 +00:00
|
|
|
|
}
|
2014-08-27 12:46:22 +00:00
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
EnablePrivilege(SE_SHUTDOWN_NAME, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
|
|
|
|
ShutDown_LogOffUser(VOID)
|
|
|
|
|
{
|
2014-08-27 12:46:22 +00:00
|
|
|
|
if (!ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
|
|
|
|
|
ShowWin32Error(GetLastError());
|
2014-08-26 18:57:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
|
|
|
|
ShutDown_SwitchUser(VOID)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 12:46:22 +00:00
|
|
|
|
VOID
|
|
|
|
|
ShutDown_LockComputer(VOID)
|
|
|
|
|
{
|
|
|
|
|
if (!LockWorkStation())
|
|
|
|
|
ShowWin32Error(GetLastError());
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 18:57:20 +00:00
|
|
|
|
VOID
|
|
|
|
|
ShutDown_Disconnect(VOID)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VOID
|
2014-08-27 12:46:22 +00:00
|
|
|
|
ShutDown_EjectComputer(VOID)
|
2014-08-26 18:57:20 +00:00
|
|
|
|
{
|
|
|
|
|
}
|