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.

172 lines
4.4 KiB

  1. /* Copyright 1999 American Power Conversion, All Rights Reserved
  2. *
  3. * Description:
  4. * The file implements the CommandExecutor. The CommandExecutor
  5. * is responsible for executing command just prior to shutdown.
  6. *
  7. *
  8. * Revision History:
  9. * sberard 01Apr1999 initial revision.
  10. * mholly 16Apr1999 run old command file if task is invalid
  11. * v-stebe 23May2000 add check to the return value of CoInitialize() (bug #112597)
  12. *
  13. */
  14. #define INITGUID
  15. #include <mstask.h>
  16. #include "cmdexe.h"
  17. #include "upsreg.h"
  18. static BOOL runOldCommandFile();
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * ExecuteShutdownTask
  24. *
  25. * Description:
  26. * This function initiates the execution of the shutdown task. The
  27. * shutdown task is used to execute commands at shutdown. The task
  28. * to execute is specified in the following registry key:
  29. * HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\UPS\Config\TaskName
  30. *
  31. * Parameters:
  32. * none
  33. *
  34. * Returns:
  35. * TRUE - if the command was executed
  36. * FALSE - if there was an error executing the command
  37. */
  38. BOOL ExecuteShutdownTask() {
  39. BOOL ret_val = FALSE;
  40. TCHAR task_name[MAX_PATH];
  41. DWORD task_name_len = sizeof(task_name);
  42. HRESULT hr;
  43. ITaskScheduler *task_sched;
  44. ITask *shutdown_task;
  45. InitUPSConfigBlock();
  46. // Get the name of the Task to run from the registry
  47. if (GetUPSConfigTaskName((LPTSTR) task_name, MAX_PATH) == ERROR_SUCCESS) {
  48. // Initialize COM
  49. if (CoInitialize(NULL) == S_OK) {
  50. // Get a handle to the ITaskScheduler COM Object
  51. hr = CoCreateInstance(&CLSID_CSchedulingAgent,
  52. NULL,
  53. CLSCTX_INPROC_SERVER,
  54. &IID_ISchedulingAgent,
  55. (LPVOID *)&task_sched);
  56. if (hr == S_OK) {
  57. if (task_sched->lpVtbl->Activate(task_sched, task_name, &IID_ITask,
  58. (IUnknown**)&shutdown_task) == S_OK) {
  59. shutdown_task->lpVtbl->Run(shutdown_task);
  60. // Release the instance of the task
  61. shutdown_task->lpVtbl->Release(shutdown_task);
  62. ret_val = TRUE;
  63. }
  64. else {
  65. ret_val = runOldCommandFile();
  66. }
  67. }
  68. // Uninitialize COM
  69. CoUninitialize();
  70. }
  71. else {
  72. // There was an error initializing COM (probably out of mem.)
  73. ret_val = FALSE;
  74. }
  75. }
  76. else {
  77. ret_val = runOldCommandFile();
  78. }
  79. return ret_val;
  80. }
  81. // UPS Service Registry values
  82. #define REGISTRY_UPS_DIRECTORY L"System\\CurrentControlSet\\Services\\UPS"
  83. #define REGISTRY_COMMAND_FILE L"CommandFile"
  84. DWORD UpsRegistryGetString(LPTSTR SubKey, LPTSTR Buffer, DWORD BufferSize)
  85. {
  86. DWORD status;
  87. DWORD type;
  88. HKEY RegistryKey;
  89. status = RegOpenKeyEx(
  90. HKEY_LOCAL_MACHINE,
  91. REGISTRY_UPS_DIRECTORY,
  92. 0,
  93. KEY_READ,
  94. &RegistryKey);
  95. if (ERROR_SUCCESS == status) {
  96. status = RegQueryValueEx(
  97. RegistryKey,
  98. SubKey,
  99. NULL,
  100. &type,
  101. (LPBYTE)Buffer,
  102. &BufferSize);
  103. RegCloseKey(RegistryKey);
  104. }
  105. return status;
  106. }
  107. BOOL runOldCommandFile()
  108. {
  109. PROCESS_INFORMATION ProcessInformation;
  110. STARTUPINFO StartupInfo;
  111. BOOL success;
  112. DWORD status;
  113. TCHAR command_file[_MAX_PATH];
  114. status = UpsRegistryGetString(REGISTRY_COMMAND_FILE,
  115. command_file, sizeof(command_file));
  116. if (ERROR_SUCCESS != status) {
  117. //
  118. // there isn't a command file configured
  119. // so just exit now reporting that we did
  120. // not run anything
  121. //
  122. return FALSE;
  123. }
  124. GetStartupInfo(&StartupInfo);
  125. StartupInfo.lpTitle = NULL;
  126. success = CreateProcess(
  127. NULL, // image name is imbedded in the command line
  128. command_file, // command line
  129. NULL, // pSecAttrProcess
  130. NULL, // pSecAttrThread
  131. FALSE, // this process will not inherit our handles
  132. 0, // dwCreationFlags
  133. NULL, // pEnvironment
  134. NULL, // pCurrentDirectory
  135. &StartupInfo,
  136. &ProcessInformation
  137. );
  138. return success;
  139. }
  140. #ifdef __cplusplus
  141. }
  142. #endif