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.

652 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Common utility routines for clusters resources
  7. Author:
  8. John Vert (jvert) 12/15/1996
  9. Revision History:
  10. --*/
  11. #include "clusres.h"
  12. #include "clusrtl.h"
  13. #include "clusudef.h"
  14. DWORD
  15. ClusResOpenDriver(
  16. HANDLE *Handle,
  17. LPWSTR DriverName
  18. )
  19. /*++
  20. Routine Description:
  21. This function opens a specified IO drivers.
  22. Arguments:
  23. Handle - pointer to location where the opened drivers handle is
  24. returned.
  25. DriverName - name of the driver to be opened.
  26. Return Value:
  27. Windows Error Code.
  28. --*/
  29. {
  30. OBJECT_ATTRIBUTES objectAttributes;
  31. IO_STATUS_BLOCK ioStatusBlock;
  32. UNICODE_STRING nameString;
  33. NTSTATUS status;
  34. *Handle = NULL;
  35. //
  36. // Open a Handle to the IP driver.
  37. //
  38. RtlInitUnicodeString(&nameString, DriverName);
  39. InitializeObjectAttributes(
  40. &objectAttributes,
  41. &nameString,
  42. OBJ_CASE_INSENSITIVE,
  43. (HANDLE) NULL,
  44. (PSECURITY_DESCRIPTOR) NULL
  45. );
  46. status = NtCreateFile(
  47. Handle,
  48. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA,
  49. &objectAttributes,
  50. &ioStatusBlock,
  51. NULL,
  52. FILE_ATTRIBUTE_NORMAL,
  53. FILE_SHARE_READ | FILE_SHARE_WRITE,
  54. FILE_OPEN_IF,
  55. 0,
  56. NULL,
  57. 0
  58. );
  59. return( RtlNtStatusToDosError( status ) );
  60. } // ClusResOpenDriver
  61. NTSTATUS
  62. ClusResDoIoctl(
  63. HANDLE Handle,
  64. DWORD IoctlCode,
  65. PVOID Request,
  66. DWORD RequestSize,
  67. PVOID Response,
  68. PDWORD ResponseSize
  69. )
  70. /*++
  71. Routine Description:
  72. Utility routine used to issue a filtering ioctl to the tcpip driver.
  73. Arguments:
  74. Handle - An open file handle on which to issue the request.
  75. IoctlCode - The IOCTL opcode.
  76. Request - A pointer to the input buffer.
  77. RequestSize - Size of the input buffer.
  78. Response - A pointer to the output buffer.
  79. ResponseSize - On input, the size in bytes of the output buffer.
  80. On output, the number of bytes returned in the output buffer.
  81. Return Value:
  82. NT Status Code.
  83. --*/
  84. {
  85. IO_STATUS_BLOCK ioStatusBlock;
  86. NTSTATUS status;
  87. ioStatusBlock.Information = 0;
  88. status = NtDeviceIoControlFile(
  89. Handle, // Driver handle
  90. NULL, // Event
  91. NULL, // APC Routine
  92. NULL, // APC context
  93. &ioStatusBlock, // Status block
  94. IoctlCode, // Control code
  95. Request, // Input buffer
  96. RequestSize, // Input buffer size
  97. Response, // Output buffer
  98. *ResponseSize // Output buffer size
  99. );
  100. if (status == STATUS_PENDING) {
  101. status = NtWaitForSingleObject(
  102. Handle,
  103. TRUE,
  104. NULL
  105. );
  106. }
  107. if (status == STATUS_SUCCESS) {
  108. status = ioStatusBlock.Status;
  109. *ResponseSize = (DWORD)ioStatusBlock.Information;
  110. }
  111. else {
  112. *ResponseSize = 0;
  113. }
  114. return(status);
  115. } // ClusResDoIoctl
  116. LPWSTR
  117. ClusResLoadMessage(
  118. DWORD MessageID
  119. )
  120. /*++
  121. Routine Description:
  122. Look up the specified string resource as stored in this DLL's resource
  123. area. Caller is responsible for freeing the buffer with LocalFree().
  124. Arguments:
  125. MessageID - message number as stored in inc\clusstrs.h
  126. Return Value:
  127. pointer to string, otherwise NULL with GLE set
  128. --*/
  129. {
  130. DWORD charsCopied;
  131. DWORD charsAllocated = 0;
  132. LPWSTR messageBuffer;
  133. HMODULE clusresHandle;
  134. DWORD returnStatus = ERROR_SUCCESS;
  135. //
  136. // get a handle to clusres
  137. //
  138. clusresHandle = LoadLibraryEx( CLUSRES_MODULE_NAME, NULL, LOAD_LIBRARY_AS_DATAFILE );
  139. if ( clusresHandle == NULL ) {
  140. return NULL;
  141. }
  142. //
  143. // start with 128 char buffer and double until we fail or we get all of the
  144. // string.
  145. //
  146. charsAllocated = 128;
  147. realloc:
  148. charsCopied = 0;
  149. messageBuffer = LocalAlloc( LMEM_FIXED, charsAllocated * sizeof( WCHAR ));
  150. if ( messageBuffer ) {
  151. charsCopied = LoadString(clusresHandle,
  152. MessageID,
  153. messageBuffer,
  154. charsAllocated);
  155. if ( charsCopied != 0 ) {
  156. if ( charsCopied == ( charsAllocated - 1 )) {
  157. LocalFree( messageBuffer );
  158. charsAllocated *= 2;
  159. goto realloc;
  160. }
  161. } else {
  162. returnStatus = GetLastError();
  163. LocalFree( messageBuffer );
  164. messageBuffer = NULL;
  165. }
  166. } else {
  167. returnStatus = ERROR_NOT_ENOUGH_MEMORY;
  168. }
  169. FreeLibrary( clusresHandle );
  170. //
  171. // if LoadString failed, set last error to its error status and not
  172. // FreeLibrary's
  173. //
  174. if ( returnStatus != ERROR_SUCCESS ) {
  175. SetLastError( returnStatus );
  176. }
  177. return messageBuffer;
  178. } // ClusResLoadMessage
  179. VOID
  180. ClusResLogEventWithName0(
  181. IN HKEY hResourceKey,
  182. IN DWORD LogLevel,
  183. IN DWORD LogModule,
  184. IN LPSTR FileName,
  185. IN DWORD LineNumber,
  186. IN DWORD MessageId,
  187. IN DWORD dwByteCount,
  188. IN PVOID lpBytes
  189. )
  190. /*++
  191. Routine Description:
  192. Logs an event to the eventlog. The display name of the resource is retrieved
  193. and passed as the first insertion string.
  194. Arguments:
  195. hResourceKey - Supplies the cluster resource key.
  196. LogLevel - Supplies the logging level, one of
  197. LOG_CRITICAL 1
  198. LOG_UNUSUAL 2
  199. LOG_NOISE 3
  200. LogModule - Supplies the module ID.
  201. FileName - Supplies the filename of the caller
  202. LineNumber - Supplies the line number of the caller
  203. MessageId - Supplies the message ID to be logged.
  204. dwByteCount - Supplies the number of error-specific bytes to log. If this
  205. is zero, lpBytes is ignored.
  206. lpBytes - Supplies the error-specific bytes to log.
  207. Return Value:
  208. ERROR_SUCCESS if successful
  209. Win32 error code otherwise
  210. --*/
  211. {
  212. DWORD BufSize;
  213. DWORD Status;
  214. WCHAR ResourceName[80];
  215. PWCHAR resName = ResourceName;
  216. DWORD dwType;
  217. //
  218. // Get the display name for this resource.
  219. //
  220. BufSize = sizeof( ResourceName );
  221. again:
  222. Status = ClusterRegQueryValue( hResourceKey,
  223. CLUSREG_NAME_RES_NAME,
  224. &dwType,
  225. (LPBYTE)resName,
  226. &BufSize );
  227. if ( Status == ERROR_MORE_DATA ) {
  228. resName = LocalAlloc( LMEM_FIXED, BufSize );
  229. if ( resName != NULL ) {
  230. goto again;
  231. }
  232. resName = ResourceName;
  233. ResourceName[0] = UNICODE_NULL;
  234. } else if ( Status != ERROR_SUCCESS ) {
  235. ResourceName[0] = '\0';
  236. }
  237. ClusterLogEvent1(LogLevel,
  238. LogModule,
  239. FileName,
  240. LineNumber,
  241. MessageId,
  242. dwByteCount,
  243. lpBytes,
  244. resName);
  245. if ( resName != ResourceName ) {
  246. LocalFree( resName );
  247. }
  248. return;
  249. } // ClusResLogEventWithName0
  250. VOID
  251. ClusResLogEventWithName1(
  252. IN HKEY hResourceKey,
  253. IN DWORD LogLevel,
  254. IN DWORD LogModule,
  255. IN LPSTR FileName,
  256. IN DWORD LineNumber,
  257. IN DWORD MessageId,
  258. IN DWORD dwByteCount,
  259. IN PVOID lpBytes,
  260. IN LPCWSTR Arg1
  261. )
  262. /*++
  263. Routine Description:
  264. Logs an event to the eventlog. The display name of the resource is retrieved
  265. and passed as the first insertion string.
  266. Arguments:
  267. hResourceKey - Supplies the cluster resource key.
  268. LogLevel - Supplies the logging level, one of
  269. LOG_CRITICAL 1
  270. LOG_UNUSUAL 2
  271. LOG_NOISE 3
  272. LogModule - Supplies the module ID.
  273. FileName - Supplies the filename of the caller
  274. LineNumber - Supplies the line number of the caller
  275. MessageId - Supplies the message ID to be logged.
  276. dwByteCount - Supplies the number of error-specific bytes to log. If this
  277. is zero, lpBytes is ignored.
  278. lpBytes - Supplies the error-specific bytes to log.
  279. Arg1 - Supplies an insertion string
  280. Return Value:
  281. ERROR_SUCCESS if successful
  282. Win32 error code otherwise
  283. --*/
  284. {
  285. DWORD BufSize;
  286. DWORD Status;
  287. WCHAR ResourceName[80];
  288. PWCHAR resName = ResourceName;
  289. DWORD dwType;
  290. //
  291. // Get the display name for this resource.
  292. //
  293. BufSize = sizeof( ResourceName );
  294. again:
  295. Status = ClusterRegQueryValue( hResourceKey,
  296. CLUSREG_NAME_RES_NAME,
  297. &dwType,
  298. (LPBYTE)resName,
  299. &BufSize );
  300. if ( Status == ERROR_MORE_DATA ) {
  301. resName = LocalAlloc( LMEM_FIXED, BufSize );
  302. if ( resName != NULL ) {
  303. goto again;
  304. }
  305. resName = ResourceName;
  306. ResourceName[0] = UNICODE_NULL;
  307. } else if ( Status != ERROR_SUCCESS ) {
  308. ResourceName[0] = '\0';
  309. }
  310. ClusterLogEvent2(LogLevel,
  311. LogModule,
  312. FileName,
  313. LineNumber,
  314. MessageId,
  315. dwByteCount,
  316. lpBytes,
  317. resName,
  318. Arg1);
  319. if ( resName != ResourceName ) {
  320. LocalFree( resName );
  321. }
  322. return;
  323. } // ClusResLogEventWithName1
  324. VOID
  325. ClusResLogEventWithName2(
  326. IN HKEY hResourceKey,
  327. IN DWORD LogLevel,
  328. IN DWORD LogModule,
  329. IN LPSTR FileName,
  330. IN DWORD LineNumber,
  331. IN DWORD MessageId,
  332. IN DWORD dwByteCount,
  333. IN PVOID lpBytes,
  334. IN LPCWSTR Arg1,
  335. IN LPCWSTR Arg2
  336. )
  337. /*++
  338. Routine Description:
  339. Logs an event to the eventlog. The display name of the resource is retrieved
  340. and passed as the first insertion string.
  341. Arguments:
  342. hResourceKey - Supplies the cluster resource key.
  343. LogLevel - Supplies the logging level, one of
  344. LOG_CRITICAL 1
  345. LOG_UNUSUAL 2
  346. LOG_NOISE 3
  347. LogModule - Supplies the module ID.
  348. FileName - Supplies the filename of the caller
  349. LineNumber - Supplies the line number of the caller
  350. MessageId - Supplies the message ID to be logged.
  351. dwByteCount - Supplies the number of error-specific bytes to log. If this
  352. is zero, lpBytes is ignored.
  353. lpBytes - Supplies the error-specific bytes to log.
  354. Arg1 - Supplies an insertion string
  355. Arg2 - Supplies the second insertion string
  356. Return Value:
  357. ERROR_SUCCESS if successful
  358. Win32 error code otherwise
  359. --*/
  360. {
  361. DWORD BufSize;
  362. DWORD Status;
  363. WCHAR ResourceName[80];
  364. PWCHAR resName = ResourceName;
  365. DWORD dwType;
  366. //
  367. // Get the display name for this resource.
  368. //
  369. BufSize = sizeof( ResourceName );
  370. again:
  371. Status = ClusterRegQueryValue( hResourceKey,
  372. CLUSREG_NAME_RES_NAME,
  373. &dwType,
  374. (LPBYTE)resName,
  375. &BufSize );
  376. if ( Status == ERROR_MORE_DATA ) {
  377. resName = LocalAlloc( LMEM_FIXED, BufSize );
  378. if ( resName != NULL ) {
  379. goto again;
  380. }
  381. resName = ResourceName;
  382. ResourceName[0] = UNICODE_NULL;
  383. } else if ( Status != ERROR_SUCCESS ) {
  384. ResourceName[0] = '\0';
  385. }
  386. ClusterLogEvent3(LogLevel,
  387. LogModule,
  388. FileName,
  389. LineNumber,
  390. MessageId,
  391. dwByteCount,
  392. lpBytes,
  393. resName,
  394. Arg1,
  395. Arg2);
  396. if ( resName != ResourceName ) {
  397. LocalFree( resName );
  398. }
  399. return;
  400. } // ClusResLogEventWithName2
  401. VOID
  402. ClusResLogEventWithName3(
  403. IN HKEY hResourceKey,
  404. IN DWORD LogLevel,
  405. IN DWORD LogModule,
  406. IN LPSTR FileName,
  407. IN DWORD LineNumber,
  408. IN DWORD MessageId,
  409. IN DWORD dwByteCount,
  410. IN PVOID lpBytes,
  411. IN LPCWSTR Arg1,
  412. IN LPCWSTR Arg2,
  413. IN LPCWSTR Arg3
  414. )
  415. /*++
  416. Routine Description:
  417. Logs an event to the eventlog. The display name of the resource is retrieved
  418. and passed as the first insertion string.
  419. Arguments:
  420. hResourceKey - Supplies the cluster resource key.
  421. LogLevel - Supplies the logging level, one of
  422. LOG_CRITICAL 1
  423. LOG_UNUSUAL 2
  424. LOG_NOISE 3
  425. LogModule - Supplies the module ID.
  426. FileName - Supplies the filename of the caller
  427. LineNumber - Supplies the line number of the caller
  428. MessageId - Supplies the message ID to be logged.
  429. dwByteCount - Supplies the number of error-specific bytes to log. If this
  430. is zero, lpBytes is ignored.
  431. lpBytes - Supplies the error-specific bytes to log.
  432. Arg1 - Supplies an insertion string
  433. Arg2 - Supplies the second insertion string
  434. Arg3 - Supplies the third insertion string
  435. Return Value:
  436. ERROR_SUCCESS if successful
  437. Win32 error code otherwise
  438. --*/
  439. {
  440. DWORD BufSize;
  441. DWORD Status;
  442. WCHAR ResourceName[80];
  443. PWCHAR resName = ResourceName;
  444. DWORD dwType;
  445. //
  446. // Get the display name for this resource.
  447. //
  448. BufSize = sizeof( ResourceName );
  449. again:
  450. Status = ClusterRegQueryValue( hResourceKey,
  451. CLUSREG_NAME_RES_NAME,
  452. &dwType,
  453. (LPBYTE)resName,
  454. &BufSize );
  455. if ( Status == ERROR_MORE_DATA ) {
  456. resName = LocalAlloc( LMEM_FIXED, BufSize );
  457. if ( resName != NULL ) {
  458. goto again;
  459. }
  460. resName = ResourceName;
  461. ResourceName[0] = UNICODE_NULL;
  462. } else if ( Status != ERROR_SUCCESS ) {
  463. ResourceName[0] = '\0';
  464. }
  465. ClusterLogEvent4(LogLevel,
  466. LogModule,
  467. FileName,
  468. LineNumber,
  469. MessageId,
  470. dwByteCount,
  471. lpBytes,
  472. resName,
  473. Arg1,
  474. Arg2,
  475. Arg3);
  476. if ( resName != ResourceName ) {
  477. LocalFree( resName );
  478. }
  479. return;
  480. }