ReactOS  0.4.12-dev-43-g63b00d8
shutdown.c
Go to the documentation of this file.
1 /*
2  * ReactOS Task Manager
3  *
4  * shutdown.c
5  *
6  * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7  * 2011 Mário Kacmár /Mario Kacmar/ aka Kario (kario@szm.sk)
8  * 2014 Robert Naumann <gonzomdx@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "precomp.h"
26 #include <ndk/exfuncs.h>
27 #include <ndk/pofuncs.h>
28 #include <ndk/rtlfuncs.h>
29 
30 // Uncomment when NtInitiatePowerAction() is implemented
31 // #define NT_INITIATE_POWERACTION_IMPLEMENTED
32 
33 static BOOL
34 EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
35 {
36  BOOL Success;
37  HANDLE hToken;
39 
42  &hToken);
43  if (!Success) return Success;
44 
45  Success = LookupPrivilegeValueW(NULL,
46  lpszPrivilegeName,
47  &tp.Privileges[0].Luid);
48  if (!Success) goto Quit;
49 
50  tp.PrivilegeCount = 1;
51  tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
52 
53  Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
54 
55 Quit:
56  CloseHandle(hToken);
57  return Success;
58 }
59 
60 VOID
62 {
64 
66  {
68  return;
69  }
70 
71 #ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
74  0, FALSE);
75 #else
78  0);
79 #endif
80 
81  if (!NT_SUCCESS(Status))
83 
85 }
86 
87 VOID
89 {
91 
93  {
95  return;
96  }
97 
98 #ifdef NT_INITIATE_POWERACTION_IMPLEMENTED
101  0, FALSE);
102 #else
105  0);
106 #endif
107 
108  if (!NT_SUCCESS(Status))
110 
112 }
113 
114 VOID
116 {
117  /* Trick: on Windows, pressing the CTRL key forces shutdown via NT API */
118  BOOL ForceShutdown = !!(GetKeyState(VK_CONTROL) & 0x8000);
119 
121  {
123  return;
124  }
125 
126  if (ForceShutdown)
127  {
129  if (!NT_SUCCESS(Status))
131  }
132  else
133  {
134  // The choice of EWX_SHUTDOWN or EWX_POWEROFF may be done with NtPowerInformation
135  if (!ExitWindowsEx(EWX_POWEROFF /* EWX_SHUTDOWN */, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
137  }
138 
140 }
141 
142 VOID
144 {
145  /* Trick: on Windows, pressing the CTRL key forces reboot via NT API */
146  BOOL ForceReboot = !!(GetKeyState(VK_CONTROL) & 0x8000);
147 
149  {
151  return;
152  }
153 
154  if (ForceReboot)
155  {
157  if (!NT_SUCCESS(Status))
159  }
160  else
161  {
162  if (!ExitWindowsEx(EWX_REBOOT, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
164  }
165 
167 }
168 
169 VOID
171 {
172  if (!ExitWindowsEx(EWX_LOGOFF, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER))
174 }
175 
176 VOID
178 {
179 }
180 
181 VOID
183 {
184  if (!LockWorkStation())
186 }
187 
188 VOID
190 {
191 }
192 
193 VOID
195 {
196 }
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
LONG NTSTATUS
Definition: precomp.h:26
VOID ShutDown_LogOffUser(VOID)
Definition: shutdown.c:170
$ULONG PrivilegeCount
Definition: setypes.h:969
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
VOID ShutDown_PowerOff(VOID)
Definition: shutdown.c:115
VOID ShutDown_EjectComputer(VOID)
Definition: shutdown.c:194
_In_ UINT64 _In_ UINT64 _In_ UINT64 _In_opt_ traverse_ptr * tp
Definition: btrfs.c:2630
#define EWX_REBOOT
Definition: winuser.h:633
#define EWX_LOGOFF
Definition: winuser.h:631
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
VOID ShutDown_Disconnect(VOID)
Definition: shutdown.c:189
NTSTATUS NTAPI NtInitiatePowerAction(IN POWER_ACTION SystemAction, IN SYSTEM_POWER_STATE MinSystemState, IN ULONG Flags, IN BOOLEAN Asynchronous)
Definition: power.c:639
VOID ShutDown_Hibernate(VOID)
Definition: shutdown.c:88
smooth NULL
Definition: ftsmooth.c:416
NTSTATUS NTAPI NtSetSystemPowerState(IN POWER_ACTION SystemAction, IN SYSTEM_POWER_STATE MinSystemState, IN ULONG Flags)
Definition: power.c:861
#define EWX_POWEROFF
Definition: winuser.h:632
SHORT WINAPI GetKeyState(_In_ int)
unsigned int BOOL
Definition: ntddk_ex.h:94
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:24
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
EXTINLINE BOOL WINAPI LockWorkStation(VOID)
Definition: ntwrapper.h:82
VOID ShutDown_StandBy(VOID)
Definition: shutdown.c:61
NTSTATUS NTAPI NtShutdownSystem(IN SHUTDOWN_ACTION Action)
Definition: shutdown.c:43
VOID ShutDown_LockComputer(VOID)
Definition: shutdown.c:182
BOOL WINAPI ExitWindowsEx(_In_ UINT, _In_ DWORD)
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:372
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
VOID ShutDown_SwitchUser(VOID)
Definition: shutdown.c:177
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:292
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:970
#define VK_CONTROL
Definition: winuser.h:2157
#define SE_SHUTDOWN_NAME
Definition: winnt_old.h:383
VOID ShowWin32Error(IN DWORD dwError)
Definition: eventvwr.c:157
VOID ShutDown_Reboot(VOID)
Definition: shutdown.c:143
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:876
static BOOL EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
Definition: shutdown.c:34