自动卸载OS X驱动程序

Automatic unloading of an OS X driver

本文关键字:驱动程序 OS 卸载      更新时间:2023-10-16

这是我的第一个问题:)

我目前正在编写一个通用的ext,它提供了一个字符设备和目标OSX 10.7+。它是纯C,没有IOKit/c++。(如果这很重要,我正在10.11上测试驱动程序。)

我想在所有指向字符设备的文件描述符关闭后卸载驱动程序,但这似乎不起作用。

根据Apple的OSKextRetainKextWithLoadTag文档:

当autoload被启用时,在ext的最后一个引用被删除后不久,如果没有未完成的引用,并且没有它的Libkern c++子类的实例(如果有的话),它将被卸载。

定义IOService子类的文本自动启用了下载。其他kext可以使用引用计数来管理自动卸载,而无需定义和创建Libkern c++对象。

如上所述,我的ext没有任何IOService子类(或任何类,就此而言),所以我应该能够使用OSKextRetainKextWithLoadTag

但是,在所有文件描述符关闭后,文本将永远保持加载状态:

static int cdev_open(dev_t dev, int flags, int devtype, struct proc *p)
{
    /* ... */
    return OSKextRetainKextWithLoadTag(OSKextGetCurrentLoadTag()) == kOSReturnSuccess) ? 0 : kOSReturnError
}
static int cdev_close(dev_t dev, int flags, int devtype, struct proc *p)
{
    /* ... */
   OSKextReleaseKextWithLoadTag(OSKextGetCurrentLoadTag());
   return 0;
}

此外,我编写了我的kext的"混合"版本,其中我用一个提供IOService子类(IOResources作为提供者)的瘦c++包装器包装了启动和停止例程,以防通用kext不再支持卸载。相同的结果

(我发现了几个使用OSKextRetainKextWithLoadTagOSKextReleaseKextWithLoadTag的通用文本的例子,但是它们太老了,不知道它们是否适用于最新版本的OS x)

知道我做错了什么吗?

谢谢。

考虑以下generic ext:

kern_return_t xxxKext_start(kmod_info_t * ki, void *d)
{
    // this should set auto unload enabled 
    // and retain a refcount on the kext
    OSKextRetainKextWithLoadTag(OSKextGetCurrentLoadTag());
    // this should call OSKext::considerUnloads()
    // and remove the search retain and the previous call retain
    OSKextReleaseKextWithLoadTag(OSKextGetCurrentLoadTag());
    // somewhere here or even before - kext suppose to be unloaded automatically (according to Apple docs)
    return KERN_SUCCESS;
}
kern_return_t xxxKext_stop(kmod_info_t *ki, void *d)
{
   return KERN_SUCCESS;
}

无论调用kextunload或类似的方法,该文本都应该在短时间后自动消失。

10.12.1它工作完美。过一段时间(不是他们在文档中描述的那么短的延迟),它将从kextstat命令中消失。

10.11.6中,由于某种原因,它将保持加载状态。

所以你没有做错任何事情,它只是在某些版本中坏了