2014-07-19 62 views
0

給出一個非常簡單的IO驅動:溝通與驅動 - OS X

class com_osxkernel_driver_IOKitTest : public IOService 
{ 
    OSDeclareDefaultStructors(com_osxkernel_driver_IOKitTest) 

    public: 
     virtual bool init (OSDictionary* dictionary = nullptr); 
     virtual void free(void); 
     virtual IOService* probe (IOService* provider, SInt32* score); 
     virtual bool start (IOService* provider); 
     virtual void stop (IOService* provider); 
}; 

這裏的PLIST:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>IOKitTest</key> 
<dict> 
    <key>CFBundleIdentifier</key> 
    <string>com.osxkernel.${PRODUCT_NAME:rfc1034identifier}</string> 
    <key>IOClass</key> 
    <string>com_osxkernel_driver_IOKitTest</string> 
    <key>IOMatchCategory</key> 
    <string>com_osxkernel_driver_IOKitTest</string> 
    <key>IOProviderClass</key> 
    <string>IOResources</string> 
    <key>IOResourceMatch</key> 
    <string>IOKit</string> 
</dict> 

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>com.apple.kpi.iokit</key> 
<string>9.0.0</string> 
<key>com.apple.kpi.libkern</key> 
<string>9.0.0</string> 
</dict> 
</plist> 

的驅動程序運行完全正常,我可以使用「內核日誌」和打字時看到它:

kextstat | grep -v com.apple 

問題是我不知道如何使用用戶空間應用程序進行通信,如何找到它? 我知道它有什麼做的字典,我嘗試使用下面的代碼來找到它,但沒有運氣(用於蘋果開發者網站):

int search_driver() 
{ 
io_iterator_t iter = 0; 
io_service_t service = 0; 
kern_return_t kr; 

CFDictionaryRef matchingDict = IOServiceMatching("IOResources"); 
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter); 
if (kr != KERN_SUCCESS) { 
    printf("Nothing found. \n"); 
    return -1; 
} 
// Iterate over all matching objects. 
while ((service = IOIteratorNext(iter)) != 0) 
{ 
    CFStringRef  className; 
    io_name_t  name; 
    // List all IOUSBDevice objects, ignoring objects that subclass IOUSBDevice. 
    className = IOObjectCopyClass(service); 
    IORegistryEntryGetName(service, name); 

    printf("Found device with name: %s\n", name); 
    CFRelease(className); 
    IOObjectRelease(service); 
    // Release the iterator. 
    IOObjectRelease(iter); 
} 
return 0; 
} 

我想:

CFDictionaryRef matchingDict = IOServiceMatching("IOResources"); 
CFDictionaryRef matchingDict = IOServiceMatching("IOService"); 

和更多。

但仍然沒有運氣。第一個發現:IOResources 第二個發現:Macbook Air 5,2

我在做什麼錯了?如何找到正在運行的驅動程序並使用簡單的用戶空間應用程序與它進行通信?

回答

0

你的IOKitTest實現文件是什麼樣的?在該文件中,您需要通過調用registerService()來註冊服務,以便客戶端找到它。它可以在啓動功能被加入,例如:

bool com_osxkernel_driver_IOKitTest::start (IOService *provider) 
{ 
    bool res = super::start(provider); 
    super::registerService(); // <---- add this 
    IOLog("IOKitTest::start\n"); 
    return res; 
} 

然後在你的search_driver()函數,你可以再看看你的確切類,而不是在IOResources小塊。

matchingDict = IOServiceMatching("com_osxkernel_driver_IOKitTest"); 

這裏有一些較早的文檔,這些函數的例子沒有registerService()調用,它不能在較新的系統上工作。讓我知道這是否解決了您的問題。