/********************************************************************** * InitiateSystemShutdownA * * @implemented */ BOOL WINAPI InitiateSystemShutdownA(LPSTR lpMachineName, LPSTR lpMessage, DWORD dwTimeout, BOOL bForceAppsClosed, BOOL bRebootAfterShutdown) { ANSI_STRING MachineNameA, MessageA; UNICODE_STRING MachineNameW, MessageW; NTSTATUS Status; BOOL res; MachineNameW.Buffer = NULL; MessageW.Buffer = NULL; if (lpMachineName) { RtlInitAnsiString(&MachineNameA, lpMachineName); Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE); if (STATUS_SUCCESS != Status) { if(MachineNameW.Buffer) RtlFreeUnicodeString(&MachineNameW); SetLastError(RtlNtStatusToDosError(Status)); return FALSE; } } if (lpMessage) { RtlInitAnsiString(&MessageA, lpMessage); Status = RtlAnsiStringToUnicodeString(&MessageW, &MessageA, TRUE); if (STATUS_SUCCESS != Status) { if (MessageW.Buffer) RtlFreeUnicodeString(&MessageW); SetLastError(RtlNtStatusToDosError(Status)); return FALSE; } } res = InitiateSystemShutdownW(MachineNameW.Buffer, MessageW.Buffer, dwTimeout, bForceAppsClosed, bRebootAfterShutdown); /* Clear the values of both strings */ if (lpMachineName) RtlFreeUnicodeString(&MachineNameW); if (lpMessage) RtlFreeUnicodeString(&MessageW); return res; }
/********************************************************************** * InitiateSystemShutdownA * * @unimplemented */ BOOL WINAPI InitiateSystemShutdownA(LPSTR lpMachineName, LPSTR lpMessage, DWORD dwTimeout, BOOL bForceAppsClosed, BOOL bRebootAfterShutdown) { ANSI_STRING MachineNameA; ANSI_STRING MessageA; UNICODE_STRING MachineNameW; UNICODE_STRING MessageW; NTSTATUS Status; INT LastError; BOOL rv; MachineNameW.Buffer = NULL; MessageW.Buffer = NULL; if (lpMachineName) { RtlInitAnsiString(&MachineNameA, lpMachineName); Status = RtlAnsiStringToUnicodeString(&MachineNameW, &MachineNameA, TRUE); if (STATUS_SUCCESS != Status) { SetLastError(RtlNtStatusToDosError(Status)); return FALSE; } } if (lpMessage) { RtlInitAnsiString(&MessageA, lpMessage); Status = RtlAnsiStringToUnicodeString(&MessageW, &MessageA, TRUE); if (STATUS_SUCCESS != Status) { if (MachineNameW.Buffer) { RtlFreeUnicodeString(&MachineNameW); } SetLastError(RtlNtStatusToDosError(Status)); return FALSE; } } rv = InitiateSystemShutdownW(MachineNameW.Buffer, MessageW.Buffer, dwTimeout, bForceAppsClosed, bRebootAfterShutdown); LastError = GetLastError(); if (lpMachineName) { RtlFreeUnicodeString(&MachineNameW); } if (lpMessage) { RtlFreeUnicodeString(&MessageW); } SetLastError(LastError); return rv; }
static int reboot(int flags) { #ifdef _WIN32_WCE if(flags != RB_AUTOBOOT) { errno = EINVAL; return -1; } #ifdef _USE_KIOCTL if(!KernelIoControl(IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL)) return -1; #else unsigned long int e = SetSystemPowerState(NULL, POWER_STATE_RESET, 0); if(e) { SetLastError(e); return -1; } #endif #else if(enable_shutdown_privilege() < 0) return -1; #ifdef _WIN32_WNT_NATIVE SHUTDOWN_ACTION action; switch(flags) { case RB_AUTOBOOT: action = ShutdownReboot; break; case RB_HALT_SYSTEM: action = ShutdownNoReboot; break; default: errno = EINVAL; return -1; } long int status = NtShutdownSystem(action); if(status < 0) { __set_errno_from_ntstatus(status); return -1; } #else #ifdef _USE_EWX unsigned int wflags = EWX_FORCE; switch(flags) { case RB_AUTOBOOT: flags |= EWX_REBOOT; break; case RB_HALT_SYSTEM: flags |= EWX_SHUTDOWN; break; default: errno = EINVAL; return -1; } if(!ExitWindowsEx(wflags, 0)) { __set_errno_from_oserror(); return -1; } #else if(!InitiateSystemShutdownW(NULL, NULL, 0, 1, flags & EWX_REBOOT)) { __set_errno_from_oserror(); return -1; } #endif #endif /* _WIN32_WNT_NATIVE */ #endif /* _WIN32_WCE */ while(1) sleep(10000); }
RTDECL(int) RTSystemShutdown(RTMSINTERVAL cMsDelay, uint32_t fFlags, const char *pszLogMsg) { AssertPtrReturn(pszLogMsg, VERR_INVALID_POINTER); AssertReturn(!(fFlags & ~RTSYSTEM_SHUTDOWN_VALID_MASK), VERR_INVALID_PARAMETER); PRTUTF16 pwszLogMsg; int rc = RTStrToUtf16(pszLogMsg, &pwszLogMsg); if (RT_FAILURE(rc)) return rc; DWORD cSecsTimeout = (cMsDelay + 499) / 1000; BOOL fRebootAfterShutdown = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_REBOOT ? TRUE : FALSE; BOOL fForceAppsClosed = fFlags & RTSYSTEM_SHUTDOWN_FORCE ? TRUE : FALSE; /* * Do the */ if (InitiateSystemShutdownW(NULL /*pwszMachineName = NULL = localhost*/, pwszLogMsg, cSecsTimeout, fForceAppsClosed, fRebootAfterShutdown)) rc = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_HALT ? VINF_SYS_MAY_POWER_OFF : VINF_SUCCESS; else { /* If we failed because of missing privileges, try get the right to shut down the system and call the api again. */ DWORD dwErr = GetLastError(); rc = RTErrConvertFromWin32(dwErr); if (dwErr == ERROR_ACCESS_DENIED) { HANDLE hToken = NULL; if (OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, TRUE /*OpenAsSelf*/, &hToken)) dwErr = NO_ERROR; else { dwErr = GetLastError(); if (dwErr == ERROR_NO_TOKEN) { if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) dwErr = NO_ERROR; else dwErr = GetLastError(); } } if (dwErr == NO_ERROR) { union { TOKEN_PRIVILEGES TokenPriv; char ab[sizeof(TOKEN_PRIVILEGES) + sizeof(LUID_AND_ATTRIBUTES)]; } u; u.TokenPriv.PrivilegeCount = 1; u.TokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (LookupPrivilegeValue(NULL /*localhost*/, SE_SHUTDOWN_NAME, &u.TokenPriv.Privileges[0].Luid)) { if (AdjustTokenPrivileges(hToken, FALSE /*DisableAllPrivileges*/, &u.TokenPriv, RT_OFFSETOF(TOKEN_PRIVILEGES, Privileges[1]), NULL, NULL) ) { if (InitiateSystemShutdownW(NULL /*pwszMachineName = NULL = localhost*/, pwszLogMsg, cSecsTimeout, fForceAppsClosed, fRebootAfterShutdown)) rc = (fFlags & RTSYSTEM_SHUTDOWN_ACTION_MASK) == RTSYSTEM_SHUTDOWN_HALT ? VINF_SYS_MAY_POWER_OFF : VINF_SUCCESS; else { dwErr = GetLastError(); rc = RTErrConvertFromWin32(dwErr); } } CloseHandle(hToken); } } } } RTUtf16Free(pwszLogMsg); return rc; }