检测 OS X 内核扩展是否可加载

Detect if OS X kernel extension is loadable

我正在尝试编写一个与自定义 USB 设备通信的应用程序。我们已经编写了一个驱动程序 (kext),当 USB 设备连接到计算机时,它会自动加载。它在安装过程中安装到适当的位置 (/System/Library/Extensions & /Library/Extensions)。

在我的应用程序 (.app) 中,我需要能够确认此 kext 已安装并且可以加载到用户的计算机上。

我最初的想法是使用"System"命令并得到"kextstat | grep mykext"的结果。然而,kextstat 只显示加载的内核扩展。 (应用程序有可能在用户连接 USB 设备之前启动)。

我唯一能想到的就是使用"kextfind | grep mykext" 来确定kext 是否存在,但是,我不知道它是否可加载。 (即它可能存在,但有人弄乱了 plist 文件,现在签名失败,所以它永远不会被加载)

知道这是可能的,因为 Apple 的系统分析器能够列出所有扩展,如果它们已加载,以及它们是否可加载。

来自 kextutil 手册页:

kextutil prints diagnostic information about kexts by default, but some options cause certain tests to be skipped. To ensure that all tests are performed, use the -print-diagnostics (-t) option. The -print-diagnostics option is typically used with -no-load (-n) after a load failure to pinpoint a problem. It can be used with any other set of options, however.

注意:当使用 -no-load (-n) 选项时,不需要 sudo(root 用户)。