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.

447 lines
11 KiB

  1. /*****************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1988-1991 **/
  4. /*****************************************************************/
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <process.h>
  9. #include <setjmp.h>
  10. #include <time.h>
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <windows.h>
  15. ULONG Iterations;
  16. struct {
  17. ULONG Flags;
  18. PUCHAR String;
  19. } ActFlags[] = {
  20. POWER_ACTION_QUERY_ALLOWED, "QueryApps",
  21. POWER_ACTION_UI_ALLOWED, "UIAllowed",
  22. POWER_ACTION_OVERRIDE_APPS, "OverrideApps",
  23. POWER_ACTION_DISABLE_WAKES, "DisableWakes",
  24. POWER_ACTION_CRITICAL, "Critical",
  25. 0, NULL
  26. };
  27. PUCHAR
  28. ActionS(
  29. IN POWER_ACTION Act
  30. )
  31. {
  32. static UCHAR line[50];
  33. PUCHAR p;
  34. switch (Act) {
  35. case PowerActionNone: p = "None"; break;
  36. case PowerActionSleep: p = "Sleep"; break;
  37. case PowerActionShutdown: p = "Shutdown"; break;
  38. case PowerActionHibernate: p = "Hibernate"; break;
  39. case PowerActionShutdownReset: p = "ShutdownReset"; break;
  40. case PowerActionShutdownOff: p = "ShutdownOff"; break;
  41. default:
  42. sprintf(line, "Unknown action %x", Act);
  43. p = line;
  44. break;
  45. }
  46. return p;
  47. }
  48. PUCHAR
  49. SysPower(
  50. IN SYSTEM_POWER_STATE State
  51. )
  52. {
  53. static UCHAR line[50];
  54. PUCHAR p;
  55. switch (State) {
  56. case PowerSystemUnspecified: p = "Unspecified"; break;
  57. case PowerSystemWorking: p = "Working"; break;
  58. case PowerSystemSleeping1: p = "S1"; break;
  59. case PowerSystemSleeping2: p = "S2"; break;
  60. case PowerSystemSleeping3: p = "S3"; break;
  61. case PowerSystemHibernate: p = "S4 - hibernate"; break;
  62. case PowerSystemShutdown: p = "Shutdown"; break;
  63. default:
  64. sprintf(line, "Unknown power state %x", State);
  65. p = line;
  66. break;
  67. }
  68. return p;
  69. }
  70. PUCHAR
  71. Action (
  72. IN PBOOLEAN CapFlag,
  73. IN PPOWER_ACTION_POLICY Act
  74. )
  75. {
  76. static UCHAR text[200];
  77. PUCHAR p;
  78. UCHAR c;
  79. ULONG i;
  80. p = text;
  81. if (CapFlag && !*CapFlag) {
  82. p += sprintf(p, "Disabled ");
  83. }
  84. p += sprintf (p, "%s", ActionS(Act->Action));
  85. if (Act->Action != PowerActionNone && Act->Flags) {
  86. c = '(';
  87. for (i=0; ActFlags[i].Flags; i++) {
  88. if (Act->Flags & ActFlags[i].Flags) {
  89. p += sprintf (p, "%c%s", c, ActFlags[i].String);
  90. c = '|';
  91. }
  92. }
  93. p += sprintf (p, ")");
  94. }
  95. if (Act->EventCode) {
  96. p += sprintf (p, "-Code=%x", Act->EventCode);
  97. }
  98. return text;
  99. }
  100. VOID
  101. SetTimerTime (
  102. IN HANDLE h,
  103. IN PUCHAR Text,
  104. IN ULONG DueTimeInMin
  105. )
  106. {
  107. LARGE_INTEGER SystemTime;
  108. LARGE_INTEGER DueTime;
  109. BOOL Status;
  110. SYSTEMTIME TimeFields;
  111. UCHAR s[200];
  112. NtQuerySystemTime (&SystemTime);
  113. GetSystemTime (&TimeFields);
  114. sprintf (s, "%d. Current time is: %d:%d:%d, ",
  115. Iterations,
  116. TimeFields.wHour,
  117. TimeFields.wMinute,
  118. TimeFields.wSecond
  119. );
  120. printf(s);
  121. DbgPrint("SHD: %s", s);
  122. TimeFields.wMinute += (USHORT) DueTimeInMin;
  123. while (TimeFields.wMinute > 59) {
  124. TimeFields.wMinute -= 60;
  125. TimeFields.wHour += 1;
  126. }
  127. sprintf (s, "timer set for %d:%d:%d (%d min) %s",
  128. TimeFields.wHour,
  129. TimeFields.wMinute,
  130. TimeFields.wSecond,
  131. DueTimeInMin,
  132. Text
  133. );
  134. printf(s);
  135. DbgPrint(s);
  136. //
  137. // Set timer as relative
  138. //
  139. DueTime.QuadPart = (ULONGLONG) -600000000L * DueTimeInMin;
  140. Status = SetWaitableTimer (
  141. h,
  142. &DueTime,
  143. 0,
  144. NULL,
  145. NULL,
  146. TRUE
  147. );
  148. if (!Status) {
  149. printf ("\nSetWaitableTimer failed with %x\n", GetLastError());
  150. DbgPrint ("\nSetWaitableTimer failed with %x\n", GetLastError());
  151. exit (1);
  152. }
  153. }
  154. VOID __cdecl
  155. main (argc, argv)
  156. int argc;
  157. char *argv[];
  158. {
  159. POWER_ACTION_POLICY Act;
  160. SYSTEM_POWER_STATE MinSystemState;
  161. NTSTATUS Status;
  162. PUCHAR p;
  163. BOOLEAN Asynchronous;
  164. BOOLEAN MaxLoop;
  165. BOOLEAN SleepLoop;
  166. HANDLE hToken, SleepTimer;
  167. ULONG DelayTime;
  168. ULONG MaxCount;
  169. ULONG Temp;
  170. ULONG WakeTime;
  171. TOKEN_PRIVILEGES tkp;
  172. OpenProcessToken (
  173. GetCurrentProcess(),
  174. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  175. &hToken
  176. );
  177. LookupPrivilegeValue (
  178. NULL,
  179. SE_SHUTDOWN_NAME,
  180. &tkp.Privileges[0].Luid
  181. );
  182. tkp.PrivilegeCount = 1;
  183. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  184. AdjustTokenPrivileges (
  185. hToken,
  186. FALSE,
  187. &tkp,
  188. 0,
  189. NULL,
  190. 0
  191. );
  192. RtlZeroMemory(&Act, sizeof(Act));
  193. MinSystemState = PowerSystemSleeping1;
  194. Asynchronous = TRUE;
  195. SleepLoop = FALSE;
  196. DelayTime = 1;
  197. WakeTime = 2;
  198. Iterations = 0;
  199. if (argc == 1) {
  200. printf ("shd shutdown|off|reset|hiber|sleep|doze [sync qapp ui oapp diswake critical] [loop] [maxloop <Int>] [waitTime <InMinutes>] [delayTime <InMinutes>]\n");
  201. exit (1);
  202. }
  203. while (argc) {
  204. argc--;
  205. p = *argv;
  206. argv += 1;
  207. if (_stricmp(p, "shutdown") == 0) Act.Action = PowerActionShutdown;
  208. if (_stricmp(p, "off") == 0) Act.Action = PowerActionShutdownOff;
  209. if (_stricmp(p, "reset") == 0) Act.Action = PowerActionShutdownReset;
  210. if (_stricmp(p, "hiber") == 0) Act.Action = PowerActionHibernate;
  211. if (_stricmp(p, "sleep") == 0) Act.Action = PowerActionSleep;
  212. if (_stricmp(p, "qapp") == 0) Act.Flags |= POWER_ACTION_QUERY_ALLOWED;
  213. if (_stricmp(p, "ui" ) == 0) Act.Flags |= POWER_ACTION_UI_ALLOWED;
  214. if (_stricmp(p, "oapp") == 0) Act.Flags |= POWER_ACTION_OVERRIDE_APPS;
  215. if (_stricmp(p, "diswake") == 0) Act.Flags |= POWER_ACTION_DISABLE_WAKES;
  216. if (_stricmp(p, "critical") == 0) Act.Flags |= POWER_ACTION_CRITICAL;
  217. if (_stricmp(p, "sync") == 0) Asynchronous = FALSE;
  218. if (_stricmp(p, "loop") == 0) SleepLoop = TRUE;
  219. if (_stricmp(p, "maxloop") == 0) {
  220. if (!argc) {
  221. printf("Must specify an Maximum number with MAXLOOP\n");
  222. exit(1);
  223. }
  224. argc--;
  225. p = *argv;
  226. argv += 1;
  227. Temp = atol(p);
  228. if (Temp) {
  229. MaxCount = Temp;
  230. SleepLoop = TRUE;
  231. MaxLoop = TRUE;
  232. }
  233. }
  234. if (_stricmp(p, "waittime") == 0) {
  235. if (!argc) {
  236. printf("Must Specify an TimeInMinutes number with WAITTIME\n");
  237. exit(1);
  238. }
  239. argc--;
  240. p = *argv;
  241. argv += 1;
  242. Temp = atol(p);
  243. if (Temp) {
  244. WakeTime = Temp;
  245. }
  246. }
  247. if (_stricmp(p, "delaytime") == 0) {
  248. if (!argc) {
  249. printf("Must Specify a TimeInMinutes number with DELAYTIME\n");
  250. exit(1);
  251. }
  252. argc--;
  253. p = *argv;
  254. argv += 1;
  255. Temp = atol(p);
  256. if (Temp) {
  257. DelayTime = Temp;
  258. }
  259. }
  260. }
  261. if (!SleepLoop) {
  262. printf ("Calling NtInitiatePowerAction %s\n",
  263. Asynchronous ? "asynchronous" : "synchronous"
  264. );
  265. printf ("System Action........: %s\n", Action(NULL, &Act));
  266. printf ("Min system state.....: %s\n", SysPower(MinSystemState));
  267. DbgPrint ("SHD: Calling NtInitiatePowerAction %s\n",
  268. Asynchronous ? "asynchronous" : "synchronous"
  269. );
  270. DbgPrint ("SHD: System Action........: %s\n", Action(NULL, &Act));
  271. DbgPrint ("SHD: Min system state.....: %s\n", SysPower(MinSystemState));
  272. Status = NtInitiatePowerAction (
  273. Act.Action,
  274. MinSystemState,
  275. Act.Flags,
  276. Asynchronous
  277. );
  278. goto exit_main;
  279. }
  280. SleepTimer = CreateWaitableTimer (
  281. NULL,
  282. TRUE,
  283. "SleepLoopTimer"
  284. );
  285. //
  286. // Remember that this is iteration #0. Do the boundary condition test
  287. // here since we don't want to do something that the user didn't want
  288. // us to do, God, forbid.
  289. //
  290. Iterations = 0;
  291. if (MaxLoop && Iterations >= MaxCount) {
  292. goto exit_main;
  293. }
  294. //
  295. // Use a while loop here, since we don't actually make use of the
  296. // check unless we have the MaxLoop set
  297. //
  298. while (1) {
  299. //
  300. // Set wake timer
  301. //
  302. SetTimerTime (SleepTimer, "Wake Time", WakeTime);
  303. //
  304. // Hibernate the system
  305. //
  306. printf (" %s\n", Action(NULL, &Act));
  307. DbgPrint (" %s\n", Action(NULL, &Act));
  308. Status = NtInitiatePowerAction (
  309. Act.Action,
  310. MinSystemState,
  311. Act.Flags,
  312. FALSE
  313. );
  314. if (!NT_SUCCESS(Status)) {
  315. printf ("NtInitiatePowerAction failure: %x\n", Status);
  316. DbgPrint ("SHD: NtInitiazePowerAction failure: %x\n", Status);
  317. exit (1);
  318. }
  319. //
  320. // Wait for wake timer
  321. //
  322. Status = WaitForSingleObject (SleepTimer, -1);
  323. if (!NT_SUCCESS(Status)) {
  324. printf ("Wake time wait failed: %x\n", Status);
  325. DbgPrint ("SHD: Wake time wait failed: %x\n", Status);
  326. exit (1);
  327. }
  328. //
  329. // Number of times we've been sucessfull
  330. //
  331. Iterations += 1;
  332. //
  333. // Have we exceeded the number of iterations?
  334. //
  335. if (MaxLoop && Iterations >= MaxCount) {
  336. break;
  337. }
  338. //
  339. // Delay between each loop
  340. //
  341. SetTimerTime (SleepTimer, "Delay\n", DelayTime);
  342. Status = WaitForSingleObject (SleepTimer, -1);
  343. if (!NT_SUCCESS(Status)) {
  344. printf ("Delay wait failed: %x\n", Status);
  345. DbgPrint ("SHD: Delay wait failed: %x\n", Status);
  346. exit (1);
  347. }
  348. }
  349. exit_main:
  350. printf ("Done. Status %x\n", Status);
  351. DbgPrint ("SHD: Done. Status %x\n", Status);
  352. exit (0);
  353. }