Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | #if DEVELOPMENT || DEBUG #include "TestIOServiceUserNotification.h" #include <IOKit/IOService.h> #include <IOKit/IOUserClient.h> #include <IOKit/IOKitServer.h> #include <kern/ipc_kobject.h> #include "../../Kernel/IOServicePrivate.h" OSDefineMetaClassAndStructors(TestIOServiceUserNotification, IOService); OSDefineMetaClassAndStructors(TestIOServiceUserNotificationUserClient, IOUserClient); bool TestIOServiceUserNotification::start(IOService * provider) { OSString * str = OSString::withCStringNoCopy("TestIOServiceUserNotificationUserClient"); bool ret = IOService::start(provider); if (ret && str != NULL) { setProperty(gIOUserClientClassKey, str); fUserNotifications = OSArray::withCapacity(1); fLock = IOLockAlloc(); registerService(); } OSSafeReleaseNULL(str); return ret; } void TestIOServiceUserNotification::free() { if (fLock) { IOLockFree(fLock); fLock = NULL; } OSSafeReleaseNULL(fUserNotifications); IOService::free(); } void TestIOServiceUserNotification::registerUserNotification(OSObject * notification) { IOLockLock(fLock); // Proactively trim the list to avoid holding too many objects trimUserNotificationsLocked(); assert(fUserNotifications->getNextIndexOfObject(notification, 0) == -1); fUserNotifications->setObject(notification); IOLockUnlock(fLock); } void TestIOServiceUserNotification::trimUserNotificationsLocked() { OSArray * remaining = OSArray::withCapacity(1); if (!remaining) { return; } fUserNotifications->iterateObjects(^(OSObject * obj) { if (obj->getRetainCount() != 1) { remaining->setObject(obj); } return false; }); fUserNotifications->release(); fUserNotifications = remaining; } size_t TestIOServiceUserNotification::getUserNotificationLeakCount() { size_t count = 0; IOLockLock(fLock); trimUserNotificationsLocked(); count = fUserNotifications->getCount(); IOLockUnlock(fLock); return count; } bool TestIOServiceUserNotificationUserClient::start(IOService * provider) { if (!IOUserClient::start(provider)) { return false; } fProvider = OSDynamicCast(TestIOServiceUserNotification, provider); assert(fProvider); return true; } IONotifier * TestIOServiceUserNotificationUserClient::registerInterest(const OSSymbol * typeOfInterest, IOServiceInterestHandler handler, void * target, void * ref) { IONotifier * notify = IOService::registerInterest(typeOfInterest, handler, target, ref); // No straightforward way to make sure registerInterest is called from the test app // Could check if handler is _ZN32IOServiceMessageUserNotification8_handlerEPvS0_jP9IOServiceS0_m // But still cannot rule out other user process regisering interest OSObject * obj = (OSObject *)target; // Just panic the system if target isn't OSObject fProvider->registerUserNotification(obj); return notify; } IOReturn TestIOServiceUserNotificationUserClient::clientClose() { if (!isInactive()) { terminate(); } return kIOReturnSuccess; } IOReturn TestIOServiceUserNotificationUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments * args, IOExternalMethodDispatch * dispatch, OSObject * target, void * reference) { if (selector == 0) { registerService(); } else if (selector == 1 && args->scalarOutputCount >= 1) { args->scalarOutput[0] = fProvider->getUserNotificationLeakCount(); } return kIOReturnSuccess; } #endif /* DEVELOPMENT || DEBUG */ |