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.

880 lines
23 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. errorlog.c
  5. Abstract:
  6. This module contains the code for the I/O error log thread.
  7. Author:
  8. Darryl E. Havens (darrylh) May 3, 1989
  9. Environment:
  10. Kernel mode, system process thread
  11. Revision History:
  12. --*/
  13. #include "iomgr.h"
  14. #include "elfkrnl.h"
  15. typedef struct _IOP_ERROR_LOG_CONTEXT {
  16. KDPC ErrorLogDpc;
  17. KTIMER ErrorLogTimer;
  18. }IOP_ERROR_LOG_CONTEXT, *PIOP_ERROR_LOG_CONTEXT;
  19. //
  20. // Declare routines local to this module.
  21. //
  22. BOOLEAN
  23. IopErrorLogConnectPort(
  24. VOID
  25. );
  26. VOID
  27. IopErrorLogDpc(
  28. IN struct _KDPC *Dpc,
  29. IN PVOID DeferredContext,
  30. IN PVOID SystemArgument1,
  31. IN PVOID SystemArgument2
  32. );
  33. PLIST_ENTRY
  34. IopErrorLogGetEntry(
  35. );
  36. VOID
  37. IopErrorLogQueueRequest(
  38. VOID
  39. );
  40. VOID
  41. IopErrorLogRequeueEntry(
  42. IN PLIST_ENTRY ListEntry
  43. );
  44. #ifdef ALLOC_PRAGMA
  45. #pragma alloc_text(PAGE, IopErrorLogThread)
  46. #pragma alloc_text(PAGE, IopErrorLogConnectPort)
  47. #pragma alloc_text(PAGE, IopErrorLogQueueRequest)
  48. #endif
  49. //
  50. // Define a global varibles used by the error logging code.
  51. //
  52. WORK_QUEUE_ITEM IopErrorLogWorkItem;
  53. #ifdef ALLOC_DATA_PRAGMA
  54. #pragma data_seg("PAGEDATA")
  55. #endif
  56. HANDLE ErrorLogPort;
  57. #ifdef ALLOC_DATA_PRAGMA
  58. #pragma data_seg()
  59. #endif
  60. BOOLEAN ErrorLogPortConnected;
  61. BOOLEAN IopErrorLogPortPending;
  62. BOOLEAN IopErrorLogDisabledThisBoot;
  63. //
  64. // Define the amount of space required for the device and driver names.
  65. //
  66. #define IO_ERROR_NAME_LENGTH 100
  67. VOID
  68. IopErrorLogThread(
  69. IN PVOID StartContext
  70. )
  71. /*++
  72. Routine Description:
  73. This is the main loop for the I/O error log thread which executes in the
  74. system process context. This routine is started when the system is
  75. initialized.
  76. Arguments:
  77. StartContext - Startup context; not used.
  78. Return Value:
  79. None.
  80. --*/
  81. {
  82. PERROR_LOG_ENTRY errorLogEntry;
  83. UNICODE_STRING nameString;
  84. PLIST_ENTRY listEntry;
  85. PIO_ERROR_LOG_MESSAGE errorMessage;
  86. NTSTATUS status;
  87. PELF_PORT_MSG portMessage;
  88. PCHAR objectName;
  89. SIZE_T messageLength;
  90. SIZE_T driverNameLength;
  91. SIZE_T deviceNameLength;
  92. ULONG objectNameLength;
  93. SIZE_T remainingLength;
  94. SIZE_T stringLength;
  95. CHAR nameBuffer[IO_ERROR_NAME_LENGTH+sizeof( OBJECT_NAME_INFORMATION )];
  96. PDRIVER_OBJECT driverObject;
  97. POBJECT_NAME_INFORMATION nameInformation;
  98. PIO_ERROR_LOG_PACKET errorData;
  99. PWSTR string;
  100. PAGED_CODE();
  101. UNREFERENCED_PARAMETER( StartContext );
  102. //
  103. // Check to see whether a connection has been made to the error log
  104. // port. If the port is not connected return.
  105. //
  106. if (!IopErrorLogConnectPort()) {
  107. //
  108. // The port could not be connected. A timer was started that will
  109. // try again later.
  110. //
  111. return;
  112. }
  113. //
  114. // Allocate and zero the port message structure, include space for the
  115. // name of the device and driver.
  116. //
  117. messageLength = IO_ERROR_LOG_MESSAGE_LENGTH;
  118. portMessage = ExAllocatePool(PagedPool, messageLength);
  119. if (portMessage == NULL) {
  120. //
  121. // The message buffer could not be allocated. Request that
  122. // the error log thread routine be called again later.
  123. //
  124. IopErrorLogQueueRequest();
  125. return;
  126. }
  127. RtlZeroMemory( portMessage, sizeof( *portMessage ) );
  128. portMessage->MessageType = IO_ERROR_LOG;
  129. errorMessage = &portMessage->u.IoErrorLogMessage;
  130. nameInformation = (PVOID) &nameBuffer[0];
  131. //
  132. // Now enter the main loop for this thread. This thread performs the
  133. // following operations:
  134. //
  135. // 1) If a connection has been made to the error log port, dequeue a
  136. // packet from the queue head and attempt to send it to the port.
  137. //
  138. // 2) If the send works, loop sending packets until there are no more
  139. // packets; otherwise, indicate that the connection has been broken,
  140. // cleanup, place the packet back onto the head of the queue and
  141. // return.
  142. //
  143. // 3) After all the packets are sent clear the pending variable and
  144. // return.
  145. //
  146. for (;;) {
  147. //
  148. // Loop dequeueing packets from the queue head and attempt to send
  149. // each to the port.
  150. //
  151. // If the send works, continue looping until there are no more packets.
  152. // Otherwise, indicate that the connection has been broken, cleanup,
  153. // place the packet back onto the head of the queue, and start from the
  154. // top of the loop again.
  155. //
  156. if (!(listEntry = IopErrorLogGetEntry())) {
  157. break;
  158. }
  159. errorLogEntry = CONTAINING_RECORD( listEntry,
  160. ERROR_LOG_ENTRY,
  161. ListEntry );
  162. //
  163. // The size of errorLogEntry is ERROR_LOG_ENTRY +
  164. // IO_ERROR_LOG_PACKET + (Extra Dump data). The size of the
  165. // initial message length should be IO_ERROR_LOG_MESSAGE +
  166. // (Extra Dump data), since IO_ERROR_LOG_MESSAGE contains an
  167. // IO_ERROR_LOG_PACKET. Using the above calculations set the
  168. // message length.
  169. //
  170. messageLength = sizeof( IO_ERROR_LOG_MESSAGE ) -
  171. sizeof( ERROR_LOG_ENTRY ) - sizeof( IO_ERROR_LOG_PACKET ) +
  172. errorLogEntry->Size;
  173. errorData = (PIO_ERROR_LOG_PACKET) (errorLogEntry + 1);
  174. //
  175. // Copy the error log packet and the extra data to the message.
  176. //
  177. RtlCopyMemory( &errorMessage->EntryData,
  178. errorData,
  179. errorLogEntry->Size - sizeof( ERROR_LOG_ENTRY ) );
  180. errorMessage->TimeStamp = errorLogEntry->TimeStamp;
  181. errorMessage->Type = IO_TYPE_ERROR_MESSAGE;
  182. //
  183. // Add the driver and device name string. These strings go
  184. // before the error log strings. Just write over the current
  185. // strings and they will be recopied later.
  186. //
  187. if (errorData->NumberOfStrings != 0) {
  188. //
  189. // Start the driver and device strings where the current
  190. // strings start.
  191. //
  192. objectName = (PCHAR) (&errorMessage->EntryData) +
  193. errorData->StringOffset;
  194. } else {
  195. //
  196. // Put the driver and device strings at the end of the
  197. // data.
  198. //
  199. objectName = (PCHAR) errorMessage + messageLength;
  200. }
  201. //
  202. // Make sure the driver offset starts on an even bountry.
  203. //
  204. objectName = (PCHAR) ((ULONG_PTR) (objectName + sizeof(WCHAR) - 1) &
  205. ~(ULONG_PTR)(sizeof(WCHAR) - 1));
  206. errorMessage->DriverNameOffset = (ULONG)(objectName - (PCHAR) errorMessage);
  207. remainingLength = (ULONG)((PCHAR) portMessage + IO_ERROR_LOG_MESSAGE_LENGTH
  208. - objectName);
  209. //
  210. // Calculate the length of the driver name and
  211. // the device name. If the driver object has a name then get
  212. // it from there; otherwise try to query the device object.
  213. //
  214. driverObject = errorLogEntry->DriverObject;
  215. driverNameLength = 0;
  216. nameString.Buffer = NULL;
  217. if (driverObject != NULL) {
  218. if (driverObject->DriverName.Buffer != NULL) {
  219. nameString.Buffer = driverObject->DriverName.Buffer;
  220. driverNameLength = driverObject->DriverName.Length;
  221. }
  222. if (driverNameLength == 0) {
  223. //
  224. // Try to query the driver object for a name.
  225. //
  226. status = ObQueryNameString( driverObject,
  227. nameInformation,
  228. IO_ERROR_NAME_LENGTH + sizeof( OBJECT_NAME_INFORMATION ),
  229. &objectNameLength );
  230. if (!NT_SUCCESS( status ) || !nameInformation->Name.Length) {
  231. //
  232. // No driver name was available.
  233. //
  234. driverNameLength = 0;
  235. } else {
  236. nameString = nameInformation->Name;
  237. }
  238. }
  239. } else {
  240. //
  241. // If no driver object, this message must be from the
  242. // kernel. We need to point the eventlog service to
  243. // an event message file containing ntstatus messages,
  244. // ie, ntdll, we do this by claiming this event is an
  245. // application popup.
  246. //
  247. nameString.Buffer = L"Application Popup";
  248. driverNameLength = wcslen(nameString.Buffer) * sizeof(WCHAR);
  249. }
  250. if (driverNameLength != 0 ) {
  251. //
  252. // Pick out the module name.
  253. //
  254. string = nameString.Buffer +
  255. (driverNameLength / sizeof(WCHAR));
  256. driverNameLength = sizeof(WCHAR);
  257. string--;
  258. while (*string != L'\\' && string != nameString.Buffer) {
  259. string--;
  260. driverNameLength += sizeof(WCHAR);
  261. }
  262. if (*string == L'\\') {
  263. string++;
  264. driverNameLength -= sizeof(WCHAR);
  265. }
  266. //
  267. // Ensure there is enough room for the driver name.
  268. // Save space for 3 NULLs one for the driver name,
  269. // one for the device name and one for strings.
  270. //
  271. if (driverNameLength > remainingLength - (3 * sizeof(WCHAR))) {
  272. driverNameLength = remainingLength - (3 * sizeof(WCHAR));
  273. }
  274. RtlCopyMemory(
  275. objectName,
  276. string,
  277. driverNameLength
  278. );
  279. }
  280. //
  281. // Add a null after the driver name even if there is no
  282. // driver name.
  283. //
  284. *((PWSTR) (objectName + driverNameLength)) = L'\0';
  285. driverNameLength += sizeof(WCHAR);
  286. //
  287. // Determine where the next string goes.
  288. //
  289. objectName += driverNameLength;
  290. remainingLength -= driverNameLength;
  291. errorMessage->EntryData.StringOffset = (USHORT)(objectName - (PCHAR) errorMessage);
  292. if (errorLogEntry->DeviceObject != NULL) {
  293. status = ObQueryNameString( errorLogEntry->DeviceObject,
  294. nameInformation,
  295. (ULONG)(IO_ERROR_NAME_LENGTH + sizeof( OBJECT_NAME_INFORMATION ) - driverNameLength),
  296. &objectNameLength );
  297. if (!NT_SUCCESS( status ) || !nameInformation->Name.Length) {
  298. //
  299. // No device name was available. Add a Null string.
  300. //
  301. nameInformation->Name.Length = 0;
  302. nameInformation->Name.Buffer = L"\0";
  303. }
  304. //
  305. // No device name was available. Add a Null string.
  306. // Always add a device name string so that the
  307. // insertion string counts are correct.
  308. //
  309. } else {
  310. //
  311. // No device name was available. Add a Null string.
  312. // Always add a device name string so that the
  313. // insertion string counts are correct.
  314. //
  315. nameInformation->Name.Length = 0;
  316. nameInformation->Name.Buffer = L"\0";
  317. }
  318. deviceNameLength = nameInformation->Name.Length;
  319. //
  320. // Ensure there is enough room for the device name.
  321. // Save space for a NULL.
  322. //
  323. if (deviceNameLength > remainingLength - (2 * sizeof(WCHAR))) {
  324. deviceNameLength = remainingLength - (2 * sizeof(WCHAR));
  325. }
  326. RtlCopyMemory( objectName,
  327. nameInformation->Name.Buffer,
  328. deviceNameLength );
  329. //
  330. // Add a null after the device name even if there is no
  331. // device name.
  332. //
  333. *((PWSTR) (objectName + deviceNameLength)) = L'\0';
  334. deviceNameLength += sizeof(WCHAR);
  335. //
  336. // Update the string count for the device object.
  337. //
  338. errorMessage->EntryData.NumberOfStrings++;
  339. objectName += deviceNameLength;
  340. remainingLength -= deviceNameLength;
  341. if (errorData->NumberOfStrings) {
  342. stringLength = errorLogEntry->Size - sizeof( ERROR_LOG_ENTRY ) -
  343. errorData->StringOffset;
  344. //
  345. // Align the length to an even byte boundary.
  346. //
  347. stringLength = ((stringLength + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1));
  348. //
  349. // Ensure there is enough room for the strings.
  350. // Save space for a NULL.
  351. //
  352. if (stringLength > remainingLength - sizeof(WCHAR)) {
  353. messageLength -= stringLength - remainingLength;
  354. stringLength = remainingLength - sizeof(WCHAR);
  355. }
  356. //
  357. // Copy the strings to the end of the message.
  358. //
  359. RtlCopyMemory( objectName,
  360. (PCHAR) errorData + errorData->StringOffset,
  361. stringLength );
  362. //
  363. // Add a null after the strings
  364. //
  365. //
  366. *((PWSTR) (objectName + stringLength)) = L'\0';
  367. }
  368. //
  369. // Update the message length.
  370. //
  371. errorMessage->DriverNameLength = (USHORT) driverNameLength;
  372. messageLength += deviceNameLength + driverNameLength;
  373. errorMessage->Size = (USHORT) messageLength;
  374. messageLength += FIELD_OFFSET ( ELF_PORT_MSG, u ) -
  375. FIELD_OFFSET (ELF_PORT_MSG, MessageType);
  376. portMessage->PortMessage.u1.s1.TotalLength = (USHORT)
  377. (sizeof( PORT_MESSAGE ) + messageLength);
  378. portMessage->PortMessage.u1.s1.DataLength = (USHORT) (messageLength);
  379. status = NtRequestPort( ErrorLogPort, (PPORT_MESSAGE) portMessage );
  380. if (!NT_SUCCESS( status )) {
  381. //
  382. // The send failed. Place the packet back onto the head of
  383. // the error log queue, forget the current connection since
  384. // it no longer works, and close the handle to the port.
  385. // Set a timer up for another attempt later.
  386. // Finally, exit the loop since there is no connection
  387. // to do any work on.
  388. //
  389. NtClose( ErrorLogPort );
  390. IopErrorLogRequeueEntry( &errorLogEntry->ListEntry );
  391. IopErrorLogQueueRequest();
  392. break;
  393. } else {
  394. //
  395. // The send worked fine. Free the packet and the update
  396. // the allocation count.
  397. //
  398. InterlockedExchangeAdd( &IopErrorLogAllocation,
  399. -((LONG) (errorLogEntry->Size )));
  400. //
  401. // Dereference the object pointers now that the name has been
  402. // captured.
  403. //
  404. if (errorLogEntry->DeviceObject != NULL) {
  405. ObDereferenceObject( errorLogEntry->DeviceObject );
  406. }
  407. if (driverObject != NULL) {
  408. ObDereferenceObject( errorLogEntry->DriverObject );
  409. }
  410. ExFreePool( errorLogEntry );
  411. } // if
  412. } // for
  413. //
  414. // Finally, free the message buffer and return.
  415. //
  416. ExFreePool(portMessage);
  417. }
  418. BOOLEAN
  419. IopErrorLogConnectPort(
  420. VOID
  421. )
  422. /*++
  423. Routine Description:
  424. This routine attempts to connect to the error log port. If the connection
  425. was made successfully and the port allows suficiently large messages, then
  426. the ErrorLogPort to the port handle, ErrorLogPortConnected is set to
  427. TRUE and TRUE is retuned. Otherwise a timer is started to queue a
  428. worker thread at a later time, unless there is a pending connection.
  429. Arguments:
  430. None.
  431. Return Value:
  432. Returns TRUE if the port was connected.
  433. --*/
  434. {
  435. UNICODE_STRING errorPortName;
  436. NTSTATUS status;
  437. ULONG maxMessageLength;
  438. SECURITY_QUALITY_OF_SERVICE dynamicQos;
  439. PAGED_CODE();
  440. //
  441. // If the ErrorLogPort is connected then return true.
  442. //
  443. if (ErrorLogPortConnected) {
  444. //
  445. // The port is connect return.
  446. //
  447. return(TRUE);
  448. }
  449. //
  450. // Set up the security quality of service parameters to use over the
  451. // port. Use the most efficient (least overhead) - which is dynamic
  452. // rather than static tracking.
  453. //
  454. dynamicQos.ImpersonationLevel = SecurityImpersonation;
  455. dynamicQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  456. dynamicQos.EffectiveOnly = TRUE;
  457. //
  458. // Generate the string structure for describing the error logger's port.
  459. //
  460. RtlInitUnicodeString( &errorPortName, ELF_PORT_NAME_U );
  461. status = NtConnectPort( &ErrorLogPort,
  462. &errorPortName,
  463. &dynamicQos,
  464. (PPORT_VIEW) NULL,
  465. (PREMOTE_PORT_VIEW) NULL,
  466. &maxMessageLength,
  467. (PVOID) NULL,
  468. (PULONG) NULL );
  469. if (NT_SUCCESS( status )) {
  470. if (maxMessageLength >= IO_ERROR_LOG_MESSAGE_LENGTH) {
  471. ErrorLogPortConnected = TRUE;
  472. return(TRUE);
  473. } else {
  474. NtClose(ErrorLogPort);
  475. }
  476. }
  477. //
  478. // The port was not successfully opened, or its message size was unsuitable
  479. // for use here. Queue a later request to run the error log thread.
  480. //
  481. IopErrorLogQueueRequest();
  482. //
  483. // The port could not be connected at this time return false.
  484. //
  485. return(FALSE);
  486. }
  487. VOID
  488. IopErrorLogDpc(
  489. IN struct _KDPC *Dpc,
  490. IN PVOID DeferredContext,
  491. IN PVOID SystemArgument1,
  492. IN PVOID SystemArgument2
  493. )
  494. /*++
  495. Routine Description:
  496. This routine queues a work request to the worker thread to process logged
  497. errors. It is called by a timer DPC when the error log port cannot be
  498. connected. The DPC structure itself is freed by this routine.
  499. Arguments:
  500. Dpc - Supplies a pointer to the DPC structure. This structure is freed by
  501. this routine.
  502. DeferredContext - Unused.
  503. SystemArgument1 - Unused.
  504. SystemArgument2 - Unused.
  505. Return Value:
  506. None
  507. --*/
  508. {
  509. UNREFERENCED_PARAMETER (DeferredContext);
  510. UNREFERENCED_PARAMETER (SystemArgument1);
  511. UNREFERENCED_PARAMETER (SystemArgument2);
  512. //
  513. // Free the DPC structure if there is one.
  514. //
  515. if (Dpc != NULL) {
  516. ExFreePool(Dpc);
  517. }
  518. ExInitializeWorkItem( &IopErrorLogWorkItem, IopErrorLogThread, NULL );
  519. ExQueueWorkItem( &IopErrorLogWorkItem, DelayedWorkQueue );
  520. }
  521. PLIST_ENTRY
  522. IopErrorLogGetEntry(
  523. )
  524. /*++
  525. Routine Description:
  526. This routine gets the next entry from the head of the error log queue
  527. and returns it to the caller.
  528. Arguments:
  529. None.
  530. Return Value:
  531. The return value is a pointer to the packet removed, or NULL if there were
  532. no packets on the queue.
  533. --*/
  534. {
  535. KIRQL irql;
  536. PLIST_ENTRY listEntry;
  537. //
  538. // Remove the next packet from the queue, if there is one.
  539. //
  540. ExAcquireSpinLock( &IopErrorLogLock, &irql );
  541. if (IsListEmpty( &IopErrorLogListHead )) {
  542. //
  543. // Indicate no more work will be done in the context of this worker
  544. // thread and indicate to the caller that no packets were located.
  545. //
  546. IopErrorLogPortPending = FALSE;
  547. listEntry = (PLIST_ENTRY) NULL;
  548. } else {
  549. //
  550. // Remove the next packet from the head of the list.
  551. //
  552. listEntry = RemoveHeadList( &IopErrorLogListHead );
  553. }
  554. ExReleaseSpinLock( &IopErrorLogLock, irql );
  555. return listEntry;
  556. }
  557. VOID
  558. IopErrorLogQueueRequest(
  559. VOID
  560. )
  561. /*++
  562. Routine Description:
  563. This routine sets a timer to fire after 30 seconds. The timer queues a
  564. DPC which then queues a worker thread request to run the error log thread
  565. routine.
  566. Arguments:
  567. None.
  568. Return Value:
  569. None.
  570. --*/
  571. {
  572. LARGE_INTEGER interval;
  573. PIOP_ERROR_LOG_CONTEXT context;
  574. PAGED_CODE();
  575. //
  576. // Allocate a context block which will contain the timer and the DPC.
  577. //
  578. context = ExAllocatePool( NonPagedPool, sizeof( IOP_ERROR_LOG_CONTEXT ) );
  579. if (context == NULL) {
  580. //
  581. // The context block could not be allocated. Clear the error log
  582. // pending bit. If there is another error then a new attempt will
  583. // be made. Note the spinlock does not need to be held here since
  584. // new attempt should be made later not right now, so if another
  585. // error log packet is currently being queue, it waits with the
  586. // others.
  587. //
  588. IopErrorLogPortPending = FALSE;
  589. return;
  590. }
  591. KeInitializeDpc( &context->ErrorLogDpc,
  592. IopErrorLogDpc,
  593. NULL );
  594. KeInitializeTimer( &context->ErrorLogTimer );
  595. //
  596. // Delay for 30 seconds and try for the port again.
  597. //
  598. interval.QuadPart = - 10 * 1000 * 1000 * 30;
  599. //
  600. // Set the timer to fire a DPC in 30 seconds.
  601. //
  602. KeSetTimer( &context->ErrorLogTimer, interval, &context->ErrorLogDpc );
  603. }
  604. VOID
  605. IopErrorLogRequeueEntry(
  606. IN PLIST_ENTRY ListEntry
  607. )
  608. /*++
  609. Routine Description:
  610. This routine puts an error packet back at the head of the error log queue
  611. since it cannot be processed at the moment.
  612. Arguments:
  613. ListEntry - Supplies a pointer to the packet to be placed back onto the
  614. error log queue.
  615. Return Value:
  616. None.
  617. --*/
  618. {
  619. KIRQL irql;
  620. //
  621. // Simply insert the packet back onto the head of the queue, indicate that
  622. // the error log port is not connected, queue a request to check again
  623. // soon, and return.
  624. //
  625. ExAcquireSpinLock( &IopErrorLogLock, &irql );
  626. InsertHeadList( &IopErrorLogListHead, ListEntry );
  627. ErrorLogPortConnected = FALSE;
  628. ExReleaseSpinLock( &IopErrorLogLock, irql );
  629. }