如何在内核模块中使用 system() 作为 stdlib.h 不能在内核程序中使用?

How to use system() in a kernel module as stdlib.h cannot be used in a kernel program?

在 Linux 中开发内核模块时,不允许使用 C 标准库。但是,如果我需要使用一些常用功能,例如 system(),我该如何实现它?

答案是:你不会。在极少数情况下,您需要执行类似于 system() 的操作,即从内核 space 调用用户 space 应用程序。在那些特殊情况下,有 usermode-helper API 可用,它允许从内核 space.

启动(并可能等待)任意用户 space 程序

但是,必须注意的是,在设计模块时,您应该真正避免依赖于其他用户space 程序的output/execution。在最好的情况下,这会减慢系统速度,而在最坏的情况下,它还会破坏 kernel/user space 隔离并引入严重漏洞。现代内核版本中 call_usermodehelper() 函数的现有使用实例几乎可以是 counted on the tips of your hands。在编写内核模块时,您基本上永远不需要做这样的事情。如果你认为你这样做,你应该先重新考虑两次。