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.

259 lines
7.7 KiB

  1. /*++
  2. Copyright (C) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. smrootnd.cpp
  5. Abstract:
  6. This object is used to represent the Performance Logs and Alerts root node
  7. --*/
  8. #include "Stdafx.h"
  9. #include "smrootnd.h"
  10. USE_HANDLE_MACROS("SMLOGCFG(smrootnd.cpp)");
  11. //
  12. // Constructor
  13. CSmRootNode::CSmRootNode()
  14. : m_bIsExpanded ( FALSE ),
  15. m_hRootNode ( NULL ),
  16. m_hParentNode ( NULL ),
  17. m_bIsExtension ( FALSE )
  18. {
  19. CString strTemp;
  20. ResourceStateManager rsm;
  21. // String allocation errors are thrown, to be
  22. // captured by rootnode alloc exception handler
  23. strTemp.LoadString ( IDS_MMC_DEFAULT_NAME );
  24. SetDisplayName ( strTemp );
  25. strTemp.LoadString ( IDS_ROOT_NODE_DESCRIPTION );
  26. SetDescription ( strTemp );
  27. strTemp.LoadString ( IDS_EXTENSION_COL_TYPE );
  28. SetType ( strTemp );
  29. return;
  30. }
  31. //
  32. // Destructor
  33. CSmRootNode::~CSmRootNode()
  34. {
  35. ASSERT (m_CounterLogService.m_QueryList.GetHeadPosition() == NULL);
  36. ASSERT (m_TraceLogService.m_QueryList.GetHeadPosition() == NULL);
  37. ASSERT (m_AlertService.m_QueryList.GetHeadPosition() == NULL);
  38. return;
  39. }
  40. void
  41. CSmRootNode::Destroy()
  42. {
  43. m_CounterLogService.Close();
  44. m_TraceLogService.Close();
  45. m_AlertService.Close();
  46. return;
  47. }
  48. BOOL
  49. CSmRootNode::IsLogService (
  50. MMC_COOKIE mmcCookie )
  51. {
  52. BOOL bReturn = FALSE;
  53. if (mmcCookie == (MMC_COOKIE)&m_CounterLogService) {
  54. bReturn = TRUE;
  55. } else if (mmcCookie == (MMC_COOKIE)&m_TraceLogService) {
  56. bReturn = TRUE;
  57. } else if (mmcCookie == (MMC_COOKIE)&m_AlertService) {
  58. bReturn = TRUE;
  59. }
  60. return bReturn;
  61. }
  62. BOOL
  63. CSmRootNode::IsAlertService (
  64. MMC_COOKIE mmcCookie )
  65. {
  66. BOOL bReturn = FALSE;
  67. if (mmcCookie == (MMC_COOKIE)&m_AlertService) {
  68. bReturn = TRUE;
  69. }
  70. return bReturn;
  71. }
  72. BOOL
  73. CSmRootNode::IsLogQuery (
  74. MMC_COOKIE mmcCookie )
  75. {
  76. PSLQUERY pPlQuery = NULL;
  77. POSITION Pos;
  78. // Handle multiple query types
  79. Pos = m_CounterLogService.m_QueryList.GetHeadPosition();
  80. while ( Pos != NULL) {
  81. pPlQuery = m_CounterLogService.m_QueryList.GetNext( Pos );
  82. if ((MMC_COOKIE)pPlQuery == mmcCookie) return TRUE;
  83. }
  84. Pos = m_TraceLogService.m_QueryList.GetHeadPosition();
  85. while ( Pos != NULL) {
  86. pPlQuery = m_TraceLogService.m_QueryList.GetNext( Pos );
  87. if ((MMC_COOKIE)pPlQuery == mmcCookie) return TRUE;
  88. }
  89. Pos = m_AlertService.m_QueryList.GetHeadPosition();
  90. while ( Pos != NULL) {
  91. pPlQuery = m_AlertService.m_QueryList.GetNext( Pos );
  92. if ((MMC_COOKIE)pPlQuery == mmcCookie) return TRUE;
  93. }
  94. return FALSE;
  95. }
  96. DWORD
  97. CSmRootNode::UpdateServiceConfig()
  98. {
  99. // If any queries are (newly) set to auto start, then set the
  100. // service to auto start. Otherwise, set to manual start.
  101. // When setting to auto start, also set failure mode to restart
  102. DWORD dwStatus = ERROR_SUCCESS;
  103. BOOL bStatus = 0;
  104. SC_HANDLE hSC = NULL;
  105. SC_HANDLE hService = NULL;
  106. BOOL bAutoStart = FALSE;
  107. DWORD pqsConfigBuff[128];
  108. QUERY_SERVICE_CONFIG* pqsConfig;
  109. SC_ACTION* parrSingleFailAction = NULL;
  110. SERVICE_FAILURE_ACTIONS structFailActions;
  111. DWORD dwMoreBytes = 0;
  112. BOOL bUpdate = FALSE;
  113. bAutoStart = ( m_CounterLogService.IsAutoStart()
  114. || m_TraceLogService.IsAutoStart()
  115. || m_AlertService.IsAutoStart() );
  116. // open SC database
  117. hSC = OpenSCManager ( GetMachineName(), NULL, GENERIC_READ );
  118. if (hSC != NULL) {
  119. // open service
  120. hService = OpenService (
  121. hSC,
  122. L"SysmonLog",
  123. SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG | SERVICE_START );
  124. if (hService != NULL) {
  125. // get current config
  126. memset (pqsConfigBuff, 0, sizeof(pqsConfigBuff));
  127. pqsConfig = (QUERY_SERVICE_CONFIG*)pqsConfigBuff;
  128. if ( QueryServiceConfig (
  129. hService,
  130. pqsConfig,
  131. sizeof(pqsConfigBuff),
  132. &dwMoreBytes)) {
  133. // See if the current status is different
  134. // from the selection. If it is, then change
  135. // the current mode.
  136. if ( bAutoStart ) {
  137. if ( SERVICE_DEMAND_START == pqsConfig->dwStartType ) {
  138. bUpdate = TRUE;
  139. }
  140. } else {
  141. // Manual start selected
  142. if ( SERVICE_AUTO_START == pqsConfig->dwStartType ) {
  143. bUpdate = TRUE;
  144. }
  145. }
  146. } else {
  147. // else unable to read the current status so update anyway
  148. bUpdate = TRUE;
  149. }
  150. if ( bUpdate ) {
  151. MFC_TRY
  152. parrSingleFailAction = new SC_ACTION[3];
  153. MFC_CATCH_DWSTATUS;
  154. if ( NULL != parrSingleFailAction ) {
  155. parrSingleFailAction[0].Delay = eRestartDelayMilliseconds;
  156. parrSingleFailAction[1].Delay = eRestartDelayMilliseconds;
  157. parrSingleFailAction[2].Delay = eRestartDelayMilliseconds;
  158. if ( bAutoStart ) {
  159. parrSingleFailAction[0].Type = SC_ACTION_RESTART;
  160. parrSingleFailAction[1].Type = SC_ACTION_RESTART;
  161. parrSingleFailAction[2].Type = SC_ACTION_RESTART;
  162. } else {
  163. parrSingleFailAction[0].Type = SC_ACTION_NONE;
  164. parrSingleFailAction[1].Type = SC_ACTION_NONE;
  165. parrSingleFailAction[2].Type = SC_ACTION_NONE;
  166. }
  167. structFailActions.dwResetPeriod = eResetDelaySeconds;
  168. structFailActions.lpRebootMsg = NULL;
  169. structFailActions.lpCommand = NULL;
  170. structFailActions.cActions = 3;
  171. structFailActions.lpsaActions = parrSingleFailAction;
  172. bStatus = ChangeServiceConfig (
  173. hService,
  174. SERVICE_NO_CHANGE,
  175. (bAutoStart ? SERVICE_AUTO_START : SERVICE_DEMAND_START),
  176. SERVICE_NO_CHANGE,
  177. NULL,
  178. NULL,
  179. NULL,
  180. NULL,
  181. NULL,
  182. NULL,
  183. NULL );
  184. if ( 0 == bStatus ) {
  185. dwStatus = GetLastError();
  186. } else {
  187. bStatus = ChangeServiceConfig2 (
  188. hService,
  189. SERVICE_CONFIG_FAILURE_ACTIONS,
  190. &structFailActions );
  191. if ( 0 == bStatus ) {
  192. dwStatus = GetLastError();
  193. }
  194. }
  195. delete [] parrSingleFailAction;
  196. } else {
  197. dwStatus = ERROR_OUTOFMEMORY;
  198. }
  199. }
  200. CloseServiceHandle (hService);
  201. } else {
  202. dwStatus = GetLastError();
  203. ASSERT (dwStatus != 0);
  204. }
  205. CloseServiceHandle (hSC);
  206. } else {
  207. dwStatus = GetLastError();
  208. } // OpenSCManager
  209. return dwStatus;
  210. }