ReactOS 0.4.16-dev-319-g6cf4263
shutdown.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: System Shutdown.
5 * COPYRIGHT: Copyright 1999-2001 Brian Palmer <brianp@reactos.org>
6 * Copyright 2011 Mário Kacmár /Mario Kacmar/ aka Kario <kario@szm.sk>
7 * Copyright 2014 Robert Naumann <gonzomdx@gmail.com>
8 */
9
10#include "precomp.h"
11#include <ndk/exfuncs.h>
12#include <ndk/pofuncs.h>
13#include <ndk/rtlfuncs.h>
14
15// Uncomment when NtInitiatePowerAction() is implemented
16// #define NT_INITIATE_POWERACTION_IMPLEMENTED
17
18static BOOL
19EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
20{
22 HANDLE hToken;
24
27 &hToken);
28 if (!Success) return Success;
29
31 lpszPrivilegeName,
32 &tp.Privileges[0].Luid);
33 if (!Success) goto Quit;
34
35 tp.PrivilegeCount = 1;
36 tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
37
39
40Quit:
41 CloseHandle(hToken);
42 return Success;
43}
44
45VOID
47{
49
51 {
53 return;
54 }
55
56#ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
59 0, FALSE);
60#else
63 0);
64#endif
65
66 if (!NT_SUCCESS(Status))
68
70}
71
72VOID
74{
76
78 {
80 return;
81 }
82
83#ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
86 0, FALSE);
87#else
90 0);
91#endif
92
93 if (!NT_SUCCESS(Status))
95
97}
98
99VOID
101{
102 /* Trick: on Windows, pressing the CTRL key forces shutdown via NT API */
103 BOOL ForceShutdown = !!(GetKeyState(VK_CONTROL) & 0x8000);
104
106 {
108 return;
109 }
110
111 if (ForceShutdown)
112 {
114 if (!NT_SUCCESS(Status))
116 }
117 else
118 {
119 // The choice of EWX_SHUTDOWN or EWX_POWEROFF may be done with NtPowerInformation
120 if (!ExitWindowsEx(EWX_POWEROFF /* EWX_SHUTDOWN */, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
122 }
123
125}
126
127VOID
129{
130 /* Trick: on Windows, pressing the CTRL key forces reboot via NT API */
131 BOOL ForceReboot = !!(GetKeyState(VK_CONTROL) & 0x8000);
132
134 {
136 return;
137 }
138
139 if (ForceReboot)
140 {
142 if (!NT_SUCCESS(Status))
144 }
145 else
146 {
147 if (!ExitWindowsEx(EWX_REBOOT, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
149 }
150
152}
153
154VOID
156{
157 if (!ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
159}
160
161VOID
163{
164}
165
166VOID
168{
169 if (!LockWorkStation())
171}
172
173VOID
175{
176}
177
178VOID
180{
181}
LONG NTSTATUS
Definition: precomp.h:26
static DWORD EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
Definition: shutdown.c:142
VOID ShutDown_Reboot(VOID)
Definition: shutdown.c:128
VOID ShutDown_StandBy(VOID)
Definition: shutdown.c:46
VOID ShutDown_SwitchUser(VOID)
Definition: shutdown.c:162
VOID ShutDown_EjectComputer(VOID)
Definition: shutdown.c:179
VOID ShutDown_Hibernate(VOID)
Definition: shutdown.c:73
VOID ShutDown_PowerOff(VOID)
Definition: shutdown.c:100
VOID ShutDown_LogOffUser(VOID)
Definition: shutdown.c:155
VOID ShutDown_Disconnect(VOID)
Definition: shutdown.c:174
VOID ShutDown_LockComputer(VOID)
Definition: shutdown.c:167
#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:33
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:374
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
#define CloseHandle
Definition: compat.h:739
#define GetCurrentProcess()
Definition: compat.h:759
_In_ uint64_t _In_ uint64_t _In_ uint64_t _In_opt_ traverse_ptr * tp
Definition: btrfs.c:2996
@ Success
Definition: eventcreate.c:712
VOID ShowWin32Error(IN DWORD dwError)
Definition: eventvwr.c:140
unsigned int BOOL
Definition: ntddk_ex.h:94
Status
Definition: gdiplustypes.h:25
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
@ ShutdownReboot
Definition: extypes.h:177
@ ShutdownPowerOff
Definition: extypes.h:178
NTSTATUS NTAPI NtShutdownSystem(IN SHUTDOWN_ACTION Action)
Definition: shutdown.c:43
NTSTATUS NTAPI NtInitiatePowerAction(IN POWER_ACTION SystemAction, IN SYSTEM_POWER_STATE MinSystemState, IN ULONG Flags, IN BOOLEAN Asynchronous)
Definition: power.c:778
NTSTATUS NTAPI NtSetSystemPowerState(IN POWER_ACTION SystemAction, IN SYSTEM_POWER_STATE MinSystemState, IN ULONG Flags)
Definition: power.c:1007
@ PowerSystemSleeping1
Definition: ntpoapi.h:37
@ PowerSystemHibernate
Definition: ntpoapi.h:40
@ PowerActionHibernate
Definition: ntpoapi.h:126
@ PowerActionSleep
Definition: ntpoapi.h:125
EXTINLINE BOOL WINAPI LockWorkStation(VOID)
Definition: ntwrapper.h:82
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define SE_SHUTDOWN_NAME
Definition: winnt_old.h:413
#define EWX_POWEROFF
Definition: winuser.h:637
#define VK_CONTROL
Definition: winuser.h:2206
#define EWX_LOGOFF
Definition: winuser.h:636
#define EWX_REBOOT
Definition: winuser.h:638
BOOL WINAPI ExitWindowsEx(_In_ UINT, _In_ DWORD)
SHORT WINAPI GetKeyState(_In_ int)
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:930
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185