与驱动程序通信 - OS X.

时间:2014-07-19 08:25:09

标签: macos kernel driver

给出一个非常简单的IO驱动程序:

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

问题是我不知道如何使用User Space应用程序进行通信,我该如何找到它? 我知道它与字典有关,我尝试使用以下代码找到它但没有运气(二手苹果开发者网站):

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

我做错了什么?如何找到正在运行的驱动程序并使用简单的用户空间应用程序与其进行通信?

1 个答案:

答案 0 :(得分:1)

您的IOKitTest实施文件是什么样的?在该文件中,您需要通过调用registerService()来注册该服务,以便客户端找到它。它可以在start函数中添加,例如:

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 nub。

matchingDict = IOServiceMatching("com_osxkernel_driver_IOKitTest");

有一些较旧的文档,这些函数的示例没有registerService()调用,这对新系统不起作用。如果这可以解决您的问题,请与我们联系。