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.

645 lines
15 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // CClusterUtils.cpp
  7. //
  8. // Description:
  9. // This file contains the definition of the CClusterUtils
  10. // class.
  11. //
  12. // Documentation:
  13. //
  14. // Header File:
  15. // CClusterUtils.h
  16. //
  17. // Maintained By:
  18. // Galen Barbee (GalenB) 14-JUN-2000
  19. //
  20. //////////////////////////////////////////////////////////////////////////////
  21. //////////////////////////////////////////////////////////////////////////////
  22. // Include Files
  23. //////////////////////////////////////////////////////////////////////////////
  24. #include "pch.h"
  25. #include "CClusterUtils.h"
  26. //////////////////////////////////////////////////////////////////////////////
  27. // Constant Definitions
  28. //////////////////////////////////////////////////////////////////////////////
  29. DEFINE_THISCLASS( "CClusterUtils" );
  30. //*************************************************************************//
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CClusterUtils class
  33. /////////////////////////////////////////////////////////////////////////////
  34. //////////////////////////////////////////////////////////////////////////////
  35. //++
  36. //
  37. // CClusterUtils::CClusterUtils
  38. //
  39. // Description:
  40. // Constructor of the CClusterUtils class.
  41. //
  42. // Arguments:
  43. // None.
  44. //
  45. // Return Value:
  46. // None.
  47. //
  48. // Remarks:
  49. // None.
  50. //
  51. //--
  52. //////////////////////////////////////////////////////////////////////////////
  53. CClusterUtils::CClusterUtils( void )
  54. {
  55. TraceFunc( "" );
  56. TraceFuncExit();
  57. } //*** CClusterUtils::CClusterUtils
  58. //////////////////////////////////////////////////////////////////////////////
  59. //++
  60. //
  61. // CClusterUtils::~CClusterUtils
  62. //
  63. // Description:
  64. // Desstructor of the CClusterUtils class.
  65. //
  66. // Arguments:
  67. // None.
  68. //
  69. // Return Value:
  70. // None.
  71. //
  72. // Remarks:
  73. // None.
  74. //
  75. //--
  76. //////////////////////////////////////////////////////////////////////////////
  77. CClusterUtils::~CClusterUtils( void )
  78. {
  79. TraceFunc( "" );
  80. TraceFuncExit();
  81. } //*** CClusterUtils::~CClusterUtils
  82. /////////////////////////////////////////////////////////////////////////////
  83. //++
  84. //
  85. // CClusterUtils::HrIsGroupOwnedByThisNode
  86. //
  87. // Description:
  88. // Is the passed in group owned by the passes in node name?
  89. // Arguments:
  90. //
  91. //
  92. // Return Value:
  93. // S_OK
  94. // The group is owned by the node.
  95. //
  96. // S_FALSE
  97. // The group is not owned by the node.
  98. //
  99. // Win32 Error
  100. // An error occurred.
  101. //
  102. // Remarks:
  103. // None.
  104. //
  105. //--
  106. //////////////////////////////////////////////////////////////////////////////
  107. HRESULT
  108. CClusterUtils::HrIsGroupOwnedByThisNode(
  109. HGROUP hGroupIn,
  110. BSTR bstrNodeNameIn
  111. )
  112. {
  113. TraceFunc1( "bstrNodeNameIn = '%ls'", bstrNodeNameIn == NULL ? L"<null>" : bstrNodeNameIn );
  114. Assert( bstrNodeNameIn != NULL );
  115. HRESULT hr;
  116. DWORD sc;
  117. WCHAR * pszNodeName = NULL;
  118. DWORD cchNodeName = 33;
  119. CLUSTER_GROUP_STATE cgs;
  120. int idx;
  121. pszNodeName = new WCHAR[ cchNodeName ];
  122. if ( pszNodeName == NULL )
  123. {
  124. hr = THR( E_OUTOFMEMORY );
  125. goto Cleanup;
  126. } // if:
  127. for ( idx = 0; ; idx++ )
  128. {
  129. Assert( idx < 2 );
  130. cgs = GetClusterGroupState( hGroupIn, pszNodeName, &cchNodeName );
  131. sc = GetLastError();
  132. if ( sc == ERROR_MORE_DATA )
  133. {
  134. delete [] pszNodeName;
  135. pszNodeName = NULL;
  136. cchNodeName++;
  137. pszNodeName = new WCHAR[ cchNodeName ];
  138. if ( pszNodeName == NULL )
  139. {
  140. hr = THR( E_OUTOFMEMORY );
  141. goto Cleanup;
  142. } // if:
  143. continue;
  144. } // if:
  145. if ( cgs == ClusterGroupStateUnknown )
  146. {
  147. TW32( sc );
  148. hr = HRESULT_FROM_WIN32( sc );
  149. goto Cleanup;
  150. } // if:
  151. if ( ClRtlStrICmp( bstrNodeNameIn, pszNodeName ) == 0 )
  152. {
  153. hr = S_OK;
  154. } // if:
  155. else
  156. {
  157. hr = S_FALSE;
  158. } // else:
  159. break;
  160. } // for:
  161. Cleanup:
  162. delete [] pszNodeName;
  163. HRETURN( hr );
  164. } //*** CClusterUtils::HrIsGroupOwnedByThisNode
  165. /////////////////////////////////////////////////////////////////////////////
  166. //++
  167. //
  168. // CClusterUtils:HrIsNodeClustered
  169. //
  170. // Description:
  171. // Is this node a member of a cluster?
  172. //
  173. // Arguments:
  174. // None.
  175. //
  176. // Return Value:
  177. // S_OK - The node is clustered.
  178. // S_FALSE - The node is NOT clustered.
  179. // HRESULT - Something failed.
  180. //
  181. //--
  182. //////////////////////////////////////////////////////////////////////////////
  183. HRESULT
  184. CClusterUtils::HrIsNodeClustered( void )
  185. {
  186. TraceFunc( "" );
  187. HRESULT hr = S_FALSE;
  188. DWORD sc;
  189. DWORD dwClusterState;
  190. //
  191. // Get the cluster state of the node.
  192. // Ignore the case where the service does not exist so that
  193. // EvictCleanup can do its job.
  194. //
  195. sc = GetNodeClusterState( NULL, &dwClusterState );
  196. if ( ( sc != ERROR_SUCCESS ) && ( sc != ERROR_SERVICE_DOES_NOT_EXIST ) )
  197. {
  198. hr = HRESULT_FROM_WIN32( TW32( sc ) );
  199. goto Cleanup;
  200. } // if : GetClusterState failed
  201. if ( ( dwClusterState == ClusterStateRunning ) || ( dwClusterState == ClusterStateNotRunning ) )
  202. {
  203. hr = S_OK;
  204. } // if:
  205. Cleanup:
  206. HRETURN( hr );
  207. } //*** CClusterUtils::HrIsNodeClustered
  208. /////////////////////////////////////////////////////////////////////////////
  209. //++
  210. //
  211. // CClusterUtils:HrEnumNodeResources
  212. //
  213. // Description:
  214. // Enumerate the resources owned by this node.
  215. //
  216. // Arguments:
  217. // None.
  218. //
  219. // Return Value:
  220. // S_OK
  221. // Success.
  222. //
  223. // Win32 Error
  224. // something failed.
  225. //
  226. // Remarks:
  227. // None.
  228. //
  229. //--
  230. //////////////////////////////////////////////////////////////////////////////
  231. HRESULT
  232. CClusterUtils::HrEnumNodeResources( BSTR bstrNodeNameIn )
  233. {
  234. TraceFunc1( "bstrNodeNameIn = '%ls'", bstrNodeNameIn == NULL ? L"<null>" : bstrNodeNameIn );
  235. HRESULT hr = S_FALSE;
  236. DWORD sc;
  237. DWORD idx;
  238. HCLUSTER hCluster = NULL;
  239. HCLUSENUM hEnum = NULL;
  240. DWORD dwType;
  241. WCHAR * pszGroupName = NULL;
  242. DWORD cchGroupName = 33;
  243. HGROUP hGroup = NULL;
  244. hCluster = OpenCluster( NULL );
  245. if ( hCluster == NULL )
  246. {
  247. sc = TW32( GetLastError() );
  248. hr = HRESULT_FROM_WIN32( sc );
  249. goto Cleanup;
  250. } // if:
  251. hEnum = ClusterOpenEnum( hCluster, CLUSTER_ENUM_GROUP );
  252. if ( hEnum == NULL )
  253. {
  254. sc = TW32( GetLastError() );
  255. hr = HRESULT_FROM_WIN32( sc );
  256. goto Cleanup;
  257. } // if:
  258. pszGroupName = new WCHAR[ cchGroupName ];
  259. if ( pszGroupName == NULL )
  260. {
  261. hr = THR( E_OUTOFMEMORY );
  262. goto Cleanup;
  263. } // if:
  264. for ( idx = 0; ; )
  265. {
  266. sc = ClusterEnum( hEnum, idx, &dwType, pszGroupName, &cchGroupName );
  267. if ( sc == ERROR_SUCCESS )
  268. {
  269. hGroup = OpenClusterGroup( hCluster, pszGroupName );
  270. if ( hGroup == NULL )
  271. {
  272. sc = TW32( GetLastError() );
  273. hr = HRESULT_FROM_WIN32( sc );
  274. goto Cleanup;
  275. } // if:
  276. hr = STHR( HrIsGroupOwnedByThisNode( hGroup, bstrNodeNameIn ) );
  277. if ( FAILED( hr ) )
  278. {
  279. goto Cleanup;
  280. } // if:
  281. if ( hr == S_OK )
  282. {
  283. hr = THR( HrLoadGroupResources( hCluster, hGroup ) );
  284. if ( FAILED( hr ) )
  285. {
  286. goto Cleanup;
  287. } // if:
  288. } // if:
  289. CloseClusterGroup( hGroup );
  290. hGroup = NULL;
  291. idx++;
  292. continue;
  293. } // if:
  294. if ( sc == ERROR_MORE_DATA )
  295. {
  296. delete [] pszGroupName;
  297. pszGroupName = NULL;
  298. cchGroupName++;
  299. pszGroupName = new WCHAR[ cchGroupName ];
  300. if ( pszGroupName == NULL )
  301. {
  302. hr = THR( E_OUTOFMEMORY );
  303. goto Cleanup;
  304. } // if:
  305. continue;
  306. } // if:
  307. if ( sc == ERROR_NO_MORE_ITEMS )
  308. {
  309. hr = S_OK;
  310. break;
  311. } // if:
  312. TW32( sc );
  313. hr = HRESULT_FROM_WIN32( sc );
  314. break;
  315. } // for:
  316. Cleanup:
  317. if ( hGroup != NULL )
  318. {
  319. CloseClusterGroup( hGroup );
  320. } // if:
  321. if ( hEnum != NULL )
  322. {
  323. ClusterCloseEnum( hEnum );
  324. } // if:
  325. if ( hCluster != NULL )
  326. {
  327. CloseCluster( hCluster );
  328. } // if:
  329. delete [] pszGroupName;
  330. HRETURN( hr );
  331. } //*** CClusterUtils::HrEnumNodeResources
  332. /////////////////////////////////////////////////////////////////////////////
  333. //++
  334. //
  335. // CClusterUtils::HrLoadGroupResources
  336. //
  337. // Description:
  338. //
  339. // Arguments:
  340. //
  341. //
  342. // Return Value:
  343. //
  344. //
  345. // Remarks:
  346. // None.
  347. //
  348. //--
  349. //////////////////////////////////////////////////////////////////////////////
  350. HRESULT
  351. CClusterUtils::HrLoadGroupResources(
  352. HCLUSTER hClusterIn,
  353. HGROUP hGroupIn
  354. )
  355. {
  356. TraceFunc( "" );
  357. HRESULT hr = S_OK;
  358. DWORD sc;
  359. HGROUPENUM hEnum = NULL;
  360. WCHAR * pszResourceName = NULL;
  361. DWORD cchResourceName = 33;
  362. DWORD dwType;
  363. DWORD idx;
  364. HRESOURCE hResource = NULL;
  365. hEnum = ClusterGroupOpenEnum( hGroupIn, CLUSTER_GROUP_ENUM_CONTAINS );
  366. if ( hEnum == NULL )
  367. {
  368. sc = TW32( GetLastError() );
  369. hr = HRESULT_FROM_WIN32( sc );
  370. goto Cleanup;
  371. } // if:
  372. pszResourceName = new WCHAR[ cchResourceName ];
  373. if ( pszResourceName == NULL )
  374. {
  375. hr = THR( E_OUTOFMEMORY );
  376. goto Cleanup;
  377. } // if:
  378. for ( idx = 0; ; )
  379. {
  380. sc = ClusterGroupEnum( hEnum, idx, &dwType, pszResourceName, &cchResourceName );
  381. if ( sc == ERROR_SUCCESS )
  382. {
  383. hResource = OpenClusterResource( hClusterIn, pszResourceName );
  384. if ( hResource == NULL )
  385. {
  386. sc = TW32( GetLastError() );
  387. hr = HRESULT_FROM_WIN32( sc );
  388. goto Cleanup;
  389. } // if:
  390. hr = STHR( HrNodeResourceCallback( hClusterIn, hResource ) );
  391. if ( FAILED( hr ) )
  392. {
  393. goto Cleanup;
  394. } // if:
  395. CloseClusterResource( hResource );
  396. hResource = NULL;
  397. idx++;
  398. continue;
  399. } // if:
  400. if ( sc == ERROR_MORE_DATA )
  401. {
  402. delete [] pszResourceName;
  403. pszResourceName = NULL;
  404. cchResourceName++;
  405. pszResourceName = new WCHAR[ cchResourceName ];
  406. if ( pszResourceName == NULL )
  407. {
  408. hr = THR( E_OUTOFMEMORY );
  409. goto Cleanup;
  410. } // if:
  411. continue;
  412. } // if:
  413. if ( sc == ERROR_NO_MORE_ITEMS )
  414. {
  415. hr = S_OK;
  416. break;
  417. } // if:
  418. TW32( sc );
  419. hr = HRESULT_FROM_WIN32( sc );
  420. break;
  421. } // for:
  422. Cleanup:
  423. if ( hResource != NULL )
  424. {
  425. CloseClusterResource( hResource );
  426. } // if:
  427. if ( hEnum != NULL )
  428. {
  429. ClusterGroupCloseEnum( hEnum );
  430. } // if:
  431. delete [] pszResourceName;
  432. HRETURN( hr );
  433. } //*** CClusterUtils::HrLoadGroupResources
  434. /////////////////////////////////////////////////////////////////////////////
  435. //++
  436. //
  437. // CClusterUtils:HrGetQuorumResourceName
  438. //
  439. // Description:
  440. //
  441. //
  442. // Arguments:
  443. // None.
  444. //
  445. // Return Value:
  446. // S_OK
  447. // Success.
  448. //
  449. // Win32 Error
  450. // something failed.
  451. //
  452. // Remarks:
  453. // None.
  454. //
  455. //--
  456. //////////////////////////////////////////////////////////////////////////////
  457. HRESULT
  458. CClusterUtils::HrGetQuorumResourceName(
  459. BSTR * pbstrQuorumResourceNameOut
  460. )
  461. {
  462. TraceFunc( "" );
  463. Assert( pbstrQuorumResourceNameOut != NULL );
  464. HRESULT hr = S_OK;
  465. HCLUSTER hCluster = NULL;
  466. DWORD sc;
  467. WCHAR * pszResourceName = NULL;
  468. DWORD cchResourceName = 33;
  469. WCHAR * pszDeviceName = NULL;
  470. DWORD cchDeviceName = 33;
  471. DWORD cbQuorumLog;
  472. int idx;
  473. hCluster = OpenCluster( NULL );
  474. if ( hCluster == NULL )
  475. {
  476. sc = TW32( GetLastError() );
  477. hr = HRESULT_FROM_WIN32( sc );
  478. goto Cleanup;
  479. } // if:
  480. pszResourceName = new WCHAR[ cchResourceName ];
  481. if ( pszResourceName == NULL )
  482. {
  483. goto OutOfMemory;
  484. } // if:
  485. pszDeviceName = new WCHAR[ cchDeviceName ];
  486. if ( pszDeviceName == NULL )
  487. {
  488. goto OutOfMemory;
  489. } // if:
  490. for ( idx = 0; ; idx++ )
  491. {
  492. Assert( idx < 2 );
  493. sc = GetClusterQuorumResource( hCluster, pszResourceName, &cchResourceName, pszDeviceName, &cchDeviceName, &cbQuorumLog );
  494. if ( sc == ERROR_MORE_DATA )
  495. {
  496. delete [] pszResourceName;
  497. pszResourceName = NULL;
  498. cchResourceName++;
  499. delete [] pszDeviceName;
  500. pszDeviceName = NULL;
  501. cchDeviceName++;
  502. pszResourceName = new WCHAR[ cchResourceName ];
  503. if ( pszResourceName == NULL )
  504. {
  505. goto OutOfMemory;
  506. } // if:
  507. pszDeviceName = new WCHAR[ cchDeviceName ];
  508. if ( pszDeviceName == NULL )
  509. {
  510. goto OutOfMemory;
  511. } // if:
  512. continue;
  513. } // if:
  514. if ( sc == ERROR_SUCCESS )
  515. {
  516. *pbstrQuorumResourceNameOut = TraceSysAllocString( pszResourceName );
  517. if ( *pbstrQuorumResourceNameOut == NULL )
  518. {
  519. goto OutOfMemory;
  520. } // if:
  521. break;
  522. } // if:
  523. TW32( sc );
  524. hr = HRESULT_FROM_WIN32( sc );
  525. goto Cleanup;
  526. } // for:
  527. hr = S_OK;
  528. goto Cleanup;
  529. OutOfMemory:
  530. hr = THR( E_OUTOFMEMORY );
  531. Cleanup:
  532. if ( hCluster != NULL )
  533. {
  534. CloseCluster( hCluster );
  535. } // if:
  536. delete [] pszResourceName;
  537. delete [] pszDeviceName;
  538. HRETURN( hr );
  539. } //*** CClusterUtils::HrGetQuorumResourceName