使用 launchd 在 OS X 中加载和配置内核扩展
我正在编写一个用于套接字过滤的网络内核扩展。为了使其可配置,用户态程序读取配置文件并通过 PF_SYSTEM 套接字将信息传递到 kext。
如果我想在系统启动时尽快启动并运行套接字过滤器,我将如何安排启动?
我当前的想法是使用 launchd 启动一个小型用户区初始化程序。该程序将使用kextload
来启动kext。之后,它将读取配置文件并通过 PF_SYSTEM 套接字与 kext 通信。完成工作后,它会快速退出。
另一种选择是有两个 launchd
项,一个用于 kext(使用 kextload
),另一个用于用户态配置文件读取器。这将避免分叉,但在其他方面是相同的。无论哪种方式,launchd
都必须运行一个快速的非守护程序用户态程序。
然而,launchd 似乎适合启动实际的守护进程,而不是用于完成工作并退出的快速任务。 开发者库文档 说:
重要提示:如果您的守护进程在启动后关闭得太快,launchd 可能会认为它已经崩溃了。继续此行为的守护进程可能会被挂起,并且在将来的请求到达时不会再次启动。为了避免这种情况,启动后至少 10 秒内不要关闭。
这给我的印象是 launchd
不是执行此操作的正确方法。我应该如何组织启动?我的整个想法是不是朝着错误的方向发展了?
(顺便说一句,我也希望用户能够在运行时更改过滤选项。我想这可以通过在需要更改时打开一个新的 PF_SYSTEM 套接字连接到 kext 来完成。)
I am writing a network kernel extension for socket filtering. To make it configurable, a userland program reads a config file and passes the information to the kext through a PF_SYSTEM
socket.
If I want to have the socket filter up and running as soon as possible at system startup, how would I choreograph the launch?
My current idea is to use launchd to start a small userland initializer program. This program would use kextload
to start the kext. After that, it would read the config file and talk to the kext through the PF_SYSTEM
socket. Having done its job, it would then quickly exit.
Another option would be to have two launchd
items, one for the kext (using kextload
) and another for the userland configuration file reader. This would avoid the fork, but would otherwise be identical. Either way, launchd
would have to run a quick non-daemon userland program.
However, launchd
seems to be geared towards launching actual daemons, not for quick tasks that do their work and exit. The developer library document says:
Important: If your daemon shuts down too quickly after being launched, launchd may think it has crashed. Daemons that continue this behavior may be suspended and not launched again when future requests arrive. To avoid this behavior, do not shut down for at least 10 seconds after launch.
This gives me the impression that launchd
is not the correct way to do this. How should I organize the launch? Is my whole idea going in the wrong direction?
(As a side note, I want to give the user the possibility to alter the filtering options during runtime too. I imagine this can be done simply by opening a new PF_SYSTEM socket connection to the kext whenever changes are needed.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为该警告仅适用于按需启动的守护进程。
I think that caveat only applies to daemons that are launched on demand.