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.

2175 lines
66 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. cmnotify.c
  5. Abstract:
  6. This module contains support for NtNotifyChangeKey.
  7. Author:
  8. Bryan M. Willman (bryanwi) 03-Feb-1992
  9. Revision History:
  10. Dragos C. Sambotin (dragoss) 16-Mar-1999
  11. - fixing race conditions that when more than one thread simultaneously operates over the post list
  12. --*/
  13. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // //
  15. // "The" POST BLOCK RULE : //
  16. // //
  17. // To operate on a post block (i.e. add or remove it from a list - notify,thread,slave), //
  18. // you should at least: //
  19. // 1. Hold the registry lock exclusively //
  20. // OR //
  21. // 2. Hold the registry lock shared and aquire the postblock mutex. //
  22. // //
  23. // //
  24. // WARNING!!! //
  25. // Failing to do that could arise in obscure registry deadlocks or usage of already freed memory (bugcheck) //
  26. // //
  27. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. // //
  30. // Other important rules to follow: //
  31. // //
  32. // 1. We DO NOT dereference objects in CmpPostApc ! //
  33. // 2. We DO NOT dereference objects while walking the notify list! //
  34. // 3. All operations with Thread PostList are done in CmpPostApc or at APC level. This should avoid two threads //
  35. // operating on the same list at the same time //
  36. // //
  37. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. #include "cmp.h"
  39. #ifdef CMP_NOTIFY_POSTBLOCK_CHECK
  40. /*++
  41. Routine Description:
  42. Check if the post block or it's slave (if any) has no reference
  43. to any key body object
  44. ++*/
  45. #define CmpCheckPostBlock(PostBlock ) \
  46. { \
  47. PCM_POST_BLOCK SlavePostBlock; \
  48. \
  49. /* this post block should have the link with key body already broken*/ \
  50. ASSERT( PostBlock->PostKeyBody == NULL ); \
  51. \
  52. /* only masters get to CmpPostApc */ \
  53. ASSERT( IsMasterPostBlock(PostBlock) ); \
  54. \
  55. if (CmpIsListEmpty(&(PostBlock->CancelPostList)) == FALSE) { \
  56. \
  57. /* get the slave and verify him too */ \
  58. SlavePostBlock = (PCM_POST_BLOCK)PostBlock->CancelPostList.Flink; \
  59. SlavePostBlock = CONTAINING_RECORD(SlavePostBlock, \
  60. CM_POST_BLOCK, \
  61. CancelPostList); \
  62. /* This should be true !*/ \
  63. ASSERT( !IsMasterPostBlock(SlavePostBlock) ); \
  64. \
  65. /* this post block shoul have the link with key body already broken */ \
  66. ASSERT( SlavePostBlock->PostKeyBody == NULL ); \
  67. } \
  68. }
  69. #else
  70. #define CmpCheckPostBlock(a) //nothing
  71. #endif
  72. //
  73. // "Back Side" of notify
  74. //
  75. extern PCMHIVE CmpMasterHive;
  76. VOID
  77. CmpReportNotifyHelper(
  78. PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  79. IN PHHIVE SearchHive,
  80. IN PHHIVE Hive,
  81. IN HCELL_INDEX Cell,
  82. IN ULONG Filter
  83. );
  84. VOID
  85. CmpCancelSlavePost(
  86. PCM_POST_BLOCK PostBlock,
  87. PLIST_ENTRY DelayedDeref
  88. );
  89. VOID
  90. CmpFreeSlavePost(
  91. PCM_POST_BLOCK MasterPostBlock
  92. );
  93. VOID
  94. CmpAddToDelayedDeref(
  95. PCM_POST_BLOCK PostBlock,
  96. PLIST_ENTRY DelayedDeref
  97. );
  98. VOID
  99. CmpDelayedDerefKeys(
  100. PLIST_ENTRY DelayedDeref
  101. );
  102. BOOLEAN
  103. CmpNotifyTriggerCheck(
  104. IN PCM_NOTIFY_BLOCK NotifyBlock,
  105. IN PHHIVE Hive,
  106. IN PCM_KEY_NODE Node
  107. );
  108. VOID
  109. CmpDummyApc(
  110. struct _KAPC *Apc,
  111. PVOID *SystemArgument1,
  112. PVOID *SystemArgument2
  113. );
  114. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  115. VOID
  116. CmpFillPostBlockBuffer(
  117. PCM_POST_BLOCK PostBlock,
  118. PUNICODE_STRING ChangedKcbName OPTIONAL
  119. );
  120. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  121. #ifdef ALLOC_PRAGMA
  122. #pragma alloc_text(PAGE,CmpReportNotify)
  123. #pragma alloc_text(PAGE,CmpReportNotifyHelper)
  124. #pragma alloc_text(PAGE,CmpPostNotify)
  125. #pragma alloc_text(PAGE,CmpPostApc)
  126. #pragma alloc_text(PAGE,CmpPostApcRunDown)
  127. #pragma alloc_text(PAGE,CmNotifyRunDown)
  128. #pragma alloc_text(PAGE,CmpFlushNotify)
  129. #pragma alloc_text(PAGE,CmpNotifyChangeKey)
  130. #pragma alloc_text(PAGE,CmpCancelSlavePost)
  131. #pragma alloc_text(PAGE,CmpFreeSlavePost)
  132. #pragma alloc_text(PAGE,CmpAddToDelayedDeref)
  133. #pragma alloc_text(PAGE,CmpDelayedDerefKeys)
  134. #pragma alloc_text(PAGE,CmpNotifyTriggerCheck)
  135. #pragma alloc_text(PAGE,CmpDummyApc)
  136. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  137. #pragma alloc_text(PAGE,CmpFillCallerBuffer)
  138. #pragma alloc_text(PAGE,CmpFillPostBlockBuffer)
  139. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  140. #endif
  141. VOID
  142. CmpDummyApc(
  143. struct _KAPC *Apc,
  144. PVOID *SystemArgument1,
  145. PVOID *SystemArgument2
  146. )
  147. /*++
  148. Routine Description:
  149. Dummy routine to prevent user-mode callers to set special kernel apcs
  150. Arguments:
  151. Apc - pointer to apc object
  152. SystemArgument1 - IN: Status value for IoStatusBlock
  153. OUT: Ptr to IoStatusBlock (2nd arg to user apc routine)
  154. SystemArgument2 - Pointer to the PostBlock
  155. Return Value:
  156. NONE.
  157. --*/
  158. {
  159. UNREFERENCED_PARAMETER(Apc);
  160. UNREFERENCED_PARAMETER(SystemArgument1);
  161. UNREFERENCED_PARAMETER(SystemArgument2);
  162. }
  163. VOID
  164. CmpReportNotify(
  165. PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  166. PHHIVE Hive,
  167. HCELL_INDEX Cell,
  168. ULONG Filter
  169. )
  170. /*++
  171. Routine Description:
  172. This routine is called when a notifiable event occurs. It will
  173. apply CmpReportNotifyHelper to the hive the event occured in,
  174. and the master hive if different.
  175. Arguments:
  176. KeyControlBlock - KCB of the key at which the event occured.
  177. For create or delete this is the created or deleted key.
  178. Hive - pointer to hive containing cell of Key at which event occured.
  179. Cell - cell of Key at which event occured
  180. (hive and cell correspond with name.)
  181. Filter - event to be reported
  182. Return Value:
  183. NONE.
  184. --*/
  185. {
  186. HCELL_INDEX CellToRelease = HCELL_NIL;
  187. PAGED_CODE();
  188. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpReportNotify:\n"));
  189. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tHive:%p Cell:%08lx Filter:%08lx\n", Hive, Cell, Filter));
  190. //
  191. // If the operation was create or delete, treat it as a change
  192. // to the parent.
  193. //
  194. if (Filter == REG_NOTIFY_CHANGE_NAME) {
  195. PCM_KEY_NODE pcell;
  196. ULONG flags;
  197. pcell = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  198. if( pcell == NULL ) {
  199. //
  200. // we couldn't map the bin containing this cell
  201. // Bad luck! notifications will be broken.
  202. //
  203. return;
  204. }
  205. CellToRelease = Cell;
  206. flags = pcell->Flags;
  207. Cell = pcell->Parent;
  208. if (flags & KEY_HIVE_ENTRY) {
  209. ASSERT( CellToRelease != HCELL_NIL );
  210. HvReleaseCell(Hive,CellToRelease);
  211. Hive = &(CmpMasterHive->Hive);
  212. pcell = (PCM_KEY_NODE)HvGetCell(Hive, Cell);
  213. if( pcell == NULL ) {
  214. //
  215. // we couldn't map the bin containing this cell
  216. // Bad luck! notifications will be broken.
  217. //
  218. return;
  219. }
  220. CellToRelease = Cell;
  221. }
  222. KeyControlBlock = KeyControlBlock->ParentKcb;
  223. //
  224. // if we're at an exit/link node, back up the real node
  225. // that MUST be it's parent.
  226. //
  227. if (pcell->Flags & KEY_HIVE_EXIT) {
  228. Cell = pcell->Parent;
  229. }
  230. ASSERT( CellToRelease != HCELL_NIL );
  231. HvReleaseCell(Hive,CellToRelease);
  232. }
  233. //
  234. // Report to notifies waiting on the event's hive
  235. //
  236. CmpReportNotifyHelper(KeyControlBlock, Hive, Hive, Cell, Filter);
  237. //
  238. // If containing hive is not the master hive, apply to master hive
  239. //
  240. if (Hive != &(CmpMasterHive->Hive)) {
  241. CmpReportNotifyHelper(KeyControlBlock,
  242. &(CmpMasterHive->Hive),
  243. Hive,
  244. Cell,
  245. Filter);
  246. }
  247. return;
  248. }
  249. BOOLEAN
  250. CmpNotifyTriggerCheck(
  251. IN PCM_NOTIFY_BLOCK NotifyBlock,
  252. IN PHHIVE Hive,
  253. IN PCM_KEY_NODE Node
  254. )
  255. /*++
  256. Routine Description:
  257. Checks if a notify can be triggered
  258. Arguments:
  259. NotifyBlock - the notify block
  260. Hive - Supplies hive containing node to match with.
  261. Node - pointer to key to match with (and check access to)
  262. Return Value:
  263. TRUE - yes.
  264. FALSE - no
  265. --*/
  266. {
  267. PCM_POST_BLOCK PostBlock;
  268. POST_BLOCK_TYPE NotifyType;
  269. PAGED_CODE();
  270. if(IsListEmpty(&(NotifyBlock->PostList)) == FALSE) {
  271. //
  272. // check if it is a kernel notify. Look at the first post block
  273. // to see that. If is a kernel post-block, then all posts in
  274. // the list should be kernel notifies
  275. //
  276. PostBlock = (PCM_POST_BLOCK)NotifyBlock->PostList.Flink;
  277. PostBlock = CONTAINING_RECORD(PostBlock,
  278. CM_POST_BLOCK,
  279. NotifyList);
  280. NotifyType = PostBlockType(PostBlock);
  281. if( NotifyType == PostAsyncKernel ) {
  282. // this is a kernel notify; always trigger it
  283. #if DBG
  284. //
  285. // DEBUG only code: All post blocks should be of the same type
  286. // (kernel/user)
  287. //
  288. while( PostBlock->NotifyList.Flink != &(NotifyBlock->PostList) ) {
  289. PostBlock = (PCM_POST_BLOCK)PostBlock->NotifyList.Flink;
  290. PostBlock = CONTAINING_RECORD(PostBlock,
  291. CM_POST_BLOCK,
  292. NotifyList);
  293. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpNotifyTriggerCheck : NotifyBlock = %p\n",NotifyBlock));
  294. ASSERT( PostBlockType(PostBlock) == NotifyType );
  295. }
  296. #endif
  297. return TRUE;
  298. }
  299. }
  300. //
  301. // else, check if the caller has the right access
  302. //
  303. return CmpCheckNotifyAccess(NotifyBlock,Hive,Node);
  304. }
  305. VOID
  306. CmpReportNotifyHelper(
  307. PCM_KEY_CONTROL_BLOCK KeyControlBlock,
  308. IN PHHIVE SearchHive,
  309. IN PHHIVE Hive,
  310. IN HCELL_INDEX Cell,
  311. IN ULONG Filter
  312. )
  313. /*++
  314. Routine Description:
  315. Scan the list of active notifies for the specified hive. For
  316. any with scope including KeyControlBlock and filter matching
  317. Filter, and with proper security access, post the notify.
  318. Arguments:
  319. Name - canonical path name (as in a key control block) of the key
  320. at which the event occured. (This is the name for
  321. reporting purposes.)
  322. SearchHive - hive to search for matches (which notify list to check)
  323. Hive - Supplies hive containing node to match with.
  324. Cell - cell identifying the node in Hive
  325. Filter - type of event
  326. Return Value:
  327. NONE.
  328. --*/
  329. {
  330. PLIST_ENTRY NotifyPtr;
  331. PCM_NOTIFY_BLOCK NotifyBlock;
  332. PCMHIVE CmSearchHive;
  333. PUNICODE_STRING NotifyName;
  334. KIRQL OldIrql;
  335. LIST_ENTRY DelayedDeref;
  336. PCM_KEY_NODE Node;
  337. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  338. PUNICODE_STRING FullKcbName;
  339. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  340. PAGED_CODE();
  341. Node = (PCM_KEY_NODE)HvGetCell(Hive,Cell);
  342. if( Node == NULL ) {
  343. //
  344. // bad luck, we cannot map the view containing this cell
  345. //
  346. return;
  347. }
  348. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  349. FullKcbName = CmpConstructName(KeyControlBlock);
  350. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  351. KeRaiseIrql(APC_LEVEL, &OldIrql);
  352. CmSearchHive = CONTAINING_RECORD(SearchHive, CMHIVE, Hive);
  353. NotifyPtr = &(CmSearchHive->NotifyList);
  354. InitializeListHead(&(DelayedDeref));
  355. while (NotifyPtr->Flink != NULL) {
  356. NotifyPtr = NotifyPtr->Flink;
  357. NotifyBlock = CONTAINING_RECORD(NotifyPtr, CM_NOTIFY_BLOCK, HiveList);
  358. if (NotifyBlock->KeyControlBlock->TotalLevels > KeyControlBlock->TotalLevels) {
  359. //
  360. // list is level sorted, we're past all shorter entries
  361. //
  362. break;
  363. } else {
  364. PCM_KEY_CONTROL_BLOCK kcb;
  365. ULONG LevelDiff, l;
  366. LevelDiff = KeyControlBlock->TotalLevels - NotifyBlock->KeyControlBlock->TotalLevels;
  367. kcb = KeyControlBlock;
  368. for (l=0; l<LevelDiff; l++) {
  369. kcb = kcb->ParentKcb;
  370. }
  371. if (kcb == NotifyBlock->KeyControlBlock) {
  372. //
  373. // This Notify path is the prefix of this kcb.
  374. //
  375. if ((NotifyBlock->Filter & Filter)
  376. &&
  377. ((NotifyBlock->WatchTree == TRUE) ||
  378. (Cell == kcb->KeyCell))
  379. )
  380. {
  381. // Filter matches, this event is relevent to this notify
  382. // AND
  383. // Either the notify spans the whole subtree, or the cell
  384. // (key) of interest is the one it applies to
  385. //
  386. // THEREFORE: The notify is relevent.
  387. //
  388. //
  389. // Correct scope, does caller have access?
  390. //
  391. if (CmpNotifyTriggerCheck(NotifyBlock,Hive,Node)) {
  392. //
  393. // Notify block has KEY_NOTIFY access to the node
  394. // the event occured at. It is relevent. Therefore,
  395. // it gets to see this event. Post and be done.
  396. //
  397. // we specify that we want no key body dereferenciation
  398. // during the CmpPostNotify call. This is to prevent the
  399. // deletion of the current notify block
  400. //
  401. CmpPostNotify(
  402. NotifyBlock,
  403. NULL,
  404. Filter,
  405. STATUS_NOTIFY_ENUM_DIR,
  406. &DelayedDeref
  407. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  408. ,
  409. FullKcbName
  410. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  411. );
  412. } // else no KEY_NOTIFY access to node event occured at
  413. } // else not relevent (wrong scope, filter, etc)
  414. }
  415. }
  416. }
  417. KeLowerIrql(OldIrql);
  418. HvReleaseCell(Hive,Cell);
  419. //
  420. // finish the job started in CmpPostNotify (i.e. dereference the keybodies
  421. // we prevented. this may cause some notifyblocks to be freed
  422. //
  423. CmpDelayedDerefKeys(&DelayedDeref);
  424. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  425. if( FullKcbName != NULL ) {
  426. ExFreePoolWithTag(FullKcbName, CM_NAME_TAG | PROTECTED_POOL);
  427. }
  428. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  429. return;
  430. }
  431. VOID
  432. CmpPostNotify(
  433. PCM_NOTIFY_BLOCK NotifyBlock,
  434. PUNICODE_STRING Name OPTIONAL,
  435. ULONG Filter,
  436. NTSTATUS Status,
  437. PLIST_ENTRY ExternalKeyDeref OPTIONAL
  438. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  439. ,
  440. PUNICODE_STRING ChangedKcbName OPTIONAL
  441. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  442. )
  443. /*++
  444. Routine Description:
  445. Actually report the notify event by signalling events, enqueing
  446. APCs, and so forth.
  447. When Status is STATUS_NOTIFY_CLEANUP:
  448. - if the post block is a slave one, just cancel it.
  449. - if the post block is a master one, cancel all slave post blocks
  450. and trigger event on the master block.
  451. Comments:
  452. This routine is using a "delayed dereferencing" technique to prevent
  453. deadlocks that may appear when a keybody is dereferenced while holding
  454. the post block lock. As for this, a list with keybodies that have to be
  455. dereferenced is constructed while walking the list of postblocks attached
  456. to the current notify block and the related (slave or master) post blocks.
  457. The list is built by tricking postblocks. For all postblock about to be
  458. freed the PostKeyBody member is added to the local list and then set to NULL
  459. on the postblock. This will avoid the key body dereferencing in CmpFreePostBlock.
  460. Instead, after the postblock lock is released, the local list is iterated and
  461. the keybodies are dereferenced and the storage for associated CM_POST_KEY_BODY
  462. objects is freed.
  463. Arguments:
  464. NotifyBlock - pointer to structure that describes the notify
  465. operation. (Where to post to)
  466. Name - name of key at which event occurred.
  467. Filter - nature of event
  468. Status - completion status to report
  469. ExternalKeyDeref - this parameter (when not NULL) specifies that the caller doesn't
  470. want any keybody to be dereferenced while in this routine
  471. Return Value:
  472. NONE.
  473. --*/
  474. {
  475. PCM_POST_BLOCK PostBlock;
  476. PCM_POST_BLOCK SlavePostBlock;
  477. LIST_ENTRY LocalDelayedDeref;
  478. KIRQL OldIrql;
  479. PLIST_ENTRY DelayedDeref;
  480. Filter;
  481. Name;
  482. PAGED_CODE();
  483. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpPostNotify:\n"));
  484. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tNotifyBlock:%p ", NotifyBlock));
  485. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tName = %wZ\n", Name));
  486. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tFilter:%08lx Status=%08lx\n", Filter, Status));
  487. ASSERT_CM_LOCK_OWNED();
  488. if( ARGUMENT_PRESENT(ExternalKeyDeref) ) {
  489. //
  490. // The caller want to do all keybody dereferencing by himself
  491. //
  492. DelayedDeref = ExternalKeyDeref;
  493. } else {
  494. // local delayed dereferencing (the caller doesn't care!)
  495. DelayedDeref = &LocalDelayedDeref;
  496. InitializeListHead(DelayedDeref);
  497. }
  498. //
  499. // Aquire exclusive access over the postlist(s)
  500. //
  501. LOCK_POST_LIST();
  502. if (IsListEmpty(&(NotifyBlock->PostList)) == TRUE) {
  503. //
  504. // Nothing to post, set a mark and return
  505. //
  506. NotifyBlock->NotifyPending = TRUE;
  507. UNLOCK_POST_LIST();
  508. return;
  509. }
  510. NotifyBlock->NotifyPending = FALSE;
  511. //
  512. // IMPLEMENTATION NOTE:
  513. // If we ever want to actually implement the code that returns
  514. // names of things that changed, this is the place to add the
  515. // name and operation type to the buffer.
  516. //
  517. //
  518. // Pull and post all the entries in the post list
  519. //
  520. while (IsListEmpty(&(NotifyBlock->PostList)) == FALSE) {
  521. //
  522. // Remove from the notify block list, and enqueue the apc.
  523. // The apc will remove itself from the thread list
  524. //
  525. PostBlock = (PCM_POST_BLOCK)RemoveHeadList(&(NotifyBlock->PostList));
  526. PostBlock = CONTAINING_RECORD(PostBlock,
  527. CM_POST_BLOCK,
  528. NotifyList);
  529. // Protect for multiple deletion of the same object
  530. CmpClearListEntry(&(PostBlock->NotifyList));
  531. if( (Status == STATUS_NOTIFY_CLEANUP) && !IsMasterPostBlock(PostBlock) ) {
  532. //
  533. // Cleanup notification (i.e. the key handle was closed or the key was deleted)
  534. // When the post is a slave one, just cancel it. Canceling means:
  535. // 1. Removing from the notify PostList (aldready done at this point - see above)
  536. // 2. Unchaining from the Master Block CancelPostList
  537. // 3. Delisting from the thread PostBlockList
  538. // 4. Actually freeing the memory
  539. //
  540. // Use Cmp variant to protect for multiple deletion of the same object
  541. CmpRemoveEntryList(&(PostBlock->CancelPostList));
  542. //
  543. // FIX 289351
  544. //
  545. // Use Cmp variant to protect for multiple deletion of the same object
  546. KeRaiseIrql(APC_LEVEL, &OldIrql);
  547. CmpRemoveEntryList(&(PostBlock->ThreadList));
  548. KeLowerIrql(OldIrql);
  549. if( PostBlock->NotifyType != PostSynchronous ) {
  550. // add to the deref list and clean the post block
  551. CmpAddToDelayedDeref(PostBlock,DelayedDeref);
  552. //
  553. // Front-end routine will do self cleanup for syncrounous notifications
  554. CmpFreePostBlock(PostBlock);
  555. }
  556. #if DBG
  557. if(PostBlock->TraceIntoDebugger) {
  558. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]\tCmpPostNotify: PostBlock:%p is a slave block,and notify is CLEANUP==> just cleanning\n", PostBlock));
  559. }
  560. #endif
  561. continue; //try the next one
  562. }
  563. //
  564. // Simulate that this block is the master one, so we can free the others
  565. // Doing that will ensure the right memory dealocation when the master
  566. // (from now on this block) will be freed.
  567. //
  568. if(!IsMasterPostBlock(PostBlock)) {
  569. //
  570. // oops.,this is not the master block, we have some more work to do
  571. //
  572. SlavePostBlock = PostBlock;
  573. do {
  574. SlavePostBlock = (PCM_POST_BLOCK)SlavePostBlock->CancelPostList.Flink;
  575. SlavePostBlock = CONTAINING_RECORD(SlavePostBlock,
  576. CM_POST_BLOCK,
  577. CancelPostList);
  578. //
  579. // reset the "master flag" if set
  580. //
  581. ClearMasterPostBlockFlag(SlavePostBlock);
  582. } while (SlavePostBlock != PostBlock);
  583. //
  584. // Make this post block the master one
  585. //
  586. SetMasterPostBlockFlag(PostBlock);
  587. }
  588. #if DBG
  589. if(PostBlock->TraceIntoDebugger) {
  590. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]\tCmpPostNotify: Master block switched to :%p\n", PostBlock));
  591. }
  592. #endif
  593. //
  594. // Cancel all slave Post requests that may be linked to self
  595. //
  596. if( PostBlockType(PostBlock) != PostSynchronous ) {
  597. //
  598. // Front-end routine will do self cleanup for syncrounous notifications
  599. CmpCancelSlavePost(PostBlock,DelayedDeref);
  600. //
  601. // Do the same for the master (in case master and slave got switched)
  602. // This will avoid dereferencing the keybody from CmpPostApc
  603. CmpAddToDelayedDeref(PostBlock,DelayedDeref);
  604. }
  605. switch (PostBlockType(PostBlock)) {
  606. case PostSynchronous:
  607. //
  608. // This is a SYNC notify call. There will be no user event,
  609. // and no user apc routine. Quick exit here, just fill in
  610. // the Status and poke the event.
  611. //
  612. // Holder of the systemevent will wake up and free the
  613. // postblock. If we free it here, we get a race & bugcheck.
  614. //
  615. // Set the flink to NULL so that the front side can tell this
  616. // has been removed if its wait aborts.
  617. //
  618. PostBlock->NotifyList.Flink = NULL;
  619. PostBlock->u->Sync.Status = Status;
  620. KeSetEvent(PostBlock->u->Sync.SystemEvent,
  621. 0,
  622. FALSE);
  623. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  624. //
  625. // store full qualified name into the post block private kernel buffer
  626. //
  627. CmpFillPostBlockBuffer(PostBlock,ChangedKcbName);
  628. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  629. break;
  630. case PostAsyncUser:
  631. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  632. //
  633. // store full qualified name into the post block private kernel buffer
  634. //
  635. CmpFillPostBlockBuffer(PostBlock,ChangedKcbName);
  636. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  637. //
  638. // Insert the APC into the queue
  639. //
  640. KeInsertQueueApc(PostBlock->u->AsyncUser.Apc,
  641. (PVOID)ULongToPtr(Status),
  642. (PVOID)PostBlock,
  643. 0);
  644. break;
  645. case PostAsyncKernel:
  646. //
  647. // Queue the work item, then free the post block.
  648. //
  649. if (PostBlock->u->AsyncKernel.WorkItem != NULL) {
  650. ExQueueWorkItem(PostBlock->u->AsyncKernel.WorkItem,
  651. PostBlock->u->AsyncKernel.QueueType);
  652. }
  653. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  654. //
  655. // fill the caller buffer (if any) - we only handle kernel mode adresses
  656. //
  657. CmpFillCallerBuffer(PostBlock,ChangedKcbName);
  658. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  659. //
  660. // Signal Event if present, and deref it.
  661. //
  662. if (PostBlock->u->AsyncKernel.Event != NULL) {
  663. KeSetEvent(PostBlock->u->AsyncKernel.Event,
  664. 0,
  665. FALSE);
  666. ObDereferenceObject(PostBlock->u->AsyncKernel.Event);
  667. }
  668. //
  669. // Multiple async kernel notification are not allowed
  670. //
  671. ASSERT(IsListEmpty(&(PostBlock->CancelPostList)) == TRUE);
  672. //
  673. // remove the post block from the thread list, and free it
  674. //
  675. // Use Cmp variant to protect for multiple deletion of the same object
  676. KeRaiseIrql(APC_LEVEL, &OldIrql);
  677. CmpRemoveEntryList(&(PostBlock->ThreadList));
  678. KeLowerIrql(OldIrql);
  679. // it was already added to delayed deref.
  680. CmpFreePostBlock(PostBlock);
  681. break;
  682. }
  683. }
  684. UNLOCK_POST_LIST();
  685. //
  686. // At this point we have a list of keybody elements that have to be dereferenciated
  687. // and the associated storage for the covering objects freed. The keybodies in this
  688. // list have only one reference count on them (they were referenced only in
  689. // NtNotifyChangeMultipleKeys), dereferencing them here should free the object
  690. //
  691. if( ARGUMENT_PRESENT(ExternalKeyDeref) ) {
  692. // do nothing; the caller wants to handle the dereferenciation by himself!
  693. } else {
  694. // dereferenciate all keybodies in the delayed list
  695. CmpDelayedDerefKeys(DelayedDeref);
  696. }
  697. return;
  698. }
  699. VOID
  700. CmpPostApc(
  701. struct _KAPC *Apc,
  702. PKNORMAL_ROUTINE *NormalRoutine,
  703. PVOID *NormalContext,
  704. PVOID *SystemArgument1,
  705. PVOID *SystemArgument2
  706. )
  707. /*++
  708. Routine Description:
  709. This is the kernel apc routine. It is called for all notifies,
  710. regardless of what form of notification the caller requested.
  711. We compute the postblock address from the apc object address.
  712. IoStatus is set. SystemEvent and UserEvent will be signalled
  713. as appropriate. If the user requested an APC, then NormalRoutine
  714. will be set at entry and executed when we exit. The PostBlock
  715. is freed here.
  716. Arguments:
  717. Apc - pointer to apc object
  718. NormalRoutine - Will be called when we return
  719. NormalContext - will be 1st argument to normal routine, ApcContext
  720. passed in when NtNotifyChangeKey was called
  721. SystemArgument1 - IN: Status value for IoStatusBlock
  722. OUT: Ptr to IoStatusBlock (2nd arg to user apc routine)
  723. SystemArgument2 - Pointer to the PostBlock
  724. Return Value:
  725. NONE.
  726. --*/
  727. {
  728. PCM_POST_BLOCK PostBlock;
  729. PAGED_CODE();
  730. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpPostApc:\n"));
  731. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tApc:%p ", Apc));
  732. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"NormalRoutine:%p\n", NormalRoutine));
  733. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tNormalContext:%08lx", NormalContext));
  734. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tSystemArgument1=IoStatusBlock:%p\n", SystemArgument1));
  735. PostBlock = *(PCM_POST_BLOCK *)SystemArgument2;
  736. #if DBG
  737. if(PostBlock->TraceIntoDebugger) {
  738. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmpPostApc: PostBlock:%p\n", PostBlock));
  739. }
  740. #endif
  741. //
  742. // Fill in IO Status Block
  743. //
  744. // IMPLEMENTATION NOTE:
  745. // If we ever want to actually implement the code that returns
  746. // names of things that changed, this is the place to copy the
  747. // buffer into the caller's buffer.
  748. //
  749. // Sundown only: Use a 32bit IO_STATUS_BLOCK if the caller is 32bit.
  750. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  751. //
  752. // It looks like the time finally came :-)
  753. //
  754. CmpFillCallerBuffer(PostBlock,PostBlock->ChangedKcbFullName);
  755. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  756. try {
  757. CmpSetIoStatus(PostBlock->u->AsyncUser.IoStatusBlock,
  758. *((ULONG *)SystemArgument1),
  759. 0L,
  760. PsGetCurrentProcess()->Wow64Process != NULL);
  761. } except (EXCEPTION_EXECUTE_HANDLER) {
  762. NOTHING;
  763. }
  764. *SystemArgument1 = PostBlock->u->AsyncUser.IoStatusBlock;
  765. //
  766. // This is an Async notify, do all work here, including
  767. // cleaning up the post block
  768. //
  769. //
  770. // Signal UserEvent if present, and deref it.
  771. //
  772. if (PostBlock->u->AsyncUser.UserEvent != NULL) {
  773. KeSetEvent(PostBlock->u->AsyncUser.UserEvent,
  774. 0,
  775. FALSE);
  776. ObDereferenceObject(PostBlock->u->AsyncUser.UserEvent);
  777. }
  778. //
  779. // remove the post block from the thread list, and free it
  780. //
  781. // Use Cmp variant to protect for multiple deletion of the same object
  782. CmpRemoveEntryList(&(PostBlock->ThreadList));
  783. // debug only checks
  784. CmpCheckPostBlock(PostBlock);
  785. //
  786. // Free the slave post block to avoid "dangling" postblocks
  787. //
  788. CmpFreeSlavePost(PostBlock);
  789. //
  790. // free this post block
  791. //
  792. CmpFreePostBlock(PostBlock);
  793. return;
  794. }
  795. VOID
  796. CmpPostApcRunDown(
  797. struct _KAPC *Apc
  798. )
  799. /*++
  800. Routine Description:
  801. This routine is called to clear away apcs in the apc queue
  802. of a thread that has been terminated.
  803. Since the apc is in the apc queue, we know that it is NOT in
  804. any NotifyBlock's post list. It is, however, in the threads's
  805. PostBlockList.
  806. Therefore, poke any user events so that waiters are not stuck,
  807. drop the references so the event can be cleaned up, delist the
  808. PostBlock and free it.
  809. Since we are cleaning up the thread, SystemEvents are not interesting.
  810. Since the apc is in the apc queue, we know that if there were any other
  811. notifications related to this one, they are cleaned up by the
  812. CmPostNotify routine
  813. Arguments:
  814. Apc - pointer to apc object
  815. Return Value:
  816. NONE.
  817. --*/
  818. {
  819. PCM_POST_BLOCK PostBlock;
  820. KIRQL OldIrql;
  821. PAGED_CODE();
  822. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpApcRunDown:"));
  823. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tApc:%p \n", Apc));
  824. KeRaiseIrql(APC_LEVEL, &OldIrql);
  825. PostBlock = (PCM_POST_BLOCK)Apc->SystemArgument2;
  826. #if DBG
  827. if(PostBlock->TraceIntoDebugger) {
  828. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmpPostApcRunDown: PostBlock:%p\n", PostBlock));
  829. }
  830. #endif
  831. //
  832. // report status and wake up any threads that might otherwise
  833. // be stuck. also drop any event references we hold
  834. //
  835. // Sundown only: Use a 32bit IO_STATUS_BLOCK if the caller is 32bit.
  836. try {
  837. CmpSetIoStatus(PostBlock->u->AsyncUser.IoStatusBlock,
  838. STATUS_NOTIFY_CLEANUP,
  839. 0L,
  840. PsGetCurrentProcess()->Wow64Process != NULL);
  841. } except (EXCEPTION_EXECUTE_HANDLER) {
  842. NOTHING;
  843. }
  844. if (PostBlock->u->AsyncUser.UserEvent != NULL) {
  845. KeSetEvent(
  846. PostBlock->u->AsyncUser.UserEvent,
  847. 0,
  848. FALSE
  849. );
  850. ObDereferenceObject(PostBlock->u->AsyncUser.UserEvent);
  851. }
  852. //
  853. // delist the post block
  854. //
  855. // Use Cmp variant to protect for multiple deletion of the same object
  856. CmpRemoveEntryList(&(PostBlock->ThreadList));
  857. //
  858. // Free the slave post block to avoid "dangling" postblocks
  859. //
  860. CmpFreeSlavePost(PostBlock);
  861. //
  862. // Free the post block. Use Ex call because PostBlocks are NOT
  863. // part of the global registry pool computation, but are instead
  864. // part of NonPagedPool with Quota.
  865. //
  866. CmpFreePostBlock(PostBlock);
  867. KeLowerIrql(OldIrql);
  868. return;
  869. }
  870. //
  871. // Cleanup procedure
  872. //
  873. VOID
  874. CmNotifyRunDown(
  875. PETHREAD Thread
  876. )
  877. /*++
  878. Routine Description:
  879. This routine is called from PspExitThread to clean up any pending
  880. notify requests.
  881. It will traverse the thread's PostBlockList, for each PostBlock it
  882. finds, it will:
  883. 1. Remove it from the relevent NotifyBlock. This requires
  884. that we hold the Registry mutex.
  885. 2. Remove it from the thread's PostBlockList. This requires
  886. that we run at APC level.
  887. 3. By the time this procedure runs, user apcs are not interesting
  888. and neither are SystemEvents, so do not bother processing
  889. them.
  890. UserEvents and IoStatusBlocks could be refered to by other
  891. threads in the same process, or even a different process,
  892. so process them so those threads know what happened, use
  893. status code of STATUS_NOTIFY_CLEANUP.
  894. If the notify is a master one, cancel all slave notifications.
  895. Else only remove this notification from the master CancelPortList
  896. 4. Free the post block.
  897. Arguments:
  898. Thread - pointer to the executive thread object for the thread
  899. we wish to do rundown on.
  900. Return Value:
  901. NONE.
  902. --*/
  903. {
  904. PCM_POST_BLOCK PostBlock;
  905. PCM_NOTIFY_BLOCK NotifyBlock;
  906. KIRQL OldIrql;
  907. PAGED_CODE();
  908. if ( IsListEmpty(&(Thread->PostBlockList)) == TRUE ) {
  909. return;
  910. }
  911. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_FLOW,"CmNotifyRunDown: ethread:%p\n", Thread));
  912. CmpLockRegistryExclusive();
  913. //
  914. // Aquire exclusive access over the postlist(s)
  915. //
  916. // This is not needed (see the rule above)
  917. //LOCK_POST_LIST();
  918. KeRaiseIrql(APC_LEVEL, &OldIrql);
  919. while (IsListEmpty(&(Thread->PostBlockList)) == FALSE) {
  920. //
  921. // remove from thread list
  922. //
  923. PostBlock = (PCM_POST_BLOCK)RemoveHeadList(&(Thread->PostBlockList));
  924. PostBlock = CONTAINING_RECORD(
  925. PostBlock,
  926. CM_POST_BLOCK,
  927. ThreadList
  928. );
  929. // Protect for multiple deletion of the same object
  930. CmpClearListEntry(&(PostBlock->ThreadList));
  931. #if DBG
  932. if(PostBlock->TraceIntoDebugger) {
  933. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_FLOW,"[CM]CmpNotifyRunDown: ethread:%p, PostBlock:%p\n", Thread,PostBlock));
  934. }
  935. #endif
  936. //
  937. // Canceling a master notification implies canceling all the slave notifications
  938. // from the CancelPostList
  939. //
  940. if(IsMasterPostBlock(PostBlock)) {
  941. #if DBG
  942. if(PostBlock->TraceIntoDebugger) {
  943. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_FLOW,"[CM]\tCmpNotifyRunDown: PostBlock:%p is a master block\n", PostBlock));
  944. }
  945. #endif
  946. //
  947. // at this point, CmpReportNotify and friends will no longer
  948. // attempt to post this post block.
  949. //
  950. if (PostBlockType(PostBlock) == PostAsyncUser) {
  951. //
  952. // report status and wake up any threads that might otherwise
  953. // be stuck. also drop any event references we hold
  954. //
  955. // Sundown only: Use a 32bit IO_STATUS_BLOCK if the caller is 32bit.
  956. try {
  957. CmpSetIoStatus(PostBlock->u->AsyncUser.IoStatusBlock,
  958. STATUS_NOTIFY_CLEANUP,
  959. 0L,
  960. PsGetCurrentProcess()->Wow64Process != NULL);
  961. } except (EXCEPTION_EXECUTE_HANDLER) {
  962. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_EXCEPTION,"!!CmNotifyRundown: code:%08lx\n", GetExceptionCode()));
  963. NOTHING;
  964. }
  965. if (PostBlock->u->AsyncUser.UserEvent != NULL) {
  966. KeSetEvent(
  967. PostBlock->u->AsyncUser.UserEvent,
  968. 0,
  969. FALSE
  970. );
  971. ObDereferenceObject(PostBlock->u->AsyncUser.UserEvent);
  972. }
  973. //
  974. // Cancel the APC. Otherwise the rundown routine will also
  975. // free the post block if the APC happens to be queued at
  976. // this point. If the APC is queued, then the post block has
  977. // already been removed from the notify list, so don't remove
  978. // it again.
  979. //
  980. if (!KeRemoveQueueApc(PostBlock->u->AsyncUser.Apc)) {
  981. //
  982. // remove from notify block's list
  983. //
  984. // Use Cmp variant to protect for multiple deletion of the same object
  985. CmpRemoveEntryList(&(PostBlock->NotifyList));
  986. //
  987. // Cancel all slave Post requests that may be linked to self
  988. //
  989. CmpCancelSlavePost(PostBlock,NULL); // we do not want delayed deref
  990. } else {
  991. //
  992. // if we are here, the apc was in the apc queue, i.e. both master and slave
  993. // post blocks were removed from the notify list. nothing more to do.
  994. //
  995. ASSERT( CmpIsListEmpty(&(PostBlock->NotifyList)) );
  996. NOTHING;
  997. }
  998. } else {
  999. //
  1000. // remove from notify block's list
  1001. //
  1002. // Use Cmp variant to protect for multiple deletion of the same object
  1003. CmpRemoveEntryList(&(PostBlock->NotifyList));
  1004. //
  1005. // Cancel all slave Post requests that may be linked to self
  1006. //
  1007. CmpCancelSlavePost(PostBlock,NULL); // we do not want delayed deref
  1008. }
  1009. //
  1010. // Free the slave Post blocks too
  1011. //
  1012. CmpFreeSlavePost(PostBlock);
  1013. //
  1014. // Free the post block. Use Ex call because PostBlocks are NOT
  1015. // part of the global registry pool computation, but are instead
  1016. // part of NonPagedPool with Quota.
  1017. //
  1018. CmpFreePostBlock(PostBlock);
  1019. } else {
  1020. #if DBG
  1021. if(PostBlock->TraceIntoDebugger) {
  1022. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_FLOW,"[CM]\tCmpNotifyRunDown: PostBlock:%p is a slave block\n", PostBlock));
  1023. }
  1024. #endif
  1025. //
  1026. // master should always be ahead of slaves; if we got here, we switched master
  1027. // and slaves back in CmpPostNotify; Show some respect and add slave at the end;
  1028. // master will control the cleanup
  1029. //
  1030. ASSERT( CmpIsListEmpty(&(PostBlock->CancelPostList)) == FALSE );
  1031. ASSERT( IsListEmpty(&(Thread->PostBlockList)) == FALSE );
  1032. InsertTailList(
  1033. &(Thread->PostBlockList),
  1034. &(PostBlock->ThreadList)
  1035. );
  1036. }
  1037. }
  1038. KeLowerIrql(OldIrql);
  1039. // This is not needed (see the rule above)
  1040. //UNLOCK_POST_LIST();
  1041. CmpUnlockRegistry();
  1042. return;
  1043. }
  1044. VOID
  1045. CmpFlushNotify(
  1046. PCM_KEY_BODY KeyBody
  1047. )
  1048. /*++
  1049. Routine Description:
  1050. Clean up notifyblock when a handle is closed or the key it refers
  1051. to is deleted.
  1052. Arguments:
  1053. KeyBody - supplies pointer to key object body for handle we
  1054. are cleaning up.
  1055. Return Value:
  1056. NONE
  1057. --*/
  1058. {
  1059. PCM_NOTIFY_BLOCK NotifyBlock;
  1060. PCMHIVE Hive;
  1061. PAGED_CODE();
  1062. ASSERT_CM_LOCK_OWNED();
  1063. if (KeyBody->NotifyBlock == NULL) {
  1064. return;
  1065. }
  1066. ASSERT( KeyBody->KeyControlBlock->Delete == FALSE );
  1067. //
  1068. // Lock the hive exclusively to prevent multiple threads from whacking
  1069. // on the list.
  1070. //
  1071. Hive = CONTAINING_RECORD(KeyBody->KeyControlBlock->KeyHive,
  1072. CMHIVE,
  1073. Hive);
  1074. CmLockHive(Hive);
  1075. //
  1076. // Reread the notify block in case it has already been freed.
  1077. //
  1078. NotifyBlock = KeyBody->NotifyBlock;
  1079. if (NotifyBlock == NULL) {
  1080. CmUnlockHive(Hive);
  1081. return;
  1082. }
  1083. //
  1084. // Clean up all PostBlocks waiting on the NotifyBlock
  1085. //
  1086. if (IsListEmpty(&(NotifyBlock->PostList)) == FALSE) {
  1087. CmpPostNotify(
  1088. NotifyBlock,
  1089. NULL,
  1090. 0,
  1091. STATUS_NOTIFY_CLEANUP,
  1092. NULL
  1093. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  1094. ,
  1095. NULL
  1096. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  1097. );
  1098. }
  1099. //
  1100. // Release the subject context
  1101. //
  1102. SeReleaseSubjectContext(&NotifyBlock->SubjectContext);
  1103. //
  1104. // IMPLEMENTATION NOTE:
  1105. // If we ever do code to report names and types of events,
  1106. // this is the place to free the buffer.
  1107. //
  1108. //
  1109. // Remove the NotifyBlock from the hive chain
  1110. //
  1111. NotifyBlock->HiveList.Blink->Flink = NotifyBlock->HiveList.Flink;
  1112. if (NotifyBlock->HiveList.Flink != NULL) {
  1113. NotifyBlock->HiveList.Flink->Blink = NotifyBlock->HiveList.Blink;
  1114. }
  1115. // Protect for multiple deletion of the same object
  1116. CmpClearListEntry(&(NotifyBlock->HiveList));
  1117. KeyBody->NotifyBlock = NULL;
  1118. #ifdef CMP_ENTRYLIST_MANIPULATION
  1119. if (IsListEmpty(&(NotifyBlock->PostList)) == FALSE) {
  1120. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpFlushNotify: NotifyBlock %08lx\n",NotifyBlock);
  1121. DbgBreakPoint();
  1122. }
  1123. //check is the notify has been deleted from the hive notify list
  1124. {
  1125. PCM_NOTIFY_BLOCK ValidNotifyBlock;
  1126. PLIST_ENTRY NotifyPtr;
  1127. NotifyPtr = &(Hive->NotifyList);
  1128. while (NotifyPtr->Flink != NULL) {
  1129. NotifyPtr = NotifyPtr->Flink;
  1130. ValidNotifyBlock = CONTAINING_RECORD(NotifyPtr, CM_NOTIFY_BLOCK, HiveList);
  1131. if( ValidNotifyBlock == NotifyBlock ) {
  1132. DbgPrintEx(DPFLTR_CONFIG_ID,DPFLTR_ERROR_LEVEL,"CmpFlushNotify: NotifyBlock %08lx is about to be deleted but is still in the hive notify list\n",NotifyBlock);
  1133. DbgBreakPoint();
  1134. }
  1135. }
  1136. }
  1137. RtlZeroMemory((PVOID)NotifyBlock, sizeof(CM_NOTIFY_BLOCK));
  1138. #endif
  1139. CmUnlockHive(Hive);
  1140. //
  1141. // Free the block, clean up the KeyBody
  1142. //
  1143. ExFreePool(NotifyBlock);
  1144. return;
  1145. }
  1146. //
  1147. // "Front Side" of notify. See also Ntapi.c: ntnotifychangekey
  1148. //
  1149. NTSTATUS
  1150. CmpNotifyChangeKey(
  1151. IN PCM_KEY_BODY KeyBody,
  1152. IN PCM_POST_BLOCK PostBlock,
  1153. IN ULONG CompletionFilter,
  1154. IN BOOLEAN WatchTree,
  1155. IN PVOID Buffer,
  1156. IN ULONG BufferSize,
  1157. IN PCM_POST_BLOCK MasterPostBlock
  1158. )
  1159. /*++
  1160. Routine Description:
  1161. This routine sets up the NotifyBlock, and attaches the PostBlock
  1162. to it. When it returns, the Notify is visible to the system,
  1163. and will receive event reports.
  1164. If there is already an event report pending, then the notify
  1165. call will be satisified at once.
  1166. Arguments:
  1167. KeyBody - pointer to key object that handle refers to, allows access
  1168. to key control block, notify block, etc.
  1169. PostBlock - pointer to structure that describes how/where the caller
  1170. is to be notified.
  1171. WARNING: PostBlock must come from Pool, THIS routine
  1172. will keep it, back side will free it. This
  1173. routine WILL free it in case of error.
  1174. CompletionFilter - what types of events the caller wants to see
  1175. WatchTree - TRUE to watch whole subtree, FALSE to watch only immediate
  1176. key the notify is applied to
  1177. Buffer - pointer to area to recieve notify data
  1178. BufferSize - size of buffer, also size user would like to allocate
  1179. for internal buffer
  1180. MasterPostBlock - the post block of the master notification. Used to
  1181. insert the PostBlock into the CancelPostList list.
  1182. Return Value:
  1183. Status.
  1184. --*/
  1185. {
  1186. PCM_NOTIFY_BLOCK NotifyBlock;
  1187. PCM_NOTIFY_BLOCK node;
  1188. PLIST_ENTRY ptr;
  1189. PCMHIVE Hive;
  1190. KIRQL OldIrql;
  1191. PAGED_CODE();
  1192. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpNotifyChangeKey:\n"));
  1193. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\tKeyBody:%p PostBlock:%p ", KeyBody, PostBlock));
  1194. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"Filter:%08lx WatchTree:%08lx\n", CompletionFilter, WatchTree));
  1195. //
  1196. // The registry lock should be aquired exclusively by the caller !!!
  1197. //
  1198. ASSERT_CM_LOCK_OWNED_EXCLUSIVE();
  1199. if (KeyBody->KeyControlBlock->Delete) {
  1200. ASSERT( KeyBody->NotifyBlock == NULL );
  1201. CmpFreePostBlock(PostBlock);
  1202. return STATUS_KEY_DELETED;
  1203. }
  1204. #if DBG
  1205. if(PostBlock->TraceIntoDebugger) {
  1206. WCHAR *NameBuffer = NULL;
  1207. UNICODE_STRING KeyName;
  1208. PCM_KEY_NODE TempNode;
  1209. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmpNotifyChangeKey: PostBlock:%p\tMasterBlock: %p\n", PostBlock,MasterPostBlock));
  1210. TempNode = (PCM_KEY_NODE)HvGetCell(KeyBody->KeyControlBlock->KeyHive, KeyBody->KeyControlBlock->KeyCell);
  1211. if( TempNode != NULL ) {
  1212. NameBuffer = ExAllocatePool(PagedPool, REG_MAX_KEY_NAME_LENGTH);
  1213. if(NameBuffer&& (KeyBody->KeyControlBlock->KeyCell != HCELL_NIL)) {
  1214. CmpInitializeKeyNameString(TempNode,&KeyName,NameBuffer);
  1215. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"\t[CM]CmpNotifyChangeKey: Key = %.*S\n",KeyName.Length / sizeof(WCHAR),KeyName.Buffer));
  1216. ExFreePool(NameBuffer);
  1217. }
  1218. HvReleaseCell(KeyBody->KeyControlBlock->KeyHive, KeyBody->KeyControlBlock->KeyCell);
  1219. }
  1220. }
  1221. #endif
  1222. Hive = (PCMHIVE)KeyBody->KeyControlBlock->KeyHive;
  1223. Hive = CONTAINING_RECORD(Hive, CMHIVE, Hive);
  1224. NotifyBlock = KeyBody->NotifyBlock;
  1225. if (NotifyBlock == NULL) {
  1226. //
  1227. // Set up new notify session
  1228. //
  1229. NotifyBlock = ExAllocatePoolWithQuotaTag(PagedPool|POOL_QUOTA_FAIL_INSTEAD_OF_RAISE,sizeof(CM_NOTIFY_BLOCK),CM_NOTIFYBLOCK_TAG);
  1230. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_POOL,"**CmpNotifyChangeKey: allocate:%08lx, ", sizeof(CM_NOTIFY_BLOCK)));
  1231. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_POOL,"type:%d, at:%p\n", PagedPool, NotifyBlock));
  1232. if (NotifyBlock == NULL) {
  1233. CmpFreePostBlock(PostBlock);
  1234. return STATUS_INSUFFICIENT_RESOURCES;
  1235. }
  1236. NotifyBlock->KeyControlBlock = KeyBody->KeyControlBlock;
  1237. NotifyBlock->Filter = CompletionFilter;
  1238. NotifyBlock->WatchTree = WatchTree;
  1239. NotifyBlock->NotifyPending = FALSE;
  1240. InitializeListHead(&(NotifyBlock->PostList));
  1241. KeyBody->NotifyBlock = NotifyBlock;
  1242. NotifyBlock->KeyBody = KeyBody;
  1243. ASSERT( KeyBody->KeyControlBlock->Delete == FALSE );
  1244. #if DBG
  1245. if(PostBlock->TraceIntoDebugger) {
  1246. WCHAR *NameBuffer = NULL;
  1247. UNICODE_STRING KeyName;
  1248. PCM_KEY_NODE TempNode;
  1249. TempNode = (PCM_KEY_NODE)HvGetCell(KeyBody->KeyControlBlock->KeyHive, KeyBody->KeyControlBlock->KeyCell);
  1250. if( TempNode != NULL ) {
  1251. NameBuffer = ExAllocatePool(PagedPool, REG_MAX_KEY_NAME_LENGTH);
  1252. if(NameBuffer) {
  1253. CmpInitializeKeyNameString(TempNode,&KeyName,NameBuffer);
  1254. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]\tCmpNotifyChangeKey: New NotifyBlock at:%p was allocated for Key = %.*S\n",NotifyBlock,KeyName.Length / sizeof(WCHAR),KeyName.Buffer));
  1255. ExFreePool(NameBuffer);
  1256. }
  1257. HvReleaseCell(KeyBody->KeyControlBlock->KeyHive, KeyBody->KeyControlBlock->KeyCell);
  1258. }
  1259. }
  1260. #endif
  1261. //
  1262. // IMPLEMENTATION NOTE:
  1263. // If we ever want to actually return the buffers full of
  1264. // data, the buffer should be allocated and its address
  1265. // stored in the notify block here.
  1266. //
  1267. //
  1268. // Capture the subject context so we can do checking once the
  1269. // notify goes off.
  1270. //
  1271. SeCaptureSubjectContext(&NotifyBlock->SubjectContext);
  1272. //
  1273. // Attach notify block to hive in properly sorted order
  1274. //
  1275. ptr = &(Hive->NotifyList);
  1276. while (TRUE) {
  1277. if (ptr->Flink == NULL) {
  1278. //
  1279. // End of list, add self after ptr.
  1280. //
  1281. ptr->Flink = &(NotifyBlock->HiveList);
  1282. NotifyBlock->HiveList.Flink = NULL;
  1283. NotifyBlock->HiveList.Blink = ptr;
  1284. break;
  1285. }
  1286. ptr = ptr->Flink;
  1287. node = CONTAINING_RECORD(ptr, CM_NOTIFY_BLOCK, HiveList);
  1288. if (node->KeyControlBlock->TotalLevels >
  1289. KeyBody->KeyControlBlock->TotalLevels)
  1290. {
  1291. //
  1292. // ptr -> notify with longer name than us, insert in FRONT
  1293. //
  1294. NotifyBlock->HiveList.Flink = ptr;
  1295. ptr->Blink->Flink = &(NotifyBlock->HiveList);
  1296. NotifyBlock->HiveList.Blink = ptr->Blink;
  1297. ptr->Blink = &(NotifyBlock->HiveList);
  1298. break;
  1299. }
  1300. }
  1301. }
  1302. //
  1303. // Add post block to front of notify block's list, and add it to thread list.
  1304. //
  1305. InsertHeadList(
  1306. &(NotifyBlock->PostList),
  1307. &(PostBlock->NotifyList)
  1308. );
  1309. if( IsMasterPostBlock(PostBlock) ) {
  1310. //
  1311. // Protect against outrageous calls
  1312. //
  1313. ASSERT(PostBlock == MasterPostBlock);
  1314. //
  1315. // When the notification is a master one, initialize the CancelPostList list
  1316. //
  1317. InitializeListHead(&(PostBlock->CancelPostList));
  1318. } else {
  1319. //
  1320. // Add PostBlock at the end of the CancelPostList list from the master post
  1321. //
  1322. InsertTailList(
  1323. &(MasterPostBlock->CancelPostList),
  1324. &(PostBlock->CancelPostList)
  1325. );
  1326. }
  1327. KeRaiseIrql(APC_LEVEL, &OldIrql);
  1328. //
  1329. // show some respect and add masters to the front and slaves to the tail
  1330. //
  1331. if( IsMasterPostBlock(PostBlock) ) {
  1332. InsertHeadList(
  1333. &(PsGetCurrentThread()->PostBlockList),
  1334. &(PostBlock->ThreadList)
  1335. );
  1336. } else {
  1337. InsertTailList(
  1338. &(PsGetCurrentThread()->PostBlockList),
  1339. &(PostBlock->ThreadList)
  1340. );
  1341. }
  1342. #if DBG
  1343. if(PostBlock->TraceIntoDebugger) {
  1344. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]\tCmpNotifyChangeKey: Attaching the post:%p\t to thread:%p\n",PostBlock,PsGetCurrentThread()));
  1345. }
  1346. #endif
  1347. KeLowerIrql(OldIrql);
  1348. //
  1349. // If there is a notify pending (will not be if we just created
  1350. // the notify block) then post it at once. Note that this call
  1351. // ALWAYS returns STATUS_PENDING unless it fails. Caller must
  1352. // ALWAYS look in IoStatusBlock to see what happened.
  1353. //
  1354. if (NotifyBlock->NotifyPending == TRUE) {
  1355. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  1356. PUNICODE_STRING FullKcbName = CmpConstructName(KeyBody->KeyControlBlock);
  1357. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  1358. CmpPostNotify(
  1359. NotifyBlock,
  1360. NULL,
  1361. 0,
  1362. STATUS_NOTIFY_ENUM_DIR,
  1363. NULL
  1364. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  1365. ,
  1366. FullKcbName
  1367. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  1368. );
  1369. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  1370. if( FullKcbName != NULL ) {
  1371. ExFreePoolWithTag(FullKcbName, CM_NAME_TAG | PROTECTED_POOL);
  1372. }
  1373. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH
  1374. //
  1375. // return STATUS_SUCCESS to signal to the caller the the notify already been triggered
  1376. //
  1377. return STATUS_SUCCESS;
  1378. }
  1379. //
  1380. // return STATUS_PENDING to signal to the caller the the notify has not been triggered yet
  1381. //
  1382. return STATUS_PENDING;
  1383. }
  1384. VOID
  1385. CmpFreeSlavePost(
  1386. PCM_POST_BLOCK MasterPostBlock
  1387. )
  1388. /*++
  1389. Routine Description:
  1390. Free the slave post block related to this master post block
  1391. Arguments:
  1392. MasterPostBlock - pointer to structure that describes the post requests.
  1393. It should be a master post!!
  1394. Return Value:
  1395. NONE.
  1396. --*/
  1397. {
  1398. PCM_POST_BLOCK SlavePostBlock;
  1399. PAGED_CODE();
  1400. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpCancelSlavePost:\t"));
  1401. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"MasterPostBlock:%p\n", MasterPostBlock));
  1402. ASSERT(IsMasterPostBlock(MasterPostBlock));
  1403. #if DBG
  1404. if(MasterPostBlock->TraceIntoDebugger) {
  1405. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmCancelSlavePost: MasterPostBlock:%p\n", MasterPostBlock));
  1406. }
  1407. #endif
  1408. if (IsListEmpty(&(MasterPostBlock->CancelPostList)) == TRUE) {
  1409. //
  1410. // Nothing to cancel, just return
  1411. //
  1412. #if DBG
  1413. if(MasterPostBlock->TraceIntoDebugger) {
  1414. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmCancelSlavePost: MasterPostBlock:%p has no slaves\n", MasterPostBlock));
  1415. }
  1416. #endif
  1417. return;
  1418. }
  1419. //
  1420. // Pull all the entries in the cancel post list and unlink them (when they are slave requests)
  1421. // We base here on the assumption that there is only one slave.
  1422. //
  1423. // NOTE!!!
  1424. // When more than slave allowed, here to modify
  1425. //
  1426. SlavePostBlock = (PCM_POST_BLOCK)MasterPostBlock->CancelPostList.Flink;
  1427. SlavePostBlock = CONTAINING_RECORD(SlavePostBlock,
  1428. CM_POST_BLOCK,
  1429. CancelPostList);
  1430. #if DBG
  1431. if(MasterPostBlock->TraceIntoDebugger) {
  1432. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmCancelSlavePost: Cleaning SlavePostBlock:%p\n", SlavePostBlock));
  1433. }
  1434. #endif
  1435. //
  1436. // This should be true !
  1437. //
  1438. ASSERT( !IsMasterPostBlock(SlavePostBlock) );
  1439. //
  1440. // Unchain from the Master CancelPostList
  1441. //
  1442. // Use Cmp variant to protect for multiple deletion of the same object
  1443. CmpRemoveEntryList(&(SlavePostBlock->CancelPostList));
  1444. //
  1445. // delist the post block from the thread postblocklist
  1446. //
  1447. // Use Cmp variant to protect for multiple deletion of the same object
  1448. CmpRemoveEntryList(&(SlavePostBlock->ThreadList));
  1449. //
  1450. // Free the post block.
  1451. //
  1452. CmpFreePostBlock(SlavePostBlock);
  1453. //
  1454. // Result validation. was it the only slave?
  1455. //
  1456. ASSERT(IsListEmpty(&(MasterPostBlock->CancelPostList)));
  1457. }
  1458. VOID
  1459. CmpCancelSlavePost(
  1460. PCM_POST_BLOCK MasterPostBlock,
  1461. PLIST_ENTRY DelayedDeref
  1462. )
  1463. /*++
  1464. Routine Description:
  1465. Unlink the slave postblock from its notify list and dereferences (or adds to the delayed deref list)
  1466. the keybody related to this thread. This should disable the slave post block.
  1467. It will be cleared later in CmpPostApc.
  1468. Arguments:
  1469. MasterPostBlock - pointer to structure that describes the post requests.
  1470. It should be a master post!!
  1471. DelayedDeref - pointer to list of delayed deref keybodies. If this parameter is not NULL,
  1472. the keybody for the slave is not cleared before calling CmpFreePostBlock,
  1473. and instead is added to the list
  1474. Return Value:
  1475. NONE.
  1476. --*/
  1477. {
  1478. PCM_POST_BLOCK SlavePostBlock;
  1479. PAGED_CODE();
  1480. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"CmpCancelSlavePost:\t"));
  1481. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"MasterPostBlock:%p\n", MasterPostBlock));
  1482. ASSERT_CM_LOCK_OWNED();
  1483. ASSERT(IsMasterPostBlock(MasterPostBlock));
  1484. #if DBG
  1485. if(MasterPostBlock->TraceIntoDebugger) {
  1486. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmCancelSlavePost: MasterPostBlock:%p\n", MasterPostBlock));
  1487. }
  1488. #endif
  1489. if (IsListEmpty(&(MasterPostBlock->CancelPostList)) == TRUE) {
  1490. //
  1491. // Nothing to cancel, just return
  1492. //
  1493. #if DBG
  1494. if(MasterPostBlock->TraceIntoDebugger) {
  1495. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmCancelSlavePost: MasterPostBlock:%p has no slaves\n", MasterPostBlock));
  1496. }
  1497. #endif
  1498. return;
  1499. }
  1500. //
  1501. // Pull all the entries in the cancel post list and unlink them (when they are slave requests)
  1502. // We base here on the assumption that there is only one slave.
  1503. //
  1504. // NOTE!!!
  1505. // When more than slave allowed, here to modify
  1506. //
  1507. SlavePostBlock = (PCM_POST_BLOCK)MasterPostBlock->CancelPostList.Flink;
  1508. SlavePostBlock = CONTAINING_RECORD(SlavePostBlock,
  1509. CM_POST_BLOCK,
  1510. CancelPostList);
  1511. #if DBG
  1512. if(MasterPostBlock->TraceIntoDebugger) {
  1513. CmKdPrintEx((DPFLTR_CONFIG_ID,CML_NOTIFY,"[CM]CmCancelSlavePost: Cleaning SlavePostBlock:%p\n", SlavePostBlock));
  1514. }
  1515. #endif
  1516. //
  1517. // This should be true !
  1518. //
  1519. ASSERT( !IsMasterPostBlock(SlavePostBlock) );
  1520. //
  1521. // Remove it from notify block's list
  1522. //
  1523. // Use Cmp variant to protect for multiple deletion of the same object
  1524. // This will disable the notifications that might come on the slave key
  1525. //
  1526. CmpRemoveEntryList(&(SlavePostBlock->NotifyList));
  1527. if( DelayedDeref ) {
  1528. //
  1529. // the caller wants to handle key body dereferenciation by himself
  1530. //
  1531. CmpAddToDelayedDeref(SlavePostBlock,DelayedDeref);
  1532. }
  1533. }
  1534. VOID
  1535. CmpAddToDelayedDeref(
  1536. PCM_POST_BLOCK PostBlock,
  1537. PLIST_ENTRY DelayedDeref
  1538. )
  1539. /*++
  1540. Routine Description:
  1541. Add the key body attached to the post block to the delayed deref list.
  1542. Cleans the post block KeyBody member, so it will not be dereferenced
  1543. when the post block is freed.
  1544. Arguments:
  1545. PostBlock - pointer to structure that describes the post requests.
  1546. DelayedDeref - the delayed deref list
  1547. Return Value:
  1548. NONE.
  1549. --*/
  1550. {
  1551. PAGED_CODE();
  1552. // common sense
  1553. ASSERT( PostBlock != NULL );
  1554. if( PostBlock->PostKeyBody ) {
  1555. //
  1556. // If the post block has a keybody attached, add it to delayed deref list and
  1557. // clear the post block member. The key body will be deref'd prior after
  1558. // postblock lock is released.
  1559. //
  1560. // extra validation
  1561. ASSERT(PostBlock->PostKeyBody->KeyBody != NULL);
  1562. ASSERT(DelayedDeref);
  1563. // add it to the end of the list
  1564. InsertTailList(
  1565. DelayedDeref,
  1566. &(PostBlock->PostKeyBody->KeyBodyList)
  1567. );
  1568. // make sure we don't deref it in CmpFreePostBlock
  1569. PostBlock->PostKeyBody = NULL;
  1570. }
  1571. return;
  1572. }
  1573. VOID
  1574. CmpDelayedDerefKeys(
  1575. PLIST_ENTRY DelayedDeref
  1576. )
  1577. /*++
  1578. Routine Description:
  1579. Walk through the entire list, dereference each keybody and free storage for the
  1580. CM_POST_KEY_BODY allocated for this purpose.
  1581. Arguments:
  1582. DelayedDeref - the delayed deref list
  1583. Return Value:
  1584. NONE.
  1585. --*/
  1586. {
  1587. PCM_POST_KEY_BODY PostKeyBody;
  1588. PAGED_CODE();
  1589. // common sense
  1590. ASSERT( DelayedDeref != NULL );
  1591. while(IsListEmpty(DelayedDeref) == FALSE) {
  1592. //
  1593. // Remove from the delayed deref list and deref the coresponding keybody
  1594. // free the storage associated with CM_POST_KEY_BODY
  1595. //
  1596. PostKeyBody = (PCM_POST_KEY_BODY)RemoveHeadList(DelayedDeref);
  1597. PostKeyBody = CONTAINING_RECORD(PostKeyBody,
  1598. CM_POST_KEY_BODY,
  1599. KeyBodyList);
  1600. // extra validation
  1601. ASSERT(PostKeyBody->KeyBody != NULL);
  1602. // this should be a valid key body
  1603. ASSERT(PostKeyBody->KeyBody->Type == KEY_BODY_TYPE);
  1604. // at last ..... dereference the key object
  1605. ObDereferenceObject(PostKeyBody->KeyBody);
  1606. // Free the storage for the CM_POST_KEY_BODY object (allocated by CmpAllocatePostBlock)
  1607. ExFreePool(PostKeyBody);
  1608. }
  1609. }
  1610. #ifdef CM_NOTIFY_CHANGED_KCB_FULLPATH
  1611. VOID
  1612. CmpFillCallerBuffer(
  1613. PCM_POST_BLOCK PostBlock,
  1614. PUNICODE_STRING ChangedKcbName
  1615. )
  1616. /*++
  1617. Routine Description:
  1618. Copies the full qualified name of the changed kcb to the
  1619. caller buffer (stored in the postblock).
  1620. Arguments:
  1621. PostBlock - post block holding the user buffer address and size
  1622. ChangedKcbName - unicode string holding the full qualified path of the kcb
  1623. - this may be null
  1624. Return Value:
  1625. NONE.
  1626. --*/
  1627. {
  1628. USHORT RequestedSize;
  1629. USHORT Length;
  1630. PUNICODE_STRING CallerUnicode;
  1631. PAGED_CODE();
  1632. if( PostBlock->CallerBuffer == NULL ) {
  1633. //
  1634. // nothing to do; the caller didn't request this info.
  1635. //
  1636. return;
  1637. }
  1638. //
  1639. // compute the requested size for the caller buffer
  1640. //
  1641. RequestedSize = sizeof(UNICODE_STRING);
  1642. if( PostBlock->CallerBufferSize < RequestedSize ) {
  1643. //
  1644. // bad luck!; not enough space- not even for an empty unicode string
  1645. //
  1646. return;
  1647. }
  1648. if(ChangedKcbName != NULL) {
  1649. Length = ChangedKcbName->Length;
  1650. } else {
  1651. Length = 0;
  1652. }
  1653. RequestedSize += Length;
  1654. //
  1655. // fill up the caller buffer
  1656. //
  1657. try {
  1658. CallerUnicode = (PUNICODE_STRING)PostBlock->CallerBuffer;
  1659. CallerUnicode->Buffer = (USHORT *) ((ULONG_PTR) CallerUnicode + sizeof(UNICODE_STRING));
  1660. CallerUnicode->MaximumLength = (USHORT)(PostBlock->CallerBufferSize - sizeof(UNICODE_STRING));
  1661. if( CallerUnicode->MaximumLength < Length ) {
  1662. Length = CallerUnicode->MaximumLength;
  1663. }
  1664. //
  1665. // copy the actual data
  1666. //
  1667. if( Length > 0 ) {
  1668. ASSERT( ChangedKcbName != NULL );
  1669. RtlCopyMemory(CallerUnicode->Buffer,ChangedKcbName->Buffer,Length);
  1670. }
  1671. CallerUnicode->Length = Length;
  1672. } except (EXCEPTION_EXECUTE_HANDLER) {
  1673. NOTHING;
  1674. }
  1675. }
  1676. VOID
  1677. CmpFillPostBlockBuffer(
  1678. PCM_POST_BLOCK PostBlock,
  1679. PUNICODE_STRING ChangedKcbName OPTIONAL
  1680. )
  1681. /*++
  1682. Routine Description:
  1683. Copies the full qualified name of the changed kcb to the
  1684. postblock private kernel buffer
  1685. Arguments:
  1686. PostBlock - post block in question
  1687. ChangedKcbName - unicode string holding the full qualified path of the kcb
  1688. - this may be null
  1689. Return Value:
  1690. NONE.
  1691. --*/
  1692. {
  1693. PUNICODE_STRING FullName;
  1694. USHORT Size;
  1695. PAGED_CODE();
  1696. //
  1697. // we only store this info in masters (or promoted)
  1698. //
  1699. ASSERT( IsMasterPostBlock(PostBlock) );
  1700. //
  1701. // copy the kcb name (if any) into the postblock kernel mode buffer
  1702. //
  1703. if( ARGUMENT_PRESENT(ChangedKcbName) && // we have a kcb name
  1704. (PostBlock->CallerBuffer != NULL) // and the user requested for the info.
  1705. ) {
  1706. Size = sizeof(UNICODE_STRING) + ChangedKcbName->Length;
  1707. //
  1708. // allocate a kernel buffer to store the name; it'll be freed in CmpFreePostBlock
  1709. //
  1710. FullName = (PUNICODE_STRING) ExAllocatePoolWithTag(PagedPool,Size,CM_FIND_LEAK_TAG43);
  1711. if (FullName) {
  1712. FullName->Buffer = (USHORT *) ((ULONG_PTR) FullName + sizeof(UNICODE_STRING));
  1713. FullName->Length = ChangedKcbName->Length;
  1714. FullName->MaximumLength = ChangedKcbName->Length;
  1715. RtlCopyMemory(FullName->Buffer,ChangedKcbName->Buffer,FullName->Length);
  1716. PostBlock->ChangedKcbFullName = FullName;
  1717. }
  1718. //
  1719. // we successfully stored the full kcb name into the post block
  1720. // the apc (or the sync side of the notification will take care
  1721. // of transfering it to the caller buffer
  1722. //
  1723. }
  1724. }
  1725. #endif //CM_NOTIFY_CHANGED_KCB_FULLPATH