Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.6 KiB

  1. /* Copyright 1999 American Power Conversion, All Rights Reserved
  2. *
  3. * Description:
  4. * The file implements the Shutdowner. The Shutdowner is reponsible
  5. * for performing a graceful shutdown of the operating system.
  6. *
  7. *
  8. * Revision History:
  9. * sberard 01Apr1999 initial revision.
  10. *
  11. */
  12. #include "nt.h"
  13. #include "ntrtl.h"
  14. #include "nturtl.h"
  15. #include "shutdown.h"
  16. #include "powrprof.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * ShutdownSystem
  22. *
  23. * Description:
  24. * This function initiates a graceful shutdown of the operating system.
  25. * This is performed through a call to the Win32 function ExitWindowsEx(..).
  26. * When called the shutdown is initated immediately and, is successful, the
  27. * function returns TRUE. Otherwise, FALSE is retuned.
  28. *
  29. * Parameters:
  30. * none
  31. *
  32. * Returns:
  33. * TRUE - if the shutdown was initiated successfully
  34. * FALSE - if errors occur while initiating shutdown
  35. */
  36. BOOL ShutdownSystem()
  37. {
  38. BOOL ret_val = FALSE;
  39. TOKEN_PRIVILEGES tkp;
  40. HANDLE process_token;
  41. SYSTEM_POWER_CAPABILITIES SysPwrCapabilities;
  42. // get the current process token so that we can
  43. // modify our current process privs.
  44. if (OpenProcessToken(GetCurrentProcess(),
  45. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &process_token)) {
  46. // Find the local unique id for SeShutdownPrivilege
  47. if (LookupPrivilegeValue(NULL, TEXT("SeShutdownPrivilege"), &tkp.Privileges[0].Luid)) {
  48. // we only want to enable one priv
  49. tkp.PrivilegeCount = 1;
  50. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  51. // now, add it all back to our current process.
  52. if (AdjustTokenPrivileges(process_token, // do it to us
  53. FALSE, // don't turn all privs off
  54. &tkp, // what we want to do
  55. 0, // don't want any prev info
  56. (PTOKEN_PRIVILEGES)NULL,
  57. 0)) {
  58. // Initiate the shutdown
  59. if (GetPwrCapabilities(&SysPwrCapabilities) && SysPwrCapabilities.SystemS5) {
  60. ret_val = ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF, (DWORD) -1);
  61. } else {
  62. ret_val = ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, (DWORD) -1);
  63. }
  64. }
  65. }
  66. }
  67. return ret_val;
  68. }
  69. #ifdef __cplusplus
  70. }
  71. #endif