objective-c - 以编程方式从 .kext 目录获取 KEXT 的 bundle 标识符

标签 objective-c c macos kernel kernel-extension

我正在构建一个内核扩展并为其创建一个 .kext 目录。 从另一个库 API,我正在使用 KextManager APIs将此 kext 加载到内核中。

一切看起来都很好,这是我加载 kext 的代码片段:

CFStringRef path = CFStringCreateWithCString(kCFAllocatorDefault, "awesome.kext", kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, true);
OSReturn result  = KextManagerLoadKextWithURL(url, NULL);

它工作得很好,但是我需要知道我的包标识符才能控制套接字连接,并且稍后可能使用 KextManagerUnloadKextWithIdentifier(kextId) API 卸载 kext。

所以我知道 XXX.kext/Contents/Info.plist 其中包含带有包标识符的 CFBundleIdentifier 键,但我需要某种方式以编程方式获取它(某些API?)而不是读取 Info.plist 文件并解析它。

我还尝试用其他内容替换 CFBundleIdentifier 字符串值并加载到内核中,它仍然工作正常,所以看起来 Info.plist 无论如何都不可靠。

有什么相关的吗?有什么建议么?谢谢! :)

最佳答案

我最近发现了两种有用的方法,都基于KextManagerCopyLoadedKextInfo API。

void handle_kext(const void* key, const void* value, void* context)
{
    // OSBundlePath - CFString (this is merely a hint stored in the kernel; the kext is not guaranteed to be at this path)
    CFStringRef bundle_path_key = CFStringCreateWithCString(kCFAllocatorDefault, "OSBundlePath", kCFStringEncodingUTF8);

    const char* bundle_id_cstring = CFStringGetCStringPtr(key, kCFStringEncodingUTF8);
    const char* bundle_path_cstring = CFStringGetCStringPtr(CFDictionaryGetValue(value, bundle_path_key, kCFStringEncodingUTF8);

    // #1 Approach: Compare your 'I-Want-This-BundleID' with loaded kext path
    // #2 Approach: Compare your 'I-Want-This-BundlePath' with loaded kext ID
}

void some_func()
{
    CFDictionaryRef loaded_kexts = KextManagerCopyLoadedKextInfo(NULL, NULL);
    CFDictionaryApplyFunction(loaded_kexts, handle_kext, NULL);
}

关于objective-c - 以编程方式从 .kext 目录获取 KEXT 的 bundle 标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51218143/

相关文章:

c - "can’ t open/tmp/ccRBTake.s 用于写入”,同时使用 gcc 编译

mysql_real_escape_string() 导致段错误

objective-c - 是否有任何函数可以检索与 inode 关联的路径?

macos - QML 导致切换到独立显卡

通过 ctypes 使用 pcap 的 python 段错误

ios - ActivityIndi​​cator没有显示在CollectionViewCell上

ios - 将 UIButton 中的图像缩放到 AspectFit?

objective-c - 如何在不阻塞 UI 的情况下高效地从 Web 服务器请求数据?

ios - UITableView 用动画删除单元格? iOS( Objective-C )

复制不同数据类型的缓冲区内容