1 | /* $Id: RTSystemShutdown-win.cpp 70615 2018-01-17 20:28:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSystemShutdown, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/system.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 |
|
---|
38 | #include <iprt/win/windows.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | RTDECL(int) RTSystemShutdown(RTMSINTERVAL cMsDelay, uint32_t fFlags, const char *pszLogMsg)
|
---|
42 | {
|
---|
43 | AssertPtrReturn(pszLogMsg, VERR_INVALID_POINTER);
|
---|
44 | AssertReturn(!(fFlags & ~RTSYSTEM_SHUTDOWN_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Before we start, try grant the necessary privileges.
|
---|
48 | */
|
---|
49 | DWORD dwErr;
|
---|
50 | HANDLE hToken = NULL;
|
---|
51 | if (OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, TRUE /*OpenAsSelf*/, &hToken))
|
---|
52 | dwErr = NO_ERROR;
|
---|
53 | else
|
---|
54 | {
|
---|
55 | dwErr = GetLastError();
|
---|
56 | if (dwErr == ERROR_NO_TOKEN)
|
---|
57 | {
|
---|
58 | if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
|
---|
59 | dwErr = NO_ERROR;
|
---|
60 | else
|
---|
61 | dwErr = GetLastError();
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if (dwErr == NO_ERROR)
|
---|
65 | {
|
---|
66 | union
|
---|
67 | {
|
---|
68 | TOKEN_PRIVILEGES TokenPriv;
|
---|
69 | char ab[sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES)];
|
---|
70 | } u;
|
---|
71 | u.TokenPriv.PrivilegeCount = 1;
|
---|
72 | u.TokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
73 | if (LookupPrivilegeValue(NULL /*localhost*/, SE_SHUTDOWN_NAME, &u.TokenPriv.Privileges[0].Luid))
|
---|
74 | {
|
---|
75 | if (!AdjustTokenPrivileges(hToken,
|
---|
76 | FALSE /*DisableAllPrivileges*/,
|
---|
77 | &u.TokenPriv,
|
---|
78 | RT_OFFSETOF(TOKEN_PRIVILEGES, Privileges[1]),
|
---|
79 | NULL,
|
---|
80 | NULL) )
|
---|
81 | dwErr = GetLastError();
|
---|
82 | }
|
---|
83 | else
|
---|
84 | dwErr = GetLastError();
|
---|
85 | CloseHandle(hToken);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Do some parameter conversion.
|
---|
90 | */
|
---|
91 | PRTUTF16 pwszLogMsg;
|
---|
92 | int rc = RTStrToUtf16(pszLogMsg, &pwszLogMsg);
|
---|
93 | if (RT_FAILURE(rc))
|
---|
94 | return rc;
|
---|
95 | DWORD cSecsTimeout = (cMsDelay + 499) / 1000;
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * If we're told to power off the system, we should try use InitiateShutdownW (6.0+)
|
---|
99 | * or ExitWindowsEx (3.50) rather than InitiateSystemShutdownW, because these other
|
---|
100 | * APIs allows us to explicitly specify that we want to power off.
|
---|
101 | *
|
---|
102 | * Note! For NT version 4, 3.51, and 3.50 the system may instaed reboot since the
|
---|
103 | * x86 HALs typically didn't know how to perform a power off.
|
---|
104 | */
|
---|
105 | bool fDone = false;
|
---|
106 | if ( (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_POWER_OFF
|
---|
107 | || (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_POWER_OFF_HALT)
|
---|
108 | {
|
---|
109 | /* This API has the grace period thing. */
|
---|
110 | decltype(InitiateShutdownW) *pfnInitiateShutdownW;
|
---|
111 | pfnInitiateShutdownW = (decltype(InitiateShutdownW) *)GetProcAddress(GetModuleHandleW(L"ADVAPI32.DLL"), "InitiateShutdownW");
|
---|
112 | if (pfnInitiateShutdownW)
|
---|
113 | {
|
---|
114 | DWORD fShutdownFlags = SHUTDOWN_POWEROFF;
|
---|
115 | if (fFlags & RTSYSTEM_SHUTDOWN_FORCE)
|
---|
116 | fShutdownFlags |= SHUTDOWN_FORCE_OTHERS | SHUTDOWN_FORCE_SELF;
|
---|
117 | DWORD fReason = SHTDN_REASON_MAJOR_OTHER | (fFlags & RTSYSTEM_SHUTDOWN_PLANNED ? SHTDN_REASON_FLAG_PLANNED : 0);
|
---|
118 | dwErr = pfnInitiateShutdownW(NULL /*pwszMachineName*/, pwszLogMsg, cSecsTimeout, fShutdownFlags, fReason);
|
---|
119 | if (dwErr == ERROR_INVALID_PARAMETER)
|
---|
120 | {
|
---|
121 | fReason &= ~SHTDN_REASON_FLAG_PLANNED; /* just in case... */
|
---|
122 | dwErr = pfnInitiateShutdownW(NULL /*pwszMachineName*/, pwszLogMsg, cSecsTimeout, fShutdownFlags, fReason);
|
---|
123 | }
|
---|
124 | if (dwErr == ERROR_SUCCESS)
|
---|
125 | {
|
---|
126 | rc = VINF_SUCCESS;
|
---|
127 | fDone = true;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (!fDone)
|
---|
132 | {
|
---|
133 | /* No grace period here, too bad. */
|
---|
134 | decltype(ExitWindowsEx) *pfnExitWindowsEx;
|
---|
135 | pfnExitWindowsEx = (decltype(ExitWindowsEx) *)GetProcAddress(GetModuleHandleW(L"USER32.DLL"), "ExitWindowsEx");
|
---|
136 | if (pfnExitWindowsEx)
|
---|
137 | {
|
---|
138 | DWORD fExitWindows = EWX_POWEROFF | EWX_SHUTDOWN;
|
---|
139 | if (fFlags & RTSYSTEM_SHUTDOWN_FORCE)
|
---|
140 | fExitWindows |= EWX_FORCE | EWX_FORCEIFHUNG;
|
---|
141 |
|
---|
142 | if (pfnExitWindowsEx(fExitWindows, SHTDN_REASON_MAJOR_OTHER))
|
---|
143 | fDone = true;
|
---|
144 | else if (pfnExitWindowsEx(fExitWindows & ~EWX_FORCEIFHUNG, SHTDN_REASON_MAJOR_OTHER))
|
---|
145 | fDone = true;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Fall back on the oldest API.
|
---|
152 | */
|
---|
153 | if (!fDone)
|
---|
154 | {
|
---|
155 | BOOL fRebootAfterShutdown = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_REBOOT
|
---|
156 | ? TRUE : FALSE;
|
---|
157 | BOOL fForceAppsClosed = fFlags & RTSYSTEM_SHUTDOWN_FORCE ? TRUE : FALSE;
|
---|
158 | if (InitiateSystemShutdownW(NULL /*pwszMachineName = NULL = localhost*/,
|
---|
159 | pwszLogMsg,
|
---|
160 | cSecsTimeout,
|
---|
161 | fForceAppsClosed,
|
---|
162 | fRebootAfterShutdown))
|
---|
163 | rc = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_HALT ? VINF_SYS_MAY_POWER_OFF : VINF_SUCCESS;
|
---|
164 | else
|
---|
165 | rc = RTErrConvertFromWin32(dwErr);
|
---|
166 | }
|
---|
167 |
|
---|
168 | RTUtf16Free(pwszLogMsg);
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 | RT_EXPORT_SYMBOL(RTSystemShutdown);
|
---|
172 |
|
---|