阅读:2457回复:0
《寒江独钓》透明加密中清除缓存的函数几个疑问
//也算是一个笔记吧,里面有几个不太清楚的问题请大家指教
//清理缓冲 《寒江独钓》p233 void cfFileCacheClear(PFILE_OBJECT pFileObject) { PFSRTL_COMMON_FCB_HEADER pFcb; LARGE_INTEGER liInterval; BOOLEAN bNeedReleaseResource = FALSE; BOOLEAN bNeedReleasePagingIoResource = FALSE; KIRQL irql; pFcb = (PFSRTL_COMMON_FCB_HEADER)pFileObject->FsContext; //从文件对象得到FCB(file control block 文件控制块) if(pFcb == NULL) return; irql = KeGetCurrentIrql(); //得到当前IRQL并保证不大于DISPATCH_LEVEL方可进行 if (irql >= DISPATCH_LEVEL) //其后函数需要在小于DISPATCH_LEVEL级别上执行 { return; } liInterval.QuadPart = -1 * (LONGLONG)50; //后面需使用的等待时间 while (TRUE) { BOOLEAN bBreak = TRUE; BOOLEAN bLockedResource = FALSE; BOOLEAN bLockedPagingIoResource = FALSE; bNeedReleaseResource = FALSE; bNeedReleasePagingIoResource = FALSE; // 到fcb中去拿锁。 //测试当前线程是否有指定资源的独占访问权。返回TRUE if (pFcb->PagingIoResource) bLockedPagingIoResource = ExIsResourceAcquiredExclusiveLite(pFcb->PagingIoResource); // 总之一定要拿到这个锁。 if (pFcb->Resource) { bLockedResource = TRUE; if (ExIsResourceAcquiredExclusiveLite(pFcb->Resource) == FALSE) //此处不解??为什么要不能互斥获得pFcb->Resource才开始以下处理? { bNeedReleaseResource = TRUE; //开始获得资源 并将标识置为真。为其后不能同时获得RESOURCE及PAGERESURCE做释放准备 //不能全部获得两设备必须释放并重新进行获取过程,直到全部获取两资源。 if (bLockedPagingIoResource) //此处不解??根据另一资源能否互斥获取来决定获得本资源时等待还是立即返回? { if (ExAcquireResourceExclusiveLite(pFcb->Resource, FALSE) == FALSE) { bBreak = FALSE; //获取失败自然BREAK标记为FALSE,继续循环 bNeedReleaseResource = FALSE; //获取失败, bLockedResource = FALSE; } } else //有独占访问权则互斥获得资源 ExAcquireResourceExclusiveLite(pFcb->Resource, TRUE); } } if (bLockedPagingIoResource == FALSE) //获取PAGINGIORESOURCE类似获取RESOURCE { if (pFcb->PagingIoResource) { bLockedPagingIoResource = TRUE; bNeedReleasePagingIoResource = TRUE; if (bLockedResource) { if (ExAcquireResourceExclusiveLite(pFcb->PagingIoResource, FALSE) == FALSE) { bBreak = FALSE; bLockedPagingIoResource = FALSE; bNeedReleasePagingIoResource = FALSE; } } else { ExAcquireResourceExclusiveLite(pFcb->PagingIoResource, TRUE); } } } if (bBreak) { break; } //只获得其中一个资源必须释放,防止死锁。等待指定时间后,继续获取 if (bNeedReleasePagingIoResource) { ExReleaseResourceLite(pFcb->PagingIoResource); } if (bNeedReleaseResource) { ExReleaseResourceLite(pFcb->Resource); } if (irql == PASSIVE_LEVEL) //等待指定时间后继续循环 { KeDelayExecutionThread(KernelMode, FALSE, &liInterval); //KeDelayExecutionThread must be running at IRQL = PASSIVE_LEVEL. } else { //其他IRQL等级下 使用这种方法等待 KEVENT waitEvent; KeInitializeEvent(&waitEvent, NotificationEvent, FALSE); KeWaitForSingleObject(&waitEvent, Executive, KernelMode, FALSE, &liInterval); } } //互斥获取资源均为刷新缓存做准备。 if (pFileObject->SectionObjectPointer) { IO_STATUS_BLOCK ioStatus; CcFlushCache(pFileObject->SectionObjectPointer, NULL, 0, &ioStatus); if (pFileObject->SectionObjectPointer->ImageSectionObject) { MmFlushImageSection(pFileObject->SectionObjectPointer,MmFlushForWrite); // MmFlushForDelete } CcPurgeCacheSection(pFileObject->SectionObjectPointer, NULL, 0, FALSE); } if (bNeedReleasePagingIoResource) { ExReleaseResourceLite(pFcb->PagingIoResource); } if (bNeedReleaseResource) { ExReleaseResourceLite(pFcb->Resource); } } 文中的几处疑惑还是来自自己对API运用的不熟悉 if (ExIsResourceAcquiredExclusiveLite(pFcb->Resource) == FALSE) //此处不解??为什么要不能互斥获得pFcb->Resource才开始以下处理? if (bLockedPagingIoResource) //此处不解??根据另一资源能否互斥获取来决定获得本资源时等待还是立即返回? 那么会不会出现 ExIsResourceAcquiredExclusiveLite 得出互斥获取两资源均为真 而没有运行ExIsResourceAcquiredExclusiveLite来获得资源便来到BREAK处跳出了?? |
|