Source code of Windows XP (NT5)
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.

644 lines
14 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 ( _wcsicmp( 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
  178. // The node is clustered.
  179. //
  180. // S_FALSE
  181. // The node is not clustered.
  182. //
  183. // Win32 Error
  184. // something failed.
  185. //
  186. // Remarks:
  187. // None.
  188. //
  189. //--
  190. //////////////////////////////////////////////////////////////////////////////
  191. HRESULT
  192. CClusterUtils::HrIsNodeClustered( void )
  193. {
  194. TraceFunc( "" );
  195. HRESULT hr = S_FALSE;
  196. DWORD sc;
  197. DWORD dwClusterState;
  198. sc = TW32( GetNodeClusterState( NULL, &dwClusterState ) );
  199. if ( sc != ERROR_SUCCESS )
  200. {
  201. hr = THR( HRESULT_FROM_WIN32( sc ) );
  202. goto Cleanup;
  203. } // if : GetClusterState() failed
  204. if ( ( dwClusterState == ClusterStateRunning ) || ( dwClusterState == ClusterStateNotRunning ) )
  205. {
  206. hr = S_OK;
  207. } // if:
  208. Cleanup:
  209. HRETURN( hr );
  210. } //*** CClusterUtils::HrIsNodeClustered()
  211. /////////////////////////////////////////////////////////////////////////////
  212. //++
  213. //
  214. // CClusterUtils:HrEnumNodeResources
  215. //
  216. // Description:
  217. // Enumerate the resources owned by this node.
  218. //
  219. // Arguments:
  220. // None.
  221. //
  222. // Return Value:
  223. // S_OK
  224. // Success.
  225. //
  226. // Win32 Error
  227. // something failed.
  228. //
  229. // Remarks:
  230. // None.
  231. //
  232. //--
  233. //////////////////////////////////////////////////////////////////////////////
  234. HRESULT
  235. CClusterUtils::HrEnumNodeResources( BSTR bstrNodeNameIn )
  236. {
  237. TraceFunc1( "bstrNodeNameIn = '%ls'", bstrNodeNameIn == NULL ? L"<null>" : bstrNodeNameIn );
  238. HRESULT hr = S_FALSE;
  239. DWORD sc;
  240. DWORD idx;
  241. HCLUSTER hCluster = NULL;
  242. HCLUSENUM hEnum = NULL;
  243. DWORD dwType;
  244. WCHAR * pszGroupName = NULL;
  245. DWORD cchGroupName = 33;
  246. HGROUP hGroup = NULL;
  247. hCluster = OpenCluster( NULL );
  248. if ( hCluster == NULL )
  249. {
  250. sc = TW32( GetLastError() );
  251. hr = HRESULT_FROM_WIN32( hr );
  252. goto Cleanup;
  253. } // if:
  254. hEnum = ClusterOpenEnum( hCluster, CLUSTER_ENUM_GROUP );
  255. if ( hEnum == NULL )
  256. {
  257. sc = TW32( GetLastError() );
  258. hr = HRESULT_FROM_WIN32( hr );
  259. goto Cleanup;
  260. } // if:
  261. pszGroupName = new WCHAR[ cchGroupName ];
  262. if ( pszGroupName == NULL )
  263. {
  264. hr = THR( E_OUTOFMEMORY );
  265. goto Cleanup;
  266. } // if:
  267. for ( idx = 0; ; )
  268. {
  269. sc = ClusterEnum( hEnum, idx, &dwType, pszGroupName, &cchGroupName );
  270. if ( sc == ERROR_SUCCESS )
  271. {
  272. hGroup = OpenClusterGroup( hCluster, pszGroupName );
  273. if ( hGroup == NULL )
  274. {
  275. sc = TW32( GetLastError() );
  276. hr = HRESULT_FROM_WIN32( sc );
  277. goto Cleanup;
  278. } // if:
  279. hr = STHR( HrIsGroupOwnedByThisNode( hGroup, bstrNodeNameIn ) );
  280. if ( FAILED( hr ) )
  281. {
  282. goto Cleanup;
  283. } // if:
  284. if ( hr == S_OK )
  285. {
  286. hr = THR( HrLoadGroupResources( hCluster, hGroup ) );
  287. } // if:
  288. CloseClusterGroup( hGroup );
  289. hGroup = NULL;
  290. idx++;
  291. continue;
  292. } // if:
  293. if ( sc == ERROR_MORE_DATA )
  294. {
  295. delete [] pszGroupName;
  296. pszGroupName = NULL;
  297. cchGroupName++;
  298. pszGroupName = new WCHAR[ cchGroupName ];
  299. if ( pszGroupName == NULL )
  300. {
  301. hr = THR( E_OUTOFMEMORY );
  302. goto Cleanup;
  303. } // if:
  304. continue;
  305. } // if:
  306. if ( sc == ERROR_NO_MORE_ITEMS )
  307. {
  308. hr = S_OK;
  309. break;
  310. } // if:
  311. TW32( sc );
  312. hr = HRESULT_FROM_WIN32( sc );
  313. break;
  314. } // for:
  315. Cleanup:
  316. if ( hGroup != NULL )
  317. {
  318. CloseClusterGroup( hGroup );
  319. } // if:
  320. if ( hEnum != NULL )
  321. {
  322. ClusterCloseEnum( hEnum );
  323. } // if:
  324. if ( hCluster != NULL )
  325. {
  326. CloseCluster( hCluster );
  327. } // if:
  328. delete [] pszGroupName;
  329. HRETURN( hr );
  330. } //*** CClusterUtils::HrEnumNodeResources()
  331. /////////////////////////////////////////////////////////////////////////////
  332. //++
  333. //
  334. // CClusterUtils::HrLoadGroupResources()
  335. //
  336. // Description:
  337. //
  338. // Arguments:
  339. //
  340. //
  341. // Return Value:
  342. //
  343. //
  344. // Remarks:
  345. // None.
  346. //
  347. //--
  348. //////////////////////////////////////////////////////////////////////////////
  349. HRESULT
  350. CClusterUtils::HrLoadGroupResources(
  351. HCLUSTER hClusterIn,
  352. HGROUP hGroupIn
  353. )
  354. {
  355. TraceFunc( "" );
  356. HRESULT hr = S_OK;
  357. DWORD sc;
  358. HGROUPENUM hEnum = NULL;
  359. WCHAR * pszResourceName = NULL;
  360. DWORD cchResourceName = 33;
  361. DWORD dwType;
  362. DWORD idx;
  363. HRESOURCE hResource = NULL;
  364. hEnum = ClusterGroupOpenEnum( hGroupIn, CLUSTER_GROUP_ENUM_CONTAINS );
  365. if ( hEnum == NULL )
  366. {
  367. sc = TW32( GetLastError() );
  368. hr = HRESULT_FROM_WIN32( sc );
  369. goto Cleanup;
  370. } // if:
  371. pszResourceName = new WCHAR[ cchResourceName ];
  372. if ( pszResourceName == NULL )
  373. {
  374. hr = THR( E_OUTOFMEMORY );
  375. goto Cleanup;
  376. } // if:
  377. for ( idx = 0; ; )
  378. {
  379. sc = ClusterGroupEnum( hEnum, idx, &dwType, pszResourceName, &cchResourceName );
  380. if ( sc == ERROR_SUCCESS )
  381. {
  382. hResource = OpenClusterResource( hClusterIn, pszResourceName );
  383. if ( hResource == NULL )
  384. {
  385. sc = TW32( GetLastError() );
  386. hr = HRESULT_FROM_WIN32( sc );
  387. goto Cleanup;
  388. } // if:
  389. hr = STHR( HrNodeResourceCallback( hClusterIn, hResource ) );
  390. if ( FAILED( hr ) )
  391. {
  392. goto Cleanup;
  393. } // if:
  394. CloseClusterResource( hResource );
  395. hResource = NULL;
  396. idx++;
  397. continue;
  398. } // if:
  399. if ( sc == ERROR_MORE_DATA )
  400. {
  401. delete [] pszResourceName;
  402. pszResourceName = NULL;
  403. cchResourceName++;
  404. pszResourceName = new WCHAR[ cchResourceName ];
  405. if ( pszResourceName == NULL )
  406. {
  407. hr = THR( E_OUTOFMEMORY );
  408. goto Cleanup;
  409. } // if:
  410. continue;
  411. } // if:
  412. if ( sc == ERROR_NO_MORE_ITEMS )
  413. {
  414. hr = S_OK;
  415. break;
  416. } // if:
  417. TW32( sc );
  418. hr = HRESULT_FROM_WIN32( sc );
  419. break;
  420. } // for:
  421. Cleanup:
  422. if ( hResource != NULL )
  423. {
  424. CloseClusterResource( hResource );
  425. } // if:
  426. if ( hEnum != NULL )
  427. {
  428. ClusterGroupCloseEnum( hEnum );
  429. } // if:
  430. delete [] pszResourceName;
  431. HRETURN( hr );
  432. } //*** CClusterUtils::HrLoadGroupResources()
  433. /////////////////////////////////////////////////////////////////////////////
  434. //++
  435. //
  436. // CClusterUtils:HrGetQuorumResourceName()
  437. //
  438. // Description:
  439. //
  440. //
  441. // Arguments:
  442. // None.
  443. //
  444. // Return Value:
  445. // S_OK
  446. // Success.
  447. //
  448. // Win32 Error
  449. // something failed.
  450. //
  451. // Remarks:
  452. // None.
  453. //
  454. //--
  455. //////////////////////////////////////////////////////////////////////////////
  456. HRESULT
  457. CClusterUtils::HrGetQuorumResourceName(
  458. BSTR * pbstrQuorumResourceNameOut
  459. )
  460. {
  461. TraceFunc( "" );
  462. Assert( pbstrQuorumResourceNameOut != NULL );
  463. HRESULT hr = S_OK;
  464. HCLUSTER hCluster = NULL;
  465. DWORD sc;
  466. WCHAR * pszResourceName = NULL;
  467. DWORD cchResourceName = 33;
  468. WCHAR * pszDeviceName = NULL;
  469. DWORD cchDeviceName = 33;
  470. DWORD cbQuorumLog;
  471. int idx;
  472. hCluster = OpenCluster( NULL );
  473. if ( hCluster == NULL )
  474. {
  475. sc = TW32( GetLastError() );
  476. hr = HRESULT_FROM_WIN32( sc );
  477. goto Cleanup;
  478. } // if:
  479. pszResourceName = new WCHAR[ cchResourceName ];
  480. if ( pszResourceName == NULL )
  481. {
  482. goto OutOfMemory;
  483. } // if:
  484. pszDeviceName = new WCHAR[ cchDeviceName ];
  485. if ( pszDeviceName == NULL )
  486. {
  487. goto OutOfMemory;
  488. } // if:
  489. for ( idx = 0; ; idx++ )
  490. {
  491. Assert( idx < 2 );
  492. sc = GetClusterQuorumResource( hCluster, pszResourceName, &cchResourceName, pszDeviceName, &cchDeviceName, &cbQuorumLog );
  493. if ( sc == ERROR_MORE_DATA )
  494. {
  495. delete [] pszResourceName;
  496. pszResourceName = NULL;
  497. cchResourceName++;
  498. delete [] pszDeviceName;
  499. pszDeviceName = NULL;
  500. cchDeviceName++;
  501. pszResourceName = new WCHAR[ cchResourceName ];
  502. if ( pszResourceName == NULL )
  503. {
  504. goto OutOfMemory;
  505. } // if:
  506. pszDeviceName = new WCHAR[ cchDeviceName ];
  507. if ( pszDeviceName == NULL )
  508. {
  509. goto OutOfMemory;
  510. } // if:
  511. continue;
  512. } // if:
  513. if ( sc == ERROR_SUCCESS )
  514. {
  515. *pbstrQuorumResourceNameOut = TraceSysAllocString( pszResourceName );
  516. if ( *pbstrQuorumResourceNameOut == NULL )
  517. {
  518. goto OutOfMemory;
  519. } // if:
  520. break;
  521. } // if:
  522. TW32( sc );
  523. hr = HRESULT_FROM_WIN32( sc );
  524. goto Cleanup;
  525. } // for:
  526. hr = S_OK;
  527. goto Cleanup;
  528. OutOfMemory:
  529. hr = THR( E_OUTOFMEMORY );
  530. Cleanup:
  531. if ( hCluster != NULL )
  532. {
  533. CloseCluster( hCluster );
  534. } // if:
  535. delete [] pszResourceName;
  536. delete [] pszDeviceName;
  537. HRETURN( hr );
  538. } //*** CClusterUtils::HrGetQuorumResourceName()