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.

246 lines
5.7 KiB

  1. /*****************************************************************
  2. *
  3. * Copyright(c) Microsoft Corp., 1988-1999
  4. *
  5. *****************************************************************/
  6. #include <stdio.h>
  7. #include <process.h>
  8. #include <setjmp.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. #include <powrprof.h>
  16. /*****************************************************************
  17. *
  18. * Globals
  19. *
  20. *****************************************************************/
  21. BOOLEAN Enable = FALSE;
  22. BOOLEAN Verbose = FALSE;
  23. BOOLEAN HiberStatus = FALSE;
  24. /*
  25. * PrintHelp
  26. *
  27. * DESCRIPTION: This routine prints the help message
  28. *
  29. * RETURNS: VOID
  30. *
  31. */
  32. VOID
  33. PrintHelp()
  34. {
  35. printf ("Enables/Disables Hibernation File\n\n");
  36. printf ("EHIB [/e | /d] [/v] [/s]\n\n");
  37. printf ("\t/e\tEnable Hibernation File\n");
  38. printf ("\t/d\tDisable Hibernation File\n");
  39. printf ("\t/s\tPrint Current Hibernate File Status\n");
  40. printf ("\t/v\tVerbose Mode On\n\n");
  41. }
  42. /*
  43. * ParseArgs
  44. *
  45. * Description:
  46. * This routine parses the input arguments and validates the
  47. * command line paramters
  48. *
  49. * Returns:
  50. * TRUE if valid command line usage/syntax
  51. * FALSE if invalid command line usage/syntax
  52. *
  53. */
  54. BOOLEAN
  55. ParseArgs(argc, argv)
  56. int argc;
  57. char *argv[];
  58. {
  59. int ii;
  60. BOOLEAN ValidArgs;
  61. //
  62. // Assume failure
  63. //
  64. ValidArgs = FALSE;
  65. if (argc < 2) {
  66. PrintHelp();
  67. } else {
  68. for (ii=1; ii<argc; ii++) {
  69. if (!strcmp(argv[ii], "/e") || !strcmp(argv[ii], "-e")) {
  70. Enable = TRUE;
  71. ValidArgs = TRUE;
  72. } else if (!strcmp(argv[ii], "/d") || !strcmp(argv[ii], "-d")) {
  73. Enable = FALSE;
  74. ValidArgs = TRUE;
  75. } else if (!strcmp(argv[ii], "/v") || !strcmp(argv[ii], "-v")) {
  76. Verbose = TRUE;
  77. } else if (!strcmp(argv[1], "/s") || !strcmp(argv[1], "-s")) {
  78. HiberStatus = TRUE;
  79. ValidArgs = TRUE;
  80. } else {
  81. ValidArgs = FALSE;
  82. break;
  83. }
  84. }
  85. if (!ValidArgs) {
  86. PrintHelp();
  87. }
  88. }
  89. return(ValidArgs);
  90. }
  91. /*
  92. * UpgradePermissions
  93. *
  94. * Description:
  95. * This routine promotes the user permissions in order to allocate
  96. * & deallocate the hibernation file.
  97. *
  98. * Returns:
  99. * VOID
  100. */
  101. VOID
  102. UpgradePermissions()
  103. {
  104. HANDLE hToken;
  105. TOKEN_PRIVILEGES tkp;
  106. OpenProcessToken (
  107. GetCurrentProcess(),
  108. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  109. &hToken
  110. );
  111. LookupPrivilegeValue (
  112. NULL,
  113. SE_CREATE_PAGEFILE_NAME,
  114. &tkp.Privileges[0].Luid
  115. );
  116. tkp.PrivilegeCount = 1;
  117. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  118. AdjustTokenPrivileges (
  119. hToken,
  120. FALSE,
  121. &tkp,
  122. 0,
  123. NULL,
  124. 0
  125. );
  126. }
  127. /*
  128. * HiberFile
  129. *
  130. * Description:
  131. * This routine allocated/deallocates the hiberfile and prints the appropriate errors messages
  132. *
  133. * Returns:
  134. * TRUE if successful
  135. * FALSE if not successful
  136. *
  137. */
  138. BOOLEAN
  139. HiberFile()
  140. {
  141. BOOLEAN RetStatus;
  142. NTSTATUS Status;
  143. SYSTEM_POWER_CAPABILITIES SysPwrCapabilities;
  144. //
  145. // Assume Failure
  146. //
  147. RetStatus = FALSE;
  148. if (GetPwrCapabilities(&SysPwrCapabilities)) {
  149. if (!SysPwrCapabilities.SystemS4) {
  150. printf("System does not support S4");
  151. } else if (HiberStatus) {
  152. if (SysPwrCapabilities.HiberFilePresent) {
  153. printf ("Reserved Hibernation File Enabled\n");
  154. } else {
  155. printf ("Reserved Hibernation File Disabled\n");
  156. }
  157. } else if (Verbose && Enable && SysPwrCapabilities.HiberFilePresent) {
  158. printf ("Reserved Hibernation File Enabled\n");
  159. RetStatus = TRUE;
  160. } else if (Verbose && !Enable && !SysPwrCapabilities.HiberFilePresent) {
  161. printf ("Reserved Hibernation File Disabled\n");
  162. RetStatus = TRUE;
  163. } else {
  164. Status = NtPowerInformation (
  165. SystemReserveHiberFile,
  166. &Enable,
  167. sizeof (Enable),
  168. NULL,
  169. 0
  170. );
  171. if (NT_SUCCESS(Status)) {
  172. if (Verbose && Enable) {
  173. printf ("Reserved Hibernation File Enabled\n");
  174. } else if (Verbose) {
  175. printf ("Reserved Hibernation File Disabled\n");
  176. }
  177. RetStatus = TRUE;
  178. } else {
  179. printf ("Error allocating/deallocating Hibernation file. Status = %x\n", Status);
  180. }
  181. }
  182. }
  183. return(RetStatus);
  184. }
  185. /*
  186. * main
  187. *
  188. * Description:
  189. * This program allocates and deallocates the reserved hibernation file
  190. *
  191. */
  192. int __cdecl
  193. main (argc, argv)
  194. int argc;
  195. char *argv[];
  196. {
  197. /* Assume Failure */
  198. int ErrorStatus = 1;
  199. //
  200. // Parse the input arguments
  201. //
  202. if (ParseArgs(argc, argv)) {
  203. //
  204. // Upgrade permissions & Allocate/Deallocate Hibernation File
  205. //
  206. UpgradePermissions();
  207. if (HiberFile()) {
  208. ErrorStatus = 0;
  209. } else {
  210. ErrorStatus = 1;
  211. }
  212. }
  213. return(ErrorStatus);
  214. }